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.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;
25 public class TransferableGraphConversion {
26 private static StatementStore extractStatements(TransferableGraph1 tg) {
27 StatementStore statements = new StatementStore();
28 int[] array = tg.statements;
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);
37 statements.add(object, inverse, subject);
42 public static IdentityStore extractIdentities(TransferableGraph1 tg) {
43 return extractIdentities(tg.resourceCount, tg.identities);
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);
54 else if(identity.definition instanceof External) {
55 External def = (External)identity.definition;
56 identities.defineChild(def.parent, def.name, identity.resource);
58 else if(identity.definition instanceof Optional) {
59 Optional def = (Optional)identity.definition;
60 identities.defineChild(def.parent, def.name, identity.resource);
62 else if(identity.definition instanceof Internal) {
63 Internal def = (Internal)identity.definition;
64 identities.defineChild(def.parent, def.name, identity.resource);
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);
77 public static GraphStore convert(TransferableGraph1 tg) {
78 return new GraphStore(
79 extractStatements(tg),
80 extractIdentities(tg),
82 new THashMap<Class<?>, IStore>()
86 public static TransferableGraph1 convert(final IGraph cg, final GraphStore store) {
89 final TIntIntHashMap inverseMap = new TIntIntHashMap();
90 final TIntHashSet withoutInverse = new TIntHashSet();
91 final Paths paths = cg.getPaths();
93 store.statements.getPredicates().forEach(new TIntProcedure() {
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);
102 withoutInverse.add(id);
107 if(!withoutInverse.isEmpty()) {
108 IdentityStore identities = store.identities;
109 StatementStore statements = store.statements;
110 int inverseOfId = identities.pathToId(paths.InverseOf);
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);
123 TransferableGraph1 tg = new TransferableGraph1(
124 store.identities.getResourceCount(),
125 store.identities.toArray(),
126 store.statements.toArray(inverseMap),
127 store.values.toArray()
133 public static TransferableGraph1 convert(final GraphStore store) {
136 TransferableGraph1 tg = new TransferableGraph1(
137 store.identities.getResourceCount(),
138 store.identities.toArray(),
139 store.statements.toArray(),
140 store.values.toArray()
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));