X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.graph%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fquery%2FTransferableGraphConversion.java;fp=bundles%2Forg.simantics.graph%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fquery%2FTransferableGraphConversion.java;h=dd0cfa1894164505822310e2b10815ae41e1fb1f;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/query/TransferableGraphConversion.java b/bundles/org.simantics.graph/src/org/simantics/graph/query/TransferableGraphConversion.java new file mode 100644 index 000000000..dd0cfa189 --- /dev/null +++ b/bundles/org.simantics.graph/src/org/simantics/graph/query/TransferableGraphConversion.java @@ -0,0 +1,158 @@ +package org.simantics.graph.query; + +import gnu.trove.list.array.TIntArrayList; +import gnu.trove.map.hash.THashMap; +import gnu.trove.map.hash.TIntIntHashMap; +import gnu.trove.procedure.TIntProcedure; +import gnu.trove.set.hash.TIntHashSet; + +import java.util.Collection; + +import org.simantics.graph.representation.External; +import org.simantics.graph.representation.Identity; +import org.simantics.graph.representation.Internal; +import org.simantics.graph.representation.Optional; +import org.simantics.graph.representation.Root; +import org.simantics.graph.representation.TransferableGraph1; +import org.simantics.graph.representation.Value; +import org.simantics.graph.representation.old.OldTransferableGraph1; +import org.simantics.graph.store.GraphStore; +import org.simantics.graph.store.IStore; +import org.simantics.graph.store.IdentityStore; +import org.simantics.graph.store.StatementStore; +import org.simantics.graph.store.ValueStore; + + +public class TransferableGraphConversion { + private static StatementStore extractStatements(TransferableGraph1 tg) { + StatementStore statements = new StatementStore(); + int[] array = tg.statements; + int i=0; + while(i= 0) + statements.add(object, inverse, subject); + } + return statements; + } + + public static IdentityStore extractIdentities(TransferableGraph1 tg) { + return extractIdentities(tg.resourceCount, tg.identities); + } + + public static IdentityStore extractIdentities(OldTransferableGraph1 tg) { + return extractIdentities(tg.resourceCount, tg.identities); + } + + private static IdentityStore extractIdentities(int resourceCount, Identity[] ids) { + IdentityStore identities = new IdentityStore(); + identities.setResourceCount(resourceCount); + for(Identity identity : ids) { + if(identity.definition instanceof Root) { + Root def = (Root)identity.definition; + identities.defineRoot(def.name, identity.resource); + } + else if(identity.definition instanceof External) { + External def = (External)identity.definition; + identities.defineChild(def.parent, def.name, identity.resource); + } + else if(identity.definition instanceof Optional) { + Optional def = (Optional)identity.definition; + identities.defineChild(def.parent, def.name, identity.resource); + } + else if(identity.definition instanceof Internal) { + Internal def = (Internal)identity.definition; + identities.defineChild(def.parent, def.name, identity.resource); + } + } + return identities; + } + + private static ValueStore extractValues(TransferableGraph1 tg) { + ValueStore values = new ValueStore(); + for(Value value : tg.values) + values.setValue(value.resource, value.value); + return values; + } + + public static GraphStore convert(TransferableGraph1 tg) { + return new GraphStore( + extractStatements(tg), + extractIdentities(tg), + extractValues(tg), + new THashMap, IStore>() + ); + } + + public static TransferableGraph1 convert(final IGraph cg, final GraphStore store) { + + // Create inverse map + final TIntIntHashMap inverseMap = new TIntIntHashMap(); + final TIntHashSet withoutInverse = new TIntHashSet(); + final Paths paths = cg.getPaths(); + + store.statements.getPredicates().forEach(new TIntProcedure() { + @Override + public boolean execute(int id) { + for(Res inverse : cg.rawGetObjects(store.idToRes(id), paths.InverseOf)) { + int inv = store.createResToId(inverse); + inverseMap.put(id, inv); + inverseMap.put(inv, id); + return true; + } + withoutInverse.add(id); + return true; + } + }); + + if(!withoutInverse.isEmpty()) { + IdentityStore identities = store.identities; + StatementStore statements = store.statements; + int inverseOfId = identities.pathToId(paths.InverseOf); + if(inverseOfId >= 0) + for(int id=0;id tgs) { + CompositeGraph graph = new CompositeGraph(paths); + for(TransferableGraph1 tg : tgs) + graph.addFragment(convert(tg)); + return graph; + } + +}