]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph.db/src/org/simantics/graph/db/VirtualGraphExport.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graph.db / src / org / simantics / graph / db / VirtualGraphExport.java
1 package org.simantics.graph.db;\r
2 \r
3 import gnu.trove.list.array.TIntArrayList;\r
4 import gnu.trove.map.hash.THashMap;\r
5 import gnu.trove.map.hash.TObjectIntHashMap;\r
6 import gnu.trove.set.hash.THashSet;\r
7 \r
8 import java.util.ArrayList;\r
9 \r
10 import org.simantics.databoard.Bindings;\r
11 import org.simantics.databoard.binding.Binding;\r
12 import org.simantics.databoard.binding.mutable.Variant;\r
13 import org.simantics.db.ReadGraph;\r
14 import org.simantics.db.Resource;\r
15 import org.simantics.db.Statement;\r
16 import org.simantics.db.VirtualGraph;\r
17 import org.simantics.db.exception.DatabaseException;\r
18 import org.simantics.db.service.VirtualGraphSupport;\r
19 import org.simantics.graph.representation.External;\r
20 import org.simantics.graph.representation.Identity;\r
21 import org.simantics.graph.representation.IdentityDefinition;\r
22 import org.simantics.graph.representation.Internal;\r
23 import org.simantics.graph.representation.TransferableGraph1;\r
24 import org.simantics.graph.representation.Value;\r
25 import org.simantics.layer0.Layer0;\r
26 \r
27 public class VirtualGraphExport {\r
28 \r
29     ReadGraph g;\r
30     VirtualGraph vg;\r
31     VirtualGraphSupport vgs;\r
32     Layer0 L0;\r
33     \r
34     THashSet<Resource> nameResources = new THashSet<Resource>(); \r
35     THashMap<Resource, String> names = new THashMap<Resource, String>();\r
36     \r
37     TObjectIntHashMap<Resource> resourceIds = new TObjectIntHashMap<Resource>();\r
38     ArrayList<Identity> identities = new ArrayList<Identity>();\r
39     ArrayList<Value> values = new ArrayList<Value>();\r
40     TIntArrayList statements = new TIntArrayList();\r
41     \r
42     private VirtualGraphExport(ReadGraph g, VirtualGraph vg) {\r
43         this.g = g;\r
44         this.vg = vg;\r
45         this.vgs = g.getService(VirtualGraphSupport.class);\r
46         this.L0 = Layer0.getInstance(g);\r
47     }\r
48     \r
49     /**\r
50      * Finds resource names and name resources.\r
51      */\r
52     private void processNames() throws DatabaseException {        \r
53         for(Statement stat : vgs.listStatements(vg)) {\r
54             Resource p = stat.getPredicate();\r
55             if(p.equals(L0.HasName)) {\r
56                 Resource s = stat.getSubject();\r
57                 Resource o = stat.getObject();\r
58                 names.put(s, (String)g.getValue(o));\r
59                 nameResources.add(o);\r
60             }\r
61         } \r
62     }\r
63     \r
64     /**\r
65      * If the resource is encountered first time, adds it\r
66      * to resourceIds map and creates an identity for it.\r
67      */\r
68     private void prepareResource(Resource resource) throws DatabaseException {\r
69         if(!resourceIds.containsKey(resource)) {    \r
70             int newId = resourceIds.size();\r
71             resourceIds.put(resource, newId);            \r
72             \r
73             String name = names.get(resource);\r
74             if(name != null) {\r
75                 Resource parent = g.getPossibleObject(resource, L0.PartOf);                \r
76                 if(parent != null) {\r
77                     prepareResource(parent);\r
78                     int parentId = resourceIds.get(parent);\r
79                     IdentityDefinition def = \r
80                         resource.isPersistent() \r
81                         ? new External(parentId, name)\r
82                         : new Internal(parentId, name);                    \r
83                     identities.add(new Identity(newId, def));\r
84                 }\r
85             }\r
86         }\r
87     }\r
88     \r
89     /**\r
90      * Process all statements of the virtual graph and\r
91      * adds them to integer table that will be part of the\r
92      * transferable graph,\r
93      */\r
94     private void processStatements() throws DatabaseException {\r
95         for(Statement stat : vgs.listStatements(vg)) {\r
96             /*\r
97              * Skips the statement if its subject or object is\r
98              * a name literal or if its predicate is ConsistsOf,\r
99              * HasName or inverse of these relations. \r
100              */\r
101             Resource s = stat.getSubject();\r
102             if(nameResources.contains(s))\r
103                 continue;\r
104             Resource p = stat.getPredicate();\r
105             if(p.equals(L0.PartOf) || p.equals(L0.ConsistsOf) \r
106                     || p.equals(L0.HasName) || p.equals(L0.NameOf))\r
107                 continue;\r
108             Resource o = stat.getObject();\r
109             if(nameResources.contains(o))\r
110                 continue;\r
111 \r
112             /*\r
113              * Adds resources to resourceIds map and generates identities.\r
114              */\r
115             prepareResource(s);\r
116             prepareResource(p);\r
117             prepareResource(o);\r
118             \r
119             /*\r
120              * Adds a statement\r
121              */\r
122             statements.add(resourceIds.get(s));            \r
123             statements.add(resourceIds.get(p));\r
124             statements.add(-1);\r
125             statements.add(resourceIds.get(o));       \r
126         }\r
127     }\r
128     \r
129     /**\r
130      * Process all values of the virtual graph.\r
131      */\r
132     private void processValues() throws DatabaseException {        \r
133         for(Resource resourceWithValue : vgs.listValues(vg)) {\r
134             if(nameResources.contains(resourceWithValue))\r
135                 continue;\r
136             Binding binding =\r
137                     Bindings.getBeanBinding(g.getDataType(resourceWithValue));\r
138             Object value = g.getValue(resourceWithValue, binding);\r
139             values.add(new Value(resourceIds.get(resourceWithValue),\r
140                     new Variant(binding, value)\r
141                     ));\r
142         }\r
143     }\r
144     \r
145     /**\r
146      * Creates a virtual graph.\r
147      */\r
148     private TransferableGraph1 getTransferableGraph() {\r
149         return new TransferableGraph1(resourceIds.size(), \r
150                 identities.toArray(new Identity[identities.size()]),\r
151                 statements.toArray(), \r
152                 values.toArray(new Value[values.size()]));\r
153     }\r
154     \r
155     /**\r
156      * Converts the contents of a virtual graph to a transferable graph.\r
157      */\r
158     public static TransferableGraph1 export(ReadGraph g, VirtualGraph vg) throws DatabaseException {\r
159         VirtualGraphExport export = new VirtualGraphExport(g, vg);\r
160         export.processNames();\r
161         export.processStatements();\r
162         export.processValues();\r
163         return export.getTransferableGraph();\r
164         \r
165     }\r
166     \r
167 }\r