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;
17 import java.util.concurrent.atomic.AtomicInteger;
19 import org.simantics.db.impl.graph.ReadGraphImpl;
20 import org.simantics.db.impl.procedure.InternalProcedure;
21 import org.simantics.db.procedure.ListenerBase;
23 final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
25 // public ArrayList<InternalProcedure<IntSet>> procs;
27 private Types(final int resource) {
31 final static Types entry(final QueryProcessor provider, final int r) {
32 return (Types)provider.typesMap.get(r);
35 final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, Types cached, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
37 Types entry = cached != null ? cached : (Types)provider.typesMap.get(r);
42 entry.clearResult(provider.querySupport);
43 entry.putEntry(provider);
45 provider.performForEach(graph, entry, parent, listener, procedure);
49 if(!entry.isReady()) {
50 throw new IllegalStateException();
52 provider.performForEach(graph, entry, parent, listener, procedure);
57 final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
59 Types entry = (Types)provider.typesMap.get(r);
64 entry.clearResult(provider.querySupport);
65 entry.putEntry(provider);
67 return (IntSet)provider.performForEach2(graph, entry, parent, null, null);
71 if(!entry.isReady()) {
72 throw new IllegalStateException();
74 return (IntSet)provider.performForEach2(graph, entry, parent, null, null);
80 final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
82 final Types entry = (Types)provider.typesMap.get(r);
84 if(parent == null && listener == null) {
85 if(entry != null && entry.isReady()) {
86 entry.performFromCache(graph, provider, procedure);
91 runner(graph, r, provider, entry, parent, listener, procedure);
95 final public static IntSet queryEach2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
98 Types entry = (Types)provider.typesMap.get(r);
99 if(entry != null && entry.isReady()) {
100 return (IntSet)entry.get(graph, provider, null);
104 return runner2(graph, r, provider, parent);
109 public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
110 return provider.typesMap.get(id);
114 public void putEntry(QueryProcessor provider) {
115 provider.typesMap.put(id, this);
119 final public void removeEntry(QueryProcessor provider) {
120 provider.typesMap.remove(id);
124 public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor queryProvider, final InternalProcedure<IntSet> procedure, final boolean store) {
126 queryProvider.querySupport.ensureLoaded(graph, id);
127 int ret = queryProvider.querySupport.getSingleInstance(id);
130 TypeHierarchy.queryEach(graph, ret, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
133 public void execute(ReadGraphImpl graph, IntSet types) {
135 addOrSet(graph, types, queryProvider);
136 procedure.execute(graph, types);
141 public void exception(ReadGraphImpl graph, Throwable t) {
142 procedure.exception(graph, t);
151 final int instanceOf = queryProvider.getInstanceOf();
152 final int inherits = queryProvider.getInherits();
153 final int subrelationOf = queryProvider.getSubrelationOf();
155 final IntSet result = new IntSet(queryProvider.querySupport);
157 final TIntProcedure addToResult = new TIntProcedure() {
159 public boolean execute(int r) {
160 synchronized(result) {
167 final AtomicInteger finishes = new AtomicInteger(0);
169 SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() {
172 public void run(ReadGraphImpl graph) {
174 if(finishes.addAndGet(1) == 3) {
175 if(store) addOrSet(graph, result, queryProvider);
176 procedure.execute(graph, result);
182 public void execute(ReadGraphImpl graph, int i) {
184 synchronized(result) {
190 SuperTypes.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
193 public void execute(ReadGraphImpl graph, IntSet types) {
194 types.forEach(addToResult);
199 public void exception(ReadGraphImpl graph, Throwable t) {
200 procedure.exception(graph, t);
209 public void finished(ReadGraphImpl graph) {
215 SyncIntProcedure inheritsProcedure = new SyncIntProcedure() {
218 public void run(ReadGraphImpl graph) {
220 int current = finishes.addAndGet(1);
222 if(store) addOrSet(graph, result, queryProvider);
223 procedure.execute(graph, result);
229 public void execute(ReadGraphImpl graph, int i) {
233 Types.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
236 public void execute(ReadGraphImpl graph, IntSet types) {
237 types.forEach(addToResult);
242 public void exception(ReadGraphImpl graph, Throwable t) {
243 procedure.exception(graph, t);
252 public void finished(ReadGraphImpl graph) {
260 SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() {
263 public void run(ReadGraphImpl graph) {
265 int current = finishes.addAndGet(1);
267 if(store) addOrSet(graph, result, queryProvider);
268 procedure.execute(graph, result);
274 public void execute(ReadGraphImpl graph, int i) {
278 Types.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
281 public void execute(ReadGraphImpl graph, IntSet types) {
283 types.forEach(addToResult);
289 public void exception(ReadGraphImpl graph, Throwable t) {
290 procedure.exception(graph, t);
299 public void finished(ReadGraphImpl graph) {
307 queryProvider.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);
308 instanceOfProcedure.finished(graph);
309 queryProvider.querySupport.getObjects(graph, id, inherits, inheritsProcedure);
310 inheritsProcedure.finished(graph);
311 queryProvider.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);
312 subrelationOfProcedure.finished(graph);
319 public String toString() {
320 return "Types[" + id + "]";
323 private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
338 final public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
342 if(handleException(graph, procedure)) return EXCEPTED;
344 IntSet result = getResult();
346 procedure.execute(graph, result);
353 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
355 final Semaphore s = new Semaphore(0);
357 computeForEach(graph, provider, new InternalProcedure<IntSet>() {
360 public void execute(ReadGraphImpl graph, IntSet result) {
365 public void exception(ReadGraphImpl graph, Throwable t) {
367 new Error("Error in recompute.", t).printStackTrace();
372 while(!s.tryAcquire()) {
373 provider.resume(graph);
379 boolean isImmutable(ReadGraphImpl graph) {
380 return graph.processor.isImmutable(id);