]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Exceptions for many functional objects are broken 88/4088/1
authorAntti Villberg <antti.villberg@semantum.fi>
Fri, 3 Apr 2020 12:53:16 +0000 (15:53 +0300)
committerAntti Villberg <antti.villberg@semantum.fi>
Fri, 3 Apr 2020 12:53:16 +0000 (15:53 +0300)
Change-Id: I03ac36d14f49b6a5e29864f32f5c5feeb8673e1c

bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Objects.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Statements.java

index 61580bfdcb6ae9a96680952b070261057b9a2fe6..27336b23d32474eda7bf7b00ab368a9cb5b56e02 100644 (file)
@@ -1511,15 +1511,15 @@ public class ReadGraphImpl implements AsyncReadGraph {
 
                try {
 
-                   int result = processor.getSingleObject(this, subject, relation);
-                   if(result == 0) return null;
-                   
-                   return processor.querySupport.getResource(result);
+                       int result = processor.getSingleObject(this, subject, relation);
+                       if(result == 0) return null;
+
+                       return processor.querySupport.getResource(result);
 
-            } catch (ManyObjectsForFunctionalRelationException e) {
+               } catch (ManyObjectsForFunctionalRelationException e) {
+
+                       throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
 
-                throw new ManyObjectsForFunctionalRelationException("subject=" + subject + ", relation=" + relation, e);
-                
                } catch (DatabaseException e) {
 
                        throw new ServiceException(e);
index f74344b2fdb807dad60e1b746580f81cb106ad80..8bac422636ff9a13d8064f5ab0ef64cee6a4d476 100644 (file)
@@ -37,569 +37,566 @@ import org.simantics.db.request.RequestFlags;
 
 public final class Objects extends CollectionBinaryQuery<IntProcedure> implements IntProcedure {
 
-       public Objects(final int r1, final int r2) {
-               super(r1, r2);
-       }
-
-       @Override
-       final public void removeEntry(QueryProcessor provider) {
-               provider.cache.remove(this);
-       }
-
-       final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Objects entry) throws DatabaseException {
-
-               class AssertionMapProc implements IntProcedure {
-
-                       boolean first = true;
-
-                       private IntArray result;
-
-                       public void addStatement(int s, int p, int o) {
-
-                               if(result.size() == 0) {
-                                       result.add(s);
-                                       result.add(p);
-                                       result.add(o);
-                               } else {
-                                       for(int i = 0;i < result.sizeOrData ; i+=3) {
-                                               int existingP = result.data[i+1];
-                                               if(p == existingP) {
-                                                       int existingO = result.data[i+2];
-                                                       if(existingO == o) return;
-                                               }
-                                       }
-                                       result.add(s);
-                                       result.add(p);
-                                       result.add(o);
-                               }
-
-                       }
-
-                       @Override
-                       public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
-                               if(result == null) {
-                                       result = QueryCacheBase.resultAssertedStatements(graph, type, r2, entry, null);
-                               } else {
-                                       if (first) {
-                                               IntArray ia = result;
-                                               result = new IntArray();
-                                               if(ia.data != null) {
-                                                       for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);
-                                               }
-                                               first = false;
-                                       }
-                                       IntArray ia = QueryCacheBase.resultAssertedStatements(graph, type, r2, entry, null);
-                                       if(ia.data != null) {
-                                               for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);
-                                       }
-                               }
-                       }
+    public Objects(final int r1, final int r2) {
+        super(r1, r2);
+    }
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                       }
+    @Override
+    final public void removeEntry(QueryProcessor provider) {
+        provider.cache.remove(this);
+    }
 
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable throwable) {
-                       }
+    final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Objects entry) throws DatabaseException {
+
+        class AssertionMapProc implements IntProcedure {
+
+            boolean first = true;
+
+            private IntArray result;
+
+            public void addStatement(int s, int p, int o) {
+
+                if(result.size() == 0) {
+                    result.add(s);
+                    result.add(p);
+                    result.add(o);
+                } else {
+                    for(int i = 0;i < result.sizeOrData ; i+=3) {
+                        int existingP = result.data[i+1];
+                        if(p == existingP) {
+                            int existingO = result.data[i+2];
+                            if(existingO == o) return;
+                        }
+                    }
+                    result.add(s);
+                    result.add(p);
+                    result.add(o);
+                }
+
+            }
+
+            @Override
+            public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
+                if(result == null) {
+                    result = QueryCacheBase.resultAssertedStatements(graph, type, r2, entry, null);
+                } else {
+                    if (first) {
+                        IntArray ia = result;
+                        result = new IntArray();
+                        if(ia.data != null) {
+                            for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);
+                        }
+                        first = false;
+                    }
+                    IntArray ia = QueryCacheBase.resultAssertedStatements(graph, type, r2, entry, null);
+                    if(ia.data != null) {
+                        for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);
+                    }
+                }
+            }
+
+            @Override
+            public void finished(ReadGraphImpl graph) {
+            }
+
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable throwable) {
+            }
 
-               }
+        }
 
-               AssertionMapProc amp = new AssertionMapProc();
+        AssertionMapProc amp = new AssertionMapProc();
 
-               // This dependency could be cut
-               QueryCache.runnerPrincipalTypes(graph, r1, entry, null, amp);
+        // This dependency could be cut
+        QueryCache.runnerPrincipalTypes(graph, r1, entry, null, amp);
 
-               return amp.result;
+        return amp.result;
 
-       }
+    }
 
