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, URIStringUtils.unescape(token));
74 if (identity == null) {
83 * Provided a tg and a resource uri, returns the identity of the tg if the uri_ matches the tg exactly
88 public static Identity getIdentity2(TransferableGraph1 tg, String uri_) {
89 Collection<Identity> identities = TransferableGraphUtils.getRoots(tg);
90 for (Identity i : identities) {
91 Identity id = getIdentity2(tg, uri_, i);
106 public static Identity getIdentity2(TransferableGraph1 tg, String uri_, Identity id) {
107 String uri = TransferableGraphUtils.getURI(tg, id.resource);
109 if (uri_.equals(uri)) {
113 if (uri_.startsWith(uri)) {
114 Collection<Identity> childIdentitiesOfRoot = TransferableGraphUtils.getChildren2(tg, id);
115 for (Identity i2 : childIdentitiesOfRoot) {
116 Identity id2 = getIdentity2(tg, uri_, i2);
126 public static Identity getIdentity(TransferableGraph1 tg, int resource) {
127 for(Identity id : tg.identities) {
128 if(id.resource == resource) return id;
133 public static TIntArrayList getStatements(TransferableGraph1 tg, int resource) {
134 TIntArrayList result = new TIntArrayList();
135 for(int i=0;i<tg.statements.length;i+=4) {
136 if(tg.statements[i] == resource) {
137 result.add(tg.statements[i+1]);
138 result.add(tg.statements[i+3]);
144 public static Collection<Identity> getChildren2(TransferableGraph1 tg, Identity parent) {
145 return getChildren2(tg, parent.resource);
148 public static Collection<Identity> getChildren2(TransferableGraph1 tg, int parentResource) {
149 TreeMap<String, Identity> result = new TreeMap<>();
150 for (Identity id : tg.identities) {
151 if (id.definition instanceof Internal) {
152 Internal internal = (Internal) id.definition;
153 if (internal.parent == parentResource)
154 result.put(internal.name, id);
157 Identity consistsOf = findExternal(tg, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
158 Identity hasName = findExternal(tg, "http://www.simantics.org/Layer0-1.1/HasName");
159 for (int i = 0; i < tg.statements.length; i += 4) {
160 if (tg.statements[i] == parentResource) {
161 if (tg.statements[i + 1] == consistsOf.resource) {
162 Identity identity = getIdentity(tg, tg.statements[i + 3]);
163 if (identity != null) {
164 if (identity.definition instanceof Internal) {
165 Internal internal = (Internal) identity.definition;
166 result.put(internal.name, identity);
169 int possibleNameResource = getPossibleObject2(tg, tg.statements[i + 3], hasName);
170 if (possibleNameResource != NOT_FOUND) {
171 Value value = findValue(tg, possibleNameResource);
174 String name = (String) value.value.getValue(Bindings.STRING);
175 result.put(name, new Identity(tg.statements[i + 3], new Internal(tg.statements[i], name)));
176 } catch (AdaptException e) {
185 return result.values();
189 * This implementation is no longer advised to use because it returns 0 as
190 * NOT_FOUND which is in fact a valid ID for resource in graph
193 public static Collection<Identity> getChildren(TransferableGraph1 tg, Identity parent) {
194 TreeMap<String,Identity> result = new TreeMap<>();
195 for(Identity id : tg.identities) {
196 if(id.definition instanceof Internal) {
197 Internal internal = (Internal)id.definition;
198 if(internal.parent == parent.resource) result.put(internal.name, id);
201 Identity consistsOf = findExternal(tg, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
202 Identity hasName = findExternal(tg, "http://www.simantics.org/Layer0-1.1/HasName");
203 for(int i=0;i<tg.statements.length;i+=4) {
204 if(tg.statements[i] == parent.resource) {
205 if(tg.statements[i+1] == consistsOf.resource) {
206 Identity identity = getIdentity(tg, tg.statements[i+3]);
207 if(identity != null) {
208 if(identity.definition instanceof Internal) {
209 Internal internal = (Internal)identity.definition;
210 result.put(internal.name, identity);
213 int possibleNameResource = getPossibleObject(tg, tg.statements[i+3], hasName);
214 if(possibleNameResource != 0) {
215 Value value = findValue(tg, possibleNameResource);
218 String name = (String)value.value.getValue(Bindings.STRING);
219 result.put(name, new Identity(tg.statements[i+3], new Internal(tg.statements[i], name)));
220 } catch (AdaptException e) {
229 return result.values();
232 public static TIntArrayList getObjects(TransferableGraph1 tg, int subject, Identity predicate) {
233 TIntArrayList result = new TIntArrayList();
234 for(int i=0;i<tg.statements.length;i+=4) {
235 if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
236 result.add(tg.statements[i+3]);
243 * This implementation is no longer advised to use because it returns 0 as
244 * NOT_FOUND which is in fact a valid ID for resource in graph
247 public static int getPossibleObject(TransferableGraph1 tg, int subject, Identity predicate) {
249 for(int i=0;i<tg.statements.length;i+=4) {
250 if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
251 if(result != 0 && tg.statements[i+3] != result) return 0;
252 result = tg.statements[i+3];
258 public static final int NOT_FOUND = -2;
260 public static int getPossibleObject2(TransferableGraph1 tg, int subject, Identity predicate) {
261 int result = NOT_FOUND;
262 for(int i=0;i<tg.statements.length;i+=4) {
263 if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
264 if(result != NOT_FOUND && tg.statements[i+3] != result)
266 result = tg.statements[i+3];
273 * @return 0 for presenting not found which is BAD
274 * @see getPossibleObject2
277 public static int getPossibleObject(TransferableGraph1 tg, Identity subject, String predicate) {
278 Identity p = findExternal(tg, predicate);
279 if(p == null) return 0;
280 return getPossibleObject(tg, subject.resource, p);
283 public static int getPossibleObject2(TransferableGraph1 tg, Identity subject, String predicate) {
284 Identity p = findExternal(tg, predicate);
287 return getPossibleObject2(tg, subject.resource, p);
290 public static Map<Identity, String> getNames(TransferableGraph1 tg, Collection<Identity> ids) {
291 Map<Identity, String> result = new HashMap<Identity, String>();
292 for(Identity id : ids) {
293 if(id.definition instanceof Internal) {
294 Internal internal = (Internal)id.definition;
295 result.put(id, internal.name);
301 public static String getName(TransferableGraph1 tg, Identity id) {
305 public static String getName(Identity id) {
306 if(id.definition instanceof Internal) {
307 Internal internal = (Internal)id.definition;
308 return internal.name;
309 } else if(id.definition instanceof External) {
310 External external = (External)id.definition;
311 return external.name;
312 } else if(id.definition instanceof Root) {
313 Root root = (Root)id.definition;
316 Optional optional = (Optional)id.definition;
317 return optional.name;
321 public static String getRootType(Identity id) {
322 if(id.definition instanceof Root) {
323 Root root = (Root)id.definition;
326 throw new IllegalArgumentException("Expected root, got " + id);
330 public static Value findValue(TransferableGraph1 tg, int subject) {
331 for(Value v : tg.values) {
332 if(v.resource == subject) return v;
337 public static String getURI(TransferableGraph1 tg, int id) {
338 return getURI(tg.identities, id);
341 public static String getURI(Identity[] identities, int id) {
342 for(Identity identity : identities) {
343 if(identity.resource == id) {
344 IdentityDefinition definition = identity.definition;
345 if(definition instanceof External) {
346 External def = (External)definition;
347 if(def.parent == -1) return "http:/";
348 else return getURI(identities, def.parent) + "/" + URIStringUtils.escape(def.name);
349 } else if(definition instanceof Root) {
350 Root def = (Root)definition;
351 if(def.name.isEmpty()) return "http:/";
353 } else if (definition instanceof Internal) {
354 Internal def = (Internal)definition;
355 return getURI(identities, def.parent) + "/" + URIStringUtils.escape(def.name);
361 return "<internal reference " + id + ">:";
364 public static TIntObjectMap<Identity> mapIdentities(TransferableGraph1 tg) {
365 return mapIdentities(tg.identities);
368 public static TIntObjectMap<Identity> mapIdentities(Identity[] identities) {
369 // Integer.MIN_VALUE cannot be the value of Identity.resource
370 TIntObjectMap<Identity> map = new TIntObjectHashMap<>(identities.length, 0.5f, Integer.MIN_VALUE);
371 for (Identity id : identities)
372 map.put(id.resource, id);
376 public static String getURI(int resourceCount, TIntObjectMap<Identity> identities, int id) {
377 Identity identity = identities.get(id);
378 if(identity != null) {
379 IdentityDefinition definition = identity.definition;
380 if(definition instanceof External) {
381 External def = (External)definition;
382 if(def.parent == -1) return "http:/";
383 else return getURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name);
384 } else if(definition instanceof Root) {
385 Root def = (Root)definition;
386 if(def.name.isEmpty()) return "http:/";
388 } else if (definition instanceof Internal) {
389 Internal def = (Internal)definition;
390 return getURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name);
395 return "<internal reference " + id + ">:";