]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/RelationInfoQuery.java
Attempt to fix regressions in new code base
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / RelationInfoQuery.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
14 import org.simantics.db.RelationInfo;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.impl.graph.ReadGraphImpl;
17 import org.simantics.db.impl.procedure.InternalProcedure;
18 import org.simantics.db.request.RequestFlags;
19
20 public final class RelationInfoQuery extends UnaryQueryP<RelationInfo> {
21
22         RelationInfoQuery(int resource) {
23                 super(resource);
24         }
25
26         @Override
27         public final void removeEntry(QueryProcessor provider) {
28                 provider.cache.remove(this);
29         }
30
31         private static void computeAssertions(ReadGraphImpl graph, int r, final boolean isFinal, final boolean isFunctional, RelationInfoQuery parent, final InternalProcedure<RelationInfo> procedure) throws DatabaseException {
32
33                 QueryProcessor processor = graph.processor;
34
35                 final int isUsedInAssertion = processor.getHasPredicateInverse();
36                 assert(isUsedInAssertion != 0);
37
38                 QueryCache.runnerDirectObjects(graph, r, isUsedInAssertion, parent, null, new IntProcedure() {
39
40                         boolean done = false;
41
42                         @Override
43                         public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
44                                 if(done) return;
45                                 done = true;
46                                 RelationInfo result = new RelationInfo(r, isFunctional, isFinal, true);
47                                 procedure.execute(graph, result);
48                         }
49
50                         @Override
51                         public void finished(ReadGraphImpl graph) throws DatabaseException {
52                                 if(done) return;
53                                 done = true;
54                                 RelationInfo result = new RelationInfo(r, isFunctional, isFinal, false);
55                                 procedure.execute(graph, result);
56                         }
57
58                         @Override
59                         public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
60                                 if(done) return;
61                                 done = true;
62                                 DatabaseException e = new DatabaseException("Internal error in RelationInfoQuery");
63                                 procedure.exception(graph, e);
64                         }
65
66                 });
67
68         }
69
70         public static void computeForEach(ReadGraphImpl graph, int r, RelationInfoQuery entry, InternalProcedure<RelationInfo> procedure_) throws DatabaseException {
71
72                 InternalProcedure<RelationInfo> procedure = entry != null ? entry : procedure_;
73
74                 QueryProcessor provider = graph.processor;
75
76                 final int superRelationOf = provider.getSuperrelationOf();
77                 assert(superRelationOf != 0);
78
79                 IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null);
80                 IntSet types = QueryCache.resultTypes(graph, r, entry, null);
81
82                 computeAssertions(graph, r, !direct.contains(superRelationOf), types.contains(graph.processor.getFunctionalRelation()), entry, procedure);
83
84                 if(entry != null) entry.performFromCache(graph, procedure_);
85
86         }
87
88         @Override
89         public void compute(ReadGraphImpl graph, final InternalProcedure<RelationInfo> procedure) throws DatabaseException {
90                 computeForEach(graph, id, this, procedure);
91         }
92
93         @Override
94         public String toString() {
95                 return "RelationInfoQuery[" + id + "]";
96         }
97
98         @Override
99         public int type() {
100                 return RequestFlags.IMMEDIATE_UPDATE;
101         }
102
103 }