--- /dev/null
+package org.simantics.graph.query;\r
+\r
+import gnu.trove.list.array.TIntArrayList;\r
+import gnu.trove.map.hash.THashMap;\r
+import gnu.trove.map.hash.TIntIntHashMap;\r
+import gnu.trove.procedure.TIntProcedure;\r
+import gnu.trove.set.hash.TIntHashSet;\r
+\r
+import java.util.Collection;\r
+\r
+import org.simantics.graph.representation.External;\r
+import org.simantics.graph.representation.Identity;\r
+import org.simantics.graph.representation.Internal;\r
+import org.simantics.graph.representation.Optional;\r
+import org.simantics.graph.representation.Root;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.graph.representation.Value;\r
+import org.simantics.graph.representation.old.OldTransferableGraph1;\r
+import org.simantics.graph.store.GraphStore;\r
+import org.simantics.graph.store.IStore;\r
+import org.simantics.graph.store.IdentityStore;\r
+import org.simantics.graph.store.StatementStore;\r
+import org.simantics.graph.store.ValueStore;\r
+\r
+\r
+public class TransferableGraphConversion {\r
+ private static StatementStore extractStatements(TransferableGraph1 tg) {\r
+ StatementStore statements = new StatementStore();\r
+ int[] array = tg.statements;\r
+ int i=0;\r
+ while(i<array.length) {\r
+ int subject = array[i++];\r
+ int predicate = array[i++];\r
+ int inverse = array[i++];\r
+ int object = array[i++];\r
+ statements.add(subject, predicate, object);\r
+ if(inverse >= 0)\r
+ statements.add(object, inverse, subject);\r
+ }\r
+ return statements;\r
+ }\r
+ \r
+ public static IdentityStore extractIdentities(TransferableGraph1 tg) {\r
+ return extractIdentities(tg.resourceCount, tg.identities);\r
+ }\r
+ \r
+ public static IdentityStore extractIdentities(OldTransferableGraph1 tg) {\r
+ return extractIdentities(tg.resourceCount, tg.identities);\r
+ }\r
+ \r
+ private static IdentityStore extractIdentities(int resourceCount, Identity[] ids) {\r
+ IdentityStore identities = new IdentityStore();\r
+ identities.setResourceCount(resourceCount);\r
+ for(Identity identity : ids) {\r
+ if(identity.definition instanceof Root) {\r
+ Root def = (Root)identity.definition;\r
+ identities.defineRoot(def.name, identity.resource);\r
+ }\r
+ else if(identity.definition instanceof External) {\r
+ External def = (External)identity.definition;\r
+ identities.defineChild(def.parent, def.name, identity.resource);\r
+ }\r
+ else if(identity.definition instanceof Optional) {\r
+ Optional def = (Optional)identity.definition;\r
+ identities.defineChild(def.parent, def.name, identity.resource);\r
+ }\r
+ else if(identity.definition instanceof Internal) {\r
+ Internal def = (Internal)identity.definition;\r
+ identities.defineChild(def.parent, def.name, identity.resource);\r
+ }\r
+ }\r
+ return identities; \r
+ }\r
+ \r
+ private static ValueStore extractValues(TransferableGraph1 tg) {\r
+ ValueStore values = new ValueStore();\r
+ for(Value value : tg.values)\r
+ values.setValue(value.resource, value.value);\r
+ return values;\r
+ }\r
+ \r
+ public static GraphStore convert(TransferableGraph1 tg) {\r
+ return new GraphStore(\r
+ extractStatements(tg),\r
+ extractIdentities(tg),\r
+ extractValues(tg),\r
+ new THashMap<Class<?>, IStore>()\r
+ );\r
+ }\r
+\r
+ public static TransferableGraph1 convert(final IGraph cg, final GraphStore store) {\r
+ \r
+ // Create inverse map\r
+ final TIntIntHashMap inverseMap = new TIntIntHashMap();\r
+ final TIntHashSet withoutInverse = new TIntHashSet();\r
+ final Paths paths = cg.getPaths();\r
+\r
+ store.statements.getPredicates().forEach(new TIntProcedure() {\r
+ @Override\r
+ public boolean execute(int id) {\r
+ for(Res inverse : cg.rawGetObjects(store.idToRes(id), paths.InverseOf)) { \r
+ int inv = store.createResToId(inverse);\r
+ inverseMap.put(id, inv);\r
+ inverseMap.put(inv, id);\r
+ return true;\r
+ }\r
+ withoutInverse.add(id);\r
+ return true;\r
+ }\r
+ }); \r
+ \r
+ if(!withoutInverse.isEmpty()) {\r
+ IdentityStore identities = store.identities;\r
+ StatementStore statements = store.statements;\r
+ int inverseOfId = identities.pathToId(paths.InverseOf);\r
+ if(inverseOfId >= 0)\r
+ for(int id=0;id<store.identities.getResourceCount();++id) {\r
+ TIntArrayList invs = statements.getObjects(id, inverseOfId);\r
+ if(!invs.isEmpty()) {\r
+ int inv = invs.getQuick(0);\r
+ inverseMap.put(id, inv);\r
+ inverseMap.put(inv, id);\r
+ }\r
+ }\r
+ }\r
+ \r
+ // Create tg\r
+ TransferableGraph1 tg = new TransferableGraph1(\r
+ store.identities.getResourceCount(),\r
+ store.identities.toArray(),\r
+ store.statements.toArray(inverseMap),\r
+ store.values.toArray()\r
+ );\r
+ \r
+ return tg;\r
+ }\r
+ \r
+ public static TransferableGraph1 convert(final GraphStore store) { \r
+ \r
+ // Create tg\r
+ TransferableGraph1 tg = new TransferableGraph1(\r
+ store.identities.getResourceCount(),\r
+ store.identities.toArray(),\r
+ store.statements.toArray(),\r
+ store.values.toArray()\r
+ );\r
+ \r
+ return tg;\r
+ }\r
+ \r
+ public static CompositeGraph convert(Paths paths, Collection<TransferableGraph1> tgs) {\r
+ CompositeGraph graph = new CompositeGraph(paths);\r
+ for(TransferableGraph1 tg : tgs)\r
+ graph.addFragment(convert(tg));\r
+ return graph;\r
+ }\r
+ \r
+}\r