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