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.concurrent.atomic.AtomicInteger;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.impl.graph.ReadGraphImpl;
18 import org.simantics.db.impl.procedure.InternalProcedure;
20 import gnu.trove.procedure.TIntProcedure;
22 public final class Types extends UnaryQueryP<IntSet> {
29 public final void removeEntry(QueryProcessor provider) {
30 provider.cache.remove(this);
34 public void compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
35 computeForEach(graph, id, this, procedure);
38 public static void computeForEach(final ReadGraphImpl graph, int id, Types entry,
39 final InternalProcedure<IntSet> procedure_) throws DatabaseException {
41 InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
43 computeForEach2(graph, id, entry, procedure);
46 entry.performFromCache(graph, procedure_);
50 public static void computeForEach2(final ReadGraphImpl graph, int id, Types parent,
51 final InternalProcedure<IntSet> procedure) throws DatabaseException {
53 QueryProcessor processor = graph.processor;
55 processor.querySupport.ensureLoaded(graph, id);
57 int ret = processor.querySupport.getSingleInstance(id);
60 TypeHierarchy.queryEach(graph, ret, processor, parent, null, new InternalProcedure<IntSet>() {
63 public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
64 procedure.execute(graph, types);
68 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
69 procedure.exception(graph, t);
78 final int instanceOf = processor.getInstanceOf();
79 final int inherits = processor.getInherits();
80 final int subrelationOf = processor.getSubrelationOf();
82 final IntSet result = new IntSet(processor.querySupport);
84 final TIntProcedure addToResult = new TIntProcedure() {
86 public boolean execute(int r) {
87 synchronized (result) {
94 final AtomicInteger finishes = new AtomicInteger(0);
96 SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() {
99 public void run(ReadGraphImpl graph) throws DatabaseException {
101 if (finishes.addAndGet(1) == 3) {
102 procedure.execute(graph, result);
108 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
114 QueryCache.runnerSuperTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
117 public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
118 types.forEach(addToResult);
123 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
124 procedure.exception(graph, t);
133 public void finished(ReadGraphImpl graph) throws DatabaseException {
139 SyncIntProcedure inheritsProcedure = new SyncIntProcedure() {
142 public void run(ReadGraphImpl graph) throws DatabaseException {
144 int current = finishes.addAndGet(1);
146 procedure.execute(graph, result);
152 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
156 QueryCache.runnerTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
159 public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
160 types.forEach(addToResult);
165 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
166 procedure.exception(graph, t);
175 public void finished(ReadGraphImpl graph) throws DatabaseException {
183 SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() {
186 public void run(ReadGraphImpl graph) throws DatabaseException {
188 int current = finishes.addAndGet(1);
190 procedure.execute(graph, result);
196 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
200 QueryCache.runnerTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
203 public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
205 types.forEach(addToResult);
211 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
212 procedure.exception(graph, t);
221 public void finished(ReadGraphImpl graph) throws DatabaseException {
229 processor.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);
230 instanceOfProcedure.finished(graph);
231 processor.querySupport.getObjects(graph, id, inherits, inheritsProcedure);
232 inheritsProcedure.finished(graph);
233 processor.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);
234 subrelationOfProcedure.finished(graph);
239 public String toString() {
240 return "Types[" + id + "]";