]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java
Merge branch 'feature/funcwrite'
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / URIToResource.java
index 251939c066e173922e1e5f0da71c5dfdc2f2daef..c466cb261cca975d10c16a70461eab15bb3f77b2 100644 (file)
-/*******************************************************************************\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.map.hash.TObjectIntHashMap;\r
-\r
-import java.util.concurrent.Semaphore;\r
-\r
-import org.simantics.databoard.util.URIStringUtils;\r
-import org.simantics.db.common.exception.DebugException;\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
-public class URIToResource extends StringQuery<InternalProcedure<Integer>> {\r
-\r
-//    public ArrayList<InternalProcedure<Integer>> procs = null;\r
-    \r
-    private URIToResource(final String id) {\r
-        super(id);\r
-    }\r
-    \r
-    final static URIToResource entry(final QueryProcessor provider, final String id) {\r
-        return (URIToResource)provider.uriToResourceMap.get(id);\r
-    }\r
-\r
-    final static void runner(ReadGraphImpl graph, final String id, CacheEntry parent, final ListenerBase listener, final InternalProcedure<Integer> procedure) {\r
-\r
-       QueryProcessor processor = graph.processor;\r
-       \r
-        URIToResource entry = (URIToResource)processor.uriToResourceMap.get(id); \r
-        if(entry == null) {\r
-\r
-               entry = new URIToResource(id);\r
-               entry.setPending();\r
-               entry.clearResult(processor.querySupport);\r
-               entry.putEntry(processor);\r
-\r
-               processor.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
-//                     if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<Integer>>();\r
-//                        entry.procs.add(procedure);\r
-//                        processor.registerDependencies(graph, entry, parent, listener, procedure, false);\r
-//                        return;\r
-                    }\r
-                }\r
-            }\r
-            \r
-            processor.performForEach(graph, entry, parent, listener, procedure);\r
-            \r
-        }\r
-        \r
-    }\r
-    \r
-    final public static void queryEach(ReadGraphImpl graph, final String id, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<Integer> procedure) {\r
-        \r
-        assert(id != null);\r
-        \r
-        if(graph.parent == null && listener == null) {\r
-               URIToResource.computeForEach(graph, id, null, procedure);\r
-        } else {\r
-            runner(graph, id, parent, listener, procedure);\r
-        }\r
-         \r
-    }\r
-     \r
-    @Override\r
-    public URIToResource getEntry(QueryProcessor provider) {\r
-        return provider.uriToResourceMap.get(id);\r
-    }\r
-    \r
-    @Override\r
-    public void putEntry(QueryProcessor provider) {\r
-        provider.uriToResourceMap.put(id, this);\r
-    }\r
-\r
-    @Override\r
-    final public void removeEntry(QueryProcessor provider) {\r
-        provider.uriToResourceMap.remove(id);\r
-    }\r
-\r
-    private void lookup(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<Integer> procedure, final String namespace, final String name) {\r
-        \r
-        NamespaceIndex.queryEach(graph, namespace, processor, this, null, new InternalProcedure<TObjectIntHashMap<String>>() {\r
-\r
-            @Override\r
-            public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {\r
-\r
-                if(index != null) {\r
-                    int result = index.get(name);\r
-                    if(result != 0) {\r
-                        addOrSet(graph, processor, result);\r
-                        procedure.execute(graph, result);\r
-                        return;\r
-                    }\r
-                }\r
-                \r
-                addOrSet(graph, processor, new Integer(0));\r
-                procedure.execute(graph, new Integer(0));\r
-\r
-            }\r
-\r
-            @Override\r
-            public void exception(ReadGraphImpl graph, Throwable t) {\r
-                except(t);\r
-                procedure.exception(graph, t);\r
-            }\r
-\r
-        });\r
-\r
-    }\r
-\r
-    private static void lookup(ReadGraphImpl graph, final URIToResource entry, final InternalProcedure<Integer> procedure, final String namespace, final String name) {\r
-        \r
-        NamespaceIndex.queryEach(graph, namespace, graph.processor, entry, null, new InternalProcedure<TObjectIntHashMap<String>>() {\r
-\r
-            @Override\r
-            public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {\r
-\r
-                if(index != null) {\r
-                    int result = index.get(name);\r
-                    if(result != 0) {\r
-                        if(entry != null) entry.addOrSet(graph, graph.processor, result);\r
-                        procedure.execute(graph, result);\r
-                        return;\r
-                    }\r
-                }\r
-                \r
-                if(entry != null) entry.addOrSet(graph, graph.processor, new Integer(0));\r
-                procedure.execute(graph, new Integer(0));\r
-\r
-            }\r
-\r
-            @Override\r
-            public void exception(ReadGraphImpl graph, Throwable t) {\r
-                if(entry != null) entry.except(t);\r
-                procedure.exception(graph, t);\r
-            }\r
-\r
-        });\r
-\r
-    }\r
-    \r
-    @Override\r
-    public void computeForEach(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<Integer> procedure) {\r
-        \r
-//     new Exception("URIToResource " + id).printStackTrace();\r
-       \r
-        if("http://".equals(id) || "http:/".equals(id)) {\r
-            \r
-            addOrSet(graph, processor, processor.getRootLibrary());\r
-            procedure.execute(graph, processor.getRootLibrary());\r
-\r
-        } else {\r
-            \r
-            final String[] parts = URIStringUtils.splitURI(id);\r
-            if (parts != null) {\r
-                lookup(graph, processor, procedure, parts[0], parts[1]);\r
-            } else {\r
-                lookup(graph, processor, procedure, "http://", id.replaceFirst("http://", ""));\r
-            }\r
-\r
-        }\r
-        \r
-    }\r
-\r
-    final private static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure) {\r
-        \r
-        if("http://".equals(id) || "http:/".equals(id)) {\r
-            \r
-               QueryProcessor processor = graph.processor;\r
-            if(entry != null) entry.addOrSet(graph, processor, processor.getRootLibrary());\r
-            procedure.execute(graph, processor.getRootLibrary());\r
-\r
-        } else {\r
-            \r
-            final String[] parts = URIStringUtils.splitURI(id);\r
-            if (parts != null) {\r
-                lookup(graph, entry, procedure, parts[0], parts[1]);\r
-            } else {\r
-                lookup(graph, entry, procedure, "http://", id.replaceFirst("http://", ""));\r
-            }\r
-\r
-        }\r
-        \r
-    }\r
-    \r
-    public void addOrSet(ReadGraphImpl graph, QueryProcessor provider, Integer result) {\r
-\r
-        assert(isPending());\r
-\r
-//        ArrayList<InternalProcedure<Integer>> p = null;\r
-\r
-        synchronized(this) {\r
-\r
-            setResult(result);\r
-            setReady();\r
-//            p = procs;\r
-//            procs = null;\r
-            \r
-        }\r
-\r
-//        if(p != null)\r
-//             for(InternalProcedure<Integer> proc : p) proc.execute(graph, result);\r
-        \r
-    }\r
-    \r
-    @Override\r
-    public String toString() {\r
-        return "URIToResource[" + id + "]";\r
-    }\r
-\r
-    @Override\r
-    public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<Integer> procedure) {\r
-        \r
-        assert(isReady());\r
-        \r
-       if(handleException(graph, procedure)) return;\r
-        \r
-        if(isExcepted()) {\r
-               procedure.exception(graph, (Throwable)statusOrException);\r
-        } else {\r
-            procedure.execute(graph, (Integer)getResult());\r
-        }\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<Integer>() {\r
-\r
-            @Override\r
-            public void execute(ReadGraphImpl graph, Integer result) {\r
-                s.release();\r
-            }\r
-            \r
-            @Override\r
-            public void exception(ReadGraphImpl graph, Throwable t) {\r
-                if(DebugException.DEBUG) new DebugException(t).printStackTrace();\r
-                throw new Error("Error in recompute.", t);\r
-            }\r
-\r
-        });\r
-        \r
-       while(!s.tryAcquire()) {\r
-               provider.resume(graph);\r
-       }\r
-        \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 gnu.trove.map.hash.TObjectIntHashMap;
+
+import java.util.concurrent.Semaphore;
+
+import org.simantics.databoard.util.URIStringUtils;
+import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.impl.graph.ReadGraphImpl;
+import org.simantics.db.impl.procedure.InternalProcedure;
+import org.simantics.db.procedure.ListenerBase;
+
+public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
+
+//    public ArrayList<InternalProcedure<Integer>> procs = null;
+    
+    private URIToResource(final String id) {
+        super(id);
+    }
+    
+    final static URIToResource entry(final QueryProcessor provider, final String id) {
+        return (URIToResource)provider.uriToResourceMap.get(id);
+    }
+
+    final static void runner(ReadGraphImpl graph, final String id, CacheEntry parent, final ListenerBase listener, final InternalProcedure<Integer> procedure) {
+
+       QueryProcessor processor = graph.processor;
+       
+        URIToResource entry = (URIToResource)processor.uriToResourceMap.get(id); 
+        if(entry == null) {
+
+               entry = new URIToResource(id);
+               entry.setPending();
+               entry.clearResult(processor.querySupport);
+               entry.putEntry(processor);
+
+               processor.performForEach(graph, entry, parent, listener, procedure);
+            
+        } else {
+               
+            if(entry.isPending()) {
+                synchronized(entry) {
+                    if(entry.isPending()) {
+                        throw new IllegalStateException();
+//                     if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<Integer>>();
+//                        entry.procs.add(procedure);
+//                        processor.registerDependencies(graph, entry, parent, listener, procedure, false);
+//                        return;
+                    }
+                }
+            }
+            
+            processor.performForEach(graph, entry, parent, listener, procedure);
+            
+        }
+        
+    }
+    
+    final public static void queryEach(ReadGraphImpl graph, final String id, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<Integer> procedure) {
+        
+        assert(id != null);
+        
+        if(graph.parent == null && listener == null) {
+               URIToResource.computeForEach(graph, id, null, procedure);
+        } else {
+            runner(graph, id, parent, listener, procedure);
+        }
+         
+    }
+     
+    @Override
+    public URIToResource getEntry(QueryProcessor provider) {
+        return provider.uriToResourceMap.get(id);
+    }
+    
+    @Override
+    public void putEntry(QueryProcessor provider) {
+        provider.uriToResourceMap.put(id, this);
+    }
+
+    @Override
+    final public void removeEntry(QueryProcessor provider) {
+        provider.uriToResourceMap.remove(id);
+    }
+
+    private void lookup(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<Integer> procedure, final String namespace, final String name) {
+        
+        NamespaceIndex.queryEach(graph, namespace, processor, this, null, new InternalProcedure<TObjectIntHashMap<String>>() {
+
+            @Override
+            public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {
+
+                if(index != null) {
+                    int result = index.get(name);
+                    if(result != 0) {
+                        addOrSet(graph, processor, result);
+                        procedure.execute(graph, result);
+                        return;
+                    }
+                }
+                
+                addOrSet(graph, processor, new Integer(0));
+                procedure.execute(graph, new Integer(0));
+
+            }
+
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable t) {
+                except(t);
+                procedure.exception(graph, t);
+            }
+
+        });
+
+    }
+
+    private static void lookup(ReadGraphImpl graph, final URIToResource entry, final InternalProcedure<Integer> procedure, final String namespace, final String name) {
+        
+        NamespaceIndex.queryEach(graph, namespace, graph.processor, entry, null, new InternalProcedure<TObjectIntHashMap<String>>() {
+
+            @Override
+            public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {
+
+                if(index != null) {
+                    int result = index.get(name);
+                    if(result != 0) {
+                        if(entry != null) entry.addOrSet(graph, graph.processor, result);
+                        procedure.execute(graph, result);
+                        return;
+                    }
+                }
+                
+                Integer zero = 0;
+                if(entry != null) entry.addOrSet(graph, graph.processor, zero);
+                procedure.execute(graph, zero);
+
+            }
+
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable t) {
+                if(entry != null) entry.except(t);
+                procedure.exception(graph, t);
+            }
+
+        });
+
+    }
+    
+    @Override
+    public void computeForEach(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<Integer> procedure) {
+        
+//     new Exception("URIToResource " + id).printStackTrace();
+       
+        if("http://".equals(id) || "http:/".equals(id)) {
+            
+            addOrSet(graph, processor, processor.getRootLibrary());
+            procedure.execute(graph, processor.getRootLibrary());
+
+        } else {
+            
+            final String[] parts = URIStringUtils.splitURI(id);
+            if (parts != null) {
+                lookup(graph, processor, procedure, parts[0], parts[1]);
+            } else {
+                lookup(graph, processor, procedure, "http://", id.replaceFirst("http://", ""));
+            }
+
+        }
+        
+    }
+
+    final private static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure) {
+        
+        if("http://".equals(id) || "http:/".equals(id)) {
+            
+               QueryProcessor processor = graph.processor;
+            if(entry != null) entry.addOrSet(graph, processor, processor.getRootLibrary());
+            procedure.execute(graph, processor.getRootLibrary());
+
+        } else {
+            
+            final String[] parts = URIStringUtils.splitURI(id);
+            if (parts != null) {
+                lookup(graph, entry, procedure, parts[0], parts[1]);
+            } else {
+                lookup(graph, entry, procedure, "http://", id.replaceFirst("http://", ""));
+            }
+
+        }
+        
+    }
+    
+    public void addOrSet(ReadGraphImpl graph, QueryProcessor provider, Integer result) {
+
+        assert(isPending());
+
+//        ArrayList<InternalProcedure<Integer>> p = null;
+
+        synchronized(this) {
+
+            setResult(result);
+            setReady();
+//            p = procs;
+//            procs = null;
+            
+        }
+
+//        if(p != null)
+//             for(InternalProcedure<Integer> proc : p) proc.execute(graph, result);
+        
+    }
+    
+    @Override
+    public String toString() {
+        return "URIToResource[" + id + "]";
+    }
+
+    @Override
+    public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<Integer> procedure) {
+        
+        assert(isReady());
+        
+       if(handleException(graph, procedure)) return;
+        
+        if(isExcepted()) {
+               procedure.exception(graph, (Throwable)statusOrException);
+        } else {
+            procedure.execute(graph, (Integer)getResult());
+        }
+        
+    }
+    
+    @Override
+    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+        
+        final Semaphore s = new Semaphore(0);
+        
+        computeForEach(graph, provider, new InternalProcedure<Integer>() {
+
+            @Override
+            public void execute(ReadGraphImpl graph, Integer result) {
+                s.release();
+            }
+            
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable t) {
+                if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+                throw new Error("Error in recompute.", t);
+            }
+
+        });
+        
+       while(!s.tryAcquire()) {
+               provider.resume(graph);
+       }
+        
+    }
+       
+}