1 package org.simantics.graph.query;
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;
9 import java.util.Collection;
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;
26 public class TransferableGraphConversion {
27 private static StatementStore extractStatements(TransferableGraph1 tg) {
28 StatementStore statements = new StatementStore();
29 int[] array = tg.statements;
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);
38 statements.add(object, inverse, subject);
43 public static IdentityStore extractIdentities(TransferableGraph1 tg) {
44 return extractIdentities(tg.resourceCount, tg.identities);
47 public static IdentityStore extractIdentities(OldTransferableGraph1 tg) {
48 return extractIdentities(tg.resourceCount, tg.identities);
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);
59 else if(identity.definition instanceof External) {
60 External def = (External)identity.definition;
61 identities.defineChild(def.parent, def.name, identity.resource);
63 else if(identity.definition instanceof Optional) {
64 Optional def = (Optional)identity.definition;
65 identities.defineChild(def.parent, def.name, identity.resource);
67 else if(identity.definition instanceof Internal) {
68 Internal def = (Internal)identity.definition;
69 identities.defineChild(def.parent, def.name, identity.resource);
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);
82 public static GraphStore convert(TransferableGraph1 tg) {
83 return new GraphStore(
84 extractStatements(tg),
85 extractIdentities(tg),
87 new THashMap<Class<?>, IStore>()
91 public static TransferableGraph1 convert(final IGraph cg, final GraphStore store) {
94 final TIntIntHashMap inverseMap = new TIntIntHashMap();
95 final TIntHashSet withoutInverse = new TIntHashSet();
96 final Paths paths = cg.getPaths();
98 store.statements.getPredicates().forEach(new TIntProcedure() {
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);
107 withoutInverse.add(id);
112 if(!withoutInverse.isEmpty()) {
113 IdentityStore identities = store.identities;
114 StatementStore statements = store.statements;
115 int inverseOfId = identities.pathToId(paths.InverseOf);
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);
128 TransferableGraph1 tg = new TransferableGraph1(
129 store.identities.getResourceCount(),
130 store.identities.toArray(),
131 store.statements.toArray(inverseMap),
132 store.values.toArray()
138 public static TransferableGraph1 convert(final GraphStore store) {
141 TransferableGraph1 tg = new TransferableGraph1(
142 store.identities.getResourceCount(),
143 store.identities.toArray(),
144 store.statements.toArray(),
145 store.values.toArray()
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));