-       final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final IntProcedure procedure) throws DatabaseException {
+    final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final IntProcedure procedure) throws DatabaseException {
 
-               IntArray map = getAssertionMap(graph, r1, r2, parent);
-               if(map == null) {
-                       procedure.finished(graph);
-                       return;
-               }
+        IntArray map = getAssertionMap(graph, r1, r2, parent);
+        if(map == null) {
+            procedure.finished(graph);
+            return;
+        }
 
-               int size = map.size();
-               if(size == 3) {
-                   int value = map.data[2];
-                   procedure.execute(graph, value);
-                   procedure.finished(graph);
-               } else if(size == 0) {
-                       procedure.finished(graph);
-               } else {
+        int size = map.size();
+        if(size == 3) {
+            int value = map.data[2];
+            procedure.execute(graph, value);
+            procedure.finished(graph);
+        } else if(size == 0) {
+            procedure.finished(graph);
+        } else {
 
-                       int candidateS = map.data[0];
-                       int candidateO = map.data[2];
+            int candidateS = map.data[0];
+            int candidateO = map.data[2];
 
-                       IntSet candidateIs = null;
-                       try {
-                               candidateIs = QueryCache.resultSuperTypes(graph, candidateS, parent, null);
-                       } catch (DatabaseException e) {
-                               procedure.exception(graph, e);
-                               return;
-                       }
+            IntSet candidateIs = null;
+            try {
+                candidateIs = QueryCache.resultSuperTypes(graph, candidateS, parent, null);
+            } catch (DatabaseException e) {
+                procedure.exception(graph, e);
+                return;
+            }
 
-                       for(int i=3;i<map.size();i+=3) {
+            for(int i=3;i<map.size();i+=3) {
 
-                               int nextS = map.data[i];
-                               int nextO = map.data[i+2];
+                int nextS = map.data[i];
+                int nextO = map.data[i+2];
 
-                               if(nextS != candidateS) {
+                if(nextS != candidateS) {
 
-                                       if(candidateIs.contains(nextS)) {
+                    if(candidateIs.contains(nextS)) {
 
-                                               // Next is a super type of candidate => ignore next
+                        // Next is a super type of candidate => ignore next
 
-                                       } else {
+                    } else {
 
-                                               IntSet nextIs = null;
-                                               try {
-                                                       nextIs = QueryCache.resultSuperTypes(graph, nextS, parent, null);
-                                               } catch (DatabaseException e) {
-                                                       procedure.exception(graph, e);
-                                                       return;
-                                               }
+                        IntSet nextIs = null;
+                        try {
+                            nextIs = QueryCache.resultSuperTypes(graph, nextS, parent, null);
+                        } catch (DatabaseException e) {
+                            procedure.exception(graph, e);
+                            return;
+                        }
 
-                                               if(nextIs.contains(candidateS)) {
+                        if(nextIs.contains(candidateS)) {
 
-                                                       // Candidate is a super type of next => next is the new candidate
+                            // Candidate is a super type of next => next is the new candidate
 
-                                                       candidateS = nextS;
-                                                       candidateO = nextO;
-                                                       candidateIs = nextIs;
+                            candidateS = nextS;
+                            candidateO = nextO;
+                            candidateIs = nextIs;
 
-                                               } else {
+                        } else {
 
-                                                       // candidate and next are unrelated => error
-                                                       ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has conflicting assertions " + r1 + ", " + r2 + " " + map , r1);
-                                                       procedure.exception(graph, exception);
-                                                       return;                                         
+                            // candidate and next are unrelated => error
+                            ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has conflicting assertions " + r1 + ", " + r2 + " " + map , r1);
+                            procedure.exception(graph, exception);
+                            return;
 
-                                               }
+                        }
 
-                                       }
+                    }
 
-                               }
+                }
 
-                       }
+            }
 
-                       procedure.execute(graph, candidateO);
-                       procedure.finished(graph);
+            procedure.execute(graph, candidateO);
+            procedure.finished(graph);
 
-               }
+        }
 
-       }
+    }
 
-       final static InternalProcedure<IntSet> NOP = new InternalProcedure<IntSet>() {
+    final static InternalProcedure<IntSet> NOP = new InternalProcedure<IntSet>() {
 
-               @Override
-               public void execute(ReadGraphImpl graph, IntSet result) {
-               }
+        @Override
+        public void execute(ReadGraphImpl graph, IntSet result) {
+        }
 
-               @Override
-               public void exception(ReadGraphImpl graph, Throwable throwable) {
-               }
+        @Override
+        public void exception(ReadGraphImpl graph, Throwable throwable) {
+        }
 
-       };
+    };
 
-       final static TripleIntProcedure NOPT = new TripleIntProcedure() {
+    final static TripleIntProcedure NOPT = new TripleIntProcedure() {
 
 
-               @Override
-               public void exception(ReadGraphImpl graph, Throwable throwable) {
-               }
+        @Override
+        public void exception(ReadGraphImpl graph, Throwable throwable) {
+        }
 
-               @Override
-               public void execute(ReadGraphImpl graph, int s, int p, int o) {
-               }
+        @Override
+        public void execute(ReadGraphImpl graph, int s, int p, int o) {
+        }
 
-               @Override
-               public void finished(ReadGraphImpl graph) {
-               }
+        @Override
+        public void finished(ReadGraphImpl graph) {
+        }
 
-       };
+    };
 
-       // Search for one statement
-       final public void computeFunctionalIndex(ReadGraphImpl graph, final QueryProcessor provider, final RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
-               computeFunctionalIndex(graph, r1(), r2(), this, ri, procedure);
-       }
+    // Search for one statement
+    final public void computeFunctionalIndex(ReadGraphImpl graph, final QueryProcessor provider, final RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
+        computeFunctionalIndex(graph, r1(), r2(), this, ri, procedure);
+    }
 
-       // Search for one statement
-       final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
+    // Search for one statement
+    final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
 
-               if(ri.isFinal) {
+        if(ri.isFinal) {
 
-                       int result = graph.processor.querySupport.getFunctionalObject(r1, r2);
+            int result = graph.processor.querySupport.getFunctionalObject(r1, r2);
 
-                       if(result == 0) {
+            if(result == 0) {
 
-                               // Check for assertions
-                               forSingleAssertion(graph, r1, r2, parent, procedure);
+                // Check for assertions
+                forSingleAssertion(graph, r1, r2, parent, procedure);
 
-                       } else if (result == -1) {
+            } else if (result == -1) {
 
-                               graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
+                graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
 
-                                       @Override
-                                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                           procedure.execute(graph, i);
-                                       }
+                    @Override
+                    public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                        procedure.execute(graph, i);
+                    }
 
-                                       @Override
-                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                               if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                                       }
+                    @Override
+                    public void exception(ReadGraphImpl graph, Throwable t) {
+                        if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+                    }
 
-                                       @Override
-                                       public void finished(ReadGraphImpl graph) {
-                                       }
+                    @Override
+                    public void finished(ReadGraphImpl graph) {
+                    }
 
-                               });
+                });
 
-                               // Check for assertions
-                               forSingleAssertion(graph, r1, r2, parent, procedure);
+                // Check for assertions
+                forSingleAssertion(graph, r1, r2, parent, procedure);
 
