]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java
Use proper URI encoding/decoding in fail-safe import
[simantics/platform.git] / bundles / org.simantics.graph / src / org / simantics / graph / representation / TransferableGraphUtils.java
1 package org.simantics.graph.representation;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.TreeMap;
8
9 import org.simantics.databoard.Bindings;
10 import org.simantics.databoard.adapter.AdaptException;
11 import org.simantics.databoard.util.URIStringUtils;
12
13 import gnu.trove.list.array.TIntArrayList;
14 import gnu.trove.map.TIntObjectMap;
15 import gnu.trove.map.hash.TIntObjectHashMap;
16
17 public class TransferableGraphUtils {
18
19         public static Collection<Identity> getRoots(TransferableGraph1 tg) {
20                 
21                 ArrayList<Identity> result = new ArrayList<Identity>();
22                 for(Identity id : tg.identities) {
23                         if(id.definition instanceof Root) result.add(id);
24                 }
25                 return result;
26                 
27         }
28         
29         public static Identity findRootWithName(TransferableGraph1 tg, String name) {
30                 
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;
35                         }
36                 }
37                 return null;
38                 
39         }
40
41         public static Identity findExternalWithName(TransferableGraph1 tg, String name) {
42                 
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;
47                         }
48                 }
49                 return null;
50                 
51         }
52
53         public static Identity findExternalWithNameAndParent(TransferableGraph1 tg, int parent, String name) {
54                 
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;
59                         }
60                 }
61                 return null;
62                 
63         }
64         
65         public static Identity findExternal(TransferableGraph1 tg, String uri) {
66                 
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) {
75                             return null;
76                         }
77                 }
78                 return identity;
79                 
80         }
81         
82         public static Identity getIdentity(TransferableGraph1 tg, int resource) {
83                 for(Identity id : tg.identities) {
84                         if(id.resource == resource) return id;
85                 }
86                 return null;
87         }
88         
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]);
95                         }
96                 }
97                 return result;
98         }
99         
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);
106                         }
107                 }
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);
118                                                 }
119                                         } else {
120                                                 int possibleNameResource = getPossibleObject(tg, tg.statements[i+3], hasName);
121                                                 if(possibleNameResource != 0) {
122                                                         Value value = findValue(tg, possibleNameResource);
123                                                         if(value != null) {
124                                                                 try {
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) {
128                                                                         e.printStackTrace();
129                                                                 }
130                                                         }
131                                                 }
132                                         }
133                                 }
134                         }
135                 }
136                 return result.values();
137         }
138         
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]);
144                         }
145                 }
146                 return result;
147         }
148         
149         public static int getPossibleObject(TransferableGraph1 tg, int subject, Identity predicate) {
150                 int result = 0;
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];
155                         }
156                 }
157                 return result;
158         }
159         
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);
164         }
165
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);
172                         }
173                 }
174                 return result;
175         }
176
177         public static String getName(TransferableGraph1 tg, Identity id) {
178                 return getName(id);
179         }
180
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;
190                         return root.name;
191                 } else  {
192                         Optional optional = (Optional)id.definition;
193                         return optional.name;
194                 }
195         }
196
197         public static String getRootType(Identity id) {
198                 if(id.definition instanceof Root) {
199                         Root root = (Root)id.definition;
200                         return root.type;
201                 } else  {
202                         throw new IllegalArgumentException("Expected root, got " + id);
203                 }
204         }
205
206         public static Value findValue(TransferableGraph1 tg, int subject) {
207                 for(Value v : tg.values) {
208                         if(v.resource == subject) return v;
209                 }
210                 return null;
211         }
212         
213         public static String getURI(TransferableGraph1 tg, int id) {
214             return getURI(tg.resourceCount, tg.identities, id);
215         }
216         
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:/";
228                         return def.name;
229                     } else if (definition instanceof Internal) {
230                         Internal def = (Internal)definition;
231                         return getURI(resourceCount, identities, def.parent) + "/" + def.name;
232                     } else {
233                         return "";
234                     }
235                 }
236             }       
237             return "<internal reference " + id + ">:";
238         }
239
240         public static TIntObjectMap<Identity> mapIdentities(TransferableGraph1 tg) {
241                 return mapIdentities(tg.identities);
242         }
243
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);
249                 return map;
250         }
251
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) + "/" + URIStringUtils.escape(def.name);
260                         } else if(definition instanceof Root) {
261                                 Root def = (Root)definition;
262                                 if(def.name.isEmpty()) return "http:/";
263                                 return def.name;
264                         } else if (definition instanceof Internal) {
265                                 Internal def = (Internal)definition;
266                                 return getURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name);
267                         } else {
268                                 return "";
269                         }
270                 }
271                 return "<internal reference " + id + ">:";
272         }
273
274 }