-/*******************************************************************************\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.common.exception.DebugException;\r
-import org.simantics.db.impl.graph.ReadGraphImpl;\r
-import org.simantics.db.procedure.ListenerBase;\r
-import org.simantics.db.request.RequestFlags;\r
-\r
-final public class Predicates extends UnaryQuery<IntProcedure> {\r
- \r
-// public ArrayList<IntProcedure> procs;\r
-\r
- public Predicates(final int r) {\r
- super(r);\r
- }\r
-\r
- public static Predicates newInstance(final int r) {\r
- return new Predicates(r);\r
- }\r
- \r
- final static Predicates entry(final QueryProcessor provider, final int r) {\r
- \r
- return (Predicates)provider.predicatesMap.get(r);\r
-\r
- }\r
- \r
- final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, Predicates cached, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {\r
-\r
- Predicates entry = cached != null ? cached : (Predicates)provider.predicatesMap.get(r); \r
- if(entry == null) {\r
- \r
- entry = new Predicates(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.isPending()) {\r
- synchronized(entry) {\r
- if(entry.isPending()) {\r
- throw new IllegalStateException();\r
- }\r
- }\r
- }\r
- provider.performForEach(graph, entry, parent, listener, procedure);\r
- }\r
-\r
- }\r
-\r
- final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {\r
-\r
- Predicates entry = (Predicates)provider.predicatesMap.get(r); \r
- if(entry == null) {\r
- \r
- entry = new Predicates(r);\r
- entry.setPending();\r
- entry.clearResult(provider.querySupport);\r
- entry.putEntry(provider);\r
- \r
- return (IntSet)provider.performForEach2(graph, entry, parent, null, null);\r
- \r
- } else {\r
- \r
- if(entry.isPending()) {\r
- synchronized(entry) {\r
- if(entry.isPending()) {\r
- throw new IllegalStateException();\r
- }\r
- }\r
- }\r
- return (IntSet)provider.performForEach(graph, entry, parent, null, null);\r
- \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 IntProcedure procedure) {\r
- \r
- assert(r != 0);\r
- \r
- final Predicates entry = (Predicates)provider.predicatesMap.get(r);\r
- \r
- if(parent == null && listener == null) {\r
- if(entry != null && entry.isReady()) { \r
- entry.performFromCache(graph, provider, procedure);\r
- return;\r
- }\r
- }\r
-\r
- runner(graph, r, provider, entry, parent, listener, procedure);\r
- \r
- }\r
-\r
- final public static IntSet queryEach2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {\r
- \r
- if(parent == null) {\r
- final Predicates entry = (Predicates)provider.predicatesMap.get(r);\r
- if(entry != null && entry.isReady()) {\r
- return (IntSet)entry.get(graph, provider, null);\r
- }\r
- }\r
-\r
- return runner2(graph, r, provider, parent);\r
- \r
- }\r
- \r
- @Override\r
- public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {\r
- return provider.predicatesMap.get(id);\r
- }\r
- \r
- @Override\r
- public void putEntry(QueryProcessor provider) {\r
- provider.predicatesMap.put(id, this);\r
- }\r
-\r
- @Override\r
- final public void removeEntry(QueryProcessor provider) {\r
- provider.predicatesMap.remove(id);\r
- }\r
- \r
- final private void forAssertions(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure procedure, final boolean store) {\r
-\r
- PrincipalTypes.queryEach(graph, id, queryProvider, store ? Predicates.this : null, null, new SyncIntProcedure() {\r
- \r
- @Override\r
- public void run(ReadGraphImpl graph) {\r
- \r
- finish(graph, queryProvider);\r
- procedure.finished(graph);\r
- \r
- }\r
- \r
- IntProcedure proc = new IntProcedure() {\r
-\r
- @Override\r
- public void execute(ReadGraphImpl graph, int i) {\r
- if(addOrSet(queryProvider, i))\r
- procedure.execute(graph, i);\r
- }\r
-\r
- @Override\r
- public void finished(ReadGraphImpl graph) {\r
- dec(graph);\r
- }\r
- \r
- @Override\r
- public void exception(ReadGraphImpl graph, Throwable t) {\r
- if(DebugException.DEBUG) new DebugException(t).printStackTrace();\r
- procedure.exception(graph, t);\r
- }\r
-\r
- }; \r
-\r
- @Override\r
- public void execute(ReadGraphImpl graph, int type) {\r
-\r
- inc();\r
- \r
- AssertedPredicates.queryEach(graph, type, queryProvider, store ? Predicates.this : null, null, proc);\r
- \r
- }\r
- \r
- @Override\r
- public void finished(ReadGraphImpl graph) {\r
- dec(graph); \r
- }\r
- \r
- });\r
- \r
-\r
- }\r
-\r
- @Override\r
- public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {\r
-\r
- DirectPredicates.queryEach(graph, id, provider, store ? Predicates.this : null, null, new IntProcedure() {\r
-\r
- @Override\r
- public void execute(ReadGraphImpl graph, final int pred) {\r
-\r
- if(addOrSet(provider, pred))\r
- procedure.execute(graph, pred);\r
-\r
- }\r
-\r
- @Override\r
- public void finished(ReadGraphImpl graph) {\r
-\r
- forAssertions(graph, provider, procedure, store);\r
-\r
- }\r
-\r
- @Override\r
- public void exception(ReadGraphImpl graph, Throwable t) {\r
- procedure.exception(graph, t);\r
- }\r
-\r
- });\r
- \r
- return getResult();\r
- \r
- }\r
- \r
- @Override\r
- public String toString() {\r
- return "Predicates2[" + id + "]";\r
- }\r
-\r
- final public void finish(final ReadGraphImpl graph, QueryProcessor provider) {\r
- \r
-// ArrayList<IntProcedure> p = null;\r
-\r
- synchronized(this) {\r
-\r
- setReady();\r
-// p = procs;\r
-// procs = null; \r
- \r
- }\r
- \r
-// if(p != null) {\r
-// \r
-// final ArrayList<IntProcedure> finalP = p;\r
-//\r
-// IntSet v = (IntSet)getResult();\r
-// v.forEach(new TIntProcedure() {\r
-//\r
-// @Override\r
-// public boolean execute(int arg0) {\r
-// for(IntProcedure proc : finalP) proc.execute(graph, arg0);\r
-// return true;\r
-// }\r
-//\r
-// });\r
-//\r
-// for(IntProcedure proc : p) proc.finished(graph);\r
-// \r
-// }\r
-\r
- }\r
-\r
- synchronized private boolean addOrSet(QueryProcessor processor, int add) {\r
-\r
- if(!isPending()) {\r
- setResult(new IntSet(null));\r
- }\r
- \r
- IntSet value = (IntSet)getResult();\r
- return value.add(add);\r
- \r
- }\r
-\r
- @Override\r
- public void clearResult(QuerySupport support) {\r
- setResult(new IntSet(support));\r
- }\r
- \r
- @Override\r
- public Object performFromCache(final ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {\r
- \r
- assert(isReady());\r
-\r
- if(handleException(graph, procedure)) return EXCEPTED;\r
- \r
- IntSet v = getResult();\r
- if(procedure != null) {\r
- v.forEach(new TIntProcedure() {\r
-\r
- @Override\r
- public boolean execute(int arg0) {\r
- procedure.execute(graph, arg0); \r
- return true;\r
- }\r
- });\r
- procedure.finished(graph);\r
- }\r
- \r
- return v;\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 IntProcedure() {\r
-\r
- @Override\r
- public void finished(ReadGraphImpl graph) {\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
- @Override\r
- public void execute(ReadGraphImpl graph, int i) {\r
- }\r
-\r
- }, true);\r
- \r
- while(!s.tryAcquire()) {\r
- provider.resume(graph);\r
- }\r
- \r
- }\r
- \r
- @Override\r
- public int type() {\r
- return RequestFlags.IMMEDIATE_UPDATE;\r
- }\r
-\r
-\r
- @Override\r
- boolean isImmutable(ReadGraphImpl graph) {\r
- return graph.processor.isImmutable(id);\r
- }\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 org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.impl.graph.ReadGraphImpl;
+import org.simantics.db.impl.procedure.InternalProcedure;
+import org.simantics.db.request.RequestFlags;
+
+import gnu.trove.procedure.TIntProcedure;
+
+final public class Predicates extends UnaryQueryP<IntSet> {
+
+ Predicates(final int r) {
+ super(r);
+ }
+
+ @Override
+ final public void removeEntry(QueryProcessor provider) {
+ provider.cache.remove(this);
+ }
+
+ final static private void forAssertions(ReadGraphImpl graph, int r, Predicates parent, final IntSet set) throws DatabaseException {
+
+ QueryCache.runnerPrincipalTypes(graph, r, parent, null, new SyncIntProcedure() {
+
+ @Override
+ public void run(ReadGraphImpl graph) throws DatabaseException {
+ }
+
+ IntProcedure proc = new IntProcedure() {
+
+ @Override
+ public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+ set.add(i);
+ }
+
+ @Override
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
+ dec(graph);
+ }
+
+ @Override
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+ if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+ }
+
+ };
+
+ @Override
+ public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
+
+ inc();
+ QueryCache.runnerAssertedPredicates(graph, type, parent, null, proc);
+
+ }
+
+ @Override
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
+ dec(graph);
+ }
+
+ });
+
+
+ }
+
+ @Override
+ public void compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+ computeForEach(graph, id, this, procedure);
+ }
+
+ public static void computeForEach(ReadGraphImpl graph, final int r, final Predicates entry, final InternalProcedure<IntSet> procedure_) throws DatabaseException {
+
+ InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
+
+ IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null);
+
+ IntSet result = new IntSet(graph.processor.querySupport);
+ forAssertions(graph, r, entry, result);
+
+ if(result.isEmpty()) {
+ procedure.execute(graph, direct);
+ } else {
+ direct.forEach(new TIntProcedure() {
+ @Override
+ public boolean execute(int value) {
+ result.add(value);
+ return true;
+ }
+ });
+ procedure.execute(graph, result);
+ }
+
+ if(entry != null) entry.performFromCache(graph, procedure_);
+
+ }
+
+ @Override
+ public String toString() {
+ return "Predicates[" + id + "]";
+ }
+
+ @Override
+ public void clearResult(QuerySupport support) {
+ setResult(new IntSet(support));
+ }
+
+ @Override
+ public int type() {
+ return RequestFlags.IMMEDIATE_UPDATE;
+ }
+
+}