-                       } else {
+            } else {
 
-                               // If functional relation was found there is no need to check assertions
+                // If functional relation was found there is no need to check assertions
                 procedure.execute(graph, result);
                 procedure.finished(graph);
 
-                               
-                       }
+            }
 
-               } else {
+        } else {
 
-                       // Note! The dependency is intentionally cut!
+            // Note! The dependency is intentionally cut!
             IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null);
             direct.forEach(graph, new SyncIntProcedure() {
 
-                               boolean found = false;
-
-                               @Override
-                               public void run(ReadGraphImpl graph) throws DatabaseException {
-
-                                       if(found) {
-                                               procedure.finished(graph);
-                                       } else {
+                /*
+                 * 0 = not found
+                 * 1 = found
+                 * 2 = exception
+                 */
+                int found = 0;
 
-                                               // Check for assertions
-                                               forSingleAssertion(graph, r1, r2, parent, procedure);
+                @Override
+                public void run(ReadGraphImpl graph) throws DatabaseException {
 
-                                       }
+                    if(found == 1) {
 
-                               }
+                        procedure.finished(graph);
 
-                               @Override
-                               public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
+                    } else if(found == 0) {
 
-                                       if(found) return;
+                        // Check for assertions
+                        forSingleAssertion(graph, r1, r2, parent, procedure);
 
-                                       if(pred == r2) {
+                    }
 
-                                               // Note! The dependency is intentionally cut!
-                                               QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
+                }
 
-                                                       @Override
-                                                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                @Override
+                public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
 
-                                                               if(!found) {
+                    if(found > 0)
+                        return;
 
-                                                                       procedure.execute(graph, i);
-                                                                       found = true;
+                    if(pred == r2) {
 
-                                                               } else {
+                        // Note! The dependency is intentionally cut!
+                        QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
 
-                                                                       ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement (r1=" + r1 + ", r2=" + r2 + ").", r1);
-                                                                       procedure.exception(graph, exception);
+                            @Override
+                            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
 
-                                                               }
+                                if(found == 0) {
 
-                                                       }
+                                    procedure.execute(graph, i);
+                                    found = 1;
 
-                                                       @Override
-                                                       public void finished(ReadGraphImpl graph) {
-                                                       }
+                                } else {
 
-                                                       @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                                               procedure.exception(graph, t);
-                                                       }
+                                    ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement (r1=" + r1 + ", r2=" + r2 + ").", r1);
+                                    procedure.exception(graph, exception);
+                                    found = 2;
 
-                                               });
+                                }
 
-                                       } else {
+                            }
 
-                                               QueryCache.runnerSuperRelations(graph, pred, parent, null, new InternalProcedure<IntSet>() {
+                            @Override
+                            public void finished(ReadGraphImpl graph) {
+                            }
 
-                                                       @Override
-                                                       public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
+                            @Override
+                            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
 
-                                                               if(found) return;
+                                procedure.exception(graph, t);
+                                found = 2;
 
-                                                               if(result.contains(r2)) {
+                            }
 
-                                                                       // Note! The dependency is intentionally cut!
-                                                                       QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
+                        });
 
-                                                                               @Override
-                                                                               public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                    } else {
 
-                                                                                       if(!found) {
+                        QueryCache.runnerSuperRelations(graph, pred, parent, null, new InternalProcedure<IntSet>() {
 
-                                                                                               procedure.execute(graph, i);
-                                                                                               found = true;
+                            @Override
+                            public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
 
-                                                                                       } else {
+                                if(found > 0)
+                                    return;
 
-                                                                                               ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement (r1=" + r1 + ", r2=" + r2 + ").", r1);
-                                                                                               procedure.exception(graph, exception);
+                                if(result.contains(r2)) {
 
-                                                                                       }
+                                    // Note! The dependency is intentionally cut!
+                                    QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
 
-                                                                               }
+                                        @Override
+                                        public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
 
-                                                                               @Override
-                                                                               public void finished(ReadGraphImpl graph) {
-                                                                               }
+                                            if(found == 0) {
 
-                                                                               @Override
-                                                                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                                                                       procedure.exception(graph, t);
-                                                                               }
+                                                procedure.execute(graph, i);
+                                                found = 1;
 
-                                                                       });
+                                            } else {
 
-                                                               }
+                                                ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement (r1=" + r1 + ", r2=" + r2 + ").", r1);
+                                                procedure.exception(graph, exception);
+                                                found = 2;
 
-                                                       }
+                                            }
 
-                                                       @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                                               procedure.exception(graph, t);
-                                                       }
+                                        }
 
-                                               });
+                                        @Override
+                                        public void finished(ReadGraphImpl graph) {
+                                        }
 
-                                       }
+                                        @Override
+                                        public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                            procedure.exception(graph, t);
+                                            found = 2;
+                                        }
 
-                               }
+                                    });
 
-                               @Override
-                               public void finished(ReadGraphImpl graph) throws DatabaseException {
+                                }
 
-                                       dec(graph);
+                            }
 
-                               }
+                            @Override
+                            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                procedure.exception(graph, t);
+                                found = 2;
+                            }
 
-                       });
+                        });
 
+                    }
 
-               }
+                }
 
-       }
+                @Override
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
+                    dec(graph);
+                }
 
-       final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final IntProcedure procedure) throws DatabaseException {
+            });
 
-               // Note! The dependency is intentionally cut!
-               QueryCache.runnerPrincipalTypes(graph, r1, null, null, new SyncIntProcedure() {
 
-                       @Override
-                       public void run(ReadGraphImpl graph) throws DatabaseException {
-                               procedure.finished(graph);
-                       }
-
-                       TripleIntProcedure proc = new TripleIntProcedure() {
+        }
 
-                               @Override
-                               public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
-                                       procedure.execute(graph, o);
-                               }
+    }
 
