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;
12 import gnu.trove.list.array.TIntArrayList;
13 import gnu.trove.map.TIntObjectMap;
14 import gnu.trove.map.hash.TIntObjectHashMap;
16 public class TransferableGraphUtils {
18 public static Collection<Identity> getRoots(TransferableGraph1 tg) {
20 ArrayList<Identity> result = new ArrayList<Identity>();
21 for(Identity id : tg.identities) {
22 if(id.definition instanceof Root) result.add(id);
28 public static Identity findRootWithName(TransferableGraph1 tg, String name) {
30 for(Identity id : tg.identities) {
31 if(id.definition instanceof Root) {
32 Root ext = (Root)id.definition;
33 if(ext.name.equals(name)) return id;
40 public static Identity findExternalWithName(TransferableGraph1 tg, String name) {
42 for(Identity id : tg.identities) {
43 if(id.definition instanceof External) {
44 External ext = (External)id.definition;
45 if(ext.name.equals(name)) return id;
52 public static Identity findExternalWithNameAndParent(TransferableGraph1 tg, int parent, String name) {
54 for(Identity id : tg.identities) {
55 if(id.definition instanceof External) {
56 External ext = (External)id.definition;
57 if(ext.name.equals(name) && ext.parent == parent) return id;
64 public static Identity findExternal(TransferableGraph1 tg, String uri) {
66 Identity identity = findExternalWithName(tg, "http:/");
67 if(identity == null) identity = findExternalWithName(tg, "");
68 if(identity == null) identity = findRootWithName(tg, "");
69 if("http:/".equals(uri)) return identity;
70 String[] tokens = uri.substring("http://".length()).split("/");
71 for(String token : tokens) {
72 identity = findExternalWithNameAndParent(tg, identity.resource, token);
78 public static Identity getIdentity(TransferableGraph1 tg, int resource) {
79 for(Identity id : tg.identities) {
80 if(id.resource == resource) return id;
85 public static TIntArrayList getStatements(TransferableGraph1 tg, int resource) {
86 TIntArrayList result = new TIntArrayList();
87 for(int i=0;i<tg.statements.length;i+=4) {
88 if(tg.statements[i] == resource) {
89 result.add(tg.statements[i+1]);
90 result.add(tg.statements[i+3]);
96 public static Collection<Identity> getChildren(TransferableGraph1 tg, Identity parent) {
97 TreeMap<String,Identity> result = new TreeMap<>();
98 for(Identity id : tg.identities) {
99 if(id.definition instanceof Internal) {
100 Internal internal = (Internal)id.definition;
101 if(internal.parent == parent.resource) result.put(internal.name, id);
104 Identity consistsOf = findExternal(tg, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
105 Identity hasName = findExternal(tg, "http://www.simantics.org/Layer0-1.1/HasName");
106 for(int i=0;i<tg.statements.length;i+=4) {
107 if(tg.statements[i] == parent.resource) {
108 if(tg.statements[i+1] == consistsOf.resource) {
109 Identity identity = getIdentity(tg, tg.statements[i+3]);
110 if(identity != null) {
111 if(identity.definition instanceof Internal) {
112 Internal internal = (Internal)identity.definition;
113 result.put(internal.name, identity);
116 int possibleNameResource = getPossibleObject(tg, tg.statements[i+3], hasName);
117 if(possibleNameResource != 0) {
118 Value value = findValue(tg, possibleNameResource);
121 String name = (String)value.value.getValue(Bindings.STRING);
122 result.put(name, new Identity(tg.statements[i+3], new Internal(tg.statements[i], name)));
123 } catch (AdaptException e) {
132 return result.values();
135 public static TIntArrayList getObjects(TransferableGraph1 tg, int subject, Identity predicate) {
136 TIntArrayList result = new TIntArrayList();
137 for(int i=0;i<tg.statements.length;i+=4) {
138 if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
139 result.add(tg.statements[i+3]);
145 public static int getPossibleObject(TransferableGraph1 tg, int subject, Identity predicate) {
147 for(int i=0;i<tg.statements.length;i+=4) {
148 if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
149 if(result != 0) return 0;
150 result = tg.statements[i+3];
156 public static int getPossibleObject(TransferableGraph1 tg, Identity subject, String predicate) {
157 Identity p = findExternal(tg, predicate);
158 if(p == null) return 0;
159 return getPossibleObject(tg, subject.resource, p);
162 public static Map<Identity, String> getNames(TransferableGraph1 tg, Collection<Identity> ids) {
163 Map<Identity, String> result = new HashMap<Identity, String>();
164 for(Identity id : ids) {
165 if(id.definition instanceof Internal) {
166 Internal internal = (Internal)id.definition;
167 result.put(id, internal.name);
173 public static String getName(TransferableGraph1 tg, Identity id) {
177 public static String getName(Identity id) {
178 if(id.definition instanceof Internal) {
179 Internal internal = (Internal)id.definition;
180 return internal.name;
181 } else if(id.definition instanceof External) {
182 External external = (External)id.definition;
183 return external.name;
184 } else if(id.definition instanceof Root) {
185 Root root = (Root)id.definition;
188 Optional optional = (Optional)id.definition;
189 return optional.name;
193 public static String getRootType(Identity id) {
194 if(id.definition instanceof Root) {
195 Root root = (Root)id.definition;
198 throw new IllegalArgumentException("Expected root, got " + id);
202 public static Value findValue(TransferableGraph1 tg, int subject) {
203 for(Value v : tg.values) {
204 if(v.resource == subject) return v;
209 public static String getURI(TransferableGraph1 tg, int id) {
210 return getURI(tg.resourceCount, tg.identities, id);
213 public static String getURI(int resourceCount, Identity[] identities, int id) {
214 for(Identity identity : identities) {
215 if(identity.resource == id) {
216 IdentityDefinition definition = identity.definition;
217 if(definition instanceof External) {
218 External def = (External)definition;
219 if(def.parent == -1) return "http:/";
220 else return getURI(resourceCount, identities, def.parent) + "/" + def.name;
221 } else if(definition instanceof Root) {
222 Root def = (Root)definition;
223 if(def.name.isEmpty()) return "http:/";
225 } else if (definition instanceof Internal) {
226 Internal def = (Internal)definition;
227 return getURI(resourceCount, identities, def.parent) + "/" + def.name;
233 return "<internal reference " + id + ">:";
236 public static TIntObjectMap<Identity> mapIdentities(TransferableGraph1 tg) {
237 return mapIdentities(tg.identities);
240 public static TIntObjectMap<Identity> mapIdentities(Identity[] identities) {
241 // Integer.MIN_VALUE cannot be the value of Identity.resource
242 TIntObjectMap<Identity> map = new TIntObjectHashMap<>(identities.length, 0.5f, Integer.MIN_VALUE);
243 for (Identity id : identities)
244 map.put(id.resource, id);
248 public static String getURI(int resourceCount, TIntObjectMap<Identity> identities, int id) {
249 Identity identity = identities.get(id);
250 if(identity != null) {
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;
267 return "<internal reference " + id + ">:";