1 package org.simantics.graph.representation;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
7 import java.util.TreeMap;
9 import org.simantics.databoard.Bindings;
10 import org.simantics.databoard.adapter.AdaptException;
11 import org.simantics.databoard.util.URIStringUtils;
13 import gnu.trove.list.array.TIntArrayList;
14 import gnu.trove.map.TIntObjectMap;
15 import gnu.trove.map.hash.TIntObjectHashMap;
17 public class TransferableGraphUtils {
19 public static Collection<Identity> getRoots(TransferableGraph1 tg) {
21 ArrayList<Identity> result = new ArrayList<Identity>();
22 for(Identity id : tg.identities) {
23 if(id.definition instanceof Root) result.add(id);
29 public static Identity findRootWithName(TransferableGraph1 tg, String name) {
31 for(Identity id : tg.identities) {
32 if(id.definition instanceof Root) {
33 Root ext = (Root)id.definition;
34 if(ext.name.equals(name)) return id;
41 public static Identity findExternalWithName(TransferableGraph1 tg, String name) {
43 for(Identity id : tg.identities) {
44 if(id.definition instanceof External) {
45 External ext = (External)id.definition;
46 if(ext.name.equals(name)) return id;
53 public static Identity findExternalWithNameAndParent(TransferableGraph1 tg, int parent, String name) {
55 for(Identity id : tg.identities) {
56 if(id.definition instanceof External) {
57 External ext = (External)id.definition;
58 if(ext.name.equals(name) && ext.parent == parent) return id;
65 public static Identity findExternal(TransferableGraph1 tg, String uri) {
67 Identity identity = findExternalWithName(tg, "http:/");
68 if(identity == null) identity = findExternalWithName(tg, "");
69 if(identity == null) identity = findRootWithName(tg, "");
70 if("http:/".equals(uri)) return identity;
71 String[] tokens = uri.substring("http://".length()).split("/");
72 for(String token : tokens) {
73 identity = findExternalWithNameAndParent(tg, identity.resource, token);
79 public static Identity getIdentity(TransferableGraph1 tg, int resource) {
80 for(Identity id : tg.identities) {
81 if(id.resource == resource) return id;
86 public static TIntArrayList getStatements(TransferableGraph1 tg, int resource) {
87 TIntArrayList result = new TIntArrayList();
88 for(int i=0;i<tg.statements.length;i+=4) {
89 if(tg.statements[i] == resource) {
90 result.add(tg.statements[i+1]);
91 result.add(tg.statements[i+3]);
97 public static Collection<Identity> getChildren(TransferableGraph1 tg, Identity parent) {
98 TreeMap<String,Identity> result = new TreeMap<>();
99 for(Identity id : tg.identities) {
100 if(id.definition instanceof Internal) {
101 Internal internal = (Internal)id.definition;
102 if(internal.parent == parent.resource) result.put(internal.name, id);
105 Identity consistsOf = findExternal(tg, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
106 Identity hasName = findExternal(tg, "http://www.simantics.org/Layer0-1.1/HasName");
107 for(int i=0;i<tg.statements.length;i+=4) {
108 if(tg.statements[i] == parent.resource) {
109 if(tg.statements[i+1] == consistsOf.resource) {
110 Identity identity = getIdentity(tg, tg.statements[i+3]);
111 if(identity != null) {
112 if(identity.definition instanceof Internal) {
113 Internal internal = (Internal)identity.definition;
114 result.put(internal.name, identity);
117 int possibleNameResource = getPossibleObject(tg, tg.statements[i+3], hasName);
118 if(possibleNameResource != 0) {
119 Value value = findValue(tg, possibleNameResource);
122 String name = (String)value.value.getValue(Bindings.STRING);
123 result.put(name, new Identity(tg.statements[i+3], new Internal(tg.statements[i], name)));
124 } catch (AdaptException e) {
133 return result.values();
136 public static TIntArrayList getObjects(TransferableGraph1 tg, int subject, Identity predicate) {
137 TIntArrayList result = new TIntArrayList();
138 for(int i=0;i<tg.statements.length;i+=4) {
139 if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
140 result.add(tg.statements[i+3]);
146 public static int getPossibleObject(TransferableGraph1 tg, int subject, Identity predicate) {
148 for(int i=0;i<tg.statements.length;i+=4) {
149 if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
150 if(result != 0) return 0;
151 result = tg.statements[i+3];
157 public static int getPossibleObject(TransferableGraph1 tg, Identity subject, String predicate) {
158 Identity p = findExternal(tg, predicate);
159 if(p == null) return 0;
160 return getPossibleObject(tg, subject.resource, p);
163 public static Map<Identity, String> getNames(TransferableGraph1 tg, Collection<Identity> ids) {
164 Map<Identity, String> result = new HashMap<Identity, String>();
165 for(Identity id : ids) {
166 if(id.definition instanceof Internal) {
167 Internal internal = (Internal)id.definition;
168 result.put(id, internal.name);
174 public static String getName(TransferableGraph1 tg, Identity id) {
178 public static String getName(Identity id) {
179 if(id.definition instanceof Internal) {
180 Internal internal = (Internal)id.definition;
181 return internal.name;
182 } else if(id.definition instanceof External) {
183 External external = (External)id.definition;
184 return external.name;
185 } else if(id.definition instanceof Root) {
186 Root root = (Root)id.definition;
189 Optional optional = (Optional)id.definition;
190 return optional.name;
194 public static String getRootType(Identity id) {
195 if(id.definition instanceof Root) {
196 Root root = (Root)id.definition;
199 throw new IllegalArgumentException("Expected root, got " + id);
203 public static Value findValue(TransferableGraph1 tg, int subject) {
204 for(Value v : tg.values) {
205 if(v.resource == subject) return v;
210 public static String getURI(TransferableGraph1 tg, int id) {
211 return getURI(tg.resourceCount, tg.identities, id);
214 public static String getURI(int resourceCount, Identity[] identities, int id) {
215 for(Identity identity : identities) {
216 if(identity.resource == id) {
217 IdentityDefinition definition = identity.definition;
218 if(definition instanceof External) {
219 External def = (External)definition;
220 if(def.parent == -1) return "http:/";
221 else return getURI(resourceCount, identities, def.parent) + "/" + def.name;
222 } else if(definition instanceof Root) {
223 Root def = (Root)definition;
224 if(def.name.isEmpty()) return "http:/";
226 } else if (definition instanceof Internal) {
227 Internal def = (Internal)definition;
228 return getURI(resourceCount, identities, def.parent) + "/" + def.name;
234 return "<internal reference " + id + ">:";
237 public static TIntObjectMap<Identity> mapIdentities(TransferableGraph1 tg) {
238 return mapIdentities(tg.identities);
241 public static TIntObjectMap<Identity> mapIdentities(Identity[] identities) {
242 // Integer.MIN_VALUE cannot be the value of Identity.resource
243 TIntObjectMap<Identity> map = new TIntObjectHashMap<>(identities.length, 0.5f, Integer.MIN_VALUE);
244 for (Identity id : identities)
245 map.put(id.resource, id);
249 public static String getURI(int resourceCount, TIntObjectMap<Identity> identities, int id) {
250 Identity identity = identities.get(id);
251 if(identity != null) {
252 IdentityDefinition definition = identity.definition;
253 if(definition instanceof External) {
254 External def = (External)definition;
255 if(def.parent == -1) return "http:/";
256 else return getURI(resourceCount, identities, def.parent) + "/" + def.name;
257 } else if(definition instanceof Root) {
258 Root def = (Root)definition;
259 if(def.name.isEmpty()) return "http:/";
261 } else if (definition instanceof Internal) {
262 Internal def = (Internal)definition;
263 return getURI(resourceCount, identities, def.parent) + "/" + def.name;
268 return "<internal reference " + id + ">:";
271 public static String getTrueURI(int resourceCount, TIntObjectMap<Identity> identities, int id) {
272 Identity identity = identities.get(id);
273 if(identity != null) {
274 IdentityDefinition definition = identity.definition;
275 if(definition instanceof External) {
276 External def = (External)definition;
277 if(def.parent == -1) return "http:/";
278 else return getTrueURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name);
279 } else if(definition instanceof Root) {
280 Root def = (Root)definition;
281 if(def.name.isEmpty()) return "http:/";
283 } else if (definition instanceof Internal) {
284 Internal def = (Internal)definition;
285 return getTrueURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name);
290 return "<internal reference " + id + ">:";