-                               @Override
-                               public void finished(ReadGraphImpl graph) throws DatabaseException {
-                                       dec(graph);
-                               }
+    final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final IntProcedure procedure) throws DatabaseException {
 
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                       if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                                       procedure.exception(graph, t);
-                                       dec(graph);
-                               }
+        // Note! The dependency is intentionally cut!
+        QueryCache.runnerPrincipalTypes(graph, r1, null, null, new SyncIntProcedure() {
 
-                       };
+            @Override
+            public void run(ReadGraphImpl graph) throws DatabaseException {
+                procedure.finished(graph);
+            }
 
-                       @Override
-                       public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
+            TripleIntProcedure proc = new TripleIntProcedure() {
 
-                               inc();
-                               QueryCache.runnerAssertedStatements(graph, type, r2, parent, null, proc);
+                @Override
+                public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
+                    procedure.execute(graph, o);
+                }
 
-                       }
+                @Override
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
+                    dec(graph);
+                }
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) throws DatabaseException {
-                               dec(graph);
-                       }
+                @Override
+                public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                    if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+                    procedure.exception(graph, t);
+                    dec(graph);
+                }
 
-               });
+            };
 
+            @Override
+            public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
 
-       }
+                inc();
+                QueryCache.runnerAssertedStatements(graph, type, r2, parent, null, proc);
 
-       final public static void computeNotFunctionalFinalIndex(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, RelationInfo ri, AsyncMultiProcedure<Resource> procedure) {
-               throw new Error();
-       }
+            }
 
-       final public void computeNotFunctionalIndex(ReadGraphImpl graph, RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
-               computeNotFunctionalIndex(graph, r1(), r2(), this, ri, procedure);
-       }
+            @Override
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
+                dec(graph);
+            }
 
-       final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
+        });
 
-               if(ri.isFinal) {
 
-                       graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
+    }
 
-                               @Override
-                               public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                       procedure.execute(graph, i);
-                               }
+    final public static void computeNotFunctionalFinalIndex(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, RelationInfo ri, AsyncMultiProcedure<Resource> procedure) {
+        throw new Error();
+    }
 
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                       if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                                       procedure.exception(graph, t);
-                               }
+    final public void computeNotFunctionalIndex(ReadGraphImpl graph, RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
+        computeNotFunctionalIndex(graph, r1(), r2(), this, ri, procedure);
+    }
 
-                               @Override
-                               public void finished(ReadGraphImpl graph) {
-                               }
+    final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
 
-                       });
+        if(ri.isFinal) {
 
-                       if(ri.isAsserted) {
-                               forAssertions(graph, r1, r2, parent, procedure);
-                       } else {
-                               procedure.finished(graph);
-                       }
+            graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
 
-               } else {
+                @Override
+                public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                    procedure.execute(graph, i);
+                }
 
-                       // Note! The dependency is intentionally cut!
-                       IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null);
-                       direct.forEach(graph, new SyncIntProcedure() {
+                @Override
+                public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                    if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+                    procedure.exception(graph, t);
+                }
 
-                               @Override
-                               public void run(ReadGraphImpl graph) throws DatabaseException {
-                                       forAssertions(graph, r1, r2, parent, procedure);
-                               }
+                @Override
+                public void finished(ReadGraphImpl graph) {
+                }
 
-                               @Override
-                               public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
+            });
 
-                                       if(pred == r2) {
+            if(ri.isAsserted) {
+                forAssertions(graph, r1, r2, parent, procedure);
+            } else {
+                procedure.finished(graph);
+            }
 
-                                               inc();
+        } else {
 
-                                               // Note! The dependency is intentionally cut!
-                                               QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
+            // Note! The dependency is intentionally cut!
+            IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null);
+            direct.forEach(graph, new SyncIntProcedure() {
 
-                                                       @Override
-                                                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                                               procedure.execute(graph, i);
-                                                       }
+                @Override
+                public void run(ReadGraphImpl graph) throws DatabaseException {
+                    forAssertions(graph, r1, r2, parent, procedure);
+                }
 
-                                                       @Override
-                                                       public void finished(ReadGraphImpl graph) throws DatabaseException {
-                                                               dec(graph);
-                                                       }
+                @Override
+                public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
 
-                                                       @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                                               procedure.exception(graph, t);
-                                                               dec(graph);
-                                                       }
+                    if(pred == r2) {
 
-                                               });
+                        // Note! The dependency is intentionally cut!
+                        QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
 
-                                       } else {
+                            @Override
+                            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                                procedure.execute(graph, i);
+                            }
 
-                                               inc();
+                            @Override
+                            public void finished(ReadGraphImpl graph) throws DatabaseException {
+                            }
 
-                                               QueryCache.runnerSuperRelations(graph, pred, parent, null, new InternalProcedure<IntSet>() {
+                            @Override
+                            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                procedure.exception(graph, t);
+                            }
 
-                                                       @Override
-                                                       public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
+                        });
 
-                                                               if(result.contains(r2)) {
+                    } else {
 
-                                                                       inc();
+                        try {
 
-                                                                       // Note! The dependency is intentionally cut!
-                                                                       QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
+                            IntSet result = QueryCache.resultSuperRelations(graph, pred, parent, null);
+                            if(result.contains(r2)) {
 
-                                                                               @Override
-                                                                               public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                                                                       procedure.execute(graph, i);
-                                                                               }
+                                inc();
 
-                                                                               @Override
-                                                                               public void finished(ReadGraphImpl graph) throws DatabaseException {
-                                                                                       dec(graph);
-                                                                               }
+                                // Note! The dependency is intentionally cut!
+                                QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
 
-                                                                               @Override
-                                                                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                                                                       if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                                                                                       procedure.exception(graph, t);
-                                                                                       dec(graph);
-                                                                               }
+                                    @Override
+                                    public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                                        procedure.execute(graph, i);
+                                    }
 
-                                                                       });
+                                    @Override
+                                    public void finished(ReadGraphImpl graph) throws DatabaseException {
+                                        dec(graph);
+                                    }
 
-                                                               }
+                                    @Override
+                                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                        if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+                                        procedure.exception(graph, t);
+                                        dec(graph);
+                                    }
 
-                                                               dec(graph);
+                                });
 
-                                                       }
+                            }
 
-                                                       @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                                               procedure.exception(graph, t);
-                                                               dec(graph);
-                                                       }
+                        } catch (Throwable e) {
+                            procedure.exception(graph, e);
+                        }
 
-                                               });
+                    }
 
-                                       }
+                }
 
-                               }
+                @Override
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
+                    dec(graph);
+                }
 
-                               @Override
-                               public void finished(ReadGraphImpl graph) throws DatabaseException {
-                                       dec(graph);
-                               }
+            });
 
-                       });
+        }
 
-               }
+    }
 
