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 java.util.ArrayList;
15 import java.util.concurrent.Semaphore;
17 import org.simantics.db.RelationInfo;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.impl.procedure.IntProcedureAdapter;import org.simantics.db.impl.procedure.InternalProcedure;
20 import org.simantics.db.procedure.ListenerBase;
23 final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure> {
25 // public ArrayList<IntProcedure> procs = null;
27 public AssertedPredicates(final int r) {
31 public static AssertedPredicates newInstance(final int r) {
32 return new AssertedPredicates(r);
35 final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
37 AssertedPredicates entry = (AssertedPredicates)provider.assertedPredicatesMap.get(r);
40 entry = new AssertedPredicates(r);
42 entry.clearResult(provider.querySupport);
43 entry.putEntry(provider);
45 provider.performForEach(graph, entry, parent, listener, procedure);
49 if(!entry.isReady()) {
51 if(!entry.isReady()) {
52 throw new IllegalStateException();
53 // if(entry.procs == null) entry.procs = new ArrayList<IntProcedure>();
54 // entry.procs.add(procedure);
59 provider.performForEach(graph, entry, parent, listener, procedure);
64 final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
66 if(parent == null && listener == null) {
67 AssertedPredicates entry = (AssertedPredicates)provider.assertedPredicatesMap.get(r);
68 if(entry != null && entry.isReady()) {
69 entry.performFromCache(graph, provider, procedure);
74 runner(graph, r, provider, parent, listener, procedure);
79 public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
80 return provider.assertedPredicatesMap.get(id);
84 public void putEntry(QueryProcessor provider) {
85 provider.assertedPredicatesMap.put(id, this);
89 final public void removeEntry(QueryProcessor provider) {
90 provider.assertedPredicatesMap.remove(id);
93 void computeInheritedAssertions(ReadGraphImpl graph, int type, final QueryProcessor queryProvider, final IntProcedure proc, final boolean store) {
95 DirectObjects.queryEach(graph, type, queryProvider.getInherits(), queryProvider, this, null, new SyncIntProcedure() {
98 public void run(ReadGraphImpl graph) {
100 // finish(graph, queryProvider);
101 // proc.finished(graph);
106 public void execute(ReadGraphImpl graph,int inh) {
110 AssertedPredicates.queryEach(graph, inh, queryProvider, AssertedPredicates.this, null, new IntProcedure() {
113 public void execute(ReadGraphImpl graph, int ass) {
116 // proc.execute(graph, ass);
121 public void finished(ReadGraphImpl graph) {
126 public void exception(ReadGraphImpl graph, Throwable t) {
127 // proc.exception(graph, t);
135 public void finished(ReadGraphImpl graph) {
146 public Object computeForEach(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure proc, final boolean store) {
148 computeInheritedAssertions(graph, id, queryProvider, proc, store);
150 DirectObjects.queryEach(graph, id, queryProvider.getAsserts(), queryProvider, this, null, new IntProcedure() {
153 public void execute(ReadGraphImpl graph, final int ass) {
155 DirectObjects.queryEach(graph, ass, queryProvider.getHasPredicate(), queryProvider, AssertedPredicates.this, null, new IntProcedure() {
158 public void execute(ReadGraphImpl graph, final int pred) {
160 addOrSetHiding(graph, pred, AssertedPredicates.this);
161 // proc.execute(graph, pred);
167 public void finished(ReadGraphImpl graph) {
171 public void exception(ReadGraphImpl graph, Throwable t) {
172 // proc.exception(graph, t);
180 public void finished(ReadGraphImpl graph) {
184 public void exception(ReadGraphImpl graph, Throwable t) {
185 // proc.exception(graph, t);
190 finish(graph, queryProvider);
192 performFromCache(graph, queryProvider, proc);
199 public String toString() {
200 return "AssertedPredicates[" + id + "]";
203 final public void finish(ReadGraphImpl graph, QueryProcessor provider) {
207 // ArrayList<IntProcedure> p = null;
218 // IntArray v = (IntArray)getResult();
219 // if(v.data == null) {
220 // if(v.sizeOrData != IntArray.NO_DATA) {
221 // for(IntProcedure proc : p) proc.execute(graph, v.sizeOrData);
224 // for(IntProcedure proc : p) {
225 // for(int i = 0;i < v.sizeOrData ; i++) proc.execute(graph, v.data[i]);
229 // for(IntProcedure proc : p) proc.finished(graph);
234 synchronized private void addOrSet(int add) {
238 IntArray value = (IntArray)getResult();
243 final static InternalProcedure<RelationInfo> ip = new InternalProcedure<RelationInfo>() {
246 public void execute(ReadGraphImpl graph, RelationInfo result) {
250 public void exception(ReadGraphImpl graph, Throwable throwable) {
255 final static InternalProcedure<IntSet> ip2 = new InternalProcedure<IntSet>() {
258 public void execute(ReadGraphImpl graph, IntSet result) {
262 public void exception(ReadGraphImpl graph, Throwable throwable) {
267 synchronized private void addOrSetHiding(ReadGraphImpl graph, int add, CacheEntry parent) {
271 IntArray value = (IntArray)getResult();
272 RelationInfo ri = RelationInfoQuery.queryEach(graph, add, graph.processor, parent, null, ip);
273 if(ri.isFunctional) {
274 // Replace existing functional predicate if found
276 IntSet supers = SuperRelations.queryEach2(graph, add, graph.processor, parent, null, ip2);
277 if(value.data == null) {
278 if(value.sizeOrData != IntArray.NO_DATA) {
279 if(supers.contains(value.sizeOrData)) {
280 value.sizeOrData = add;
285 for(int i = 0;i < value.sizeOrData ; i++) {
286 if(supers.contains(value.data[i])) {
292 } catch (Throwable e) {
298 // No replacement - append
304 public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
308 if(handleException(graph, procedure)) return EXCEPTED;
310 final IntArray value = (IntArray)getResult();
311 if(value.data == null) {
312 if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
314 for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
317 procedure.finished(graph);
324 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
326 final Semaphore s = new Semaphore(0);
328 computeForEach(graph, provider, new IntProcedureAdapter() {
331 public void finished(ReadGraphImpl graph) {
336 public void exception(ReadGraphImpl graph, Throwable t) {
338 new Error("Error in recompute.", t).printStackTrace();
343 while(!s.tryAcquire()) {
344 provider.resume(graph);