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 gnu.trove.procedure.TIntProcedure;
16 import java.util.concurrent.Semaphore;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.impl.procedure.InternalProcedure;
20 import org.simantics.db.procedure.ListenerBase;
22 final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
24 // public ArrayList<InternalProcedure<IntSet>> procs = null;
26 private SuperTypes(final int resource) {
30 final static SuperTypes runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
32 SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r);
35 entry = new SuperTypes(r);
37 entry.clearResult(provider.querySupport);
38 entry.putEntry(provider);
40 provider.performForEach(graph, entry, parent, listener, procedure);
46 if(!entry.isReady()) {
48 if(!entry.isReady()) {
49 throw new IllegalStateException();
50 // if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<IntSet>>();
51 // entry.procs.add(procedure);
52 // provider.registerDependencies(graph, entry, parent, listener, procedure, false);
57 provider.performForEach(graph, entry, parent, listener, procedure);
64 final public static SuperTypes queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
66 if(parent == null && listener == null) {
67 SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r);
68 if(entry != null && entry.isReady()) {
69 entry.performFromCache(graph, provider, procedure);
74 return runner(graph, r, parent, provider, listener, procedure);
79 public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
80 return provider.superTypesMap.get(id);
84 public void putEntry(QueryProcessor provider) {
85 provider.superTypesMap.put(id, this);
89 final public void removeEntry(QueryProcessor provider) {
90 provider.superTypesMap.remove(id);
94 public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, boolean store) {
96 final int inherits = provider.getInherits();
98 final CacheEntry parent = graph.parent;
100 assert(graph.parent != this);
102 final IntSet result = new IntSet(provider.querySupport);
104 final TIntProcedure addToResult = new TIntProcedure() {
106 public boolean execute(int r) {
107 synchronized(result) {
114 DirectObjects.queryEach(graph, id, inherits, provider, this, null, new SyncIntProcedure() {
117 public void run(ReadGraphImpl graph) {
119 addOrSet(graph, result, provider);
120 procedure.execute(graph, result);
125 public void execute(ReadGraphImpl graph, final int i) {
127 assert(graph.parent == parent);
129 synchronized(result) {
135 SuperTypes.queryEach(graph, i, provider, SuperTypes.this, null, new InternalProcedure<IntSet>() {
138 public void execute(ReadGraphImpl graph, IntSet types) {
140 types.forEach(addToResult);
146 public void exception(ReadGraphImpl graph, Throwable t) {
147 procedure.exception(graph, t);
155 public void finished(ReadGraphImpl graph) {
166 public String toString() {
167 return "SuperTypes2[" + id + "]";
170 private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
174 // ArrayList<InternalProcedure<IntSet>> p = null;
187 // IntSet v = (IntSet)getResult();
189 // for(InternalProcedure<IntSet> proc : p) proc.execute(graph, v);
196 public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
200 if(handleException(graph, procedure)) return null;
202 IntSet result = getResult();
204 procedure.execute(graph, result);
211 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
213 final Semaphore s = new Semaphore(0);
215 computeForEach(graph, provider, new InternalProcedure<IntSet>() {
218 public void execute(ReadGraphImpl graph, IntSet result) {
223 public void exception(ReadGraphImpl graph, Throwable t) {
225 new Error("Error in recompute.", t).printStackTrace();
230 while(!s.tryAcquire()) {
231 provider.resume(graph);
237 boolean isImmutable(ReadGraphImpl graph) {
238 return graph.processor.isImmutable(id);