-       }
+    public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
+        computeForEach(graph, r1(), r2(), this, procedure);
+        return getResult();
+    }
 
-       public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
-               computeForEach(graph, r1(), r2(), this, procedure);
-               return getResult();
-       }
+    public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure_) throws DatabaseException {
 
-       public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure_) throws DatabaseException {
+        IntProcedure procedure = entry != null ? entry : procedure_;
 
-           IntProcedure procedure = entry != null ? entry : procedure_;
-           
         RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, r2, entry, null);
         graph.ensureLoaded(r1, r2);
         if(ri.isFunctional) {
@@ -607,63 +604,63 @@ public final class Objects extends CollectionBinaryQuery<IntProcedure> implement
         } else {
             computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
         }
-               
-               if(entry != null) entry.performFromCache(graph, procedure_);
 
-       }
+        if(entry != null) entry.performFromCache(graph, procedure_);
 
-       @Override
-       public String toString() {
-               return "Objects[" + r1() + " - " + r2() + "]";
-       }
+    }
 
-       @Override
-       public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
+    @Override
+    public String toString() {
+        return "Objects[" + r1() + " - " + r2() + "]";
+    }
 
-               assert(isReady());
+    @Override
+    public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
 
-               if(handleException(graph, procedure)) return getResult();
+        assert(isReady());
 
-               final IntArray value = (IntArray)getResult();
-               if(value.data == null) {
-                       if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
-               } else {
-                       for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
-               }
+        if(handleException(graph, procedure)) return getResult();
 
-               procedure.finished(graph);
-               
-               return value;
+        final IntArray value = (IntArray)getResult();
+        if(value.data == null) {
+            if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
+        } else {
+            for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
+        }
+
+        procedure.finished(graph);
 
-       }
+        return value;
 
-       @Override
-       public void recompute(ReadGraphImpl graph) throws DatabaseException {
+    }
 
-               compute(graph, new IntProcedureAdapter() {
+    @Override
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                       }
+        compute(graph, new IntProcedureAdapter() {
 
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               new Error("Error in recompute.", t).printStackTrace();
-                       }
+            @Override
+            public void finished(ReadGraphImpl graph) {
+            }
 
-               });
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable t) {
+                new Error("Error in recompute.", t).printStackTrace();
+            }
 
-       }
+        });
 
-       @Override
-       public int type() {
-               return RequestFlags.IMMEDIATE_UPDATE;
-       }
+    }
 
-       @Override
-       boolean isImmutable(ReadGraphImpl graph) {
-               return graph.processor.isImmutable(r1());
-       }
+    @Override
+    public int type() {
+        return RequestFlags.IMMEDIATE_UPDATE;
+    }
+
+    @Override
+    boolean isImmutable(ReadGraphImpl graph) {
+        return graph.processor.isImmutable(r1());
+    }
 
     @Override
     public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
index 6652f1b6bac9aac657ec22aa8c2f2d329591b8aa..218777413fe496a3afbba9d8dfb142a1fd0f1194 100644 (file)
@@ -11,8 +11,6 @@
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.concurrent.atomic.AtomicBoolean;
-
 import org.simantics.db.RelationInfo;
 import org.simantics.db.common.exception.DebugException;
 import org.simantics.db.exception.DatabaseException;
@@ -24,93 +22,93 @@ import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.request.RequestFlags;
 
 public final class Statements extends CollectionBinaryQuery<TripleIntProcedure> implements TripleIntProcedure {
-       
+
     public Statements(final int r1, final int r2) {
         super(r1, r2);
     }
-    
+
     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) throws DatabaseException {
-        
-       assert(r1 != 0);
-       assert(r2 != 0);
-       
+
+        assert(r1 != 0);
+        assert(r2 != 0);
+
         if(parent == null && listener == null) {
-               Statements.computeForEach(graph, r1, r2, null, procedure);
-               return;
+            Statements.computeForEach(graph, r1, r2, null, procedure);
+            return;
         }
-        
+
         QueryCache.runnerStatements(graph, r1, r2, parent, listener, procedure);
-         
+
     }
 
-       @Override
-       final public void removeEntry(QueryProcessor provider) {
+    @Override
+    final public void removeEntry(QueryProcessor provider) {
         provider.cache.remove(this);
-       }
-       
+    }
+
     final static TripleIntProcedure NOPT = new TripleIntProcedure() {
 
 
-               @Override
-               public void exception(ReadGraphImpl graph, Throwable throwable) {
-               }
+        @Override
+        public void exception(ReadGraphImpl graph, Throwable throwable) {
+        }
 
-               @Override
-               public void execute(ReadGraphImpl graph, int s, int p, int o) {
-               }
+        @Override
+        public void execute(ReadGraphImpl graph, int s, int p, int o) {
+        }
+
+        @Override
+        public void finished(ReadGraphImpl graph) {
+        }
 
-               @Override
-               public void finished(ReadGraphImpl graph) {
-               }
-       
     };
-       
+
     final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Statements entry) throws DatabaseException {
-       
+
         class AssertionMapProc implements IntProcedure {
-               
-               boolean first = true;
-
-               private IntArray result;
-
-               public void addStatement(int s, int p, int o) {
-                       
-                       if(result.size() == 0) {
-                               result.add(s);
-                               result.add(p);
-                               result.add(o);
-                       } else {
-                               for(int i = 0;i < result.sizeOrData ; i+=3) {
-                                       int existingP = result.data[i+1];
-                                       if(p == existingP) {
-                                               int existingO = result.data[i+2];
-                                               if(existingO == o) return;
-                                       }
-                               }
-                               result.add(s);
-                               result.add(p);
-                               result.add(o);
-                       }
-                       
-               }
-               
+
+            boolean first = true;
+
+            private IntArray result;
+
+            public void addStatement(int s, int p, int o) {
+
+                if(result.size() == 0) {
+                    result.add(s);
+                    result.add(p);
+                    result.add(o);
+                } else {
+                    for(int i = 0;i < result.sizeOrData ; i+=3) {
+                        int existingP = result.data[i+1];
+                        if(p == existingP) {
+                            int existingO = result.data[i+2];
+                            if(existingO == o) return;
+                        }
+                    }
+                    result.add(s);
+                    result.add(p);
+                    result.add(o);
+                }
+
+            }
+
             @Override
             public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
                 if(result == null) {
-                       result = QueryCache.resultAssertedStatements(graph, type, r2, entry, null);
+                    result = QueryCache.resultAssertedStatements(graph, type, r2, entry, null);
                 } else {
-                       if (first) {
-                               IntArray ia = result;
-                               result = new IntArray();
-                               if(ia.data != null) {
-                                       for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);
-                               }
-                               first = false;
-                       }
-                       IntArray ia = QueryCache.resultAssertedStatements(graph, type, r2, entry, null);
-                       if(ia.data != null) {
-                               for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);
-                       }
+                    if (first) {
+                        IntArray ia = result;
+                        result = new IntArray();
+                        if(ia.data != null) {
+                            for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);
+                        }
+                        first = false;
+                    }
+                    IntArray ia = QueryCache.resultAssertedStatements(graph, type, r2, entry, null);
+                    if(ia.data != null) {
+                        for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);
+                    }
                 }
             }
 
