X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.graph%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fquery%2FTransferableGraphConversion.java;h=9c96f834dd61c6ca75292630b2481ab765b47129;hp=dd0cfa1894164505822310e2b10815ae41e1fb1f;hb=b123fe92f27f72b9132ec52c9494fb8d8715d2ed;hpb=969bd23cab98a79ca9101af33334000879fb60c5 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 index dd0cfa189..9c96f834d 100644 --- a/bundles/org.simantics.graph/src/org/simantics/graph/query/TransferableGraphConversion.java +++ b/bundles/org.simantics.graph/src/org/simantics/graph/query/TransferableGraphConversion.java @@ -1,158 +1,153 @@ -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; - } - -} +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.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); + } + + 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; + } + +}