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.common.exception.DebugException;
\r
19 import org.simantics.db.impl.graph.ReadGraphImpl;
\r
20 import org.simantics.db.procedure.ListenerBase;
\r
21 import org.simantics.db.request.RequestFlags;
\r
23 final public class Predicates extends UnaryQuery<IntProcedure> {
\r
25 // public ArrayList<IntProcedure> procs;
\r
27 public Predicates(final int r) {
\r
31 public static Predicates newInstance(final int r) {
\r
32 return new Predicates(r);
\r
35 final static Predicates entry(final QueryProcessor provider, final int r) {
\r
37 return (Predicates)provider.predicatesMap.get(r);
\r
41 final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, Predicates cached, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
\r
43 Predicates entry = cached != null ? cached : (Predicates)provider.predicatesMap.get(r);
\r
46 entry = new Predicates(r);
\r
48 entry.clearResult(provider.querySupport);
\r
49 entry.putEntry(provider);
\r
51 provider.performForEach(graph, entry, parent, listener, procedure);
\r
55 if(entry.isPending()) {
\r
56 synchronized(entry) {
\r
57 if(entry.isPending()) {
\r
58 throw new IllegalStateException();
\r
62 provider.performForEach(graph, entry, parent, listener, procedure);
\r
67 final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
\r
69 Predicates entry = (Predicates)provider.predicatesMap.get(r);
\r
72 entry = new Predicates(r);
\r
74 entry.clearResult(provider.querySupport);
\r
75 entry.putEntry(provider);
\r
77 return (IntSet)provider.performForEach2(graph, entry, parent, null, null);
\r
81 if(entry.isPending()) {
\r
82 synchronized(entry) {
\r
83 if(entry.isPending()) {
\r
84 throw new IllegalStateException();
\r
88 return (IntSet)provider.performForEach(graph, entry, parent, null, null);
\r
94 final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
\r
98 final Predicates entry = (Predicates)provider.predicatesMap.get(r);
\r
100 if(parent == null && listener == null) {
\r
101 if(entry != null && entry.isReady()) {
\r
102 entry.performFromCache(graph, provider, procedure);
\r
107 runner(graph, r, provider, entry, parent, listener, procedure);
\r
111 final public static IntSet queryEach2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
\r
113 if(parent == null) {
\r
114 final Predicates entry = (Predicates)provider.predicatesMap.get(r);
\r
115 if(entry != null && entry.isReady()) {
\r
116 return (IntSet)entry.get(graph, provider, null);
\r
120 return runner2(graph, r, provider, parent);
\r
125 public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
\r
126 return provider.predicatesMap.get(id);
\r
130 public void putEntry(QueryProcessor provider) {
\r
131 provider.predicatesMap.put(id, this);
\r
135 final public void removeEntry(QueryProcessor provider) {
\r
136 provider.predicatesMap.remove(id);
\r
139 final private void forAssertions(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure procedure, final boolean store) {
\r
141 PrincipalTypes.queryEach(graph, id, queryProvider, store ? Predicates.this : null, null, new SyncIntProcedure() {
\r
144 public void run(ReadGraphImpl graph) {
\r
146 finish(graph, queryProvider);
\r
147 procedure.finished(graph);
\r
151 IntProcedure proc = new IntProcedure() {
\r
154 public void execute(ReadGraphImpl graph, int i) {
\r
155 if(addOrSet(queryProvider, i))
\r
156 procedure.execute(graph, i);
\r
160 public void finished(ReadGraphImpl graph) {
\r
165 public void exception(ReadGraphImpl graph, Throwable t) {
\r
166 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
\r
167 procedure.exception(graph, t);
\r
173 public void execute(ReadGraphImpl graph, int type) {
\r
177 AssertedPredicates.queryEach(graph, type, queryProvider, store ? Predicates.this : null, null, proc);
\r
182 public void finished(ReadGraphImpl graph) {
\r
192 public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
\r
194 DirectPredicates.queryEach(graph, id, provider, store ? Predicates.this : null, null, new IntProcedure() {
\r
197 public void execute(ReadGraphImpl graph, final int pred) {
\r
199 if(addOrSet(provider, pred))
\r
200 procedure.execute(graph, pred);
\r
205 public void finished(ReadGraphImpl graph) {
\r
207 forAssertions(graph, provider, procedure, store);
\r
212 public void exception(ReadGraphImpl graph, Throwable t) {
\r
213 procedure.exception(graph, t);
\r
218 return getResult();
\r
223 public String toString() {
\r
224 return "Predicates2[" + id + "]";
\r
227 final public void finish(final ReadGraphImpl graph, QueryProcessor provider) {
\r
229 // ArrayList<IntProcedure> p = null;
\r
231 synchronized(this) {
\r
241 // final ArrayList<IntProcedure> finalP = p;
\r
243 // IntSet v = (IntSet)getResult();
\r
244 // v.forEach(new TIntProcedure() {
\r
247 // public boolean execute(int arg0) {
\r
248 // for(IntProcedure proc : finalP) proc.execute(graph, arg0);
\r
254 // for(IntProcedure proc : p) proc.finished(graph);
\r
260 synchronized private boolean addOrSet(QueryProcessor processor, int add) {
\r
263 setResult(new IntSet(null));
\r
266 IntSet value = (IntSet)getResult();
\r
267 return value.add(add);
\r
272 public void clearResult(QuerySupport support) {
\r
273 setResult(new IntSet(support));
\r
277 public Object performFromCache(final ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
\r
281 if(handleException(graph, procedure)) return EXCEPTED;
\r
283 IntSet v = getResult();
\r
284 if(procedure != null) {
\r
285 v.forEach(new TIntProcedure() {
\r
288 public boolean execute(int arg0) {
\r
289 procedure.execute(graph, arg0);
\r
293 procedure.finished(graph);
\r
301 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
\r
303 final Semaphore s = new Semaphore(0);
\r
305 computeForEach(graph, provider, new IntProcedure() {
\r
308 public void finished(ReadGraphImpl graph) {
\r
313 public void exception(ReadGraphImpl graph, Throwable t) {
\r
314 throw new Error("Error in recompute.", t);
\r
318 public void execute(ReadGraphImpl graph, int i) {
\r
323 while(!s.tryAcquire()) {
\r
324 provider.resume(graph);
\r
330 public int type() {
\r
331 return RequestFlags.IMMEDIATE_UPDATE;
\r
336 boolean isImmutable(ReadGraphImpl graph) {
\r
337 return graph.processor.isImmutable(id);
\r