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.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 final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
24 Types(final int resource) {
29 final public void removeEntry(QueryProcessor provider) {
30 provider.cache.remove(this);
34 public Object compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
35 computeForEach(graph, id, this, procedure);
39 public static void computeForEach(final ReadGraphImpl graph, int id, Types entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
41 assert(procedure != null);
43 QueryProcessor processor = graph.processor;
45 processor.querySupport.ensureLoaded(graph, id);
47 int ret = processor.querySupport.getSingleInstance(id);
50 TypeHierarchy.queryEach(graph, ret, processor, entry, null, new InternalProcedure<IntSet>() {
53 public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
56 entry.addOrSet(graph, types, processor);
59 procedure.execute(graph, types);
64 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
65 procedure.exception(graph, t);
74 final int instanceOf = processor.getInstanceOf();
75 final int inherits = processor.getInherits();
76 final int subrelationOf = processor.getSubrelationOf();
78 final IntSet result = new IntSet(processor.querySupport);
80 final TIntProcedure addToResult = new TIntProcedure() {
82 public boolean execute(int r) {
83 synchronized(result) {
90 final AtomicInteger finishes = new AtomicInteger(0);
92 SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() {
95 public void run(ReadGraphImpl graph) throws DatabaseException {
97 if(finishes.addAndGet(1) == 3) {
98 if(entry != null) entry.addOrSet(graph, result, processor);
99 procedure.execute(graph, result);
105 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
111 QueryCache.runnerSuperTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
114 public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
115 types.forEach(addToResult);
120 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
121 procedure.exception(graph, t);
130 public void finished(ReadGraphImpl graph) throws DatabaseException {
136 SyncIntProcedure inheritsProcedure = new SyncIntProcedure() {
139 public void run(ReadGraphImpl graph) throws DatabaseException {
141 int current = finishes.addAndGet(1);
143 if(entry != null) entry.addOrSet(graph, result, processor);
144 procedure.execute(graph, result);
150 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
154 QueryCache.runnerTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
157 public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
158 types.forEach(addToResult);
163 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
164 procedure.exception(graph, t);
173 public void finished(ReadGraphImpl graph) throws DatabaseException {
181 SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() {
184 public void run(ReadGraphImpl graph) throws DatabaseException {
186 int current = finishes.addAndGet(1);
188 if(entry != null) entry.addOrSet(graph, result, processor);
189 procedure.execute(graph, result);
195 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
199 QueryCache.runnerTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
202 public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
204 types.forEach(addToResult);
210 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
211 procedure.exception(graph, t);
220 public void finished(ReadGraphImpl graph) throws DatabaseException {
228 processor.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);
229 instanceOfProcedure.finished(graph);
230 processor.querySupport.getObjects(graph, id, inherits, inheritsProcedure);
231 inheritsProcedure.finished(graph);
232 processor.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);
233 subrelationOfProcedure.finished(graph);
235 if(entry != null) entry.finish();
240 public String toString() {
241 return "Types[" + id + "]";
244 private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
254 IntSet result = getResult();
261 final public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
265 if(handleException(graph, procedure)) return EXCEPTED;
267 IntSet result = getResult();
269 procedure.execute(graph, result);
276 public void recompute(ReadGraphImpl graph) throws DatabaseException {
278 compute(graph, new InternalProcedure<IntSet>() {
281 public void execute(ReadGraphImpl graph, IntSet result) {
285 public void exception(ReadGraphImpl graph, Throwable t) {
286 new Error("Error in recompute.", t).printStackTrace();
294 boolean isImmutable(ReadGraphImpl graph) {
295 return graph.processor.isImmutable(id);