1 package org.simantics.graph.query;
\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
9 import java.util.Collection;
\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
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
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
38 statements.add(object, inverse, subject);
\r
43 public static IdentityStore extractIdentities(TransferableGraph1 tg) {
\r
44 return extractIdentities(tg.resourceCount, tg.identities);
\r
47 public static IdentityStore extractIdentities(OldTransferableGraph1 tg) {
\r
48 return extractIdentities(tg.resourceCount, tg.identities);
\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
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
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
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
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
82 public static GraphStore convert(TransferableGraph1 tg) {
\r
83 return new GraphStore(
\r
84 extractStatements(tg),
\r
85 extractIdentities(tg),
\r
87 new THashMap<Class<?>, IStore>()
\r
91 public static TransferableGraph1 convert(final IGraph cg, final GraphStore store) {
\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
98 store.statements.getPredicates().forEach(new TIntProcedure() {
\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
107 withoutInverse.add(id);
\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
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
138 public static TransferableGraph1 convert(final GraphStore store) {
\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
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