1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 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.InternalProcedure;
22 import gnu.trove.procedure.TIntProcedure;
23 import gnu.trove.set.hash.TIntHashSet;
25 public final class PrincipalTypes extends CollectionUnaryQuery {
27 PrincipalTypes(final int resource) {
32 final public void removeEntry(QueryProcessor provider) {
33 provider.cache.remove(this);
38 private TIntHashSet set = null;
39 public int single = 0;
41 public boolean add(int val) {
42 if(single == val) return false;
47 if(set == null) set = new TIntHashSet(4);
54 if(single == 0) return 0;
55 if(set == null) return 1;
56 return set.size() + 1;
60 public int[] toArray() {
62 int[] result = Arrays.copyOf(set.toArray(), set.size() + 1);
63 result[set.size()] = single;
68 public void forEach(TIntProcedure proc) {
70 if(set != null) set.forEach(proc);
76 public void compute(final ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
77 computeForEach(graph, id, this, proc);
80 public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final IntProcedure procedure_) throws DatabaseException {
82 IntProcedure procedure = entry != null ? entry : procedure_;
84 Object result = computeForEach2(graph, id, entry, procedure);
86 if(entry != null) entry.performFromCache(graph, procedure_);
92 public static Object computeForEach2(final ReadGraphImpl graph, final int id, final PrincipalTypes parent, final IntProcedure procedure) throws DatabaseException {
94 QueryProcessor provider = graph.processor;
96 provider.querySupport.ensureLoaded(graph, id);
99 int ret = provider.querySupport.getSingleInstance(id);
101 procedure.execute(graph, ret);
102 procedure.finished(graph);
106 final int instanceOf = provider.getInstanceOf();
107 final int inherits = provider.getInherits();
108 final int subrelationOf = provider.getSubrelationOf();
110 final Ints indirect = new Ints();
111 final Ints material = new Ints();
113 IntProcedure directProc = new IntProcedure() {
116 public void execute(ReadGraphImpl graph, int i) {
121 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
122 procedure.exception(graph, t);
126 public void finished(ReadGraphImpl graph) {
131 IntProcedure indirectProc = new IntProcedure() {
134 public void execute(ReadGraphImpl graph, int i) {
139 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
140 procedure.exception(graph, t);
144 public void finished(ReadGraphImpl graph) {
149 provider.querySupport.getObjects(graph, id, instanceOf, directProc);
150 provider.querySupport.getObjects(graph, id, inherits, indirectProc);
151 provider.querySupport.getObjects(graph, id, subrelationOf, indirectProc);
153 if(indirect.size() == 0) {
154 int size = material.size();
156 procedure.finished(graph);
158 } else if(size == 1) {
159 int single = material.single;
160 procedure.execute(graph, single);
161 procedure.finished(graph);
164 addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure);
169 indirect.forEach(new TIntProcedure() {
174 public boolean execute(final int arg0) {
176 return execute0(arg0);
177 } catch (DatabaseException e) {
178 Logger.defaultLogError(e);
183 public boolean execute0(final int arg0) throws DatabaseException {
187 int current = (++finishes);
188 if(current == indirect.size()) {
189 int size = material.size();
191 procedure.finished(graph);
193 } else if(size == 1) {
194 int single = material.single;
195 procedure.execute(graph, single);
196 procedure.finished(graph);
199 addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure);
206 QueryCache.runnerPrincipalTypes(graph, arg0, parent, null, new IntProcedure() {
209 public void execute(ReadGraphImpl graph, int i) {
210 synchronized(material) {
216 public void finished(ReadGraphImpl graph) throws DatabaseException {
218 int current = (++finishes);
219 if(current == indirect.size()) {
220 int size = material.size();
222 procedure.finished(graph);
224 } else if(size == 1) {
225 int single = material.single;
226 procedure.execute(graph, single);
227 procedure.finished(graph);
230 addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure);
238 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
239 procedure.exception(graph, t);
254 private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final IntProcedure proc) throws DatabaseException {
256 for(int i : material) {
257 if(!rejects.contains(i)) {
258 proc.execute(graph, i);
262 proc.finished(graph);
268 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 {
270 if(index == material.length) {
271 finish(graph, rejects, material, proc);
275 int type = material[index++];
276 while(rejects.contains(type)) {
277 if(index == material.length) {
278 finish(graph, rejects, material, proc);
281 type = material[index++];
284 final int nextIndex = index;
286 QueryCache.runnerSuperTypes(graph, type, parent, null, new InternalProcedure<IntSet>() {
289 public void execute(ReadGraphImpl graph, IntSet supers) throws DatabaseException {
291 synchronized(rejects) {
293 supers.forEach(new TIntProcedure() {
296 public boolean execute(int arg0) {
305 addPrincipalType(graph, rejects, material, nextIndex, provider, parent, proc);
310 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
311 proc.exception(graph, t);
319 public String toString() {
320 return "PrincipalTypes[" + id + "]";
324 protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry<?>> result) {