]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph/src/org/simantics/graph/query/TransferableGraphConversion.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graph / src / org / simantics / graph / query / TransferableGraphConversion.java
1 package org.simantics.graph.query;\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.TIntIntHashMap;\r
6 import gnu.trove.procedure.TIntProcedure;\r
7 import gnu.trove.set.hash.TIntHashSet;\r
8 \r
9 import java.util.Collection;\r
10 \r
11 import org.simantics.graph.representation.External;\r
12 import org.simantics.graph.representation.Identity;\r
13 import org.simantics.graph.representation.Internal;\r
14 import org.simantics.graph.representation.Optional;\r
15 import org.simantics.graph.representation.Root;\r
16 import org.simantics.graph.representation.TransferableGraph1;\r
17 import org.simantics.graph.representation.Value;\r
18 import org.simantics.graph.representation.old.OldTransferableGraph1;\r
19 import org.simantics.graph.store.GraphStore;\r
20 import org.simantics.graph.store.IStore;\r
21 import org.simantics.graph.store.IdentityStore;\r
22 import org.simantics.graph.store.StatementStore;\r
23 import org.simantics.graph.store.ValueStore;\r
24 \r
25 \r
26 public class TransferableGraphConversion {\r
27         private static StatementStore extractStatements(TransferableGraph1 tg) {\r
28                 StatementStore statements = new StatementStore();\r
29                 int[] array = tg.statements;\r
30                 int i=0;\r
31                 while(i<array.length) {\r
32                         int subject = array[i++];\r
33                         int predicate = array[i++];\r
34                         int inverse = array[i++];\r
35                         int object = array[i++];\r
36                         statements.add(subject, predicate, object);\r
37                         if(inverse >= 0)\r
38                                 statements.add(object, inverse, subject);\r
39                 }\r
40                 return statements;\r
41         }\r
42         \r
43         public static IdentityStore extractIdentities(TransferableGraph1 tg) {\r
44             return extractIdentities(tg.resourceCount, tg.identities);\r
45         }\r
46         \r
47         public static IdentityStore extractIdentities(OldTransferableGraph1 tg) {\r
48         return extractIdentities(tg.resourceCount, tg.identities);\r
49     }\r
50         \r
51         private static IdentityStore extractIdentities(int resourceCount, Identity[] ids) {\r
52             IdentityStore identities = new IdentityStore();\r
53         identities.setResourceCount(resourceCount);\r
54         for(Identity identity : ids) {\r
55             if(identity.definition instanceof Root) {\r
56                 Root def = (Root)identity.definition;\r
57                 identities.defineRoot(def.name, identity.resource);\r
58             }\r
59             else if(identity.definition instanceof External) {\r
60                 External def = (External)identity.definition;\r
61                 identities.defineChild(def.parent, def.name, identity.resource);\r
62             }\r
63             else if(identity.definition instanceof Optional) {\r
64                 Optional def = (Optional)identity.definition;\r
65                 identities.defineChild(def.parent, def.name, identity.resource);\r
66             }\r
67             else if(identity.definition instanceof Internal) {\r
68                 Internal def = (Internal)identity.definition;\r
69                 identities.defineChild(def.parent, def.name, identity.resource);\r
70             }\r
71         }\r
72         return identities;          \r
73         }\r
74         \r
75         private static ValueStore extractValues(TransferableGraph1 tg) {\r
76                 ValueStore values = new ValueStore();\r
77                 for(Value value : tg.values)\r
78                         values.setValue(value.resource, value.value);\r
79                 return values;\r
80         }\r
81         \r
82         public static GraphStore convert(TransferableGraph1 tg) {\r
83                 return new GraphStore(\r
84                                 extractStatements(tg),\r
85                                 extractIdentities(tg),\r
86                                 extractValues(tg),\r
87                                 new THashMap<Class<?>, IStore>()\r
88                                 );\r
89         }\r
90 \r
91         public static TransferableGraph1 convert(final IGraph cg, final GraphStore store) {\r
92         \r
93                 // Create inverse map\r
94                 final TIntIntHashMap inverseMap = new TIntIntHashMap();\r
95                 final TIntHashSet withoutInverse = new TIntHashSet();\r
96                 final Paths paths = cg.getPaths();\r
97 \r
98                 store.statements.getPredicates().forEach(new TIntProcedure() {\r
99                         @Override\r
100                         public boolean execute(int id) {\r
101                                 for(Res inverse : cg.rawGetObjects(store.idToRes(id), paths.InverseOf)) {                                       \r
102                                         int inv = store.createResToId(inverse);\r
103                                         inverseMap.put(id, inv);\r
104                                         inverseMap.put(inv, id);\r
105                                         return true;\r
106                                 }\r
107                                 withoutInverse.add(id);\r
108                                 return true;\r
109                         }\r
110                 });             \r
111                 \r
112                 if(!withoutInverse.isEmpty()) {\r
113                         IdentityStore identities = store.identities;\r
114                         StatementStore statements = store.statements;\r
115                         int inverseOfId = identities.pathToId(paths.InverseOf);\r
116                         if(inverseOfId >= 0)\r
117                                 for(int id=0;id<store.identities.getResourceCount();++id) {\r
118                                         TIntArrayList invs = statements.getObjects(id, inverseOfId);\r
119                                         if(!invs.isEmpty()) {\r
120                                                 int inv = invs.getQuick(0);\r
121                                                 inverseMap.put(id, inv);\r
122                                                 inverseMap.put(inv, id);\r
123                                         }\r
124                                 }\r
125                 }\r
126                 \r
127                 // Create tg\r
128                 TransferableGraph1 tg = new TransferableGraph1(\r
129                                 store.identities.getResourceCount(),\r
130                                 store.identities.toArray(),\r
131                                 store.statements.toArray(inverseMap),\r
132                                 store.values.toArray()\r
133                                 );\r
134                 \r
135                 return tg;\r
136         }\r
137         \r
138         public static TransferableGraph1 convert(final GraphStore store) {              \r
139                 \r
140                 // Create tg\r
141                 TransferableGraph1 tg = new TransferableGraph1(\r
142                                 store.identities.getResourceCount(),\r
143                                 store.identities.toArray(),\r
144                                 store.statements.toArray(),\r
145                                 store.values.toArray()\r
146                                 );\r
147                 \r
148                 return tg;\r
149         }\r
150         \r
151         public static CompositeGraph convert(Paths paths, Collection<TransferableGraph1> tgs) {\r
152                 CompositeGraph graph = new CompositeGraph(paths);\r
153                 for(TransferableGraph1 tg : tgs)\r
154                         graph.addFragment(convert(tg));\r
155                 return graph;\r
156         }\r
157         \r
158 }\r