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