1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.impl.query;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.concurrent.Semaphore;
\r
17 import java.util.concurrent.atomic.AtomicBoolean;
\r
19 import org.simantics.db.RelationInfo;
\r
20 import org.simantics.db.common.exception.DebugException;
\r
21 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
\r
22 import org.simantics.db.impl.graph.ReadGraphImpl;
\r
23 import org.simantics.db.impl.procedure.InternalProcedure;
\r
24 import org.simantics.db.impl.procedure.TripleIntProcedureAdapter;
\r
25 import org.simantics.db.procedure.ListenerBase;
\r
26 import org.simantics.db.request.RequestFlags;
\r
28 final public class Statements extends CollectionBinaryQuery<TripleIntProcedure> {
\r
30 // public ArrayList<TripleIntProcedure> procs = null;
\r
32 public Statements(final int r1, final int r2) {
\r
36 final static Statements entry(final QueryProcessor processor, final int r1, final int r2) {
\r
38 return (Statements)processor.statementsMap.get(id(r1,r2));
\r
42 final static Collection<Statements> entries(final QueryProcessor processor, final int r1) {
\r
43 return processor.statementsMap.values(r1);
\r
46 final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {
\r
48 QueryProcessor processor = graph.processor;
\r
50 Statements entry = (Statements)processor.statementsMap.get(id(r1,r2));
\r
53 entry = new Statements(r1, r2);
\r
55 entry.clearResult(processor.querySupport);
\r
56 entry.putEntry(processor);
\r
58 processor.performForEach(graph, entry, parent, listener, procedure);
\r
62 if(entry.isPending()) {
\r
63 synchronized(entry) {
\r
64 if(entry.isPending()) {
\r
65 throw new IllegalStateException();
\r
66 // if(entry.procs == null) entry.procs = new ArrayList<TripleIntProcedure>();
\r
67 // entry.procs.add(procedure);
\r
68 // processor.registerDependencies(graph, entry, parent, listener, procedure, false);
\r
74 processor.performForEach(graph, entry, parent, listener, procedure);
\r
80 final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {
\r
85 if(parent == null && listener == null) {
\r
86 Statements.computeForEach(graph, r1, r2, null, procedure);
\r
90 runner(graph, r1, r2, parent, listener, procedure);
\r
95 public BinaryQuery<TripleIntProcedure> getEntry(QueryProcessor provider) {
\r
96 return provider.statementsMap.get(id);
\r
100 public void putEntry(QueryProcessor provider) {
\r
101 provider.statementsMap.put(id, this);
\r
105 final public void removeEntry(QueryProcessor provider) {
\r
106 provider.statementsMap.remove(id);
\r
109 final static TripleIntProcedure NOPT = new TripleIntProcedure() {
\r
113 public void exception(ReadGraphImpl graph, Throwable throwable) {
\r
117 public void execute(ReadGraphImpl graph, int s, int p, int o) {
\r
121 public void finished(ReadGraphImpl graph) {
\r
126 final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Statements entry) {
\r
128 class AssertionMapProc implements IntProcedure {
\r
130 boolean first = true;
\r
132 private IntArray result;
\r
134 public void addStatement(int s, int p, int o) {
\r
136 if(result.size() == 0) {
\r
141 for(int i = 0;i < result.sizeOrData ; i+=3) {
\r
142 int existingP = result.data[i+1];
\r
143 if(p == existingP) {
\r
144 int existingO = result.data[i+2];
\r
145 if(existingO == o) return;
\r
156 public void execute(ReadGraphImpl graph, int type) {
\r
157 AssertedStatements stms = AssertedStatements.queryEach(graph, type, r2, graph.processor, entry, null, NOPT);
\r
158 if(result == null) {
\r
159 result = stms.getResult();
\r
162 IntArray ia = result;
\r
163 result = new IntArray();
\r
164 if(ia.data != null) {
\r
165 for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);
\r
169 IntArray ia = stms.getResult();
\r
170 if(ia.data != null) {
\r
171 for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);
\r
177 public void finished(ReadGraphImpl graph) {
\r
181 public void exception(ReadGraphImpl graph, Throwable throwable) {
\r
186 AssertionMapProc amp = new AssertionMapProc();
\r
188 // This dependency could be cut
\r
189 PrincipalTypes.queryEach(graph, r1, graph.processor, entry, null, amp);
\r
195 final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) {
\r
197 IntArray map = getAssertionMap(graph, r1, r2, entry);
\r
199 if(entry != null) entry.finish(graph, procedure);
\r
200 else procedure.finished(graph);
\r
204 int size = map.size();
\r
207 int s = map.data[0];
\r
208 int p = map.data[1];
\r
209 int o = map.data[2];
\r
211 if(entry != null) {
\r
212 entry.addOrSetFunctional(s,p,o);
\r
213 entry.finish(graph, procedure);
\r
215 procedure.execute(graph, s,p,o);
\r
216 procedure.finished(graph);
\r
219 } else if(size == 0) {
\r
221 if(entry != null) entry.finish(graph, procedure);
\r
222 else procedure.finished(graph);
\r
226 int candidateS = map.data[0];
\r
227 int candidateP = map.data[1];
\r
228 int candidateO = map.data[2];
\r
230 SuperTypes candidate = SuperTypes.queryEach(graph, candidateS, graph.processor, entry, null, NOP);
\r
231 if(candidate.isExcepted()) {
\r
232 if(entry != null) entry.except((Throwable)candidate.getResult());
\r
233 procedure.exception(graph, (Throwable)candidate.getResult());
\r
236 IntSet candidateIs = candidate.getResult();
\r
238 for(int i=3;i<map.size();i+=3) {
\r
240 int nextS = map.data[i];
\r
241 int nextP = map.data[i+1];
\r
242 int nextO = map.data[i+2];
\r
244 if(nextS != candidateS) {
\r
246 if(candidateIs.contains(nextS)) {
\r
248 // Next is a super type of candidate => ignore next
\r
252 SuperTypes next = SuperTypes.queryEach(graph, nextS, graph.processor, entry, null, NOP);
\r
253 if(next.isExcepted()) {
\r
254 if(entry != null) entry.except((Throwable)next.getResult());
\r
255 procedure.exception(graph, (Throwable)next.getResult());
\r
258 IntSet nextIs = next.getResult();
\r
260 if(nextIs.contains(candidateS)) {
\r
262 // Candidate is a super type of next => next is the new candidate
\r
264 candidateS = nextS;
\r
265 candidateP = nextP;
\r
266 candidateO = nextO;
\r
267 candidateIs = nextIs;
\r
271 // candidate and next are unrelated => error
\r
272 ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has conflicting assertions.", r1);
\r
274 if(entry != null) entry.except(exception);
\r
275 procedure.exception(graph, exception);
\r
286 if(entry != null) {
\r
287 entry.addOrSetFunctional(candidateS, candidateP, candidateO);
\r
288 entry.finish(graph, procedure);
\r
290 procedure.execute(graph, candidateS, candidateP, candidateO);
\r
291 procedure.finished(graph);
\r
298 final static InternalProcedure<IntSet> NOP = new InternalProcedure<IntSet>() {
\r
301 public void execute(ReadGraphImpl graph, IntSet result) {
\r
305 public void exception(ReadGraphImpl graph, Throwable throwable) {
\r
310 // Search for one statement
\r
311 final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final RelationInfo ri, final TripleIntProcedure procedure) {
\r
315 int result = graph.processor.querySupport.getFunctionalObject(r1, r2);
\r
319 // Check for assertions
\r
320 forSingleAssertion(graph, r1, r2, entry, procedure);
\r
322 } else if(result == -1) {
\r
324 graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
\r
327 public void execute(ReadGraphImpl graph, int i) {
\r
328 if(entry != null) entry.addOrSetFunctional(r1, r2, i);
\r
329 else procedure.execute(graph, r1, r2, i);
\r
333 public void exception(ReadGraphImpl graph, Throwable t) {
\r
334 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
\r
338 public void finished(ReadGraphImpl graph) {
\r
343 // Check for assertions
\r
344 forSingleAssertion(graph, r1, r2, entry, procedure);
\r
348 // If functional relation was found there is no need to check assertions
\r
349 if(entry != null) {
\r
350 entry.addOrSetFunctional(r1, r2, result);
\r
351 entry.finish(graph, procedure);
\r
353 procedure.execute(graph, r1, r2, result);
\r
354 procedure.finished(graph);
\r
362 final AtomicBoolean found = new AtomicBoolean(false);
\r
364 // Note! The dependency is intentionally cut!
\r
365 DirectPredicates.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() {
\r
368 public void run(ReadGraphImpl graph) {
\r
371 if(entry != null) entry.finish(graph, procedure);
\r
372 else procedure.finished(graph);
\r
375 // Check for assertions
\r
376 forSingleAssertion(graph, r1, r2, entry, procedure);
\r
383 public void execute(ReadGraphImpl graph, final int pred) {
\r
385 if(found.get()) return;
\r
391 // Note! The dependency is intentionally cut!
\r
392 DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {
\r
395 public void execute(ReadGraphImpl graph, int i) {
\r
397 if(found.compareAndSet(false, true)) {
\r
399 if(entry != null) entry.addOrSetFunctional(r1, pred, i);
\r
400 else procedure.execute(graph, r1, pred, i);
\r
404 ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1);
\r
405 if(entry != null) entry.except(exception);
\r
406 procedure.exception(graph, exception);
\r
413 public void finished(ReadGraphImpl graph) {
\r
418 public void exception(ReadGraphImpl graph, Throwable t) {
\r
419 procedure.exception(graph, t);
\r
429 SuperRelations.queryEach(graph, pred, graph.processor, entry, null, new InternalProcedure<IntSet>() {
\r
432 public void execute(ReadGraphImpl graph, IntSet result) {
\r
439 if(result.contains(r2)) {
\r
443 // Note! The dependency is intentionally cut!
\r
444 DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {
\r
447 public void execute(ReadGraphImpl graph, int i) {
\r
449 if(found.compareAndSet(false, true)) {
\r
451 if(entry != null) entry.addOrSetFunctional(r1, pred, i);
\r
452 else procedure.execute(graph, r1, pred, i);
\r
456 ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1);
\r
457 if(entry != null) entry.except(exception);
\r
458 procedure.exception(graph, exception);
\r
465 public void finished(ReadGraphImpl graph) {
\r
470 public void exception(ReadGraphImpl graph, Throwable t) {
\r
471 procedure.exception(graph, t);
\r
484 public void exception(ReadGraphImpl graph, Throwable t) {
\r
485 procedure.exception(graph, t);
\r
496 public void finished(ReadGraphImpl graph) {
\r
508 final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) {
\r
510 PrincipalTypes.queryEach(graph, r1, graph.processor, entry, null, new SyncIntProcedure() {
\r
513 public void run(ReadGraphImpl graph) {
\r
515 if(entry != null) entry.finish(graph, procedure);
\r
516 else procedure.finished(graph);
\r
520 TripleIntProcedure proc = new TripleIntProcedureAdapter() {
\r
523 public void execute(ReadGraphImpl graph, int s, int p, int o) {
\r
524 if(entry != null) entry.addOrSet(s, p, o);
\r
525 else procedure.execute(graph, s, p, o);
\r
529 public void finished(ReadGraphImpl graph) {
\r
534 public void exception(ReadGraphImpl graph, Throwable t) {
\r
536 procedure.exception(graph, t);
\r
542 public void execute(ReadGraphImpl graph, int type) {
\r
546 AssertedStatements.queryEach(graph, type, r2, graph.processor, entry, null, proc);
\r
551 public void finished(ReadGraphImpl graph) {
\r
556 public void exception(ReadGraphImpl graph, Throwable t) {
\r
565 final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final RelationInfo ri, final TripleIntProcedure procedure) {
\r
569 graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
\r
572 public void execute(ReadGraphImpl graph, int i) {
\r
573 if(entry != null) entry.addOrSet(r1, r2, i);
\r
574 else procedure.execute(graph, r1, r2, i);
\r
578 public void exception(ReadGraphImpl graph, Throwable t) {
\r
579 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
\r
580 procedure.exception(graph, t);
\r
584 public void finished(ReadGraphImpl graph) {
\r
589 if(ri.isAsserted) {
\r
590 forAssertions(graph, r1, r2, entry, procedure);
\r
592 if(entry != null) entry.finish(graph, procedure);
\r
593 else procedure.finished(graph);
\r
598 // Note! The dependency is intentionally cut!
\r
599 DirectPredicates.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() {
\r
602 public void run(ReadGraphImpl graph) {
\r
604 forAssertions(graph, r1, r2, entry, procedure);
\r
609 public void execute(ReadGraphImpl graph, final int pred2) {
\r
615 // Note! The dependency is intentionally cut!
\r
616 DirectObjects.queryEach(graph, r1, pred2, graph.processor, null, null, new IntProcedure() {
\r
619 public void execute(ReadGraphImpl graph, int i) {
\r
620 if(entry != null) entry.addOrSet(r1, pred2, i);
\r
621 else procedure.execute(graph, r1, pred2, i);
\r
625 public void finished(ReadGraphImpl graph) {
\r
630 public void exception(ReadGraphImpl graph, Throwable t) {
\r
631 procedure.exception(graph, t);
\r
643 IntSet result = SuperRelations.queryEach2(graph, pred2, graph.processor, entry, null, null);
\r
644 if(result.contains(r2)) {
\r
648 // Note! The dependency is intentionally cut!
\r
649 DirectObjects.queryEach(graph, r1, pred2, graph.processor, null, null, new IntProcedure() {
\r
652 public void execute(ReadGraphImpl graph, int i) {
\r
653 if(entry != null) entry.addOrSet(r1, pred2, i);
\r
654 else procedure.execute(graph, r1, pred2, i);
\r
659 public void finished(ReadGraphImpl graph) {
\r
664 public void exception(ReadGraphImpl graph, Throwable t) {
\r
665 procedure.exception(graph, t);
\r
673 } catch (Throwable e) {
\r
674 procedure.exception(graph, e);
\r
682 public void finished(ReadGraphImpl graph) {
\r
693 public void computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final TripleIntProcedure procedure, final boolean store) {
\r
694 computeForEach(graph, r1(), r2(), this, procedure);
\r
697 public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) {
\r
699 RelationInfoQuery riEntry = RelationInfoQuery.probe(graph, r2);
\r
700 if(riEntry != null) {
\r
701 RelationInfo ri = riEntry.getResult();
\r
702 graph.ensureLoaded(r1, r2);
\r
703 if(ri.isFunctional) {
\r
704 computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
\r
706 computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
\r
711 RelationInfoQuery.queryEach(graph, r2, graph.processor, entry, null, new InternalProcedure<RelationInfo>() {
\r
714 public void execute(ReadGraphImpl graph, final RelationInfo ri) {
\r
716 graph.ensureLoaded(r1, r2);
\r
717 if(ri.isFunctional) {
\r
718 computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
\r
720 computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
\r
726 public void exception(ReadGraphImpl graph, Throwable t) {
\r
727 procedure.exception(graph, t);
\r
735 public String toString() {
\r
736 return "Statements[" + r1() + " - " + r2() + "]";
\r
739 final private void finish(ReadGraphImpl graph, TripleIntProcedure procedure) {
\r
741 assert(assertPending());
\r
743 // ArrayList<TripleIntProcedure> p = null;
\r
745 synchronized(this) {
\r
753 IntArray v = (IntArray)getResult();
\r
754 final IntArray value = (IntArray)getResult();
\r
758 // for(TripleIntProcedure proc : p) {
\r
759 // for(int i=0;i<value.size();i+=3) {
\r
760 // proc.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
\r
763 // for(int i=0;i<value.size();i+=3) {
\r
764 // procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
\r
767 // for(TripleIntProcedure proc : p) proc.finished(graph);
\r
771 for(int i=0;i<value.size();i+=3) {
\r
772 procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
\r
775 procedure.finished(graph);
\r
779 // final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
\r
781 // assert(isPending());
\r
783 // ArrayList<TripleIntProcedure> p = null;
\r
785 // synchronized(this) {
\r
795 // final IntArray value = (IntArray)getResult();
\r
796 // for(TripleIntProcedure proc : p) {
\r
797 // for(int i=0;i<value.size();i+=3) {
\r
798 // proc.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
\r
802 // for(TripleIntProcedure proc : p) proc.finished(graph);
\r
808 synchronized public void addOrSet(int s, int p, int o) {
\r
810 assert(assertPending());
\r
812 IntArray value = (IntArray)getResult();
\r
819 final static public int r1(long id) {
\r
820 return (int)(id>>>32);
\r
823 final static public int r2(long id) {
\r
827 final public void addOrSetFunctional(int s, long po) {
\r
829 addOrSetFunctional(s, r1(po), r2(po));
\r
833 final public void addOrSetFunctional(int s, int p, int o) {
\r
835 assert(assertPending());
\r
837 IntArray value = (IntArray)getResult();
\r
845 public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) {
\r
849 if(handleException(graph, procedure)) return;
\r
851 final IntArray value = (IntArray)getResult();
\r
852 for(int i=0;i<value.size();i+=3) {
\r
853 procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
\r
856 procedure.finished(graph);
\r
861 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
\r
863 final Semaphore s = new Semaphore(0);
\r
865 computeForEach(graph, provider, new TripleIntProcedureAdapter() {
\r
868 public void finished(ReadGraphImpl graph) {
\r
873 public void exception(ReadGraphImpl graph, Throwable t) {
\r
874 new Error("Error in recompute.", t).printStackTrace();
\r
880 while(!s.tryAcquire()) {
\r
881 provider.resume(graph);
\r
886 // } catch (InterruptedException e) {
\r
887 // throw new Error(e);
\r
893 public int type() {
\r
894 return RequestFlags.IMMEDIATE_UPDATE;
\r
899 boolean isImmutable(ReadGraphImpl graph) {
\r
900 return graph.processor.isImmutable(r1());
\r