]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/RelationInfoQuery.java
Still working for multiple readers
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / RelationInfoQuery.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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 final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<RelationInfo>> {
21
22         RelationInfoQuery(final int resource) {
23         super(resource);
24     }
25     
26         @Override
27         final public 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 entry, final InternalProcedure<RelationInfo> proc) 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, entry, 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                                 if(entry != null) entry.setResult(result);
48                                 proc.execute(graph, result);
49                         }
50
51                         @Override
52                         public void finished(ReadGraphImpl graph) throws DatabaseException {
53                                 if(done) return;
54                                 done = true;
55                                 RelationInfo result = new RelationInfo(r, isFunctional, isFinal, false);
56                                 if(entry != null) entry.setResult(result);
57                                 proc.execute(graph, result);
58                         }
59
60                         @Override
61                         public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
62                                 if(done) return;
63                                 done = true;
64                                 DatabaseException e = new DatabaseException("Internal error in RelationInfoQuery");
65                                 if(entry != null) entry.except(e);
66                                 proc.exception(graph, e);
67                         }
68                         
69                 });
70                 
71         }
72         
73         public static void computeForEach(ReadGraphImpl graph, int r, RelationInfoQuery entry, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
74                 
75                 QueryProcessor provider = graph.processor;
76                 
77             final int superRelationOf = provider.getSuperrelationOf();
78         assert(superRelationOf != 0);
79         
80         IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null);
81         IntSet types = QueryCache.resultTypes(graph, r, entry, null);
82         
83         computeAssertions(graph, r, !direct.contains(superRelationOf), types.contains(graph.processor.getFunctionalRelation()), entry, procedure);
84         
85         }
86
87         //@Override
88         public Object compute(ReadGraphImpl graph, final InternalProcedure<RelationInfo> procedure) throws DatabaseException {
89                 computeForEach(graph, id, this, procedure);
90                 return getResult();
91         }
92     
93     @Override
94     public String toString() {
95         return "RelationInfoQuery[" + id + "]";
96     }
97
98 //    public void addOrSet(ReadGraphImpl graph, final RelationInfo result, final QueryProcessor provider) {
99 //        
100 //      assert(isPending());
101 //      
102 //      synchronized(this) {
103 //
104 //          setResult(result);
105 //          setReady();
106 //              
107 //      }
108 //        
109 //    }
110     
111     @Override
112     public void setResult(Object result) {
113         super.setResult(result);
114         if(!(result instanceof RelationInfo) && !(result == NO_RESULT))
115                 System.err.println("foo");
116         setReady();
117     }
118
119     @Override
120     public Object performFromCache(ReadGraphImpl graph, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
121
122         assert(isReady());
123         
124         if(handleException(graph, procedure)) return EXCEPTED;
125         
126         RelationInfo result = getResult();
127         
128         procedure.execute(graph, result);
129         
130         return result;
131
132     }
133     
134     @Override
135     public int type() {
136         return RequestFlags.IMMEDIATE_UPDATE;
137     }
138
139         @Override
140         public void recompute(ReadGraphImpl graph) throws DatabaseException {
141                 
142         compute(graph, new InternalProcedure<RelationInfo>() {
143
144             @Override
145             public void execute(ReadGraphImpl graph, RelationInfo result) {
146             }
147             
148             @Override
149             public void exception(ReadGraphImpl graph, Throwable t) {
150                 throw new Error("Error in recompute.", t);
151             }
152
153         });
154                 
155         }
156     
157 }