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%2Frepresentation%2FTransferableGraphUtils.java;h=10637ad65caf0d861d52e3d1adfffdee66d5f2ca;hp=4b113fa499b6bc1208d64b62e2be5be66713cc10;hb=c3adc8c88589a2b987ea524dd7139bc7da5c7178;hpb=e3f78a075fb589c0c940f6e13d92a45a39a3ac0f diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java b/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java index 4b113fa49..10637ad65 100644 --- a/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java +++ b/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java @@ -16,278 +16,339 @@ import gnu.trove.map.hash.TIntObjectHashMap; public class TransferableGraphUtils { - public static Collection getRoots(TransferableGraph1 tg) { - - ArrayList result = new ArrayList(); - for(Identity id : tg.identities) { - if(id.definition instanceof Root) result.add(id); - } - return result; - - } - - public static Identity findRootWithName(TransferableGraph1 tg, String name) { - - for(Identity id : tg.identities) { - if(id.definition instanceof Root) { - Root ext = (Root)id.definition; - if(ext.name.equals(name)) return id; - } - } - return null; - - } + public static Collection getRoots(TransferableGraph1 tg) { + + ArrayList result = new ArrayList(); + for(Identity id : tg.identities) { + if(id.definition instanceof Root) result.add(id); + } + return result; + + } + + public static Identity findRootWithName(TransferableGraph1 tg, String name) { + + for(Identity id : tg.identities) { + if(id.definition instanceof Root) { + Root ext = (Root)id.definition; + if(ext.name.equals(name)) return id; + } + } + return null; + + } - public static Identity findExternalWithName(TransferableGraph1 tg, String name) { - - for(Identity id : tg.identities) { - if(id.definition instanceof External) { - External ext = (External)id.definition; - if(ext.name.equals(name)) return id; - } - } - return null; - - } + public static Identity findExternalWithName(TransferableGraph1 tg, String name) { + + for(Identity id : tg.identities) { + if(id.definition instanceof External) { + External ext = (External)id.definition; + if(ext.name.equals(name)) return id; + } + } + return null; + + } - public static Identity findExternalWithNameAndParent(TransferableGraph1 tg, int parent, String name) { - - for(Identity id : tg.identities) { - if(id.definition instanceof External) { - External ext = (External)id.definition; - if(ext.name.equals(name) && ext.parent == parent) return id; - } - } - return null; - - } - - public static Identity findExternal(TransferableGraph1 tg, String uri) { - - Identity identity = findExternalWithName(tg, "http:/"); - if(identity == null) identity = findExternalWithName(tg, ""); - if(identity == null) identity = findRootWithName(tg, ""); - if("http:/".equals(uri)) return identity; - String[] tokens = uri.substring("http://".length()).split("/"); - for(String token : tokens) { - identity = findExternalWithNameAndParent(tg, identity.resource, token); - } - return identity; - - } - - public static Identity getIdentity(TransferableGraph1 tg, int resource) { - for(Identity id : tg.identities) { - if(id.resource == resource) return id; - } - return null; - } - - public static TIntArrayList getStatements(TransferableGraph1 tg, int resource) { - TIntArrayList result = new TIntArrayList(); - for(int i=0;i getChildren(TransferableGraph1 tg, Identity parent) { - TreeMap result = new TreeMap<>(); - for(Identity id : tg.identities) { - if(id.definition instanceof Internal) { - Internal internal = (Internal)id.definition; - if(internal.parent == parent.resource) result.put(internal.name, id); - } - } - Identity consistsOf = findExternal(tg, "http://www.simantics.org/Layer0-1.1/ConsistsOf"); - Identity hasName = findExternal(tg, "http://www.simantics.org/Layer0-1.1/HasName"); - for(int i=0;i getNames(TransferableGraph1 tg, Collection ids) { - Map result = new HashMap(); - for(Identity id : ids) { - if(id.definition instanceof Internal) { - Internal internal = (Internal)id.definition; - result.put(id, internal.name); - } - } - return result; - } + public static Collection getChildren2(TransferableGraph1 tg, Identity parent) { + return getChildren2(tg, parent.resource); + } + + public static Collection getChildren2(TransferableGraph1 tg, int parentResource) { + TreeMap result = new TreeMap<>(); + for (Identity id : tg.identities) { + if (id.definition instanceof Internal) { + Internal internal = (Internal) id.definition; + if (internal.parent == parentResource) + result.put(internal.name, id); + } + } + Identity consistsOf = findExternal(tg, "http://www.simantics.org/Layer0-1.1/ConsistsOf"); + Identity hasName = findExternal(tg, "http://www.simantics.org/Layer0-1.1/HasName"); + for (int i = 0; i < tg.statements.length; i += 4) { + if (tg.statements[i] == parentResource) { + if (tg.statements[i + 1] == consistsOf.resource) { + Identity identity = getIdentity(tg, tg.statements[i + 3]); + if (identity != null) { + if (identity.definition instanceof Internal) { + Internal internal = (Internal) identity.definition; + result.put(internal.name, identity); + } + } else { + int possibleNameResource = getPossibleObject2(tg, tg.statements[i + 3], hasName); + if (possibleNameResource != NOT_FOUND) { + Value value = findValue(tg, possibleNameResource); + if (value != null) { + try { + String name = (String) value.value.getValue(Bindings.STRING); + result.put(name, new Identity(tg.statements[i + 3], new Internal(tg.statements[i], name))); + } catch (AdaptException e) { + e.printStackTrace(); + } + } + } + } + } + } + } + return result.values(); + } - public static String getName(TransferableGraph1 tg, Identity id) { - return getName(id); - } + /** + * This implementation is no longer advised to use because it returns 0 as + * NOT_FOUND which is in fact a valid ID for resource in graph + */ + @Deprecated + public static Collection getChildren(TransferableGraph1 tg, Identity parent) { + TreeMap result = new TreeMap<>(); + for(Identity id : tg.identities) { + if(id.definition instanceof Internal) { + Internal internal = (Internal)id.definition; + if(internal.parent == parent.resource) result.put(internal.name, id); + } + } + Identity consistsOf = findExternal(tg, "http://www.simantics.org/Layer0-1.1/ConsistsOf"); + Identity hasName = findExternal(tg, "http://www.simantics.org/Layer0-1.1/HasName"); + for(int i=0;i:"; - } + public static Map getNames(TransferableGraph1 tg, Collection ids) { + Map result = new HashMap(); + for(Identity id : ids) { + if(id.definition instanceof Internal) { + Internal internal = (Internal)id.definition; + result.put(id, internal.name); + } + } + return result; + } - public static TIntObjectMap mapIdentities(TransferableGraph1 tg) { - return mapIdentities(tg.identities); - } + public static String getName(TransferableGraph1 tg, Identity id) { + return getName(id); + } - public static TIntObjectMap mapIdentities(Identity[] identities) { - // Integer.MIN_VALUE cannot be the value of Identity.resource - TIntObjectMap map = new TIntObjectHashMap<>(identities.length, 0.5f, Integer.MIN_VALUE); - for (Identity id : identities) - map.put(id.resource, id); - return map; - } + public static String getName(Identity id) { + if(id.definition instanceof Internal) { + Internal internal = (Internal)id.definition; + return internal.name; + } else if(id.definition instanceof External) { + External external = (External)id.definition; + return external.name; + } else if(id.definition instanceof Root) { + Root root = (Root)id.definition; + return root.name; + } else { + Optional optional = (Optional)id.definition; + return optional.name; + } + } - public static String getURI(int resourceCount, TIntObjectMap identities, int id) { - Identity identity = identities.get(id); - if(identity != null) { - IdentityDefinition definition = identity.definition; - if(definition instanceof External) { - External def = (External)definition; - if(def.parent == -1) return "http:/"; - else return getURI(resourceCount, identities, def.parent) + "/" + def.name; - } else if(definition instanceof Root) { - Root def = (Root)definition; - if(def.name.isEmpty()) return "http:/"; - return def.name; - } else if (definition instanceof Internal) { - Internal def = (Internal)definition; - return getURI(resourceCount, identities, def.parent) + "/" + def.name; - } else { - return ""; - } - } - return ":"; - } - - public static String getTrueURI(int resourceCount, TIntObjectMap identities, int id) { - Identity identity = identities.get(id); - if(identity != null) { - IdentityDefinition definition = identity.definition; - if(definition instanceof External) { - External def = (External)definition; - if(def.parent == -1) return "http:/"; - else return getTrueURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name); - } else if(definition instanceof Root) { - Root def = (Root)definition; - if(def.name.isEmpty()) return "http:/"; - return def.name; - } else if (definition instanceof Internal) { - Internal def = (Internal)definition; - return getTrueURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name); - } else { - return ""; - } - } - return ":"; - } + public static String getRootType(Identity id) { + if(id.definition instanceof Root) { + Root root = (Root)id.definition; + return root.type; + } else { + throw new IllegalArgumentException("Expected root, got " + id); + } + } + + public static Value findValue(TransferableGraph1 tg, int subject) { + for(Value v : tg.values) { + if(v.resource == subject) return v; + } + return null; + } + + public static String getURI(TransferableGraph1 tg, int id) { + return getURI(tg.identities, id); + } + + public static String getURI(Identity[] identities, int id) { + for(Identity identity : identities) { + if(identity.resource == id) { + IdentityDefinition definition = identity.definition; + if(definition instanceof External) { + External def = (External)definition; + if(def.parent == -1) return "http:/"; + else return getURI(identities, def.parent) + "/" + def.name; + } else if(definition instanceof Root) { + Root def = (Root)definition; + if(def.name.isEmpty()) return "http:/"; + return def.name; + } else if (definition instanceof Internal) { + Internal def = (Internal)definition; + return getURI(identities, def.parent) + "/" + def.name; + } else { + return ""; + } + } + } + return ":"; + } + + public static TIntObjectMap mapIdentities(TransferableGraph1 tg) { + return mapIdentities(tg.identities); + } + + public static TIntObjectMap mapIdentities(Identity[] identities) { + // Integer.MIN_VALUE cannot be the value of Identity.resource + TIntObjectMap map = new TIntObjectHashMap<>(identities.length, 0.5f, Integer.MIN_VALUE); + for (Identity id : identities) + map.put(id.resource, id); + return map; + } + + public static String getURI(int resourceCount, TIntObjectMap identities, int id) { + Identity identity = identities.get(id); + if(identity != null) { + IdentityDefinition definition = identity.definition; + if(definition instanceof External) { + External def = (External)definition; + if(def.parent == -1) return "http:/"; + else return getURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name); + } else if(definition instanceof Root) { + Root def = (Root)definition; + if(def.name.isEmpty()) return "http:/"; + return def.name; + } else if (definition instanceof Internal) { + Internal def = (Internal)definition; + return getURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name); + } else { + return ""; + } + } + return ":"; + } }