]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / SuperTypes.java
diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java
new file mode 100644 (file)
index 0000000..e55b1e1
--- /dev/null
@@ -0,0 +1,241 @@
+/*******************************************************************************\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 SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {\r
+    \r
+//     public ArrayList<InternalProcedure<IntSet>> procs = null;\r
+       \r
+    private SuperTypes(final int resource) {\r
+        super(resource);\r
+    }\r
+    \r
+    final static SuperTypes runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {\r
+\r
+       SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r);\r
+        if(entry == null) {\r
+               \r
+               entry = new SuperTypes(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;\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 entry;\r
+                    }\r
+               }\r
+            }\r
+            provider.performForEach(graph, entry, parent, listener, procedure);\r
+        }\r
+        \r
+        return entry;\r
+\r
+    }\r
+    \r
+    final public static SuperTypes 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
+               SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r);\r
+               if(entry != null && entry.isReady()) { \r
+                       entry.performFromCache(graph, provider, procedure);\r
+                       return entry;\r
+               }\r
+        }\r
+        \r
+        return runner(graph, r, parent, provider, listener, procedure);\r
+         \r
+    }\r
+       \r
+       @Override\r
+       public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {\r
+        return provider.superTypesMap.get(id);\r
+       }\r
+       \r
+       @Override\r
+       public void putEntry(QueryProcessor provider) {\r
+        provider.superTypesMap.put(id, this);\r
+       }\r
+\r
+       @Override\r
+       final public void removeEntry(QueryProcessor provider) {\r
+               provider.superTypesMap.remove(id);\r
+       }\r
+\r
+       @Override\r
+       public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, boolean store) {\r
+\r
+        final int inherits = provider.getInherits();\r
+        \r
+        final CacheEntry parent = graph.parent;\r
+        \r
+        assert(graph.parent != this);\r
+\r
+        final IntSet result = new IntSet(provider.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
+        DirectObjects.queryEach(graph, id, inherits, provider, this, null, new SyncIntProcedure() {\r
+            \r
+            @Override\r
+            public void run(ReadGraphImpl graph) {\r
+\r
+                addOrSet(graph, result, provider);\r
+                procedure.execute(graph, result);\r
+                \r
+            }\r
+            \r
+            @Override\r
+            public void execute(ReadGraphImpl graph, final int i) {\r
+                \r
+               assert(graph.parent == parent);\r
+               \r
+               synchronized(result) {\r
+                       result.add(i);\r
+               }\r
+                \r
+                inc();\r
+\r
+                SuperTypes.queryEach(graph, i, provider, SuperTypes.this, 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
+                   }\r
+\r
+                });\r
+                \r
+            }\r
+\r
+            @Override\r
+            public void finished(ReadGraphImpl graph) {\r
+                dec(graph);\r
+            }\r
+            \r
+        });\r
+        \r
+        return result;\r
+        \r
+    }\r
+    \r
+    @Override\r
+    public String toString() {\r
+       return "SuperTypes2[" + 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
+}\r