@@ -118,322 +116,336 @@ public final class Statements extends CollectionBinaryQuery<TripleIntProcedure>
             public void finished(ReadGraphImpl graph) {
             }
 
-               @Override
-               public void exception(ReadGraphImpl graph, Throwable throwable) {
-               }
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable throwable) {
+            }
 
         }
-        
+
         AssertionMapProc amp = new AssertionMapProc();
 
         // This dependency could be cut
         QueryCache.runnerPrincipalTypes(graph, r1, entry, null, amp);
-       
+
         return amp.result;
-       
+
     }
-       
+
     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, parent);
-       if(map == null) {
-               procedure.finished(graph);
+
+        IntArray map = getAssertionMap(graph, r1, r2, parent);
+        if(map == null) {
+            procedure.finished(graph);
             return;
-       }
-        
+        }
+
         int size = map.size();
         if(size == 3) {
 
-               int s = map.data[0];
-               int p = map.data[1];
-               int o = map.data[2];
-               
-                       procedure.execute(graph, s,p,o);
-                       procedure.finished(graph);
+            int s = map.data[0];
+            int p = map.data[1];
+            int o = map.data[2];
+
+            procedure.execute(graph, s,p,o);
+            procedure.finished(graph);
 
         } else if(size == 0) {
 
-               procedure.finished(graph);
-               
+            procedure.finished(graph);
+
         } else {
 
-               int candidateS = map.data[0];
-               int candidateP = map.data[1];
-               int candidateO = map.data[2];
-               
-                       IntSet candidateIs = null;
-                       try {
-                               candidateIs = QueryCache.resultSuperTypes(graph, candidateS, parent, null);
-                       } catch (DatabaseException e) {
-                               procedure.exception(graph, e);
-                               return;
-                       }
-
-                       for(int i=3;i<map.size();i+=3) {
-
-                               int nextS = map.data[i];
-                               int nextP = map.data[i+1];
-                               int nextO = map.data[i+2];
-                       
-                               if(nextS != candidateS) {
-
-                               if(candidateIs.contains(nextS)) {
-                                       
-                                       // Next is a super type of candidate => ignore next
-                                       
-                               } else {
-                               
-                                       IntSet nextIs = null;
-                                       try {
-                                               nextIs = QueryCache.resultSuperTypes(graph, nextS, parent, null);
-                                       } catch (DatabaseException e) {
-                                               procedure.exception(graph, e);
-                                               return;
-                                       }
-                                       
-                                       if(nextIs.contains(candidateS)) {
-
-                                               // Candidate is a super type of next => next is the new candidate
-                                               
-                                               candidateS = nextS;
-                                               candidateP = nextP;
-                                               candidateO = nextO;
-                                               candidateIs = nextIs;
-                                               
-                                       } else {
-                                               // candidate and next are unrelated => error
-                                               ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has conflicting assertions.", r1);
-                                               procedure.exception(graph, exception);
-                                               return;                                         
-                                       }
-                               
-                               }
-                               
-                       }
-                       
-               }
-               
+            int candidateS = map.data[0];
+            int candidateP = map.data[1];
+            int candidateO = map.data[2];
+
+            IntSet candidateIs = null;
+            try {
+                candidateIs = QueryCache.resultSuperTypes(graph, candidateS, parent, null);
+            } catch (DatabaseException e) {
+                procedure.exception(graph, e);
+                return;
+            }
+
+            for(int i=3;i<map.size();i+=3) {
+
+                int nextS = map.data[i];
+                int nextP = map.data[i+1];
+                int nextO = map.data[i+2];
+
+                if(nextS != candidateS) {
+
+                    if(candidateIs.contains(nextS)) {
+
+                        // Next is a super type of candidate => ignore next
+
+                    } else {
+
+                        IntSet nextIs = null;
+                        try {
+                            nextIs = QueryCache.resultSuperTypes(graph, nextS, parent, null);
+                        } catch (DatabaseException e) {
+                            procedure.exception(graph, e);
+                            return;
+                        }
+
+                        if(nextIs.contains(candidateS)) {
+
+                            // Candidate is a super type of next => next is the new candidate
+
+                            candidateS = nextS;
+                            candidateP = nextP;
+                            candidateO = nextO;
+                            candidateIs = nextIs;
+
+                        } else {
+                            // candidate and next are unrelated => error
+                            ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has conflicting assertions.", r1);
+                            procedure.exception(graph, exception);
+                            return;                                            
+                        }
+
+                    }
+
+                }
+
+            }
+
             procedure.execute(graph, candidateS, candidateP, candidateO);
             procedure.finished(graph);
-               
+
         }
-        
+
     }
-    
+
     final static InternalProcedure<IntSet> NOP = new InternalProcedure<IntSet>() {
 
-               @Override
-               public void execute(ReadGraphImpl graph, IntSet result) {
-               }
+        @Override
+        public void execute(ReadGraphImpl graph, IntSet result) {
+        }
+
+        @Override
+        public void exception(ReadGraphImpl graph, Throwable throwable) {
+        }
 
-               @Override
-               public void exception(ReadGraphImpl graph, Throwable throwable) {
-               }
-       
     };
