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