/*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
* in Industry THTH ry.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
*******************************************************************************/
package org.simantics.db.impl.query;
-import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import org.simantics.db.RelationInfo;
+import org.simantics.db.exception.DatabaseException;
import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
import org.simantics.db.impl.graph.ReadGraphImpl;
import org.simantics.db.impl.procedure.InternalProcedure;
import org.simantics.db.impl.procedure.TripleIntProcedureAdapter;
-import org.simantics.db.procedure.ListenerBase;
import org.simantics.db.request.RequestFlags;
final public class AssertedStatements extends CollectionBinaryQuery<TripleIntProcedure> {
-// public ArrayList<TripleIntProcedure> procs;
-
public AssertedStatements(final int r1, final int r2) {
super(r1, r2);
}
public static AssertedStatements newInstance(final int r1, final int r2) {
return new AssertedStatements(r1, r2);
}
-
- final static AssertedStatements runner(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final AssertedStatements cached, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {
-
- AssertedStatements entry = cached != null ? cached : (AssertedStatements)provider.assertedStatementsMap.get(id(r1,r2));
- if(entry == null) {
-
- entry = new AssertedStatements(r1, r2);
- entry.setPending();
- entry.clearResult(provider.querySupport);
- entry.putEntry(provider);
-
- provider.performForEach(graph, entry, parent, listener, procedure);
-
- return entry;
-
- } else {
-
- if(entry.isPending()) {
- synchronized(entry) {
- if(entry.isPending()) {
- throw new IllegalStateException();
-// if(entry.procs == null) entry.procs = new ArrayList<TripleIntProcedure>();
-// entry.procs.add(procedure);
-// provider.registerDependencies(graph, entry, parent, listener, procedure, false);
-// return entry;
- }
- }
- }
- provider.performForEach(graph, entry, parent, listener, procedure);
- }
-
- return entry;
-
- }
-
- final public static AssertedStatements queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {
-
- assert(r1 != 0);
- assert(r2 != 0);
-
- final AssertedStatements entry = (AssertedStatements)provider.assertedStatementsMap.get(id(r1,r2));
-
- if(parent == null && !(listener != null)) {
- if(entry != null && entry.isReady()) {
- entry.performFromCache(graph, provider, procedure);
- return entry;
- }
- }
-
- return runner(graph, r1, r2, provider, entry, parent, listener, procedure);
-
- }
-
- @Override
- public BinaryQuery<TripleIntProcedure> getEntry(QueryProcessor provider) {
- return provider.assertedStatementsMap.get(id);
- }
-
- @Override
- public void putEntry(QueryProcessor provider) {
- provider.assertedStatementsMap.put(id, this);
- }
@Override
final public void removeEntry(QueryProcessor provider) {
- provider.assertedStatementsMap.remove(id);
+ provider.cache.remove(this);
}
- void computeInheritedAssertions(ReadGraphImpl graph, int type, final int predicate, final RelationInfo ri, final QueryProcessor queryProvider, final TripleIntProcedure proc) {
+ static void computeInheritedAssertions(ReadGraphImpl graph, int type, final int predicate, final RelationInfo ri, final AssertedStatements entry, final TripleIntProcedure proc) throws DatabaseException {
-// final AtomicBoolean found = new AtomicBoolean(0);
+ QueryProcessor processor = graph.processor;
- DirectObjects.queryEach(graph, type, queryProvider.getInherits(), queryProvider, this, null, new SyncIntProcedure() {
+ QueryCache.runnerDirectObjects(graph, type, processor.getInherits(), entry, null, new SyncIntProcedure() {
@Override
- public void run(ReadGraphImpl graph) {
+ public void run(ReadGraphImpl graph) throws DatabaseException {
// if(ri.isFunctional && found.get() == 1) {
//
//
// }
- finish(graph, queryProvider);
+ finish(graph, entry);
proc.finished(graph);
}
@Override
- public void execute(ReadGraphImpl graph, int inh) {
+ public void execute(ReadGraphImpl graph, int inh) throws DatabaseException {
// if(ri.isFunctional && found.get() == 1) return;
inc();
- AssertedStatements.queryEach(graph, inh, predicate, queryProvider, AssertedStatements.this, null, new TripleIntProcedureAdapter() {
+ QueryCache.runnerAssertedStatements(graph, inh, predicate, entry, null, new TripleIntProcedureAdapter() {
@Override
- public void execute(ReadGraphImpl graph, int s, int p, int o) {
+ public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
// if(ri.isFunctional) {
//
//// if(found.get() == 1) return;
//
// if(found.compareAndSet(0, o)) {
- if(addOrSet(s,p,o))
+ if(addOrSet(s,p,o, entry))
proc.execute(graph, s, p, o);
// }
// // If this was a duplicate, we can ignore this
}
@Override
- public void finished(ReadGraphImpl graph) {
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
dec(graph);
}
@Override
- public void exception(ReadGraphImpl graph, Throwable t) {
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
proc.exception(graph, t);
dec(graph);
}
}
@Override
- public void finished(ReadGraphImpl graph) {
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
dec(graph);
}
}
- void computeLocalAssertions(ReadGraphImpl graph, final int type, final int predicate, final RelationInfo ri, final QueryProcessor queryProvider, final TripleIntProcedure proc) {
+ static void computeLocalAssertions(ReadGraphImpl graph, final int type, final int predicate, final RelationInfo ri, final AssertedStatements entry, final TripleIntProcedure proc) throws DatabaseException {
+
+ }
+
+ //@Override
+ public Object compute(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException {
+ computeForEach(graph, r1(), r2(), this, procedure);
+ return getResult();
+ }
+
+ public static void computeForEach(ReadGraphImpl graph, final int type, final int predicate, final AssertedStatements entry, final TripleIntProcedure procedure) throws DatabaseException {
+
+ RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, predicate, entry, null);
+
final AtomicInteger found = new AtomicInteger(0);
- DirectObjects.queryEach(graph, type, queryProvider.getAsserts(), queryProvider, this, null, new SyncIntProcedure() {
+ QueryProcessor processor = graph.processor;
+
+ QueryCache.runnerDirectObjects(graph, type, processor.getAsserts(), entry, null, new SyncIntProcedure() {
@Override
- public void run(ReadGraphImpl graph) {
+ public void run(ReadGraphImpl graph) throws DatabaseException {
if(ri.isFunctional && found.get() > 1) {
ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one asserted statement.");
- except(exception);
- proc.exception(graph, exception);
+ except(exception, entry);
+ procedure.exception(graph, exception);
return;
}
if(ri.isFunctional && found.get() == 1) {
- finish(graph, queryProvider);
- proc.finished(graph);
+ finish(graph, entry);
+ procedure.finished(graph);
return;
}
- computeInheritedAssertions(graph, type, predicate, ri, queryProvider, proc);
+ computeInheritedAssertions(graph, type, predicate, ri, entry, procedure);
}
@Override
- public void execute(ReadGraphImpl graph, final int ass) {
+ public void execute(ReadGraphImpl graph, final int ass) throws DatabaseException {
if(ri.isFunctional && found.get() > 1) return;
inc();
- DirectObjects.queryEach(graph, ass, queryProvider.getHasPredicate(), queryProvider, AssertedStatements.this, null, new IntProcedure() {
+ QueryCache.runnerDirectObjects(graph, ass, processor.getHasPredicate(), entry, null, new IntProcedure() {
@Override
- public void execute(ReadGraphImpl graph, final int pred) {
+ public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
if(ri.isFunctional) {
inc();
- DirectObjects.queryEach(graph, ass, queryProvider.getHasObject(), queryProvider, AssertedStatements.this, null, new IntProcedure() {
+ QueryCache.runnerDirectObjects(graph, ass, processor.getHasObject(), entry, null, new IntProcedure() {
@Override
- public void execute(ReadGraphImpl graph, final int object) {
+ public void execute(ReadGraphImpl graph, final int object) throws DatabaseException {
if(found.get() > 1) return;
if(pred == predicate) {
if(found.getAndIncrement() == 0) {
- if(addOrSet(type, pred, object))
- proc.execute(graph, type, pred, object);
+ if(addOrSet(type, pred, object, entry))
+ procedure.execute(graph, type, pred, object);
}
return;
inc();
- SuperRelations.queryEach(graph, pred, queryProvider, AssertedStatements.this, null, new InternalProcedure<IntSet>() {
+ QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
@Override
- public void execute(ReadGraphImpl graph, IntSet result) {
+ public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
if(found.get() > 1) {
dec(graph);
if(result.contains(predicate)) {
if(found.getAndIncrement() == 0) {
- if(addOrSet(type, pred, object))
- proc.execute(graph, type, pred, object);
+ if(addOrSet(type, pred, object, entry))
+ procedure.execute(graph, type, pred, object);
}
}
}
@Override
- public void exception(ReadGraphImpl graph, Throwable t) {
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
- proc.exception(graph, t);
+ procedure.exception(graph, t);
dec(graph);
}
}
@Override
- public void finished(ReadGraphImpl graph) {
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
dec(graph);
}
@Override
- public void exception(ReadGraphImpl graph, Throwable t) {
- proc.exception(graph, t);
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+ procedure.exception(graph, t);
dec(graph);
}
inc();
- DirectObjects.queryEach(graph, ass, queryProvider.getHasObject(), queryProvider, AssertedStatements.this, null, new IntProcedure() {
+ QueryCache.runnerDirectObjects(graph, ass, processor.getHasObject(), entry, null, new IntProcedure() {
@Override
- public void execute(ReadGraphImpl graph, final int object) {
+ public void execute(ReadGraphImpl graph, final int object) throws DatabaseException {
if(pred == predicate) {
- addOrSet(type, pred, object);
- proc.execute(graph, type, pred, object);
+ addOrSet(type, pred, object, entry);
+ procedure.execute(graph, type, pred, object);
return;
}
inc();
- SuperRelations.queryEach(graph, pred, queryProvider, AssertedStatements.this, null, new InternalProcedure<IntSet>() {
+ QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
@Override
- public void execute(ReadGraphImpl graph, IntSet result) {
+ public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
if(result.contains(predicate)) {
- addOrSet(type, pred, object);
- proc.execute(graph, type, pred, object);
+ addOrSet(type, pred, object, entry);
+ procedure.execute(graph, type, pred, object);
}
}
@Override
- public void exception(ReadGraphImpl graph, Throwable t) {
- proc.exception(graph, t);
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+ procedure.exception(graph, t);
dec(graph);
}
}
@Override
- public void finished(ReadGraphImpl graph) {
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
dec(graph);
}
@Override
- public void exception(ReadGraphImpl graph, Throwable t) {
- proc.exception(graph, t);
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+ procedure.exception(graph, t);
dec(graph);
}
}
@Override
- public void finished(ReadGraphImpl graph) {
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
dec(graph);
}
@Override
- public void exception(ReadGraphImpl graph, Throwable t) {
- proc.exception(graph, t);
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+ procedure.exception(graph, t);
dec(graph);
}
}
@Override
- public void finished(ReadGraphImpl graph) {
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
dec(graph);
}
});
- }
-
- @Override
- public void computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final TripleIntProcedure procedure, boolean store) {
-
- RelationInfoQuery.queryEach(graph, r2(), provider, this, null, new InternalProcedure<RelationInfo>() {
-
- @Override
- public void execute(ReadGraphImpl graph, RelationInfo ri) {
-
- computeLocalAssertions(graph, r1(), r2(), ri, provider, procedure);
-
- }
-
- @Override
- public void exception(ReadGraphImpl graph, Throwable t) {
- procedure.exception(graph, t);
- }
-
- });
-
}
@Override
public String toString() {
- return "AssertedStatements2[" + r1() + " - " + r2() + "]";
+ return "AssertedStatements[" + r1() + " - " + r2() + "]";
}
private boolean addOrSet(int s, int p, int o) {
-
+
assert(isPending());
IntArray value = (IntArray)getResult();
}
- final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
+ static boolean addOrSet(int s, int p, int o, AssertedStatements entry) {
+ if(entry != null) {
+ return entry.addOrSet(s, p, o);
+ } else {
+ return true;
+ }
+ }
+
+ static void finish(ReadGraphImpl graph, AssertedStatements entry) {
- assert(isPending());
-
-// ArrayList<TripleIntProcedure> p = null;
-
- synchronized(this) {
-
- setReady();
-// p = procs;
-// procs = null;
-
- }
-
-// if(p != null) {
-// final IntArray value = (IntArray)getResult();
-// for(TripleIntProcedure proc : p) {
-// for(int i=0;i<value.size();i+=3) {
-// proc.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
-// }
-// }
-//
-// for(TripleIntProcedure proc : p) proc.finished(graph);
-// }
+ assert(entry.isPending());
+ if(entry != null) {
+ synchronized(entry) {
+ entry.setReady();
+ }
+ }
}
+
+ static void except(Throwable t, AssertedStatements entry) {
+ if(entry != null) {
+ synchronized(entry) {
+ entry.except(t);
+ }
+ }
+ }
@Override
- public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) {
+ public Object performFromCache(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException {
assert(isReady());
- if(handleException(graph, procedure)) return;
+ if(handleException(graph, procedure)) return getResult();
final IntArray value = (IntArray)getResult();
for(int i=0;i<value.size();i+=3) {
procedure.finished(graph);
+ return value;
+
}
@Override
- public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+ public void recompute(ReadGraphImpl graph) throws DatabaseException {
- final Semaphore s = new Semaphore(0);
-
- computeForEach(graph, provider, new TripleIntProcedureAdapter() {
+ compute(graph, new TripleIntProcedureAdapter() {
@Override
public void finished(ReadGraphImpl graph) {
- s.release();
}
@Override
throw new Error("Error in recompute", t);
}
- }, true);
-
- try {
- s.acquire();
- } catch (InterruptedException e) {
- throw new Error(e);
- }
+ });
}