]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graph.db/src/org/simantics/graph/db/VirtualGraphExport.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.graph.db / src / org / simantics / graph / db / VirtualGraphExport.java
index 18f08ce09292ab3717353ad766920cec1e800f7d..d06899d980eb19dc786f07b623a7f12687e8c6e6 100644 (file)
-package org.simantics.graph.db;\r
-\r
-import gnu.trove.list.array.TIntArrayList;\r
-import gnu.trove.map.hash.THashMap;\r
-import gnu.trove.map.hash.TObjectIntHashMap;\r
-import gnu.trove.set.hash.THashSet;\r
-\r
-import java.util.ArrayList;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.binding.Binding;\r
-import org.simantics.databoard.binding.mutable.Variant;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Statement;\r
-import org.simantics.db.VirtualGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.service.VirtualGraphSupport;\r
-import org.simantics.graph.representation.External;\r
-import org.simantics.graph.representation.Identity;\r
-import org.simantics.graph.representation.IdentityDefinition;\r
-import org.simantics.graph.representation.Internal;\r
-import org.simantics.graph.representation.TransferableGraph1;\r
-import org.simantics.graph.representation.Value;\r
-import org.simantics.layer0.Layer0;\r
-\r
-public class VirtualGraphExport {\r
-\r
-    ReadGraph g;\r
-    VirtualGraph vg;\r
-    VirtualGraphSupport vgs;\r
-    Layer0 L0;\r
-    \r
-    THashSet<Resource> nameResources = new THashSet<Resource>(); \r
-    THashMap<Resource, String> names = new THashMap<Resource, String>();\r
-    \r
-    TObjectIntHashMap<Resource> resourceIds = new TObjectIntHashMap<Resource>();\r
-    ArrayList<Identity> identities = new ArrayList<Identity>();\r
-    ArrayList<Value> values = new ArrayList<Value>();\r
-    TIntArrayList statements = new TIntArrayList();\r
-    \r
-    private VirtualGraphExport(ReadGraph g, VirtualGraph vg) {\r
-        this.g = g;\r
-        this.vg = vg;\r
-        this.vgs = g.getService(VirtualGraphSupport.class);\r
-        this.L0 = Layer0.getInstance(g);\r
-    }\r
-    \r
-    /**\r
-     * Finds resource names and name resources.\r
-     */\r
-    private void processNames() throws DatabaseException {        \r
-        for(Statement stat : vgs.listStatements(vg)) {\r
-            Resource p = stat.getPredicate();\r
-            if(p.equals(L0.HasName)) {\r
-                Resource s = stat.getSubject();\r
-                Resource o = stat.getObject();\r
-                names.put(s, (String)g.getValue(o));\r
-                nameResources.add(o);\r
-            }\r
-        } \r
-    }\r
-    \r
-    /**\r
-     * If the resource is encountered first time, adds it\r
-     * to resourceIds map and creates an identity for it.\r
-     */\r
-    private void prepareResource(Resource resource) throws DatabaseException {\r
-        if(!resourceIds.containsKey(resource)) {    \r
-            int newId = resourceIds.size();\r
-            resourceIds.put(resource, newId);            \r
-            \r
-            String name = names.get(resource);\r
-            if(name != null) {\r
-                Resource parent = g.getPossibleObject(resource, L0.PartOf);                \r
-                if(parent != null) {\r
-                    prepareResource(parent);\r
-                    int parentId = resourceIds.get(parent);\r
-                    IdentityDefinition def = \r
-                        resource.isPersistent() \r
-                        ? new External(parentId, name)\r
-                        : new Internal(parentId, name);                    \r
-                    identities.add(new Identity(newId, def));\r
-                }\r
-            }\r
-        }\r
-    }\r
-    \r
-    /**\r
-     * Process all statements of the virtual graph and\r
-     * adds them to integer table that will be part of the\r
-     * transferable graph,\r
-     */\r
-    private void processStatements() throws DatabaseException {\r
-        for(Statement stat : vgs.listStatements(vg)) {\r
-            /*\r
-             * Skips the statement if its subject or object is\r
-             * a name literal or if its predicate is ConsistsOf,\r
-             * HasName or inverse of these relations. \r
-             */\r
-            Resource s = stat.getSubject();\r
-            if(nameResources.contains(s))\r
-                continue;\r
-            Resource p = stat.getPredicate();\r
-            if(p.equals(L0.PartOf) || p.equals(L0.ConsistsOf) \r
-                    || p.equals(L0.HasName) || p.equals(L0.NameOf))\r
-                continue;\r
-            Resource o = stat.getObject();\r
-            if(nameResources.contains(o))\r
-                continue;\r
-\r
-            /*\r
-             * Adds resources to resourceIds map and generates identities.\r
-             */\r
-            prepareResource(s);\r
-            prepareResource(p);\r
-            prepareResource(o);\r
-            \r
-            /*\r
-             * Adds a statement\r
-             */\r
-            statements.add(resourceIds.get(s));            \r
-            statements.add(resourceIds.get(p));\r
-            statements.add(-1);\r
-            statements.add(resourceIds.get(o));       \r
-        }\r
-    }\r
-    \r
-    /**\r
-     * Process all values of the virtual graph.\r
-     */\r
-    private void processValues() throws DatabaseException {        \r
-        for(Resource resourceWithValue : vgs.listValues(vg)) {\r
-            if(nameResources.contains(resourceWithValue))\r
-                continue;\r
-            Binding binding =\r
-                    Bindings.getBeanBinding(g.getDataType(resourceWithValue));\r
-            Object value = g.getValue(resourceWithValue, binding);\r
-            values.add(new Value(resourceIds.get(resourceWithValue),\r
-                    new Variant(binding, value)\r
-                    ));\r
-        }\r
-    }\r
-    \r
-    /**\r
-     * Creates a virtual graph.\r
-     */\r
-    private TransferableGraph1 getTransferableGraph() {\r
-        return new TransferableGraph1(resourceIds.size(), \r
-                identities.toArray(new Identity[identities.size()]),\r
-                statements.toArray(), \r
-                values.toArray(new Value[values.size()]));\r
-    }\r
-    \r
-    /**\r
-     * Converts the contents of a virtual graph to a transferable graph.\r
-     */\r
-    public static TransferableGraph1 export(ReadGraph g, VirtualGraph vg) throws DatabaseException {\r
-        VirtualGraphExport export = new VirtualGraphExport(g, vg);\r
-        export.processNames();\r
-        export.processStatements();\r
-        export.processValues();\r
-        return export.getTransferableGraph();\r
-        \r
-    }\r
-    \r
-}\r
+package org.simantics.graph.db;
+
+import gnu.trove.list.array.TIntArrayList;
+import gnu.trove.map.hash.THashMap;
+import gnu.trove.map.hash.TObjectIntHashMap;
+import gnu.trove.set.hash.THashSet;
+
+import java.util.ArrayList;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.mutable.Variant;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Statement;
+import org.simantics.db.VirtualGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.service.VirtualGraphSupport;
+import org.simantics.graph.representation.External;
+import org.simantics.graph.representation.Identity;
+import org.simantics.graph.representation.IdentityDefinition;
+import org.simantics.graph.representation.Internal;
+import org.simantics.graph.representation.TransferableGraph1;
+import org.simantics.graph.representation.Value;
+import org.simantics.layer0.Layer0;
+
+public class VirtualGraphExport {
+
+    ReadGraph g;
+    VirtualGraph vg;
+    VirtualGraphSupport vgs;
+    Layer0 L0;
+    
+    THashSet<Resource> nameResources = new THashSet<Resource>(); 
+    THashMap<Resource, String> names = new THashMap<Resource, String>();
+    
+    TObjectIntHashMap<Resource> resourceIds = new TObjectIntHashMap<Resource>();
+    ArrayList<Identity> identities = new ArrayList<Identity>();
+    ArrayList<Value> values = new ArrayList<Value>();
+    TIntArrayList statements = new TIntArrayList();
+    
+    private VirtualGraphExport(ReadGraph g, VirtualGraph vg) {
+        this.g = g;
+        this.vg = vg;
+        this.vgs = g.getService(VirtualGraphSupport.class);
+        this.L0 = Layer0.getInstance(g);
+    }
+    
+    /**
+     * Finds resource names and name resources.
+     */
+    private void processNames() throws DatabaseException {        
+        for(Statement stat : vgs.listStatements(vg)) {
+            Resource p = stat.getPredicate();
+            if(p.equals(L0.HasName)) {
+                Resource s = stat.getSubject();
+                Resource o = stat.getObject();
+                names.put(s, (String)g.getValue(o));
+                nameResources.add(o);
+            }
+        } 
+    }
+    
+    /**
+     * If the resource is encountered first time, adds it
+     * to resourceIds map and creates an identity for it.
+     */
+    private void prepareResource(Resource resource) throws DatabaseException {
+        if(!resourceIds.containsKey(resource)) {    
+            int newId = resourceIds.size();
+            resourceIds.put(resource, newId);            
+            
+            String name = names.get(resource);
+            if(name != null) {
+                Resource parent = g.getPossibleObject(resource, L0.PartOf);                
+                if(parent != null) {
+                    prepareResource(parent);
+                    int parentId = resourceIds.get(parent);
+                    IdentityDefinition def = 
+                        resource.isPersistent() 
+                        ? new External(parentId, name)
+                        : new Internal(parentId, name);                    
+                    identities.add(new Identity(newId, def));
+                }
+            }
+        }
+    }
+    
+    /**
+     * Process all statements of the virtual graph and
+     * adds them to integer table that will be part of the
+     * transferable graph,
+     */
+    private void processStatements() throws DatabaseException {
+        for(Statement stat : vgs.listStatements(vg)) {
+            /*
+             * Skips the statement if its subject or object is
+             * a name literal or if its predicate is ConsistsOf,
+             * HasName or inverse of these relations. 
+             */
+            Resource s = stat.getSubject();
+            if(nameResources.contains(s))
+                continue;
+            Resource p = stat.getPredicate();
+            if(p.equals(L0.PartOf) || p.equals(L0.ConsistsOf) 
+                    || p.equals(L0.HasName) || p.equals(L0.NameOf))
+                continue;
+            Resource o = stat.getObject();
+            if(nameResources.contains(o))
+                continue;
+
+            /*
+             * Adds resources to resourceIds map and generates identities.
+             */
+            prepareResource(s);
+            prepareResource(p);
+            prepareResource(o);
+            
+            /*
+             * Adds a statement
+             */
+            statements.add(resourceIds.get(s));            
+            statements.add(resourceIds.get(p));
+            statements.add(-1);
+            statements.add(resourceIds.get(o));       
+        }
+    }
+    
+    /**
+     * Process all values of the virtual graph.
+     */
+    private void processValues() throws DatabaseException {        
+        for(Resource resourceWithValue : vgs.listValues(vg)) {
+            if(nameResources.contains(resourceWithValue))
+                continue;
+            Binding binding =
+                    Bindings.getBeanBinding(g.getDataType(resourceWithValue));
+            Object value = g.getValue(resourceWithValue, binding);
+            values.add(new Value(resourceIds.get(resourceWithValue),
+                    new Variant(binding, value)
+                    ));
+        }
+    }
+    
+    /**
+     * Creates a virtual graph.
+     */
+    private TransferableGraph1 getTransferableGraph() {
+        return new TransferableGraph1(resourceIds.size(), 
+                identities.toArray(new Identity[identities.size()]),
+                statements.toArray(), 
+                values.toArray(new Value[values.size()]));
+    }
+    
+    /**
+     * Converts the contents of a virtual graph to a transferable graph.
+     */
+    public static TransferableGraph1 export(ReadGraph g, VirtualGraph vg) throws DatabaseException {
+        VirtualGraphExport export = new VirtualGraphExport(g, vg);
+        export.processNames();
+        export.processStatements();
+        export.processValues();
+        return export.getTransferableGraph();
+        
+    }
+    
+}