]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedPredicates.java
98833067faf81050f879e2ec6b3594e7ec363426
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / AssertedPredicates.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 java.util.concurrent.Semaphore;
15
16 import org.simantics.db.RelationInfo;
17 import org.simantics.db.impl.graph.ReadGraphImpl;
18 import org.simantics.db.impl.procedure.IntProcedureAdapter;
19 import org.simantics.db.impl.procedure.InternalProcedure;
20 import org.simantics.db.procedure.ListenerBase;
21
22
23 final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure> {
24         
25     public AssertedPredicates(final int r) {
26         super(r);
27     }
28
29     public static AssertedPredicates newInstance(final int r) {
30         return new AssertedPredicates(r);
31     }
32     
33     final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
34
35         AssertedPredicates entry = (AssertedPredicates)provider.cache.assertedPredicatesMap.get(r);
36         if(entry == null) {
37                 
38                 entry = new AssertedPredicates(r);
39                 entry.setPending();
40                 entry.clearResult(provider.querySupport);
41                 entry.putEntry(provider);
42                 
43             provider.performForEach(graph, entry, parent, listener, procedure);
44             
45         } else {
46                 
47             if(!entry.isReady()) {
48                 synchronized(entry) {
49                     if(!entry.isReady()) {
50                         throw new IllegalStateException();
51 //                      if(entry.procs == null) entry.procs = new ArrayList<IntProcedure>();
52 //                      entry.procs.add(procedure);
53 //                      return;
54                     }
55                 }
56             }
57             provider.performForEach(graph, entry, parent, listener, procedure);
58         }
59
60     }
61     
62     final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
63         
64         if(parent == null && listener == null) {
65                 AssertedPredicates entry = (AssertedPredicates)provider.cache.assertedPredicatesMap.get(r);
66                 if(entry != null && entry.isReady()) { 
67                         entry.performFromCache(graph, provider, procedure);
68                         return;
69                 }
70         }
71         
72         runner(graph, r, provider, parent, listener, procedure);
73          
74     }
75
76     @Override
77     public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
78         return provider.cache.assertedPredicatesMap.get(id);
79     }
80         
81         @Override
82         public void putEntry(QueryProcessor provider) {
83         provider.cache.assertedPredicatesMap.put(id, this);
84         }
85
86         @Override
87         final public void removeEntry(QueryProcessor provider) {
88             provider.cache.assertedPredicatesMap.remove(id);
89         }
90         
91         void computeInheritedAssertions(ReadGraphImpl graph, int type, final QueryProcessor queryProvider, final IntProcedure proc, final boolean store) {
92
93             DirectObjects.queryEach(graph, type, queryProvider.getInherits(), queryProvider, this, null, new SyncIntProcedure() {
94
95             @Override
96             public void run(ReadGraphImpl graph) {
97                 
98 //                finish(graph, queryProvider);
99 //                      proc.finished(graph);
100                 
101             }
102
103             @Override
104             public void execute(ReadGraphImpl graph,int inh) {
105
106                 inc();
107                 
108                 AssertedPredicates.queryEach(graph, inh, queryProvider, AssertedPredicates.this, null, new IntProcedure() {
109
110                     @Override
111                     public void execute(ReadGraphImpl graph, int ass) {
112                             
113                         addOrSet(ass);
114 //                      proc.execute(graph, ass);
115                         
116                     }
117
118                     @Override
119                     public void finished(ReadGraphImpl graph) {
120                         dec(graph);
121                     }
122                                 
123                                 @Override
124                                 public void exception(ReadGraphImpl graph, Throwable t) {
125 //                                      proc.exception(graph, t);
126                     }
127                     
128                 });
129                 
130             }
131
132             @Override
133             public void finished(ReadGraphImpl graph) {
134                 
135                 dec(graph);
136                 
137             }
138             
139         });
140
141         }
142
143     @Override
144     public Object computeForEach(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure proc, final boolean store) {
145
146         computeInheritedAssertions(graph, id, queryProvider, proc, store);
147
148         DirectObjects.queryEach(graph, id, queryProvider.getAsserts(), queryProvider, this, null, new IntProcedure() {
149
150             @Override
151             public void execute(ReadGraphImpl graph, final int ass) {
152                 
153                 DirectObjects.queryEach(graph, ass, queryProvider.getHasPredicate(), queryProvider, AssertedPredicates.this, null, new IntProcedure() {
154
155                     @Override
156                     public void execute(ReadGraphImpl graph, final int pred) {
157
158                         addOrSetHiding(graph, pred, AssertedPredicates.this);
159 //                      proc.execute(graph, pred);
160                         return;
161                         
162                     }
163
164                     @Override
165                     public void finished(ReadGraphImpl graph) {
166                     }
167                                 
168                                 @Override
169                                 public void exception(ReadGraphImpl graph, Throwable t) {
170 //                                      proc.exception(graph, t);
171                     }
172
173                 });
174                 
175             }
176
177             @Override
178             public void finished(ReadGraphImpl graph) {
179             }
180
181                         @Override
182                         public void exception(ReadGraphImpl graph, Throwable t) {
183 //                              proc.exception(graph, t);
184                         }
185             
186         });
187         
188         finish(graph, queryProvider);
189
190         performFromCache(graph, queryProvider, proc);
191         
192         return getResult();
193         
194     }
195     
196     @Override
197     public String toString() {
198         return "AssertedPredicates[" + id + "]";
199     }
200     
201     final public void finish(ReadGraphImpl graph, QueryProcessor provider) {
202         
203         assert(!isReady());
204
205 //        ArrayList<IntProcedure> p = null;
206
207         synchronized(this) {
208
209                 setReady();
210 //            p = procs;
211 //            procs = null; 
212         
213         }
214
215 //        if(p != null) {
216 //              IntArray v = (IntArray)getResult();
217 //              if(v.data == null) {
218 //                  if(v.sizeOrData != IntArray.NO_DATA) {
219 //                      for(IntProcedure proc : p) proc.execute(graph, v.sizeOrData);
220 //                  }
221 //              } else {
222 //                  for(IntProcedure proc : p) {
223 //                      for(int i = 0;i < v.sizeOrData ; i++) proc.execute(graph, v.data[i]);
224 //                  }
225 //              }
226 //              
227 //              for(IntProcedure proc : p) proc.finished(graph);
228 //        }
229
230     }
231
232     synchronized private void addOrSet(int add) {
233
234         assert(isPending());
235         
236         IntArray value = (IntArray)getResult(); 
237         value.add(add);
238         
239     }
240
241         final static InternalProcedure<RelationInfo> ip = new InternalProcedure<RelationInfo>() {
242
243                 @Override
244                 public void execute(ReadGraphImpl graph, RelationInfo result) {
245                 }
246
247                 @Override
248                 public void exception(ReadGraphImpl graph, Throwable throwable) {
249                 }
250
251         };
252
253         final static InternalProcedure<IntSet> ip2 = new InternalProcedure<IntSet>() {
254
255                 @Override
256                 public void execute(ReadGraphImpl graph, IntSet result) {
257                 }
258
259                 @Override
260                 public void exception(ReadGraphImpl graph, Throwable throwable) {
261                 }
262
263         };
264
265         synchronized private void addOrSetHiding(ReadGraphImpl graph, int add, CacheEntry parent) {
266
267         assert(isPending());
268         
269         IntArray value = (IntArray)getResult(); 
270                 RelationInfo ri = RelationInfoQuery.queryEach(graph, add, graph.processor, parent, null, ip);
271                 if(ri.isFunctional) {
272                         // Replace existing functional predicate if found
273                         try {
274                                 IntSet supers = SuperRelations.queryEach2(graph, add, graph.processor, parent, null, ip2);
275                         if(value.data == null) {
276                             if(value.sizeOrData != IntArray.NO_DATA) {
277                                 if(supers.contains(value.sizeOrData)) {
278                                         value.sizeOrData = add;
279                                         return;
280                                 }
281                             }
282                         } else {
283                             for(int i = 0;i < value.sizeOrData ; i++) {
284                                 if(supers.contains(value.data[i])) {
285                                         value.data[i] = add;
286                                         return;
287                                 }
288                             }
289                         }
290                         } catch (Throwable e) {
291                                 except(e);
292                                 return;
293                         }
294                 }
295                 
296             // No replacement - append
297         value.add(add);
298
299     }
300     
301     @Override
302     public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
303
304         assert(isReady());
305         
306         if(handleException(graph, procedure)) return EXCEPTED;
307         
308         final IntArray value = (IntArray)getResult();
309         if(value.data == null) {
310             if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
311         } else {
312             for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
313         }
314
315         procedure.finished(graph);
316         
317         return value;
318         
319     }
320     
321     @Override
322     public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
323         
324         final Semaphore s = new Semaphore(0);
325
326         computeForEach(graph, provider, new IntProcedureAdapter() {
327
328             @Override
329             public void finished(ReadGraphImpl graph) {
330                 s.release();
331             }
332
333             @Override
334             public void exception(ReadGraphImpl graph, Throwable t) {
335                 s.release();
336                 new Error("Error in recompute.", t).printStackTrace();
337             }
338
339         }, true);
340
341         while(!s.tryAcquire()) {
342                 provider.resume(graph);
343         }
344         
345     }
346     
347     
348 }