--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.impl.query;\r
+\r
+import java.util.ArrayList;\r
+import java.util.concurrent.Semaphore;\r
+import java.util.concurrent.atomic.AtomicBoolean;\r
+\r
+import org.simantics.db.RelationInfo;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.impl.graph.ReadGraphImpl;\r
+import org.simantics.db.impl.procedure.InternalProcedure;\r
+import org.simantics.db.procedure.ListenerBase;\r
+import org.simantics.db.request.RequestFlags;\r
+\r
+final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<RelationInfo>> {\r
+ \r
+// public ArrayList<InternalProcedure<RelationInfo>> procs = null;\r
+\r
+ private RelationInfoQuery(final int resource) {\r
+ super(resource);\r
+ }\r
+ \r
+ final static RelationInfo runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<RelationInfo> procedure) {\r
+\r
+ RelationInfoQuery entry = (RelationInfoQuery)provider.relationInfoMap.get(r);\r
+ if(entry == null) {\r
+ \r
+ entry = new RelationInfoQuery(r);\r
+ entry.setPending();\r
+ entry.clearResult(provider.querySupport);\r
+ entry.putEntry(provider);\r
+ \r
+ provider.performForEach(graph, entry, parent, listener, procedure);\r
+ \r
+ return entry.getResult();\r
+ \r
+ } else {\r
+ \r
+ if(!entry.isReady()) {\r
+ synchronized(entry) {\r
+ if(!entry.isReady()) {\r
+ throw new IllegalStateException();\r
+// if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<RelationInfo>>();\r
+// entry.procs.add(procedure);\r
+// provider.registerDependencies(graph, entry, parent, listener, procedure, false);\r
+// return entry.getResult();\r
+ }\r
+ }\r
+ }\r
+ provider.performForEach(graph, entry, parent, listener, procedure);\r
+ \r
+ return entry.getResult();\r
+ \r
+ }\r
+\r
+ }\r
+ \r
+ final public static RelationInfo queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<RelationInfo> procedure) {\r
+ \r
+ RelationInfoQuery entry = (RelationInfoQuery)provider.relationInfoMap.get(r);\r
+ if(entry != null && entry.isReady()) {\r
+ entry.performFromCache(graph, provider, procedure);\r
+ return entry.getResult();\r
+ }\r
+\r
+ return runner(graph, r, provider, parent, listener, procedure);\r
+ \r
+ }\r
+ \r
+ final public static RelationInfoQuery probe(ReadGraphImpl graph, int resource) {\r
+ \r
+ final int thread = graph.thread(resource);\r
+ RelationInfoQuery entry = (RelationInfoQuery)graph.processor.relationInfoMap.get(resource);\r
+ if(entry != null && entry.isReady()) {\r
+ return entry;\r
+ } else {\r
+ return null;\r
+ }\r
+ \r
+ }\r
+\r
+ @Override\r
+ public UnaryQuery<InternalProcedure<RelationInfo>> getEntry(QueryProcessor provider) {\r
+ return provider.relationInfoMap.get(id);\r
+ }\r
+ \r
+ @Override\r
+ public void putEntry(QueryProcessor provider) {\r
+ provider.relationInfoMap.put(id, this);\r
+ }\r
+\r
+ @Override\r
+ final public void removeEntry(QueryProcessor provider) {\r
+ provider.relationInfoMap.remove(id);\r
+ }\r
+\r
+ private void computeAssertions(ReadGraphImpl graph, final boolean isFinal, final boolean isFunctional, final QueryProcessor queryProvider, final InternalProcedure<RelationInfo> proc) {\r
+\r
+ final int isUsedInAssertion = queryProvider.getHasPredicateInverse();\r
+ assert(isUsedInAssertion != 0);\r
+ \r
+ DirectObjects.queryEach(graph, id, isUsedInAssertion, queryProvider, this, null, new IntProcedure() {\r
+\r
+ AtomicBoolean done = new AtomicBoolean(false);\r
+ \r
+ @Override\r
+ public void execute(ReadGraphImpl graph, int i) {\r
+ if(done.compareAndSet(false, true)) {\r
+// System.err.println("Assertions for relation " + id);\r
+ RelationInfo result = new RelationInfo(id, isFunctional, isFinal, true);\r
+ addOrSet(graph, result, queryProvider);\r
+ proc.execute(graph, result);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void finished(ReadGraphImpl graph) {\r
+ if(done.compareAndSet(false, true)) {\r
+// System.err.println("No assertions for relation " + id);\r
+ RelationInfo result = new RelationInfo(id, isFunctional, isFinal, false);\r
+ addOrSet(graph, result, queryProvider);\r
+ proc.execute(graph, result);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void exception(ReadGraphImpl graph, Throwable throwable) {\r
+ if(done.compareAndSet(false, true)) {\r
+ DatabaseException e = new DatabaseException("Internal error in RelationInfoQuery");\r
+ except(e);\r
+ proc.exception(graph, e);\r
+ }\r
+ }\r
+ \r
+ });\r
+ \r
+// Types.queryEach(callerThread, id, queryProvider, this, null, new InternalProcedure<IntSet>() {\r
+// \r
+// @Override\r
+// public void execute(int callerThread, IntSet types) {\r
+// computeAssertions(callerThread, isFinal, isFunctional, queryProvider, proc);\r
+//// \r
+//// \r
+////// System.out.println("RelationInfoQuery: computeTypes execute " + types);\r
+////\r
+//// RelationInfo result = new RelationInfo(id, types.contains(queryProvider.getFunctionalRelation()), isFinal);\r
+//// \r
+//// addOrSet(callerThread, result, queryProvider);\r
+//// \r
+//// proc.execute(callerThread, result);\r
+//// \r
+// }\r
+// \r
+// @Override\r
+// public void exception(int callerThread, Throwable t) {\r
+// proc.exception(callerThread, t);\r
+// }\r
+//\r
+// });\r
+\r
+ }\r
+ \r
+ private void computeTypes(ReadGraphImpl graph, final boolean isFinal, final QueryProcessor queryProvider, final InternalProcedure<RelationInfo> proc) {\r
+ \r
+// System.out.println("RelationInfoQuery: computeTypes " + id);\r
+\r
+ Types.queryEach(graph, id, queryProvider, this, null, new InternalProcedure<IntSet>() {\r
+ \r
+ @Override\r
+ public void execute(ReadGraphImpl graph, IntSet types) {\r
+ computeAssertions(graph, isFinal, types.contains(queryProvider.getFunctionalRelation()), queryProvider, proc);\r
+//\r
+//// System.out.println("RelationInfoQuery: computeTypes execute " + types);\r
+//\r
+// RelationInfo result = new RelationInfo(id, types.contains(queryProvider.getFunctionalRelation()), isFinal);\r
+// \r
+// addOrSet(callerThread, result, queryProvider);\r
+// \r
+// proc.execute(callerThread, result);\r
+// \r
+ }\r
+ \r
+ @Override\r
+ public void exception(ReadGraphImpl graph, Throwable t) {\r
+ proc.exception(graph, t);\r
+ }\r
+\r
+ });\r
+\r
+ }\r
+ \r
+ @Override\r
+ public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<RelationInfo> procedure, boolean store) {\r
+ \r
+// System.out.println("RelationInfoQuery computeForEach begin " + id + " " + getResult() + " " + statusOrException);\r
+\r
+ final int superRelationOf = provider.getSuperrelationOf();\r
+ assert(superRelationOf != 0);\r
+ \r
+ DirectPredicates.queryEach(graph, id, provider, this, null, new IntProcedure() {\r
+ \r
+ boolean found = false;\r
+\r
+ @Override\r
+ public void execute(ReadGraphImpl graph, int i) {\r
+// System.out.println("RelationInfoQuery: execute " + i + " super = " + superRelationOf);\r
+ if(i == superRelationOf) {\r
+ computeTypes(graph, false, provider, procedure);\r
+ found = true;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void finished(ReadGraphImpl graph) {\r
+// System.out.println("RelationInfoQuery: finished");\r
+ if(!found) {\r
+ computeTypes(graph, true, provider, procedure);\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public void exception(ReadGraphImpl graph, Throwable t) {\r
+// System.out.println("RelationInfoQuery: exception");\r
+ procedure.exception(graph, t);\r
+ }\r
+\r
+ });\r
+ \r
+ return getResult();\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public String toString() {\r
+ return "RelationInfoQuery[" + id + "]";\r
+ }\r
+\r
+ public void addOrSet(ReadGraphImpl graph, final RelationInfo result, final QueryProcessor provider) {\r
+ \r
+ assert(isPending());\r
+ \r
+// ArrayList<InternalProcedure<RelationInfo>> p = null;\r
+ \r
+ synchronized(this) {\r
+\r
+ setResult(result);\r
+ setReady();\r
+ \r
+// p = procs;\r
+// procs = null;\r
+ \r
+ }\r
+ \r
+// if(p != null) {\r
+// for(InternalProcedure<RelationInfo> proc : p) \r
+// proc.execute(graph, (RelationInfo)result);\r
+// }\r
+ \r
+ } \r
+\r
+ @Override\r
+ public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<RelationInfo> procedure) {\r
+\r
+ assert(isReady());\r
+ \r
+ if(handleException(graph, procedure)) return EXCEPTED;\r
+ \r
+ RelationInfo result = getResult();\r
+ \r
+ procedure.execute(graph, result);\r
+ \r
+ return result;\r
+\r
+ }\r
+ \r
+ @Override\r
+ public void recompute(ReadGraphImpl graph, QueryProcessor provider) {\r
+ \r
+ final Semaphore s = new Semaphore(0);\r
+ \r
+ computeForEach(graph, provider, new InternalProcedure<RelationInfo>() {\r
+\r
+ @Override\r
+ public void execute(ReadGraphImpl graph, RelationInfo result) {\r
+ s.release();\r
+ }\r
+ \r
+ @Override\r
+ public void exception(ReadGraphImpl graph, Throwable t) {\r
+ throw new Error("Error in recompute.", t);\r
+ }\r
+\r
+ }, true);\r
+ \r
+ while(!s.tryAcquire()) {\r
+ provider.resume(graph);\r
+ }\r
+// try {\r
+// s.acquire();\r
+// } catch (InterruptedException e) {\r
+// throw new Error(e);\r
+// }\r
+\r
+ }\r
+ \r
+ @Override\r
+ public int type() {\r
+ return RequestFlags.IMMEDIATE_UPDATE;\r
+ }\r
+ \r
+}\r