import org.simantics.db.procedure.ListenerBase;
import org.simantics.db.request.RequestFlags;
-final public class Statements extends CollectionBinaryQuery<TripleIntProcedure> {
+final public class Statements extends CollectionBinaryQuery<TripleIntProcedure> implements TripleIntProcedure {
public Statements(final int r1, final int r2) {
super(r1, r2);
}
- final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) throws DatabaseException {
-
- if(entry != null) {
- assert(entry.isPending());
- }
+ final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final TripleIntProcedure procedure) throws DatabaseException {
- IntArray map = getAssertionMap(graph, r1, r2, entry);
+ IntArray map = getAssertionMap(graph, r1, r2, parent);
if(map == null) {
- if(entry != null) entry.finish(graph, procedure);
- else procedure.finished(graph);
+ procedure.finished(graph);
return;
}
int p = map.data[1];
int o = map.data[2];
- if(entry != null) {
- entry.addOrSetFunctional(s,p,o);
- entry.finish(graph, procedure);
- } else {
procedure.execute(graph, s,p,o);
procedure.finished(graph);
- }
} else if(size == 0) {
- if(entry != null) entry.finish(graph, procedure);
- else procedure.finished(graph);
+ procedure.finished(graph);
} else {
IntSet candidateIs = null;
try {
- candidateIs = QueryCache.resultSuperTypes(graph, candidateS, entry, null);
+ candidateIs = QueryCache.resultSuperTypes(graph, candidateS, parent, null);
} catch (DatabaseException e) {
- if(entry != null) entry.except(e);
procedure.exception(graph, e);
return;
}
-
-// SuperTypes candidate = SuperTypes.queryEach(graph, candidateS, graph.processor, entry, null, NOP);
-// if(candidate.isExcepted()) {
-// if(entry != null) entry.except((Throwable)candidate.getResult());
-// procedure.exception(graph, (Throwable)candidate.getResult());
-// return;
-// }
-// IntSet candidateIs = candidate.getResult();
-
for(int i=3;i<map.size();i+=3) {
int nextS = map.data[i];
IntSet nextIs = null;
try {
- nextIs = QueryCache.resultSuperTypes(graph, nextS, entry, null);
+ nextIs = QueryCache.resultSuperTypes(graph, nextS, parent, null);
} catch (DatabaseException e) {
- if(entry != null) entry.except(e);
procedure.exception(graph, e);
return;
}
-
-// SuperTypes next = SuperTypes.queryEach(graph, nextS, graph.processor, entry, null, NOP);
-// if(next.isExcepted()) {
-// if(entry != null) entry.except((Throwable)next.getResult());
-// procedure.exception(graph, (Throwable)next.getResult());
-// return;
-// }
-// IntSet nextIs = next.getResult();
if(nextIs.contains(candidateS)) {
candidateIs = nextIs;
} else {
-
// candidate and next are unrelated => error
ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has conflicting assertions.", r1);
-
- if(entry != null) entry.except(exception);
procedure.exception(graph, exception);
return;
-
}
}
}
- if(entry != null) {
- entry.addOrSetFunctional(candidateS, candidateP, candidateO);
- entry.finish(graph, procedure);
- } else {
- procedure.execute(graph, candidateS, candidateP, candidateO);
- procedure.finished(graph);
- }
+ procedure.execute(graph, candidateS, candidateP, candidateO);
+ procedure.finished(graph);
}
};
// Search for one statement
- final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException {
+ final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException {
if(ri.isFinal) {
if(result == 0) {
// Check for assertions
- forSingleAssertion(graph, r1, r2, entry, procedure);
+ forSingleAssertion(graph, r1, r2, parent, procedure);
} else if(result == -1) {
@Override
public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
- if(entry != null) entry.addOrSetFunctional(r1, r2, i);
- else procedure.execute(graph, r1, r2, i);
+ procedure.execute(graph, r1, r2, i);
}
@Override
});
// Check for assertions
- forSingleAssertion(graph, r1, r2, entry, procedure);
+ forSingleAssertion(graph, r1, r2, parent, procedure);
} else {
// If functional relation was found there is no need to check assertions
- if(entry != null) {
- entry.addOrSetFunctional(r1, r2, result);
- entry.finish(graph, procedure);
- } else {
- procedure.execute(graph, r1, r2, result);
- procedure.finished(graph);
- }
+ procedure.execute(graph, r1, r2, result);
+ procedure.finished(graph);
}
public void run(ReadGraphImpl graph) throws DatabaseException {
if(found.get()) {
- if(entry != null) entry.finish(graph, procedure);
- else procedure.finished(graph);
+ procedure.finished(graph);
} else {
-
- // Check for assertions
- forSingleAssertion(graph, r1, r2, entry, procedure);
-
+ // Check for assertions
+ forSingleAssertion(graph, r1, r2, parent, procedure);
}
}
public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
if(found.compareAndSet(false, true)) {
-
- if(entry != null) entry.addOrSetFunctional(r1, pred, i);
- else procedure.execute(graph, r1, pred, i);
-
+ procedure.execute(graph, r1, pred, i);
} else {
-
ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1);
- if(entry != null) entry.except(exception);
procedure.exception(graph, exception);
-
}
}
inc();
- QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
+ QueryCache.runnerSuperRelations(graph, pred, parent, null, new InternalProcedure<IntSet>() {
@Override
public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
if(found.compareAndSet(false, true)) {
-
- if(entry != null) entry.addOrSetFunctional(r1, pred, i);
- else procedure.execute(graph, r1, pred, i);
-
+ procedure.execute(graph, r1, pred, i);
} else {
-
ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1);
- if(entry != null) entry.except(exception);
procedure.exception(graph, exception);
-
}
}
}
- final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) throws DatabaseException {
+ final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final TripleIntProcedure procedure) throws DatabaseException {
- QueryCache.runnerPrincipalTypes(graph, r1, entry, null, new SyncIntProcedure() {
+ QueryCache.runnerPrincipalTypes(graph, r1, parent, null, new SyncIntProcedure() {
@Override
public void run(ReadGraphImpl graph) throws DatabaseException {
-
- if(entry != null) entry.finish(graph, procedure);
- else procedure.finished(graph);
-
+ procedure.finished(graph);
}
TripleIntProcedure proc = new TripleIntProcedureAdapter() {
@Override
public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
- if(entry != null) entry.addOrSet(s, p, o);
- else procedure.execute(graph, s, p, o);
+ procedure.execute(graph, s, p, o);
}
@Override
@Override
public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
-
inc();
- QueryCache.runnerAssertedStatements(graph, type, r2, entry, null, proc);
-
+ QueryCache.runnerAssertedStatements(graph, type, r2, parent, null, proc);
}
@Override
}
- final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException {
+ final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException {
if(ri.isFinal) {
@Override
public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
- if(entry != null) entry.addOrSet(r1, r2, i);
- else procedure.execute(graph, r1, r2, i);
+ procedure.execute(graph, r1, r2, i);
}
@Override
});
if(ri.isAsserted) {
- forAssertions(graph, r1, r2, entry, procedure);
+ forAssertions(graph, r1, r2, parent, procedure);
} else {
- if(entry != null) entry.finish(graph, procedure);
- else procedure.finished(graph);
+ procedure.finished(graph);
}
} else {
@Override
public void run(ReadGraphImpl graph) throws DatabaseException {
-
- forAssertions(graph, r1, r2, entry, procedure);
-
+ forAssertions(graph, r1, r2, parent, procedure);
}
@Override
@Override
public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
- if(entry != null) entry.addOrSet(r1, pred2, i);
- else procedure.execute(graph, r1, pred2, i);
+ procedure.execute(graph, r1, pred2, i);
}
@Override
try {
- IntSet result = QueryCache.resultSuperRelations(graph, pred2, entry, null);
+ IntSet result = QueryCache.resultSuperRelations(graph, pred2, parent, null);
if(result.contains(r2)) {
inc();
@Override
public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
- if(entry != null) entry.addOrSet(r1, pred2, i);
- else procedure.execute(graph, r1, pred2, i);
+ procedure.execute(graph, r1, pred2, i);
}
}
- public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) throws DatabaseException {
-
- if(entry != null) {
- assert(entry.isPending());
- }
+ public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure_) throws DatabaseException {
+
+ TripleIntProcedure procedure = entry != null ? entry : procedure_;
- QueryCache.runnerRelationInfoQuery(graph, r2, entry, null, new InternalProcedure<RelationInfo>() {
-
- @Override
- public void execute(ReadGraphImpl graph, final RelationInfo ri) throws DatabaseException {
-
- graph.ensureLoaded(r1, r2);
- if(ri.isFunctional) {
- computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
- } else {
- computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
- }
-
- }
-
- @Override
- public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
- procedure.exception(graph, t);
- }
+ RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, r2, entry, null);
+ graph.ensureLoaded(r1, r2);
+ if(ri.isFunctional) {
+ computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
+ } else {
+ computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
+ }
- });
+ if(entry != null) entry.performFromCache(graph, procedure_);
}
boolean isImmutable(ReadGraphImpl graph) {
return graph.processor.isImmutable(r1());
}
+
+ @Override
+ public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
+ addOrSet(s, p, o);
+ }
+
+ @Override
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
+ setReady();
+ }
+
+ @Override
+ public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+ except(throwable);
+ }
}