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 TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
24 // public ArrayList<InternalProcedure<IntSet>> procs = null;
26 private TypeHierarchy(final int resource) {
30 final static void runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
32 TypeHierarchy entry = (TypeHierarchy)provider.typeHierarchyMap.get(r);
35 entry = new TypeHierarchy(r);
37 entry.clearResult(provider.querySupport);
38 entry.putEntry(provider);
40 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);
60 final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
62 if(parent == null && listener == null) {
63 TypeHierarchy entry = (TypeHierarchy)provider.typeHierarchyMap.get(r);
64 if(entry != null && entry.isReady()) {
65 entry.performFromCache(graph, provider, procedure);
70 runner(graph, r, parent, provider, listener, procedure);
75 public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
76 return provider.typeHierarchyMap.get(id);
80 public void putEntry(QueryProcessor provider) {
81 provider.typeHierarchyMap.put(id, this);
85 final public void removeEntry(QueryProcessor provider) {
86 provider.typeHierarchyMap.remove(id);
90 public IntSet computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, boolean store) {
92 final IntSet result = new IntSet(provider.querySupport, id);
94 final TIntProcedure addToResult = new TIntProcedure() {
96 public boolean execute(int r) {
102 SuperTypes.queryEach(graph, id, provider, TypeHierarchy.this, null, new InternalProcedure<IntSet>() {
105 public void execute(ReadGraphImpl graph, IntSet types) {
107 types.forEach(addToResult);
108 addOrSet(graph, result, provider);
109 procedure.execute(graph, result);
114 public void exception(ReadGraphImpl graph, Throwable t) {
115 procedure.exception(graph, t);
125 public String toString() {
126 return "TypeHierarchy[" + id + "]";
129 private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
133 // ArrayList<InternalProcedure<IntSet>> p = null;
146 // IntSet v = (IntSet)getResult();
148 // for(InternalProcedure<IntSet> proc : p) proc.execute(graph, v);
155 public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
159 if(handleException(graph, procedure)) return null;
161 IntSet result = getResult();
163 procedure.execute(graph, result);
170 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
172 final Semaphore s = new Semaphore(0);
174 computeForEach(graph, provider, new InternalProcedure<IntSet>() {
177 public void execute(ReadGraphImpl graph, IntSet result) {
182 public void exception(ReadGraphImpl graph, Throwable t) {
184 new Error("Error in recompute.", t).printStackTrace();
189 while(!s.tryAcquire()) {
190 provider.resume(graph);
196 boolean isImmutable(ReadGraphImpl graph) {
197 return graph.processor.isImmutable(id);