1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.impl.query;
\r
14 import gnu.trove.procedure.TIntProcedure;
\r
16 import java.util.concurrent.Semaphore;
\r
18 import org.simantics.db.impl.graph.ReadGraphImpl;
\r
19 import org.simantics.db.impl.procedure.InternalProcedure;
\r
20 import org.simantics.db.procedure.ListenerBase;
\r
22 final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
\r
24 // public ArrayList<InternalProcedure<IntSet>> procs = null;
\r
26 private SuperTypes(final int resource) {
\r
30 final static SuperTypes runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
\r
32 SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r);
\r
35 entry = new SuperTypes(r);
\r
37 entry.clearResult(provider.querySupport);
\r
38 entry.putEntry(provider);
\r
40 provider.performForEach(graph, entry, parent, listener, procedure);
\r
46 if(!entry.isReady()) {
\r
47 synchronized(entry) {
\r
48 if(!entry.isReady()) {
\r
49 throw new IllegalStateException();
\r
50 // if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<IntSet>>();
\r
51 // entry.procs.add(procedure);
\r
52 // provider.registerDependencies(graph, entry, parent, listener, procedure, false);
\r
57 provider.performForEach(graph, entry, parent, listener, procedure);
\r
64 final public static SuperTypes queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
\r
66 if(parent == null && listener == null) {
\r
67 SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r);
\r
68 if(entry != null && entry.isReady()) {
\r
69 entry.performFromCache(graph, provider, procedure);
\r
74 return runner(graph, r, parent, provider, listener, procedure);
\r
79 public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
\r
80 return provider.superTypesMap.get(id);
\r
84 public void putEntry(QueryProcessor provider) {
\r
85 provider.superTypesMap.put(id, this);
\r
89 final public void removeEntry(QueryProcessor provider) {
\r
90 provider.superTypesMap.remove(id);
\r
94 public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, boolean store) {
\r
96 final int inherits = provider.getInherits();
\r
98 final CacheEntry parent = graph.parent;
\r
100 assert(graph.parent != this);
\r
102 final IntSet result = new IntSet(provider.querySupport);
\r
104 final TIntProcedure addToResult = new TIntProcedure() {
\r
106 public boolean execute(int r) {
\r
107 synchronized(result) {
\r
114 DirectObjects.queryEach(graph, id, inherits, provider, this, null, new SyncIntProcedure() {
\r
117 public void run(ReadGraphImpl graph) {
\r
119 addOrSet(graph, result, provider);
\r
120 procedure.execute(graph, result);
\r
125 public void execute(ReadGraphImpl graph, final int i) {
\r
127 assert(graph.parent == parent);
\r
129 synchronized(result) {
\r
135 SuperTypes.queryEach(graph, i, provider, SuperTypes.this, null, new InternalProcedure<IntSet>() {
\r
138 public void execute(ReadGraphImpl graph, IntSet types) {
\r
140 types.forEach(addToResult);
\r
146 public void exception(ReadGraphImpl graph, Throwable t) {
\r
147 procedure.exception(graph, t);
\r
155 public void finished(ReadGraphImpl graph) {
\r
166 public String toString() {
\r
167 return "SuperTypes2[" + id + "]";
\r
170 private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
\r
172 assert(!isReady());
\r
174 // ArrayList<InternalProcedure<IntSet>> p = null;
\r
176 synchronized(this) {
\r
187 // IntSet v = (IntSet)getResult();
\r
189 // for(InternalProcedure<IntSet> proc : p) proc.execute(graph, v);
\r
196 public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
\r
200 if(handleException(graph, procedure)) return null;
\r
202 IntSet result = getResult();
\r
204 procedure.execute(graph, result);
\r
211 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
\r
213 final Semaphore s = new Semaphore(0);
\r
215 computeForEach(graph, provider, new InternalProcedure<IntSet>() {
\r
218 public void execute(ReadGraphImpl graph, IntSet result) {
\r
223 public void exception(ReadGraphImpl graph, Throwable t) {
\r
225 new Error("Error in recompute.", t).printStackTrace();
\r
230 while(!s.tryAcquire()) {
\r
231 provider.resume(graph);
\r
237 boolean isImmutable(ReadGraphImpl graph) {
\r
238 return graph.processor.isImmutable(id);
\r