-       
-       // Search for one statement
-       final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException {
-        
+
+    // Search for one statement
+    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) {
-            
-               int result = graph.processor.querySupport.getFunctionalObject(r1, r2);
 
-               if(result == 0) {
+            int result = graph.processor.querySupport.getFunctionalObject(r1, r2);
 
-               // Check for assertions
-               forSingleAssertion(graph, r1, r2, parent, procedure);
+            if(result == 0) {
 
-               } else if(result == -1) {
+                // Check for assertions
+                forSingleAssertion(graph, r1, r2, parent, procedure);
 
-               graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
+            } else if(result == -1) {
+
+                graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
+
+                    @Override
+                    public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                        procedure.execute(graph, r1, r2, i);
+                    }
 
-                       @Override
-                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                               procedure.execute(graph, r1, r2, i);
-                       }
+                    @Override
+                    public void exception(ReadGraphImpl graph, Throwable t) {
+                        if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+                    }
 
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                       }
+                    @Override
+                    public void finished(ReadGraphImpl graph) {
+                    }
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                       }
+                });
 
-               });
+                // Check for assertions
+                forSingleAssertion(graph, r1, r2, parent, procedure);
 
-               // Check for assertions
-               forSingleAssertion(graph, r1, r2, parent, procedure);
-                       
-               } else {
+            } else {
 
-               // If functional relation was found there is no need to check assertions
+                // If functional relation was found there is no need to check assertions
                 procedure.execute(graph, r1, r2, result);
                 procedure.finished(graph);
-                       
-               }
 
-            
+            }
+
+
         } else {
-            
-            final AtomicBoolean found = new AtomicBoolean(false);
-            
+
             // Note! The dependency is intentionally cut!
             IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null);
             direct.forEach(graph, new SyncIntProcedure() {
-                
+
+                /*
+                 * 0 = not found
+                 * 1 = found
+                 * 2 = exception
+                 */
+                int found = 0;
+
                 @Override
                 public void run(ReadGraphImpl graph) throws DatabaseException {
-                    
-                    if(found.get()) {
-                       procedure.finished(graph);
-                    } else {
+
+                    if(found == 1) {
+
+                        procedure.finished(graph);
+
+                    } else if(found == 0) {
+
                         // Check for assertions
                         forSingleAssertion(graph, r1, r2, parent, procedure);
+
                     }
-                    
+
                 }
 
                 @Override
                 public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
-                    
-                    if(found.get()) return;
+
+                    if(found > 0)
+                        return;
 
                     if(pred == r2) {
-                        
-                        inc();
-                        
+
                         // Note! The dependency is intentionally cut!
                         QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
 
                             @Override
                             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                
-                               if(found.compareAndSet(false, true)) {
+
+                                if(found == 0) {
+
                                     procedure.execute(graph, r1, pred, i);
-                               } else {
-                                       ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1);
-                                       procedure.exception(graph, exception);
-                               }
+                                    found = 1;
+
+                                } else {
+
+                                    ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1);
+                                    procedure.exception(graph, exception);
+                                    found = 2;
+
+                                }
 
                             }
 
                             @Override
                             public void finished(ReadGraphImpl graph) throws DatabaseException {
-                                dec(graph);
                             }
-                                               
-                                               @Override
-                                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                                       procedure.exception(graph, t);
-                                dec(graph);
-                                   }
+
+                            @Override
+                            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+
+                                procedure.exception(graph, t);
+                                found = 2;
+
+                            }
 
                         });
 
                     } else {
-                        
-                        inc();
-                    
+
                         QueryCache.runnerSuperRelations(graph, pred, parent, null, new InternalProcedure<IntSet>() {
-    
+
                             @Override
                             public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
-                                
-                                if(found.get()) {
-                                    dec(graph);
+
+                                if(found > 0)
                                     return;
-                                }
 
                                 if(result.contains(r2)) {
-                                    
+
                                     inc();
-                                    
+
                                     // Note! The dependency is intentionally cut!
                                     QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
-    
+
                                         @Override
                                         public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                            
-                                               if(found.compareAndSet(false, true)) {
-                                               procedure.execute(graph, r1, pred, i);
-                                               } else {
-                                               ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1);
-                                               procedure.exception(graph, exception);
-                                               }
-                                            
+
+                                            if(found == 0) {
+
+                                                procedure.execute(graph, r1, pred, i);
+                                                found = 1;
+
+                                            } else {
+
+                                                ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement (r1=" + r1 + ", r2=" + r2 + ").", r1);
+                                                procedure.exception(graph, exception);
+                                                found = 2;
+
+                                            }
+
                                         }
-    
+
                                         @Override
                                         public void finished(ReadGraphImpl graph) throws DatabaseException {
-                                            dec(graph);
                                         }
-                                                       
-                                                       @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                                               procedure.exception(graph, t);
-                                            dec(graph);
-                                           }
+
+                                        @Override
+                                        public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                            procedure.exception(graph, t);
+                                            found = 2;
+                                        }
 
                                     });
-    
+
                                 }
-                                
-                                dec(graph);
-                                
+
+                            }
+
+                            @Override
+                            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                procedure.exception(graph, t);
+                                found = 2;
                             }
-                                               
-                                               @Override
-                                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                                       procedure.exception(graph, t);
-                                dec(graph);
-                                   }
 
                         });
-                        
+
                     }
-                    
+
                 }
 
                 @Override
                 public void finished(ReadGraphImpl graph) throws DatabaseException {
                     dec(graph);
                 }
-                
+
             });
-            
+
         }
 
-       }
-    
+    }
+
     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, parent, null, new SyncIntProcedure() {
-            
+        QueryCache.runnerPrincipalTypes(graph, r1, parent, null, new SyncIntProcedure() {
+
             @Override
             public void run(ReadGraphImpl graph) throws DatabaseException {
                 procedure.finished(graph);
             }
-            
+
             TripleIntProcedure proc = new TripleIntProcedureAdapter() {
 
                 @Override
                 public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
-                       procedure.execute(graph, s, p, o);
+                    procedure.execute(graph, s, p, o);
                 }
 
                 @Override
                 public void finished(ReadGraphImpl graph) throws DatabaseException {
                     dec(graph);
                 }
-                               
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+
+                @Override
+                public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                     dec(graph);
-                                       procedure.exception(graph, t);
-                   }
+                    procedure.exception(graph, t);
+                }
 
             }; 
 
@@ -442,57 +454,57 @@ public final class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                 inc();
                 QueryCache.runnerAssertedStatements(graph, type, r2, parent, null, proc);
             }
-            
+
             @Override
             public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);       
             }
