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]);
149 public static int getPossibleObject(TransferableGraph1 tg, int subject, Identity predicate) {
151 for(int i=0;i<tg.statements.length;i+=4) {
152 if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
153 if(result != 0) return 0;
154 result = tg.statements[i+3];
160 public static int getPossibleObject(TransferableGraph1 tg, Identity subject, String predicate) {
161 Identity p = findExternal(tg, predicate);
162 if(p == null) return 0;
163 return getPossibleObject(tg, subject.resource, p);
166 public static Map<Identity, String> getNames(TransferableGraph1 tg, Collection<Identity> ids) {
167 Map<Identity, String> result = new HashMap<Identity, String>();
168 for(Identity id : ids) {
169 if(id.definition instanceof Internal) {
170 Internal internal = (Internal)id.definition;
171 result.put(id, internal.name);
177 public static String getName(TransferableGraph1 tg, Identity id) {
181 public static String getName(Identity id) {
182 if(id.definition instanceof Internal) {
183 Internal internal = (Internal)id.definition;
184 return internal.name;
185 } else if(id.definition instanceof External) {
186 External external = (External)id.definition;
187 return external.name;
188 } else if(id.definition instanceof Root) {
189 Root root = (Root)id.definition;
192 Optional optional = (Optional)id.definition;
193 return optional.name;
197 public static String getRootType(Identity id) {
198 if(id.definition instanceof Root) {
199 Root root = (Root)id.definition;
202 throw new IllegalArgumentException("Expected root, got " + id);
206 public static Value findValue(TransferableGraph1 tg, int subject) {
207 for(Value v : tg.values) {
208 if(v.resource == subject) return v;
213 public static String getURI(TransferableGraph1 tg, int id) {
214 return getURI(tg.resourceCount, tg.identities, id);
217 public static String getURI(int resourceCount, Identity[] identities, int id) {
218 for(Identity identity : identities) {
219 if(identity.resource == id) {
220 IdentityDefinition definition = identity.definition;
221 if(definition instanceof External) {
222 External def = (External)definition;
223 if(def.parent == -1) return "http:/";
224 else return getURI(resourceCount, identities, def.parent) + "/" + def.name;
225 } else if(definition instanceof Root) {
226 Root def = (Root)definition;
227 if(def.name.isEmpty()) return "http:/";
229 } else if (definition instanceof Internal) {
230 Internal def = (Internal)definition;
231 return getURI(resourceCount, identities, def.parent) + "/" + def.name;
237 return "<internal reference " + id + ">:";
240 public static TIntObjectMap<Identity> mapIdentities(TransferableGraph1 tg) {
241 return mapIdentities(tg.identities);
244 public static TIntObjectMap<Identity> mapIdentities(Identity[] identities) {
245 // Integer.MIN_VALUE cannot be the value of Identity.resource
246 TIntObjectMap<Identity> map = new TIntObjectHashMap<>(identities.length, 0.5f, Integer.MIN_VALUE);
247 for (Identity id : identities)
248 map.put(id.resource, id);
252 public static String getURI(int resourceCount, TIntObjectMap<Identity> identities, int id) {
253 Identity identity = identities.get(id);
254 if(identity != null) {
255 IdentityDefinition definition = identity.definition;
256 if(definition instanceof External) {
257 External def = (External)definition;
258 if(def.parent == -1) return "http:/";
259 else return getURI(resourceCount, identities, def.parent) + "/" + def.name;
260 } else if(definition instanceof Root) {
261 Root def = (Root)definition;
262 if(def.name.isEmpty()) return "http:/";
264 } else if (definition instanceof Internal) {
265 Internal def = (Internal)definition;
266 return getURI(resourceCount, identities, def.parent) + "/" + def.name;
271 return "<internal reference " + id + ">:";
274 public static String getTrueURI(int resourceCount, TIntObjectMap<Identity> identities, int id) {
275 Identity identity = identities.get(id);
276 if(identity != null) {
277 IdentityDefinition definition = identity.definition;
278 if(definition instanceof External) {
279 External def = (External)definition;
280 if(def.parent == -1) return "http:/";
281 else return getTrueURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name);
282 } else if(definition instanceof Root) {
283 Root def = (Root)definition;
284 if(def.name.isEmpty()) return "http:/";
286 } else if (definition instanceof Internal) {
287 Internal def = (Internal)definition;
288 return getTrueURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name);
293 return "<internal reference " + id + ">:";