1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.query;
14 import gnu.trove.procedure.TIntProcedure;
15 import gnu.trove.set.hash.TIntHashSet;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.concurrent.Semaphore;
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;
26 final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
28 // public ArrayList<IntProcedure> procs = null;
30 private PrincipalTypes(final int resource) {
34 final static PrincipalTypes entry(final QueryProcessor provider, final int r) {
35 return (PrincipalTypes)provider.principalTypesMap.get(r);
38 final static void runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
40 QueryProcessor processor = graph.processor;
42 PrincipalTypes entry = (PrincipalTypes)processor.principalTypesMap.get(r);
45 entry = new PrincipalTypes(r);
47 entry.clearResult(processor.querySupport);
48 entry.putEntry(processor);
50 processor.performForEach(graph, entry, parent, listener, procedure);
54 if(entry.isPending()) {
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);
65 processor.performForEach(graph, entry, parent, listener, procedure);
70 final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
74 if(parent == null && listener == null) {
75 PrincipalTypes.computeForEach(graph, r, null, graph.processor, procedure);
77 runner(graph, r, parent, listener, procedure);
83 public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
84 return provider.principalTypesMap.get(id);
88 public void putEntry(QueryProcessor provider) {
89 provider.principalTypesMap.put(id, this);
93 final public void removeEntry(QueryProcessor provider) {
94 provider.principalTypesMap.remove(id);
99 private TIntHashSet set = null;
100 public int single = 0;
102 public boolean add(int val) {
103 if(single == val) return false;
108 if(set == null) set = new TIntHashSet(4);
115 if(single == 0) return 0;
116 if(set == null) return 1;
117 return set.size() + 1;
121 public int[] toArray() {
123 int[] result = Arrays.copyOf(set.toArray(), set.size() + 1);
124 result[set.size()] = single;
129 public void forEach(TIntProcedure proc) {
130 proc.execute(single);
131 if(set != null) set.forEach(proc);
137 public Object computeForEach(final ReadGraphImpl procedureGraph, final QueryProcessor provider, final IntProcedure proc, final boolean store) {
139 return computeForEach(procedureGraph, id, this, provider, proc);
143 public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final QueryProcessor provider, final IntProcedure proc) {
145 provider.querySupport.ensureLoaded(graph, id);
148 int ret = provider.querySupport.getSingleInstance(id);
152 entry.finish(graph, provider);
154 proc.execute(graph, ret);
155 proc.finished(graph);
159 final int instanceOf = provider.getInstanceOf();
160 final int inherits = provider.getInherits();
161 final int subrelationOf = provider.getSubrelationOf();
163 final Koss indirect = new Koss();
164 final Koss material = new Koss();
166 IntProcedure directProc = new IntProcedure() {
169 public void execute(ReadGraphImpl graph, int i) {
174 public void exception(ReadGraphImpl graph, Throwable t) {
175 proc.exception(graph, t);
179 public void finished(ReadGraphImpl graph) {
184 IntProcedure indirectProc = new IntProcedure() {
187 public void execute(ReadGraphImpl graph, int i) {
192 public void exception(ReadGraphImpl graph, Throwable t) {
193 proc.exception(graph, t);
197 public void finished(ReadGraphImpl graph) {
202 provider.querySupport.getObjects(graph, id, instanceOf, directProc);
203 provider.querySupport.getObjects(graph, id, inherits, indirectProc);
204 provider.querySupport.getObjects(graph, id, subrelationOf, indirectProc);
206 if(indirect.size() == 0) {
207 int size = material.size();
209 if(entry != null) entry.finish(graph, provider);
210 proc.finished(graph);
212 } else if(size == 1) {
213 int single = material.single;
216 entry.finish(graph, provider);
218 proc.execute(graph, single);
219 proc.finished(graph);
222 addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
227 // final AtomicInteger finishes = new AtomicInteger(0);
229 indirect.forEach(new TIntProcedure() {
234 public boolean execute(final int arg0) {
238 int current = (++finishes);
239 if(current == indirect.size()) {
240 int size = material.size();
242 if(entry != null) entry.finish(graph, provider);
243 proc.finished(graph);
245 } else if(size == 1) {
246 int single = material.single;
249 entry.finish(graph, provider);
251 proc.execute(graph, single);
252 proc.finished(graph);
255 addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
262 PrincipalTypes.queryEach(graph, arg0, provider, entry, null, new IntProcedure() {
265 public void execute(ReadGraphImpl graph, int i) {
266 synchronized(material) {
272 public void finished(ReadGraphImpl graph) {
274 int current = (++finishes);
275 if(current == indirect.size()) {
276 int size = material.size();
278 if(entry != null) entry.finish(graph, provider);
279 proc.finished(graph);
281 } else if(size == 1) {
282 int single = material.single;
285 entry.finish(graph, provider);
287 proc.execute(graph, single);
288 proc.finished(graph);
291 addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
299 public void exception(ReadGraphImpl graph, Throwable t) {
300 proc.exception(graph, t);
315 private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final PrincipalTypes entry, final QueryProcessor provider, final IntProcedure proc) {
318 for(int i : material) {
319 if(!rejects.contains(i)) {
323 entry.finish(graph, provider);
326 for(int i : material) {
327 if(!rejects.contains(i)) {
328 proc.execute(graph, i);
332 proc.finished(graph);
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) {
340 // if((counter++ % 500) == 0) System.out.println("PT " + counter + " mat = " + material.length);
342 if(index == material.length) {
343 finish(graph, rejects, material, entry, provider, proc);
347 int type = material[index++];
348 while(rejects.contains(type)) {
349 if(index == material.length) {
350 finish(graph, rejects, material, entry, provider, proc);
353 type = material[index++];
355 final int nextIndex = index;
357 SuperTypes.queryEach(graph, type, provider, entry, null, new InternalProcedure<IntSet>() {
360 public void execute(ReadGraphImpl graph, IntSet supers) {
362 synchronized(rejects) {
364 supers.forEach(new TIntProcedure() {
367 public boolean execute(int arg0) {
376 addPrincipalType(graph, rejects, material, nextIndex, provider, entry, proc);
381 public void exception(ReadGraphImpl graph, Throwable t) {
382 proc.exception(graph, t);
390 public String toString() {
391 return "PrincipalTypes[" + id + "]";
394 final private void add(int val) {
396 IntArray v = (IntArray)getResult();
400 final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
404 // ArrayList<IntProcedure> p = null;
416 // IntArray v = (IntArray)getResult();
418 // if(v.data == null) {
419 // if(v.sizeOrData != IntArray.NO_DATA) {
420 // for(IntProcedure proc : p) proc.execute(graph, v.sizeOrData);
423 // for(IntProcedure proc : p) {
424 // for(int i = 0;i < v.sizeOrData ; i++) proc.execute(graph, v.data[i]);
429 // for(IntProcedure proc : p) proc.finished(graph);
437 public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
441 if(handleException(graph, procedure)) return EXCEPTED;
443 final IntArray value = (IntArray)getResult();
444 if(value.data == null) {
445 if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
447 for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
450 procedure.finished(graph);
457 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
459 final Semaphore s = new Semaphore(0);
461 computeForEach(graph, provider, new IntProcedureAdapter() {
464 public void finished(ReadGraphImpl graph) {
469 public void exception(ReadGraphImpl graph, Throwable t) {
471 new Error("Error in recompute.", t).printStackTrace();
476 while(!s.tryAcquire()) {
477 provider.resume(graph);
484 boolean isImmutable(ReadGraphImpl graph) {
485 return graph.processor.isImmutable(id);
489 protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry> result) {
490 // for(Objects o : Objects.entries(processor, id)) result.add(o);