-            
+
             @Override
             public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                 dec(graph);
             }
-            
+
         });
-        
+
 
     }
 
-       final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, 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) {
+        if(ri.isFinal) {
 
-                       graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
+            graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
 
-                               @Override
-                               public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                       procedure.execute(graph, r1, r2, i);
-                               }
+                @Override
+                public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                    procedure.execute(graph, r1, r2, i);
+                }
 
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                       if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                                       procedure.exception(graph, t);
-                               }
+                @Override
+                public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                    if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+                    procedure.exception(graph, t);
+                }
 
-                               @Override
-                               public void finished(ReadGraphImpl graph) {
-                               }
+                @Override
+                public void finished(ReadGraphImpl graph) {
+                }
 
-                       });
+            });
 
-                       if(ri.isAsserted) {
-                               forAssertions(graph, r1, r2, parent, procedure);
-                       } else {
-                               procedure.finished(graph);
-                       }
+            if(ri.isAsserted) {
+                forAssertions(graph, r1, r2, parent, procedure);
+            } else {
+                procedure.finished(graph);
+            }
 
         } else {
 
             // Note! The dependency is intentionally cut!
             IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null);
             direct.forEach(graph, new SyncIntProcedure() {
-                
+
                 @Override
                 public void run(ReadGraphImpl graph) throws DatabaseException {
                     forAssertions(graph, r1, r2, parent, procedure);
@@ -502,36 +514,30 @@ public final class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                 public void execute(ReadGraphImpl graph, final int pred2) throws DatabaseException {
 
                     if(pred2 == r2) {
-                        
-                        inc();
-                        
+
                         // Note! The dependency is intentionally cut!
                         QueryCache.runnerDirectObjects(graph, r1, pred2, null, null, new IntProcedure() {
 
                             @Override
                             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                               procedure.execute(graph, r1, pred2, i);
+                                procedure.execute(graph, r1, pred2, i);
                             }
 
                             @Override
                             public void finished(ReadGraphImpl graph) throws DatabaseException {
-                                dec(graph);
                             }
-                                               
-                                               @Override
-                                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                                       procedure.exception(graph, t);
-                                dec(graph);
-                                   }
+
+                            @Override
+                            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                procedure.exception(graph, t);
+                            }
 
                         });
 
                     } else {
-                    
-//                        inc();
 
                         try {
-                            
+
                             IntSet result = QueryCache.resultSuperRelations(graph, pred2, parent, null);
                             if(result.contains(r2)) {
 
@@ -560,30 +566,30 @@ public final class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                                 });
 
                             }
-                            
+
                         } catch (Throwable e) {
                             procedure.exception(graph, e);
                         }
 
                     }
-                    
+
                 }
 
                 @Override
                 public void finished(ReadGraphImpl graph) throws DatabaseException {
                     dec(graph);
                 }
-                
+
             });
-            
+
         }
-        
+
     }
-       
+
     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_;
-       
+
         RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, r2, entry, null);
         graph.ensureLoaded(r1, r2);
         if(ri.isFunctional) {
@@ -593,16 +599,16 @@ public final class Statements extends CollectionBinaryQuery<TripleIntProcedure>
         }
 
         if(entry != null) entry.performFromCache(graph, procedure_);
-        
+
     }
-    
+
     @Override
     public String toString() {
-       return "Statements[" + r1() + " - " + r2() + "]";
+        return "Statements[" + r1() + " - " + r2() + "]";
     }
 
     final private void finish(ReadGraphImpl graph, TripleIntProcedure procedure) throws DatabaseException {
-       
+
         assert(assertPending());
 
         synchronized(this) {
@@ -614,95 +620,95 @@ public final class Statements extends CollectionBinaryQuery<TripleIntProcedure>
         final IntArray value = (IntArray)getResult();
 
         for(int i=0;i<value.size();i+=3) {
-               procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
+            procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
         }
-        
+
         procedure.finished(graph);
-       
+
     }
 
     synchronized public void addOrSet(int s, int p, int o) {
-        
-       assert(assertPending());
-        
-       IntArray value = (IntArray)getResult();
+
+        assert(assertPending());
+
+        IntArray value = (IntArray)getResult();
         value.add(s);
         value.add(p);
         value.add(o);
-        
+
     }
 
     final static public int r1(long id) {
         return (int)(id>>>32);
     }
-    
+
     final static public int r2(long id) {
         return (int)id;
     }
-    
+
     final public void addOrSetFunctional(int s, long po) {
-       
-       addOrSetFunctional(s, r1(po), r2(po));
-       
+
+        addOrSetFunctional(s, r1(po), r2(po));
+
     }
 
     final public void addOrSetFunctional(int s, int p, int o) {
-        
-       assert(assertPending());
-        
-       IntArray value = (IntArray)getResult();
+
+        assert(assertPending());
+
+        IntArray value = (IntArray)getResult();
         value.add(s);
         value.add(p);
         value.add(o);
-        
+
     }
-    
+
     @Override
     public Object performFromCache(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException {
 
-       assert(isReady());
+        assert(isReady());
 
         final IntArray value = (IntArray)getResult();
 
-       if(handleException(graph, procedure)) return value;
-       
+        if(handleException(graph, procedure)) return value;
+
         for(int i=0;i<value.size();i+=3) {
-               procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
+            procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
         }
 
         procedure.finished(graph);
-        
+
         return value;
-        
+
     }
-    
+
     @Override
     public void recompute(ReadGraphImpl graph) throws DatabaseException {
-        
+
         computeForEach(graph, r1(), r2(), this, new TripleIntProcedureAdapter() {
 
             @Override
             public void finished(ReadGraphImpl graph) {
             }
-                       
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               new Error("Error in recompute.", t).printStackTrace();
+
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable t) {
+                new Error("Error in recompute.", t).printStackTrace();
             }
 
         });
-        
+
     }
 
     @Override
     public int type() {
         return RequestFlags.IMMEDIATE_UPDATE;
     }
-    
+
 
     @Override
     boolean isImmutable(ReadGraphImpl graph) {
-       return graph.processor.isImmutable(r1());
+        return graph.processor.isImmutable(r1());
     }
 
     @Override
@@ -719,5 +725,5 @@ public final class Statements extends CollectionBinaryQuery<TripleIntProcedure>
     public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
         except(throwable);
     }
-    
+
 }