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;
16 import java.util.concurrent.atomic.AtomicBoolean;
18 import org.simantics.db.RelationInfo;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.impl.graph.ReadGraphImpl;
21 import org.simantics.db.impl.procedure.InternalProcedure;
22 import org.simantics.db.procedure.ListenerBase;
23 import org.simantics.db.request.RequestFlags;
25 final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<RelationInfo>> {
27 // public ArrayList<InternalProcedure<RelationInfo>> procs = null;
29 private RelationInfoQuery(final int resource) {
33 final static RelationInfo runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<RelationInfo> procedure) {
35 RelationInfoQuery entry = (RelationInfoQuery)provider.relationInfoMap.get(r);
38 entry = new RelationInfoQuery(r);
40 entry.clearResult(provider.querySupport);
41 entry.putEntry(provider);
43 provider.performForEach(graph, entry, parent, listener, procedure);
45 return entry.getResult();
49 if(!entry.isReady()) {
51 if(!entry.isReady()) {
52 throw new IllegalStateException();
53 // if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<RelationInfo>>();
54 // entry.procs.add(procedure);
55 // provider.registerDependencies(graph, entry, parent, listener, procedure, false);
56 // return entry.getResult();
60 provider.performForEach(graph, entry, parent, listener, procedure);
62 return entry.getResult();
68 final public static RelationInfo queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<RelationInfo> procedure) {
70 RelationInfoQuery entry = (RelationInfoQuery)provider.relationInfoMap.get(r);
71 if(entry != null && entry.isReady()) {
72 entry.performFromCache(graph, provider, procedure);
73 return entry.getResult();
76 return runner(graph, r, provider, parent, listener, procedure);
80 final public static RelationInfoQuery probe(ReadGraphImpl graph, int resource) {
82 final int thread = graph.thread(resource);
83 RelationInfoQuery entry = (RelationInfoQuery)graph.processor.relationInfoMap.get(resource);
84 if(entry != null && entry.isReady()) {
93 public UnaryQuery<InternalProcedure<RelationInfo>> getEntry(QueryProcessor provider) {
94 return provider.relationInfoMap.get(id);
98 public void putEntry(QueryProcessor provider) {
99 provider.relationInfoMap.put(id, this);
103 final public void removeEntry(QueryProcessor provider) {
104 provider.relationInfoMap.remove(id);
107 private void computeAssertions(ReadGraphImpl graph, final boolean isFinal, final boolean isFunctional, final QueryProcessor queryProvider, final InternalProcedure<RelationInfo> proc) {
109 final int isUsedInAssertion = queryProvider.getHasPredicateInverse();
110 assert(isUsedInAssertion != 0);
112 DirectObjects.queryEach(graph, id, isUsedInAssertion, queryProvider, this, null, new IntProcedure() {
114 AtomicBoolean done = new AtomicBoolean(false);
117 public void execute(ReadGraphImpl graph, int i) {
118 if(done.compareAndSet(false, true)) {
119 // System.err.println("Assertions for relation " + id);
120 RelationInfo result = new RelationInfo(id, isFunctional, isFinal, true);
121 addOrSet(graph, result, queryProvider);
122 proc.execute(graph, result);
127 public void finished(ReadGraphImpl graph) {
128 if(done.compareAndSet(false, true)) {
129 // System.err.println("No assertions for relation " + id);
130 RelationInfo result = new RelationInfo(id, isFunctional, isFinal, false);
131 addOrSet(graph, result, queryProvider);
132 proc.execute(graph, result);
137 public void exception(ReadGraphImpl graph, Throwable throwable) {
138 if(done.compareAndSet(false, true)) {
139 DatabaseException e = new DatabaseException("Internal error in RelationInfoQuery");
141 proc.exception(graph, e);
147 // Types.queryEach(callerThread, id, queryProvider, this, null, new InternalProcedure<IntSet>() {
150 // public void execute(int callerThread, IntSet types) {
151 // computeAssertions(callerThread, isFinal, isFunctional, queryProvider, proc);
154 ////// System.out.println("RelationInfoQuery: computeTypes execute " + types);
156 //// RelationInfo result = new RelationInfo(id, types.contains(queryProvider.getFunctionalRelation()), isFinal);
158 //// addOrSet(callerThread, result, queryProvider);
160 //// proc.execute(callerThread, result);
165 // public void exception(int callerThread, Throwable t) {
166 // proc.exception(callerThread, t);
173 private void computeTypes(ReadGraphImpl graph, final boolean isFinal, final QueryProcessor queryProvider, final InternalProcedure<RelationInfo> proc) {
175 // System.out.println("RelationInfoQuery: computeTypes " + id);
177 Types.queryEach(graph, id, queryProvider, this, null, new InternalProcedure<IntSet>() {
180 public void execute(ReadGraphImpl graph, IntSet types) {
181 computeAssertions(graph, isFinal, types.contains(queryProvider.getFunctionalRelation()), queryProvider, proc);
183 //// System.out.println("RelationInfoQuery: computeTypes execute " + types);
185 // RelationInfo result = new RelationInfo(id, types.contains(queryProvider.getFunctionalRelation()), isFinal);
187 // addOrSet(callerThread, result, queryProvider);
189 // proc.execute(callerThread, result);
194 public void exception(ReadGraphImpl graph, Throwable t) {
195 proc.exception(graph, t);
203 public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<RelationInfo> procedure, boolean store) {
205 // System.out.println("RelationInfoQuery computeForEach begin " + id + " " + getResult() + " " + statusOrException);
207 final int superRelationOf = provider.getSuperrelationOf();
208 assert(superRelationOf != 0);
210 DirectPredicates.queryEach(graph, id, provider, this, null, new IntProcedure() {
212 boolean found = false;
215 public void execute(ReadGraphImpl graph, int i) {
216 // System.out.println("RelationInfoQuery: execute " + i + " super = " + superRelationOf);
217 if(i == superRelationOf) {
218 computeTypes(graph, false, provider, procedure);
224 public void finished(ReadGraphImpl graph) {
225 // System.out.println("RelationInfoQuery: finished");
227 computeTypes(graph, true, provider, procedure);
232 public void exception(ReadGraphImpl graph, Throwable t) {
233 // System.out.println("RelationInfoQuery: exception");
234 procedure.exception(graph, t);
244 public String toString() {
245 return "RelationInfoQuery[" + id + "]";
248 public void addOrSet(ReadGraphImpl graph, final RelationInfo result, final QueryProcessor provider) {
252 // ArrayList<InternalProcedure<RelationInfo>> p = null;
265 // for(InternalProcedure<RelationInfo> proc : p)
266 // proc.execute(graph, (RelationInfo)result);
272 public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<RelationInfo> procedure) {
276 if(handleException(graph, procedure)) return EXCEPTED;
278 RelationInfo result = getResult();
280 procedure.execute(graph, result);
287 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
289 final Semaphore s = new Semaphore(0);
291 computeForEach(graph, provider, new InternalProcedure<RelationInfo>() {
294 public void execute(ReadGraphImpl graph, RelationInfo result) {
299 public void exception(ReadGraphImpl graph, Throwable t) {
300 throw new Error("Error in recompute.", t);
305 while(!s.tryAcquire()) {
306 provider.resume(graph);
310 // } catch (InterruptedException e) {
311 // throw new Error(e);
318 return RequestFlags.IMMEDIATE_UPDATE;