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;
17 import org.simantics.db.common.utils.Logger;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.impl.graph.ReadGraphImpl;
20 import org.simantics.db.impl.procedure.IntProcedureAdapter;
21 import org.simantics.db.impl.procedure.InternalProcedure;
23 import gnu.trove.procedure.TIntProcedure;
24 import gnu.trove.set.hash.TIntHashSet;
26 final public class PrincipalTypes extends CollectionUnaryQuery {
28 PrincipalTypes(final int resource) {
33 final public void removeEntry(QueryProcessor provider) {
34 provider.cache.remove(this);
39 private TIntHashSet set = null;
40 public int single = 0;
42 public boolean add(int val) {
43 if(single == val) return false;
48 if(set == null) set = new TIntHashSet(4);
55 if(single == 0) return 0;
56 if(set == null) return 1;
57 return set.size() + 1;
61 public int[] toArray() {
63 int[] result = Arrays.copyOf(set.toArray(), set.size() + 1);
64 result[set.size()] = single;
69 public void forEach(TIntProcedure proc) {
71 if(set != null) set.forEach(proc);
77 public void compute(final ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
78 computeForEach(graph, id, this, proc);
81 public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final IntProcedure procedure_) throws DatabaseException {
83 IntProcedure procedure = entry != null ? entry : procedure_;
85 Object result = computeForEach2(graph, id, entry, procedure);
87 if(entry != null) entry.performFromCache(graph, procedure_);
93 public static Object computeForEach2(final ReadGraphImpl graph, final int id, final PrincipalTypes parent, final IntProcedure procedure) throws DatabaseException {
95 QueryProcessor provider = graph.processor;
97 provider.querySupport.ensureLoaded(graph, id);
100 int ret = provider.querySupport.getSingleInstance(id);
102 procedure.execute(graph, ret);
103 procedure.finished(graph);
107 final int instanceOf = provider.getInstanceOf();
108 final int inherits = provider.getInherits();
109 final int subrelationOf = provider.getSubrelationOf();
111 final Koss indirect = new Koss();
112 final Koss material = new Koss();
114 IntProcedure directProc = new IntProcedure() {
117 public void execute(ReadGraphImpl graph, int i) {
122 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
123 procedure.exception(graph, t);
127 public void finished(ReadGraphImpl graph) {
132 IntProcedure indirectProc = new IntProcedure() {
135 public void execute(ReadGraphImpl graph, int i) {
140 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
141 procedure.exception(graph, t);
145 public void finished(ReadGraphImpl graph) {
150 provider.querySupport.getObjects(graph, id, instanceOf, directProc);
151 provider.querySupport.getObjects(graph, id, inherits, indirectProc);
152 provider.querySupport.getObjects(graph, id, subrelationOf, indirectProc);
154 if(indirect.size() == 0) {
155 int size = material.size();
157 procedure.finished(graph);
159 } else if(size == 1) {
160 int single = material.single;
161 procedure.execute(graph, single);
162 procedure.finished(graph);
165 addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure);
170 indirect.forEach(new TIntProcedure() {
175 public boolean execute(final int arg0) {
177 return execute0(arg0);
178 } catch (DatabaseException e) {
179 Logger.defaultLogError(e);
184 public boolean execute0(final int arg0) throws DatabaseException {
188 int current = (++finishes);
189 if(current == indirect.size()) {
190 int size = material.size();
192 procedure.finished(graph);
194 } else if(size == 1) {
195 int single = material.single;
196 procedure.execute(graph, single);
197 procedure.finished(graph);
200 addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure);
207 QueryCache.runnerPrincipalTypes(graph, arg0, parent, null, new IntProcedure() {
210 public void execute(ReadGraphImpl graph, int i) {
211 synchronized(material) {
217 public void finished(ReadGraphImpl graph) throws DatabaseException {
219 int current = (++finishes);
220 if(current == indirect.size()) {
221 int size = material.size();
223 procedure.finished(graph);
225 } else if(size == 1) {
226 int single = material.single;
227 procedure.execute(graph, single);
228 procedure.finished(graph);
231 addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure);
239 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
240 procedure.exception(graph, t);
255 private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final IntProcedure proc) throws DatabaseException {
257 for(int i : material) {
258 if(!rejects.contains(i)) {
259 proc.execute(graph, i);
263 proc.finished(graph);
269 private static void addPrincipalType(final ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, int index, final QueryProcessor provider, final PrincipalTypes parent, final IntProcedure proc) throws DatabaseException {
271 if(index == material.length) {
272 finish(graph, rejects, material, proc);
276 int type = material[index++];
277 while(rejects.contains(type)) {
278 if(index == material.length) {
279 finish(graph, rejects, material, proc);
282 type = material[index++];
285 final int nextIndex = index;
287 QueryCache.runnerSuperTypes(graph, type, parent, null, new InternalProcedure<IntSet>() {
290 public void execute(ReadGraphImpl graph, IntSet supers) throws DatabaseException {
292 synchronized(rejects) {
294 supers.forEach(new TIntProcedure() {
297 public boolean execute(int arg0) {
306 addPrincipalType(graph, rejects, material, nextIndex, provider, parent, proc);
311 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
312 proc.exception(graph, t);
320 public String toString() {
321 return "PrincipalTypes[" + id + "]";
325 protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry<?>> result) {