- 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);
- if (identity == null) {
- return null;
- }
- }
- 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<tg.statements.length;i+=4) {
- if(tg.statements[i] == resource) {
- result.add(tg.statements[i+1]);
- result.add(tg.statements[i+3]);
- }
- }
- return result;
- }
-
- public static Collection<Identity> getChildren(TransferableGraph1 tg, Identity parent) {
- TreeMap<String,Identity> 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<tg.statements.length;i+=4) {
- if(tg.statements[i] == parent.resource) {
- 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 = getPossibleObject(tg, tg.statements[i+3], hasName);
- if(possibleNameResource != 0) {
- 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 TIntArrayList getObjects(TransferableGraph1 tg, int subject, Identity predicate) {
- TIntArrayList result = new TIntArrayList();
- for(int i=0;i<tg.statements.length;i+=4) {
- if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
- result.add(tg.statements[i+3]);
- }
- }
- return result;
- }
-
+ 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;
+ }
+ else if(id.definition instanceof Optional) {
+ Optional ext = (Optional)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);
+ if (identity == null) {
+ return null;
+ }
+ }
+ 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<tg.statements.length;i+=4) {
+ if(tg.statements[i] == resource) {
+ result.add(tg.statements[i+1]);
+ result.add(tg.statements[i+3]);
+ }
+ }
+ return result;
+ }
+
+ public static Collection<Identity> getChildren2(TransferableGraph1 tg, Identity parent) {
+ return getChildren2(tg, parent.resource);
+ }
+
+ public static Collection<Identity> getChildren2(TransferableGraph1 tg, int parentResource) {
+ TreeMap<String, Identity> 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();
+ }
+
+ /**
+ * 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<Identity> getChildren(TransferableGraph1 tg, Identity parent) {
+ TreeMap<String,Identity> 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<tg.statements.length;i+=4) {
+ if(tg.statements[i] == parent.resource) {
+ 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 = getPossibleObject(tg, tg.statements[i+3], hasName);
+ if(possibleNameResource != 0) {
+ 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 TIntArrayList getObjects(TransferableGraph1 tg, int subject, Identity predicate) {
+ TIntArrayList result = new TIntArrayList();
+ for(int i=0;i<tg.statements.length;i+=4) {
+ if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
+ result.add(tg.statements[i+3]);
+ }
+ }
+ return result;
+ }
+