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