1 package org.simantics.graph.representation;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
8 public class TransferableGraphUtils {
10 public static Collection<Identity> getRoots(TransferableGraph1 tg) {
12 ArrayList<Identity> result = new ArrayList<Identity>();
13 for(Identity id : tg.identities) {
14 if(id.definition instanceof Root) result.add(id);
20 public static Identity findRootWithName(TransferableGraph1 tg, String name) {
22 for(Identity id : tg.identities) {
23 if(id.definition instanceof Root) {
24 Root ext = (Root)id.definition;
25 if(ext.name.equals(name)) return id;
32 public static Identity findExternalWithName(TransferableGraph1 tg, String name) {
34 for(Identity id : tg.identities) {
35 if(id.definition instanceof External) {
36 External ext = (External)id.definition;
37 if(ext.name.equals(name)) return id;
44 public static Identity findExternalWithNameAndParent(TransferableGraph1 tg, int parent, String name) {
46 for(Identity id : tg.identities) {
47 if(id.definition instanceof External) {
48 External ext = (External)id.definition;
49 if(ext.name.equals(name) && ext.parent == parent) return id;
56 public static Identity findExternal(TransferableGraph1 tg, String uri) {
58 Identity identity = findExternalWithName(tg, "http:/");
59 if(identity == null) identity = findExternalWithName(tg, "");
60 if(identity == null) identity = findRootWithName(tg, "");
61 if("http:/".equals(uri)) return identity;
62 String[] tokens = uri.substring("http://".length()).split("/");
63 for(String token : tokens) {
64 identity = findExternalWithNameAndParent(tg, identity.resource, token);
70 public static Collection<Identity> getChildren(TransferableGraph1 tg, Identity parent) {
71 ArrayList<Identity> result = new ArrayList<Identity>();
72 System.err.println("children for " + parent.resource);
73 for(Identity id : tg.identities) {
74 if(id.definition instanceof Internal) {
75 Internal internal = (Internal)id.definition;
76 System.err.println("internal with parent " + internal.parent);
77 if(internal.parent == parent.resource) result.add(id);
80 findExternal(tg, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
81 for(int i=0;i<tg.statements.length;i+=4) {
82 if(tg.statements[i] == parent.resource)
83 System.err.println("related to parent " + tg.statements[i+3]);
88 public static int getPossibleObject(TransferableGraph1 tg, Identity subject, String predicate) {
89 Identity p = findExternal(tg, predicate);
90 if(p == null) return 0;
92 for(int i=0;i<tg.statements.length;i+=4) {
93 if(tg.statements[i] == subject.resource && tg.statements[i+1] == p.resource) {
94 if(result != 0) return 0;
95 result = tg.statements[i+3];
101 public static Map<Identity, String> getNames(TransferableGraph1 tg, Collection<Identity> ids) {
102 Map<Identity, String> result = new HashMap<Identity, String>();
103 for(Identity id : ids) {
104 if(id.definition instanceof Internal) {
105 Internal internal = (Internal)id.definition;
106 result.put(id, internal.name);
112 public static String getName(TransferableGraph1 tg, Identity id) {
116 public static String getName(Identity id) {
117 if(id.definition instanceof Internal) {
118 Internal internal = (Internal)id.definition;
119 return internal.name;
120 } else if(id.definition instanceof External) {
121 External external = (External)id.definition;
122 return external.name;
123 } else if(id.definition instanceof Root) {
124 Root root = (Root)id.definition;
127 Optional optional = (Optional)id.definition;
128 return optional.name;
132 public static String getRootType(Identity id) {
133 if(id.definition instanceof Root) {
134 Root root = (Root)id.definition;
137 throw new IllegalArgumentException("Expected root, got " + id);
141 public static Value findValue(TransferableGraph1 tg, int subject) {
142 for(Value v : tg.values) {
143 if(v.resource == subject) return v;
148 public static String getURI(TransferableGraph1 tg, int id) {
149 return getURI(tg.resourceCount, tg.identities, id);
152 public static String getURI(int resourceCount, Identity[] identities, int id) {
153 for(Identity identity : identities) {
154 if(identity.resource == id) {
155 IdentityDefinition definition = identity.definition;
156 if(definition instanceof External) {
157 External def = (External)definition;
158 if(def.parent == -1) return "http:/";
159 else return getURI(resourceCount, identities, def.parent) + "/" + def.name;
160 } else if(definition instanceof Root) {
161 Root def = (Root)definition;
163 } else if (definition instanceof Internal) {
164 Internal def = (Internal)definition;
165 System.err.println("External URI error: parent was internal '" + def.name + "'");
172 return "<internal reference " + id + ">:";