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);
74 if (identity == null) {
82 public static Identity getIdentity(TransferableGraph1 tg, int resource) {
83 for(Identity id : tg.identities) {
84 if(id.resource == resource) return id;
89 public static TIntArrayList getStatements(TransferableGraph1 tg, int resource) {
90 TIntArrayList result = new TIntArrayList();
91 for(int i=0;i<tg.statements.length;i+=4) {
92 if(tg.statements[i] == resource) {
93 result.add(tg.statements[i+1]);
94 result.add(tg.statements[i+3]);
100 public static Collection<Identity> getChildren(TransferableGraph1 tg, Identity parent) {
101 TreeMap<String,Identity> result = new TreeMap<>();
102 for(Identity id : tg.identities) {
103 if(id.definition instanceof Internal) {
104 Internal internal = (Internal)id.definition;
105 if(internal.parent == parent.resource) result.put(internal.name, id);
108 Identity consistsOf = findExternal(tg, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
109 Identity hasName = findExternal(tg, "http://www.simantics.org/Layer0-1.1/HasName");
110 for(int i=0;i<tg.statements.length;i+=4) {
111 if(tg.statements[i] == parent.resource) {
112 if(tg.statements[i+1] == consistsOf.resource) {
113 Identity identity = getIdentity(tg, tg.statements[i+3]);
114 if(identity != null) {
115 if(identity.definition instanceof Internal) {
116 Internal internal = (Internal)identity.definition;
117 result.put(internal.name, identity);
120 int possibleNameResource = getPossibleObject(tg, tg.statements[i+3], hasName);
121 if(possibleNameResource != 0) {
122 Value value = findValue(tg, possibleNameResource);
125 String name = (String)value.value.getValue(Bindings.STRING);
126 result.put(name, new Identity(tg.statements[i+3], new Internal(tg.statements[i], name)));
127 } catch (AdaptException e) {
136 return result.values();
139 public static TIntArrayList getObjects(TransferableGraph1 tg, int subject, Identity predicate) {
140 TIntArrayList result = new TIntArrayList();
141 for(int i=0;i<tg.statements.length;i+=4) {
142 if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
143 result.add(tg.statements[i+3]);
150 * This implementation is no longer advised to use because it returns 0 as
151 * NOT_FOUND which is in fact a valid ID for resource in graph
154 public static int getPossibleObject(TransferableGraph1 tg, int subject, Identity predicate) {
156 for(int i=0;i<tg.statements.length;i+=4) {
157 if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
158 if(result != 0 && tg.statements[i+3] != result) return 0;
159 result = tg.statements[i+3];
165 public static final int NOT_FOUND = -2;
167 public static int getPossibleObject2(TransferableGraph1 tg, int subject, Identity predicate) {
168 int result = NOT_FOUND;
169 for(int i=0;i<tg.statements.length;i+=4) {
170 if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
171 if(result != NOT_FOUND && tg.statements[i+3] != result)
173 result = tg.statements[i+3];
180 * @return 0 for presenting not found which is BAD
181 * @see getPossibleObject2
184 public static int getPossibleObject(TransferableGraph1 tg, Identity subject, String predicate) {
185 Identity p = findExternal(tg, predicate);
186 if(p == null) return 0;
187 return getPossibleObject(tg, subject.resource, p);
190 public static int getPossibleObject2(TransferableGraph1 tg, Identity subject, String predicate) {
191 Identity p = findExternal(tg, predicate);
194 return getPossibleObject2(tg, subject.resource, p);
197 public static Map<Identity, String> getNames(TransferableGraph1 tg, Collection<Identity> ids) {
198 Map<Identity, String> result = new HashMap<Identity, String>();
199 for(Identity id : ids) {
200 if(id.definition instanceof Internal) {
201 Internal internal = (Internal)id.definition;
202 result.put(id, internal.name);
208 public static String getName(TransferableGraph1 tg, Identity id) {
212 public static String getName(Identity id) {
213 if(id.definition instanceof Internal) {
214 Internal internal = (Internal)id.definition;
215 return internal.name;
216 } else if(id.definition instanceof External) {
217 External external = (External)id.definition;
218 return external.name;
219 } else if(id.definition instanceof Root) {
220 Root root = (Root)id.definition;
223 Optional optional = (Optional)id.definition;
224 return optional.name;
228 public static String getRootType(Identity id) {
229 if(id.definition instanceof Root) {
230 Root root = (Root)id.definition;
233 throw new IllegalArgumentException("Expected root, got " + id);
237 public static Value findValue(TransferableGraph1 tg, int subject) {
238 for(Value v : tg.values) {
239 if(v.resource == subject) return v;
244 public static String getURI(TransferableGraph1 tg, int id) {
245 return getURI(tg.resourceCount, tg.identities, id);
248 public static String getURI(int resourceCount, Identity[] identities, int id) {
249 for(Identity identity : identities) {
250 if(identity.resource == id) {
251 IdentityDefinition definition = identity.definition;
252 if(definition instanceof External) {
253 External def = (External)definition;
254 if(def.parent == -1) return "http:/";
255 else return getURI(resourceCount, identities, def.parent) + "/" + def.name;
256 } else if(definition instanceof Root) {
257 Root def = (Root)definition;
258 if(def.name.isEmpty()) return "http:/";
260 } else if (definition instanceof Internal) {
261 Internal def = (Internal)definition;
262 return getURI(resourceCount, identities, def.parent) + "/" + def.name;
268 return "<internal reference " + id + ">:";
271 public static TIntObjectMap<Identity> mapIdentities(TransferableGraph1 tg) {
272 return mapIdentities(tg.identities);
275 public static TIntObjectMap<Identity> mapIdentities(Identity[] identities) {
276 // Integer.MIN_VALUE cannot be the value of Identity.resource
277 TIntObjectMap<Identity> map = new TIntObjectHashMap<>(identities.length, 0.5f, Integer.MIN_VALUE);
278 for (Identity id : identities)
279 map.put(id.resource, id);
283 public static String getURI(int resourceCount, TIntObjectMap<Identity> identities, int id) {
284 Identity identity = identities.get(id);
285 if(identity != null) {
286 IdentityDefinition definition = identity.definition;
287 if(definition instanceof External) {
288 External def = (External)definition;
289 if(def.parent == -1) return "http:/";
290 else return getURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name);
291 } else if(definition instanceof Root) {
292 Root def = (Root)definition;
293 if(def.name.isEmpty()) return "http:/";
295 } else if (definition instanceof Internal) {
296 Internal def = (Internal)definition;
297 return getURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name);
302 return "<internal reference " + id + ">:";