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