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 org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.impl.graph.ReadGraphImpl;
16 import org.simantics.db.impl.procedure.InternalProcedure;
17 import org.simantics.db.procedure.ListenerBase;
19 import gnu.trove.procedure.TIntProcedure;
21 final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
23 SuperTypes(final int resource) {
27 // final public static SuperTypes queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
28 // return QueryCache.runnerSuperTypes(graph, r, parent, listener, procedure);
32 final public void removeEntry(QueryProcessor provider) {
33 provider.cache.remove(this);
37 public Object compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
38 return computeForEach(graph, id, this, procedure);
41 private static void addOrSet(ReadGraphImpl graph, SuperTypes entry, IntSet value) {
43 entry.addOrSet(graph, value, graph.processor);
47 public static Object computeForEach(ReadGraphImpl graph, int r, SuperTypes entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
49 QueryProcessor provider = graph.processor;
51 final int inherits = provider.getInherits();
53 final IntSet result = new IntSet(provider.querySupport);
55 final TIntProcedure addToResult = new TIntProcedure() {
57 public boolean execute(int r) {
58 synchronized(result) {
65 QueryCache.runnerDirectObjects(graph, r, inherits, entry, null, new SyncIntProcedure() {
68 public void run(ReadGraphImpl graph) throws DatabaseException {
70 addOrSet(graph, entry, result);
71 procedure.execute(graph, result);
76 public void execute(ReadGraphImpl graph, final int i) throws DatabaseException {
78 // assert(graph.parent == parent);
80 synchronized(result) {
86 QueryCache.runnerSuperTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
89 public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
91 types.forEach(addToResult);
97 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
98 procedure.exception(graph, t);
106 public void finished(ReadGraphImpl graph) throws DatabaseException {
117 public String toString() {
118 return "SuperTypes2[" + id + "]";
121 private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
136 public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
140 if(handleException(graph, procedure)) return null;
142 IntSet result = getResult();
144 procedure.execute(graph, result);
151 public void recompute(ReadGraphImpl graph) throws DatabaseException {
153 compute(graph, new InternalProcedure<IntSet>() {
156 public void execute(ReadGraphImpl graph, IntSet result) {
160 public void exception(ReadGraphImpl graph, Throwable t) {
161 new Error("Error in recompute.", t).printStackTrace();
169 boolean isImmutable(ReadGraphImpl graph) {
170 return graph.processor.isImmutable(id);