-/*******************************************************************************\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
-import java.util.concurrent.atomic.AtomicInteger;\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 Types extends UnaryQuery<InternalProcedure<IntSet>> {\r
- \r
-// public ArrayList<InternalProcedure<IntSet>> procs;\r
- \r
- private Types(final int resource) {\r
- super(resource);\r
- }\r
- \r
- final static Types entry(final QueryProcessor provider, final int r) {\r
- return (Types)provider.typesMap.get(r);\r
- }\r
- \r
- final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, Types cached, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {\r
-\r
- Types entry = cached != null ? cached : (Types)provider.typesMap.get(r); \r
- if(entry == null) {\r
- \r
- entry = new Types(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
- throw new IllegalStateException();\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
- Types entry = (Types)provider.typesMap.get(r); \r
- if(entry == null) {\r
- \r
- entry = new Types(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.isReady()) {\r
- throw new IllegalStateException();\r
- }\r
- return (IntSet)provider.performForEach2(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 InternalProcedure<IntSet> procedure) {\r
- \r
- final Types entry = (Types)provider.typesMap.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
- Types entry = (Types)provider.typesMap.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<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {\r
- return provider.typesMap.get(id);\r
- }\r
- \r
- @Override\r
- public void putEntry(QueryProcessor provider) {\r
- provider.typesMap.put(id, this);\r
- }\r
-\r
- @Override\r
- final public void removeEntry(QueryProcessor provider) {\r
- provider.typesMap.remove(id);\r
- }\r
-\r
- @Override\r
- public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor queryProvider, final InternalProcedure<IntSet> procedure, final boolean store) {\r
-\r
- queryProvider.querySupport.ensureLoaded(graph, id);\r
- int ret = queryProvider.querySupport.getSingleInstance(id);\r
- if(ret > 0) {\r
-\r
- TypeHierarchy.queryEach(graph, ret, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {\r
-\r
- @Override\r
- public void execute(ReadGraphImpl graph, IntSet types) {\r
-\r
- addOrSet(graph, types, queryProvider);\r
- procedure.execute(graph, types);\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
- final int instanceOf = queryProvider.getInstanceOf();\r
- final int inherits = queryProvider.getInherits();\r
- final int subrelationOf = queryProvider.getSubrelationOf();\r
-\r
- final IntSet result = new IntSet(queryProvider.querySupport);\r
- \r
- final TIntProcedure addToResult = new TIntProcedure() {\r
- @Override\r
- public boolean execute(int r) {\r
- synchronized(result) {\r
- result.add(r);\r
- }\r
- return true;\r
- }\r
- };\r
- \r
- final AtomicInteger finishes = new AtomicInteger(0);\r
- \r
- SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() {\r
- \r
- @Override\r
- public void run(ReadGraphImpl graph) {\r
- \r
- if(finishes.addAndGet(1) == 3) {\r
- if(store) addOrSet(graph, result, queryProvider);\r
- procedure.execute(graph, result);\r
- }\r
- \r
- }\r
- \r
- @Override\r
- public void execute(ReadGraphImpl graph, int i) {\r
- \r
- synchronized(result) {\r
- result.add(i);\r
- }\r
- \r
- inc();\r
-\r
- SuperTypes.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {\r
-\r
- @Override\r
- public void execute(ReadGraphImpl graph, IntSet types) {\r
- types.forEach(addToResult);\r
- dec(graph);\r
- }\r
- \r
- @Override\r
- public void exception(ReadGraphImpl graph, Throwable t) {\r
- procedure.exception(graph, t);\r
- dec(graph);\r
- }\r
-\r
- });\r
- \r
- }\r
-\r
- @Override\r
- public void finished(ReadGraphImpl graph) {\r
- dec(graph);\r
- }\r
- \r
- }; \r
- \r
- SyncIntProcedure inheritsProcedure = new SyncIntProcedure() {\r
- \r
- @Override\r
- public void run(ReadGraphImpl graph) {\r
-\r
- int current = finishes.addAndGet(1);\r
- if(current == 3) {\r
- if(store) addOrSet(graph, result, queryProvider);\r
- procedure.execute(graph, result);\r
- }\r
- \r
- }\r
- \r
- @Override\r
- public void execute(ReadGraphImpl graph, int i) {\r
- \r
- inc();\r
-\r
- Types.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {\r
-\r
- @Override\r
- public void execute(ReadGraphImpl graph, IntSet types) {\r
- types.forEach(addToResult);\r
- dec(graph);\r
- }\r
- \r
- @Override\r
- public void exception(ReadGraphImpl graph, Throwable t) {\r
- procedure.exception(graph, t);\r
- dec(graph);\r
- }\r
-\r
- });\r
- \r
- }\r
-\r
- @Override\r
- public void finished(ReadGraphImpl graph) {\r
-\r
- dec(graph);\r
-\r
- }\r
- \r
- }; \r
-\r
- SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() {\r
-\r
- @Override\r
- public void run(ReadGraphImpl graph) {\r
-\r
- int current = finishes.addAndGet(1);\r
- if(current == 3) {\r
- if(store) addOrSet(graph, result, queryProvider);\r
- procedure.execute(graph, result);\r
- }\r
- \r
- }\r
- \r
- @Override\r
- public void execute(ReadGraphImpl graph, int i) {\r
- \r
- inc();\r
- \r
- Types.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {\r
-\r
- @Override\r
- public void execute(ReadGraphImpl graph, IntSet types) {\r
-\r
- types.forEach(addToResult);\r
- dec(graph);\r
- \r
- }\r
- \r
- @Override\r
- public void exception(ReadGraphImpl graph, Throwable t) {\r
- procedure.exception(graph, t);\r
- dec(graph);\r
- }\r
-\r
- });\r
- \r
- }\r
-\r
- @Override\r
- public void finished(ReadGraphImpl graph) {\r
- \r
- dec(graph);\r
-\r
- }\r
- \r
- }; \r
- \r
- queryProvider.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);\r
- instanceOfProcedure.finished(graph);\r
- queryProvider.querySupport.getObjects(graph, id, inherits, inheritsProcedure);\r
- inheritsProcedure.finished(graph);\r
- queryProvider.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);\r
- subrelationOfProcedure.finished(graph);\r
- \r
- return result;\r
- \r
- }\r
- \r
- @Override\r
- public String toString() {\r
- return "Types[" + id + "]";\r
- }\r
- \r
- private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {\r
- \r
- assert(!isReady());\r
-\r
- synchronized(this) {\r
- \r
- value.trim();\r
- setResult(value);\r
- setReady();\r
- \r
- }\r
- \r
- }\r
- \r
- @Override\r
- final public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {\r
- \r
- assert(isReady());\r
- \r
- if(handleException(graph, procedure)) return EXCEPTED;\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
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2018 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 java.util.concurrent.atomic.AtomicInteger;
+
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.impl.graph.ReadGraphImpl;
+import org.simantics.db.impl.procedure.InternalProcedure;
+
+import gnu.trove.procedure.TIntProcedure;
+
+public final class Types extends UnaryQueryP<IntSet> {
+
+ Types(int resource) {
+ super(resource);
+ }
+
+ @Override
+ public final void removeEntry(QueryProcessor provider) {
+ provider.cache.remove(this);
+ }
+
+ @Override
+ public void compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+ computeForEach(graph, id, this, procedure);
+ }
+
+ public static void computeForEach(final ReadGraphImpl graph, int id, Types entry,
+ final InternalProcedure<IntSet> procedure_) throws DatabaseException {
+
+ InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
+
+ computeForEach2(graph, id, entry, procedure);
+
+ if (entry != null)
+ entry.performFromCache(graph, procedure_);
+
+ }
+
+ public static void computeForEach2(final ReadGraphImpl graph, int id, Types parent,
+ final InternalProcedure<IntSet> procedure) throws DatabaseException {
+
+ QueryProcessor processor = graph.processor;
+
+ processor.querySupport.ensureLoaded(graph, id);
+
+ int ret = processor.querySupport.getSingleInstance(id);
+ if (ret > 0) {
+
+ TypeHierarchy.queryEach(graph, ret, processor, parent, null, new InternalProcedure<IntSet>() {
+
+ @Override
+ public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
+ procedure.execute(graph, types);
+ }
+
+ @Override
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+ procedure.exception(graph, t);
+ }
+
+ });
+
+ return;
+
+ }
+
+ final int instanceOf = processor.getInstanceOf();
+ final int inherits = processor.getInherits();
+ final int subrelationOf = processor.getSubrelationOf();
+
+ final IntSet result = new IntSet(processor.querySupport);
+
+ final TIntProcedure addToResult = new TIntProcedure() {
+ @Override
+ public boolean execute(int r) {
+ synchronized (result) {
+ result.add(r);
+ }
+ return true;
+ }
+ };
+
+ final AtomicInteger finishes = new AtomicInteger(0);
+
+ SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() {
+
+ @Override
+ public void run(ReadGraphImpl graph) throws DatabaseException {
+
+ if (finishes.addAndGet(1) == 3) {
+ procedure.execute(graph, result);
+ }
+
+ }
+
+ @Override
+ public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+
+ result.add(i);
+
+ inc();
+
+ QueryCache.runnerSuperTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
+
+ @Override
+ public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
+ types.forEach(addToResult);
+ dec(graph);
+ }
+
+ @Override
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+ procedure.exception(graph, t);
+ dec(graph);
+ }
+
+ });
+
+ }
+
+ @Override
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
+ dec(graph);
+ }
+
+ };
+
+ SyncIntProcedure inheritsProcedure = new SyncIntProcedure() {
+
+ @Override
+ public void run(ReadGraphImpl graph) throws DatabaseException {
+
+ int current = finishes.addAndGet(1);
+ if (current == 3) {
+ procedure.execute(graph, result);
+ }
+
+ }
+
+ @Override
+ public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+
+ inc();
+
+ QueryCache.runnerTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
+
+ @Override
+ public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
+ types.forEach(addToResult);
+ dec(graph);
+ }
+
+ @Override
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+ procedure.exception(graph, t);
+ dec(graph);
+ }
+
+ });
+
+ }
+
+ @Override
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
+
+ dec(graph);
+
+ }
+
+ };
+
+ SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() {
+
+ @Override
+ public void run(ReadGraphImpl graph) throws DatabaseException {
+
+ int current = finishes.addAndGet(1);
+ if (current == 3) {
+ procedure.execute(graph, result);
+ }
+
+ }
+
+ @Override
+ public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+
+ inc();
+
+ QueryCache.runnerTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
+
+ @Override
+ public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
+
+ types.forEach(addToResult);
+ dec(graph);
+
+ }
+
+ @Override
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+ procedure.exception(graph, t);
+ dec(graph);
+ }
+
+ });
+
+ }
+
+ @Override
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
+
+ dec(graph);
+
+ }
+
+ };
+
+ processor.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);
+ instanceOfProcedure.finished(graph);
+ processor.querySupport.getObjects(graph, id, inherits, inheritsProcedure);
+ inheritsProcedure.finished(graph);
+ processor.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);
+ subrelationOfProcedure.finished(graph);
+
+ }
+
+ @Override
+ public String toString() {
+ return "Types[" + id + "]";
+ }
+
+}