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 {
43 System.err.println("asd");
45 assert(procedure != null);
47 QueryProcessor processor = graph.processor;
49 processor.querySupport.ensureLoaded(graph, id);
51 int ret = processor.querySupport.getSingleInstance(id);
54 TypeHierarchy.queryEach(graph, ret, processor, entry, null, new InternalProcedure<IntSet>() {
57 public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
59 if(entry != null) entry.addOrSet(graph, types, processor);
60 procedure.execute(graph, types);
65 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
66 procedure.exception(graph, t);
73 final int instanceOf = processor.getInstanceOf();
74 final int inherits = processor.getInherits();
75 final int subrelationOf = processor.getSubrelationOf();
77 final IntSet result = new IntSet(processor.querySupport);
79 final TIntProcedure addToResult = new TIntProcedure() {
81 public boolean execute(int r) {
82 synchronized(result) {
89 final AtomicInteger finishes = new AtomicInteger(0);
91 SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() {
94 public void run(ReadGraphImpl graph) throws DatabaseException {
96 if(finishes.addAndGet(1) == 3) {
97 if(entry != null) entry.addOrSet(graph, result, processor);
98 procedure.execute(graph, result);
104 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
106 synchronized(result) {
112 QueryCache.runnerSuperTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
115 public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
116 types.forEach(addToResult);
121 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
122 procedure.exception(graph, t);
131 public void finished(ReadGraphImpl graph) throws DatabaseException {
137 SyncIntProcedure inheritsProcedure = new SyncIntProcedure() {
140 public void run(ReadGraphImpl graph) throws DatabaseException {
142 int current = finishes.addAndGet(1);
144 if(entry != null) entry.addOrSet(graph, result, processor);
145 procedure.execute(graph, result);
151 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
155 QueryCache.runnerTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
158 public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
159 types.forEach(addToResult);
164 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
165 procedure.exception(graph, t);
174 public void finished(ReadGraphImpl graph) throws DatabaseException {
182 SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() {
185 public void run(ReadGraphImpl graph) throws DatabaseException {
187 int current = finishes.addAndGet(1);
189 if(entry != null) entry.addOrSet(graph, result, processor);
190 procedure.execute(graph, result);
196 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
200 QueryCache.runnerTypes(graph, i, entry, 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);
236 if(entry != null) entry.finish();
241 public String toString() {
242 return "Types[" + id + "]";
245 private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
255 IntSet result = getResult();
262 final public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
266 if(handleException(graph, procedure)) return EXCEPTED;
268 IntSet result = getResult();
270 procedure.execute(graph, result);
277 public void recompute(ReadGraphImpl graph) throws DatabaseException {
279 compute(graph, new InternalProcedure<IntSet>() {
282 public void execute(ReadGraphImpl graph, IntSet result) {
286 public void exception(ReadGraphImpl graph, Throwable t) {
287 new Error("Error in recompute.", t).printStackTrace();
295 boolean isImmutable(ReadGraphImpl graph) {
296 return graph.processor.isImmutable(id);