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 java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.concurrent.Semaphore;
18 import org.simantics.db.common.utils.Logger;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.impl.graph.ReadGraphImpl;
21 import org.simantics.db.impl.procedure.IntProcedureAdapter;
22 import org.simantics.db.impl.procedure.InternalProcedure;
23 import org.simantics.db.procedure.ListenerBase;
25 import gnu.trove.procedure.TIntProcedure;
26 import gnu.trove.set.hash.TIntHashSet;
28 final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
30 PrincipalTypes(final int resource) {
35 final public void removeEntry(QueryProcessor provider) {
36 provider.cache.remove(this);
41 private TIntHashSet set = null;
42 public int single = 0;
44 public boolean add(int val) {
45 if(single == val) return false;
50 if(set == null) set = new TIntHashSet(4);
57 if(single == 0) return 0;
58 if(set == null) return 1;
59 return set.size() + 1;
63 public int[] toArray() {
65 int[] result = Arrays.copyOf(set.toArray(), set.size() + 1);
66 result[set.size()] = single;
71 public void forEach(TIntProcedure proc) {
73 if(set != null) set.forEach(proc);
79 public Object compute(final ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
80 return computeForEach(graph, id, this, proc);
83 public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final IntProcedure proc) throws DatabaseException {
85 QueryProcessor provider = graph.processor;
87 provider.querySupport.ensureLoaded(graph, id);
90 int ret = provider.querySupport.getSingleInstance(id);
94 entry.finish(graph, provider);
96 proc.execute(graph, ret);
101 final int instanceOf = provider.getInstanceOf();
102 final int inherits = provider.getInherits();
103 final int subrelationOf = provider.getSubrelationOf();
105 final Koss indirect = new Koss();
106 final Koss material = new Koss();
108 IntProcedure directProc = new IntProcedure() {
111 public void execute(ReadGraphImpl graph, int i) {
116 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
117 proc.exception(graph, t);
121 public void finished(ReadGraphImpl graph) {
126 IntProcedure indirectProc = new IntProcedure() {
129 public void execute(ReadGraphImpl graph, int i) {
134 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
135 proc.exception(graph, t);
139 public void finished(ReadGraphImpl graph) {
144 provider.querySupport.getObjects(graph, id, instanceOf, directProc);
145 provider.querySupport.getObjects(graph, id, inherits, indirectProc);
146 provider.querySupport.getObjects(graph, id, subrelationOf, indirectProc);
148 if(indirect.size() == 0) {
149 int size = material.size();
151 if(entry != null) entry.finish(graph, provider);
152 proc.finished(graph);
154 } else if(size == 1) {
155 int single = material.single;
158 entry.finish(graph, provider);
160 proc.execute(graph, single);
161 proc.finished(graph);
164 addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
169 // final AtomicInteger finishes = new AtomicInteger(0);
171 indirect.forEach(new TIntProcedure() {
176 public boolean execute(final int arg0) {
178 return execute0(arg0);
179 } catch (DatabaseException e) {
180 Logger.defaultLogError(e);
185 public boolean execute0(final int arg0) throws DatabaseException {
189 int current = (++finishes);
190 if(current == indirect.size()) {
191 int size = material.size();
193 if(entry != null) entry.finish(graph, provider);
194 proc.finished(graph);
196 } else if(size == 1) {
197 int single = material.single;
200 entry.finish(graph, provider);
202 proc.execute(graph, single);
203 proc.finished(graph);
206 addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
213 QueryCache.runnerPrincipalTypes(graph, arg0, entry, null, new IntProcedure() {
216 public void execute(ReadGraphImpl graph, int i) {
217 synchronized(material) {
223 public void finished(ReadGraphImpl graph) throws DatabaseException {
225 int current = (++finishes);
226 if(current == indirect.size()) {
227 int size = material.size();
229 if(entry != null) entry.finish(graph, provider);
230 proc.finished(graph);
232 } else if(size == 1) {
233 int single = material.single;
236 entry.finish(graph, provider);
238 proc.execute(graph, single);
239 proc.finished(graph);
242 addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
250 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
251 proc.exception(graph, t);
266 private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final PrincipalTypes entry, final QueryProcessor provider, final IntProcedure proc) throws DatabaseException {
269 for(int i : material) {
270 if(!rejects.contains(i)) {
274 entry.finish(graph, provider);
277 for(int i : material) {
278 if(!rejects.contains(i)) {
279 proc.execute(graph, i);
283 proc.finished(graph);
289 private static void addPrincipalType(final ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, int index, final QueryProcessor provider, final PrincipalTypes entry, final IntProcedure proc) throws DatabaseException {
291 // if((counter++ % 500) == 0) System.out.println("PT " + counter + " mat = " + material.length);
293 if(index == material.length) {
294 finish(graph, rejects, material, entry, provider, proc);
298 int type = material[index++];
299 while(rejects.contains(type)) {
300 if(index == material.length) {
301 finish(graph, rejects, material, entry, provider, proc);
304 type = material[index++];
306 final int nextIndex = index;
308 QueryCache.runnerSuperTypes(graph, type, entry, null, new InternalProcedure<IntSet>() {
311 public void execute(ReadGraphImpl graph, IntSet supers) throws DatabaseException {
313 synchronized(rejects) {
315 supers.forEach(new TIntProcedure() {
318 public boolean execute(int arg0) {
327 addPrincipalType(graph, rejects, material, nextIndex, provider, entry, proc);
332 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
333 proc.exception(graph, t);
341 public String toString() {
342 return "PrincipalTypes[" + id + "]";
345 final private void add(int val) {
347 IntArray v = (IntArray)getResult();
351 final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
361 public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
365 if(handleException(graph, procedure)) return EXCEPTED;
367 final IntArray value = (IntArray)getResult();
368 if(value.data == null) {
369 if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
371 for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
374 procedure.finished(graph);
381 public void recompute(ReadGraphImpl graph) throws DatabaseException {
383 compute(graph, new IntProcedureAdapter() {
386 public void finished(ReadGraphImpl graph) {
390 public void exception(ReadGraphImpl graph, Throwable t) {
391 new Error("Error in recompute.", t).printStackTrace();
400 boolean isImmutable(ReadGraphImpl graph) {
401 return graph.processor.isImmutable(id);
405 protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry<?>> result) {