-/*******************************************************************************\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 gnu.trove.procedure.TIntProcedure;\r
-\r
-import java.util.concurrent.Semaphore;\r
-\r
-import org.simantics.db.impl.graph.ReadGraphImpl;\r
-import org.simantics.db.impl.procedure.InternalProcedure;\r
-import org.simantics.db.procedure.ListenerBase;\r
-\r
-final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {\r
- \r
-// public ArrayList<InternalProcedure<IntSet>> procs = null;\r
- \r
- private TypeHierarchy(final int resource) {\r
- super(resource);\r
- }\r
- \r
- final static void runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {\r
-\r
- TypeHierarchy entry = (TypeHierarchy)provider.typeHierarchyMap.get(r);\r
- if(entry == null) {\r
-\r
- entry = new TypeHierarchy(r);\r
- entry.setPending();\r
- entry.clearResult(provider.querySupport);\r
- entry.putEntry(provider);\r
- \r
- provider.performForEach(graph, entry, parent, listener, procedure);\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<IntSet>>();\r
-// entry.procs.add(procedure);\r
-// provider.registerDependencies(graph, entry, parent, listener, procedure, false);\r
-// return;\r
- }\r
- }\r
- }\r
- provider.performForEach(graph, entry, parent, listener, procedure);\r
- }\r
-\r
- }\r
- \r
- final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {\r
- \r
- if(parent == null && listener == null) {\r
- TypeHierarchy entry = (TypeHierarchy)provider.typeHierarchyMap.get(r);\r
- if(entry != null && entry.isReady()) { \r
- entry.performFromCache(graph, provider, procedure);\r
- return;\r
- }\r
- }\r
- \r
- runner(graph, r, parent, provider, listener, procedure);\r
- \r
- }\r
- \r
- @Override\r
- public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {\r
- return provider.typeHierarchyMap.get(id);\r
- }\r
- \r
- @Override\r
- public void putEntry(QueryProcessor provider) {\r
- provider.typeHierarchyMap.put(id, this);\r
- }\r
-\r
- @Override\r
- final public void removeEntry(QueryProcessor provider) {\r
- provider.typeHierarchyMap.remove(id);\r
- }\r
-\r
- @Override\r
- public IntSet computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, boolean store) {\r
-\r
- final IntSet result = new IntSet(provider.querySupport, id);\r
- \r
- final TIntProcedure addToResult = new TIntProcedure() {\r
- @Override\r
- public boolean execute(int r) {\r
- result.add(r);\r
- return true;\r
- }\r
- };\r
- \r
- SuperTypes.queryEach(graph, id, provider, TypeHierarchy.this, null, new InternalProcedure<IntSet>() {\r
-\r
- @Override\r
- public void execute(ReadGraphImpl graph, IntSet types) {\r
-\r
- types.forEach(addToResult);\r
- addOrSet(graph, result, provider);\r
- procedure.execute(graph, result); \r
-\r
- }\r
-\r
- @Override\r
- public void exception(ReadGraphImpl graph, Throwable t) {\r
- procedure.exception(graph, t);\r
- }\r
-\r
- });\r
- \r
- return result;\r
- \r
- }\r
- \r
- @Override\r
- public String toString() {\r
- return "TypeHierarchy[" + id + "]";\r
- }\r
-\r
- private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {\r
- \r
- assert(!isReady());\r
-\r
-// ArrayList<InternalProcedure<IntSet>> p = null;\r
-\r
- synchronized(this) {\r
- \r
- value.trim();\r
- setResult(value);\r
- setReady();\r
-// p = procs;\r
-// procs = null; \r
- \r
- }\r
-\r
-// if(p != null) {\r
-// IntSet v = (IntSet)getResult();\r
-// if(v != null) {\r
-// for(InternalProcedure<IntSet> proc : p) proc.execute(graph, v);\r
-// }\r
-// }\r
- \r
- }\r
-\r
- @Override\r
- public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {\r
- \r
- assert(isReady());\r
-\r
- if(handleException(graph, procedure)) return null;\r
- \r
- IntSet 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<IntSet>() {\r
-\r
- @Override\r
- public void execute(ReadGraphImpl graph, IntSet result) {\r
- s.release();\r
- }\r
-\r
- @Override\r
- public void exception(ReadGraphImpl graph, Throwable t) {\r
- s.release();\r
- new Error("Error in recompute.", t).printStackTrace();\r
- }\r
-\r
- }, true);\r
-\r
- while(!s.tryAcquire()) {\r
- provider.resume(graph);\r
- }\r
- \r
- }\r
- \r
- @Override\r
- boolean isImmutable(ReadGraphImpl graph) {\r
- return graph.processor.isImmutable(id);\r
- } \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.impl.query;
+
+import gnu.trove.procedure.TIntProcedure;
+
+import java.util.concurrent.Semaphore;
+
+import org.simantics.db.impl.graph.ReadGraphImpl;
+import org.simantics.db.impl.procedure.InternalProcedure;
+import org.simantics.db.procedure.ListenerBase;
+
+final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
+
+// public ArrayList<InternalProcedure<IntSet>> procs = null;
+
+ private TypeHierarchy(final int resource) {
+ super(resource);
+ }
+
+ final static void runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
+
+ TypeHierarchy entry = (TypeHierarchy)provider.typeHierarchyMap.get(r);
+ if(entry == null) {
+
+ entry = new TypeHierarchy(r);
+ entry.setPending();
+ entry.clearResult(provider.querySupport);
+ entry.putEntry(provider);
+
+ provider.performForEach(graph, entry, parent, listener, procedure);
+
+ } else {
+
+ if(!entry.isReady()) {
+ synchronized(entry) {
+ if(!entry.isReady()) {
+ throw new IllegalStateException();
+// if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<IntSet>>();
+// entry.procs.add(procedure);
+// provider.registerDependencies(graph, entry, parent, listener, procedure, false);
+// return;
+ }
+ }
+ }
+ provider.performForEach(graph, entry, parent, listener, procedure);
+ }
+
+ }
+
+ final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
+
+ if(parent == null && listener == null) {
+ TypeHierarchy entry = (TypeHierarchy)provider.typeHierarchyMap.get(r);
+ if(entry != null && entry.isReady()) {
+ entry.performFromCache(graph, provider, procedure);
+ return;
+ }
+ }
+
+ runner(graph, r, parent, provider, listener, procedure);
+
+ }
+
+ @Override
+ public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
+ return provider.typeHierarchyMap.get(id);
+ }
+
+ @Override
+ public void putEntry(QueryProcessor provider) {
+ provider.typeHierarchyMap.put(id, this);
+ }
+
+ @Override
+ final public void removeEntry(QueryProcessor provider) {
+ provider.typeHierarchyMap.remove(id);
+ }
+
+ @Override
+ public IntSet computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, boolean store) {
+
+ final IntSet result = new IntSet(provider.querySupport, id);
+
+ final TIntProcedure addToResult = new TIntProcedure() {
+ @Override
+ public boolean execute(int r) {
+ result.add(r);
+ return true;
+ }
+ };
+
+ SuperTypes.queryEach(graph, id, provider, TypeHierarchy.this, null, new InternalProcedure<IntSet>() {
+
+ @Override
+ public void execute(ReadGraphImpl graph, IntSet types) {
+
+ types.forEach(addToResult);
+ addOrSet(graph, result, provider);
+ procedure.execute(graph, result);
+
+ }
+
+ @Override
+ public void exception(ReadGraphImpl graph, Throwable t) {
+ procedure.exception(graph, t);
+ }
+
+ });
+
+ return result;
+
+ }
+
+ @Override
+ public String toString() {
+ return "TypeHierarchy[" + id + "]";
+ }
+
+ private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
+
+ assert(!isReady());
+
+// ArrayList<InternalProcedure<IntSet>> p = null;
+
+ synchronized(this) {
+
+ value.trim();
+ setResult(value);
+ setReady();
+// p = procs;
+// procs = null;
+
+ }
+
+// if(p != null) {
+// IntSet v = (IntSet)getResult();
+// if(v != null) {
+// for(InternalProcedure<IntSet> proc : p) proc.execute(graph, v);
+// }
+// }
+
+ }
+
+ @Override
+ public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
+
+ assert(isReady());
+
+ if(handleException(graph, procedure)) return null;
+
+ IntSet result = getResult();
+
+ procedure.execute(graph, result);
+
+ return result;
+
+ }
+
+ @Override
+ public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+
+ final Semaphore s = new Semaphore(0);
+
+ computeForEach(graph, provider, new InternalProcedure<IntSet>() {
+
+ @Override
+ public void execute(ReadGraphImpl graph, IntSet result) {
+ s.release();
+ }
+
+ @Override
+ public void exception(ReadGraphImpl graph, Throwable t) {
+ s.release();
+ new Error("Error in recompute.", t).printStackTrace();
+ }
+
+ }, true);
+
+ while(!s.tryAcquire()) {
+ provider.resume(graph);
+ }
+
+ }
+
+ @Override
+ boolean isImmutable(ReadGraphImpl graph) {
+ return graph.processor.isImmutable(id);
+ }
+}