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 private SuperTypes(final int resource) {
28 final static SuperTypes runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
30 SuperTypes entry = (SuperTypes)provider.cache.superTypesMap.get(r);
33 entry = new SuperTypes(r);
35 entry.clearResult(provider.querySupport);
36 entry.putEntry(provider);
38 provider.performForEach(graph, entry, parent, listener, procedure);
44 if(!entry.isReady()) {
46 if(!entry.isReady()) {
47 throw new IllegalStateException();
48 // if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<IntSet>>();
49 // entry.procs.add(procedure);
50 // provider.registerDependencies(graph, entry, parent, listener, procedure, false);
55 provider.performForEach(graph, entry, parent, listener, procedure);
62 final public static SuperTypes queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
64 if(parent == null && listener == null) {
65 SuperTypes entry = (SuperTypes)provider.cache.superTypesMap.get(r);
66 if(entry != null && entry.isReady()) {
67 entry.performFromCache(graph, provider, procedure);
72 return runner(graph, r, parent, provider, listener, procedure);
77 public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
78 return provider.cache.superTypesMap.get(id);
82 public void putEntry(QueryProcessor provider) {
83 provider.cache.superTypesMap.put(id, this);
87 final public void removeEntry(QueryProcessor provider) {
88 provider.cache.superTypesMap.remove(id);
92 public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, boolean store) {
94 final int inherits = provider.getInherits();
96 final CacheEntry parent = graph.parent;
98 assert(graph.parent != this);
100 final IntSet result = new IntSet(provider.querySupport);
102 final TIntProcedure addToResult = new TIntProcedure() {
104 public boolean execute(int r) {
105 synchronized(result) {
112 DirectObjects.queryEach(graph, id, inherits, provider, this, null, new SyncIntProcedure() {
115 public void run(ReadGraphImpl graph) {
117 addOrSet(graph, result, provider);
118 procedure.execute(graph, result);
123 public void execute(ReadGraphImpl graph, final int i) {
125 assert(graph.parent == parent);
127 synchronized(result) {
133 SuperTypes.queryEach(graph, i, provider, SuperTypes.this, null, new InternalProcedure<IntSet>() {
136 public void execute(ReadGraphImpl graph, IntSet types) {
138 types.forEach(addToResult);
144 public void exception(ReadGraphImpl graph, Throwable t) {
145 procedure.exception(graph, t);
153 public void finished(ReadGraphImpl graph) {
164 public String toString() {
165 return "SuperTypes2[" + id + "]";
168 private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
172 // ArrayList<InternalProcedure<IntSet>> p = null;
185 // IntSet v = (IntSet)getResult();
187 // for(InternalProcedure<IntSet> proc : p) proc.execute(graph, v);
194 public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
198 if(handleException(graph, procedure)) return null;
200 IntSet result = getResult();
202 procedure.execute(graph, result);
209 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
211 final Semaphore s = new Semaphore(0);
213 computeForEach(graph, provider, new InternalProcedure<IntSet>() {
216 public void execute(ReadGraphImpl graph, IntSet result) {
221 public void exception(ReadGraphImpl graph, Throwable t) {
223 new Error("Error in recompute.", t).printStackTrace();
228 while(!s.tryAcquire()) {
229 provider.resume(graph);
235 boolean isImmutable(ReadGraphImpl graph) {
236 return graph.processor.isImmutable(id);