]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PrincipalTypes.java
25e2b6fe3d317e682d41ebf42f60c2f6a628a83d
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / PrincipalTypes.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 import gnu.trove.set.hash.TIntHashSet;
16
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.concurrent.Semaphore;
20
21 import org.simantics.db.impl.graph.ReadGraphImpl;
22 import org.simantics.db.impl.procedure.IntProcedureAdapter;
23 import org.simantics.db.impl.procedure.InternalProcedure;
24 import org.simantics.db.procedure.ListenerBase;
25
26 final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
27
28 //      public ArrayList<IntProcedure> procs = null;
29
30         private PrincipalTypes(final int resource) {
31                 super(resource);
32         }
33
34         final static PrincipalTypes entry(final QueryProcessor provider, final int r) {
35                 return (PrincipalTypes)provider.principalTypesMap.get(r);
36         }
37
38         final static void runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
39
40         QueryProcessor processor = graph.processor;
41                 
42                 PrincipalTypes entry = (PrincipalTypes)processor.principalTypesMap.get(r); 
43                 if(entry == null) {
44
45                         entry = new PrincipalTypes(r);
46                 entry.setPending();
47                 entry.clearResult(processor.querySupport);
48                 entry.putEntry(processor);
49                         
50                 processor.performForEach(graph, entry, parent, listener, procedure);
51                         
52                 } else {
53                         
54             if(entry.isPending()) {
55                 synchronized(entry) {
56                     if(entry.isPending()) {
57                         throw new IllegalStateException();
58 //                                              if(entry.procs == null) entry.procs = new ArrayList<IntProcedure>(1);
59 //                                              entry.procs.add(procedure);
60 //                                              processor.registerDependencies(graph, entry, parent, listener, procedure, false);
61 //                                              return;
62                                         }
63                                 }
64                         }
65             processor.performForEach(graph, entry, parent, listener, procedure);
66                 }
67
68         }
69
70         final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
71
72                 assert(r != 0);
73
74         if(parent == null && listener == null) {
75                 PrincipalTypes.computeForEach(graph, r, null, graph.processor, procedure);
76         } else {
77                 runner(graph, r, parent, listener, procedure);
78         }
79
80         }
81
82         @Override
83         public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
84                 return provider.principalTypesMap.get(id);
85         }
86
87         @Override
88         public void putEntry(QueryProcessor provider) {
89                 provider.principalTypesMap.put(id, this);
90         }
91
92         @Override
93         final public void removeEntry(QueryProcessor provider) {
94                 provider.principalTypesMap.remove(id);
95         }
96
97         static class Koss {
98
99                 private TIntHashSet set = null;
100                 public int single = 0;
101
102                 public boolean add(int val) {
103                         if(single == val) return false;
104                         if(single == 0) {
105                                 single = val;
106                                 return true;
107                         }
108                         if(set == null) set = new TIntHashSet(4);
109                         set.add(val);
110                         return true;
111                 }
112
113                 public int size() {
114
115                         if(single == 0) return 0;
116                         if(set == null) return 1;
117                         return set.size() + 1;
118
119                 }
120
121                 public int[] toArray() {
122
123                         int[] result = Arrays.copyOf(set.toArray(), set.size() + 1);
124                         result[set.size()] = single;
125                         return result;
126
127                 }
128
129                 public void forEach(TIntProcedure proc) {
130                         proc.execute(single);
131                         if(set != null) set.forEach(proc);
132                 }
133
134         }
135
136         @Override
137         public Object computeForEach(final ReadGraphImpl procedureGraph, final QueryProcessor provider, final IntProcedure proc, final boolean store) {
138
139                 return computeForEach(procedureGraph, id, this, provider, proc);
140                 
141         }
142         
143         public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final QueryProcessor provider, final IntProcedure proc) {
144                 
145                 provider.querySupport.ensureLoaded(graph, id);
146                 assert(id != 0);
147
148                 int ret = provider.querySupport.getSingleInstance(id);
149                 if(ret > 0) {
150                         if(entry != null) {
151                                 entry.add(ret);
152                                 entry.finish(graph, provider);
153                         }
154                         proc.execute(graph, ret);
155                         proc.finished(graph);
156                         return ret;
157                 }
158
159                 final int instanceOf = provider.getInstanceOf();
160                 final int inherits = provider.getInherits();
161                 final int subrelationOf = provider.getSubrelationOf();
162
163                 final Koss indirect = new Koss();
164                 final Koss material = new Koss();
165
166                 IntProcedure directProc = new IntProcedure() {
167
168                         @Override
169                         public void execute(ReadGraphImpl graph, int i) {
170                                 material.add(i);
171                         }
172
173                         @Override
174                         public void exception(ReadGraphImpl graph, Throwable t) {
175                                 proc.exception(graph, t);
176                         }
177
178                         @Override
179                         public void finished(ReadGraphImpl graph) {
180                         }
181
182                 };
183
184                 IntProcedure indirectProc = new IntProcedure() {
185
186                         @Override
187                         public void execute(ReadGraphImpl graph, int i) {
188                                 indirect.add(i);
189                         }
190
191                         @Override
192                         public void exception(ReadGraphImpl graph, Throwable t) {
193                                 proc.exception(graph, t);
194                         }
195
196                         @Override
197                         public void finished(ReadGraphImpl graph) {
198                         }
199
200                 };
201
202                 provider.querySupport.getObjects(graph, id, instanceOf, directProc);
203                 provider.querySupport.getObjects(graph, id, inherits, indirectProc);
204                 provider.querySupport.getObjects(graph, id, subrelationOf, indirectProc);
205
206                 if(indirect.size() == 0) {
207                         int size = material.size(); 
208                         if(size == 0) {
209                                 if(entry != null) entry.finish(graph, provider);
210                                 proc.finished(graph);
211                                 return null;
212                         } else if(size == 1) {
213                                 int single = material.single; 
214                                 if(entry != null) {
215                                         entry.add(single);
216                                         entry.finish(graph, provider);
217                                 }
218                                 proc.execute(graph, single);
219                                 proc.finished(graph);
220                                 return single;
221                         } else {
222                                 addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
223                                 return null;
224                         }
225                 }
226
227 //              final AtomicInteger finishes = new AtomicInteger(0);
228
229                 indirect.forEach(new TIntProcedure() {
230
231                         int finishes = 0;
232                         
233                         @Override
234                         public boolean execute(final int arg0) {
235
236                                 // No self-loops!
237                                 if(arg0 == id) {
238                                         int current = (++finishes);
239                                         if(current == indirect.size()) {
240                                                 int size = material.size(); 
241                                                 if(size == 0) {
242                                                         if(entry != null) entry.finish(graph, provider);
243                                                         proc.finished(graph);
244                                                         return true;
245                                                 } else if(size == 1) {
246                                                         int single = material.single; 
247                                                         if(entry != null) {
248                                                                 entry.add(single);
249                                                                 entry.finish(graph, provider);
250                                                         }
251                                                         proc.execute(graph, single);
252                                                         proc.finished(graph);
253                                                         return true;
254                                                 } else {
255                                                         addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
256                                                         return true;
257                                                 }
258                                         }
259                                         return true;
260                                 }
261
262                                 PrincipalTypes.queryEach(graph, arg0, provider, entry, null, new IntProcedure() {
263
264                                         @Override
265                                         public void execute(ReadGraphImpl graph, int i) {
266                                                 synchronized(material) {
267                                                         material.add(i);
268                                                 }
269                                         }
270
271                                         @Override
272                                         public void finished(ReadGraphImpl graph) {
273
274                                                 int current = (++finishes);
275                                                 if(current == indirect.size()) {
276                                                         int size = material.size(); 
277                                                         if(size == 0) {
278                                                                 if(entry != null) entry.finish(graph, provider);
279                                                                 proc.finished(graph);
280                                                                 return;
281                                                         } else if(size == 1) {
282                                                                 int single = material.single; 
283                                                                 if(entry != null) {
284                                                                         entry.add(single);
285                                                                         entry.finish(graph, provider);
286                                                                 }
287                                                                 proc.execute(graph, single);
288                                                                 proc.finished(graph);
289                                                                 return;
290                                                         } else {
291                                                                 addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
292                                                                 return;
293                                                         }
294                                                 }
295
296                                         }
297
298                                         @Override
299                                         public void exception(ReadGraphImpl graph, Throwable t) {
300                                                 proc.exception(graph, t);
301                                         }
302
303                                 });
304
305                                 return true;
306
307                         }
308
309                 });
310                 
311                 return null;
312
313         }
314
315         private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final PrincipalTypes entry, final QueryProcessor provider, final IntProcedure proc) {
316
317                 if(entry != null) {
318                         for(int i : material) {
319                                 if(!rejects.contains(i)) {
320                                         entry.add(i);
321                                 }
322                         }
323                         entry.finish(graph, provider);
324                 }
325
326                 for(int i : material) {
327                         if(!rejects.contains(i)) {
328                                 proc.execute(graph, i);
329                         }
330                 }
331
332                 proc.finished(graph);
333
334                 return;
335
336         }
337
338         private static void addPrincipalType(final ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, int index, final QueryProcessor provider, final PrincipalTypes entry, final IntProcedure proc) {
339
340                 //        if((counter++ % 500) == 0) System.out.println("PT " + counter + " mat = " + material.length);
341
342                 if(index == material.length) { 
343                         finish(graph, rejects, material, entry, provider, proc);
344                         return;
345                 }
346
347                 int type = material[index++];
348                 while(rejects.contains(type)) {
349                         if(index == material.length) { 
350                                 finish(graph, rejects, material, entry, provider, proc);
351                                 return;
352                         }
353                         type = material[index++];
354                 }
355                 final int nextIndex = index;
356
357                 SuperTypes.queryEach(graph, type, provider, entry, null, new InternalProcedure<IntSet>() {
358
359                         @Override
360                         public void execute(ReadGraphImpl graph, IntSet supers) {
361
362                                 synchronized(rejects) {
363
364                                         supers.forEach(new TIntProcedure() {
365
366                                                 @Override
367                                                 public boolean execute(int arg0) {
368                                                         rejects.add(arg0);
369                                                         return true;
370                                                 }
371
372                                         });
373
374                                 }
375
376                                 addPrincipalType(graph, rejects, material, nextIndex, provider, entry, proc);
377
378                         }
379
380                         @Override
381                         public void exception(ReadGraphImpl graph, Throwable t) {
382                                 proc.exception(graph, t);
383                         }
384
385                 });
386
387         }
388
389         @Override
390         public String toString() {
391                 return "PrincipalTypes[" + id + "]";
392         }
393
394         final private void add(int val) {
395                 assert(isPending());
396                 IntArray v = (IntArray)getResult();
397                 v.add(val);
398         }
399
400         final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
401
402                 assert(isPending());
403
404 //              ArrayList<IntProcedure> p = null;
405
406                 synchronized(this) {
407
408                         setReady();
409 //                      p = procs;
410 //                      procs = null;
411
412                 }
413
414 //              if(p != null) {
415 //
416 //                      IntArray v = (IntArray)getResult();
417 //                      if(v != null) {
418 //                              if(v.data == null) {
419 //                                      if(v.sizeOrData != IntArray.NO_DATA) {
420 //                                              for(IntProcedure proc : p) proc.execute(graph, v.sizeOrData);
421 //                                      }
422 //                              } else {
423 //                                      for(IntProcedure proc : p) {
424 //                                              for(int i = 0;i < v.sizeOrData ; i++) proc.execute(graph, v.data[i]);
425 //                                      }
426 //                              }
427 //                      }
428 //
429 //                      for(IntProcedure proc : p) proc.finished(graph);
430 //
431 //              }
432
433
434         }
435
436         @Override
437         public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
438
439                 assert(isReady());
440
441         if(handleException(graph, procedure)) return EXCEPTED;
442                 
443                 final IntArray value = (IntArray)getResult();
444                 if(value.data == null) {
445                         if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
446                 } else {
447                         for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
448                 }
449
450                 procedure.finished(graph);
451                 
452                 return getResult();
453
454         }
455
456         @Override
457         public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
458
459                 final Semaphore s = new Semaphore(0);
460
461                 computeForEach(graph, provider, new IntProcedureAdapter() {
462
463                         @Override
464                         public void finished(ReadGraphImpl graph) {
465                                 s.release();
466                         }
467
468                         @Override
469                         public void exception(ReadGraphImpl graph, Throwable t) {
470                                 s.release();
471                                 new Error("Error in recompute.", t).printStackTrace();
472                         }
473
474                 }, true);
475
476         while(!s.tryAcquire()) {
477                 provider.resume(graph);
478         }
479
480         }
481
482
483     @Override
484     boolean isImmutable(ReadGraphImpl graph) {
485         return graph.processor.isImmutable(id);
486     }
487     
488     @Override
489     protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry> result) {
490 //              for(Objects o : Objects.entries(processor, id)) result.add(o);
491     }
492
493 }