]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java
Fixed URI problems related to EnternalEntities during 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                 }
75                 return identity;
76                 
77         }
78         
79         public static Identity getIdentity(TransferableGraph1 tg, int resource) {
80                 for(Identity id : tg.identities) {
81                         if(id.resource == resource) return id;
82                 }
83                 return null;
84         }
85         
86         public static TIntArrayList getStatements(TransferableGraph1 tg, int resource) {
87                 TIntArrayList result = new TIntArrayList();
88                 for(int i=0;i<tg.statements.length;i+=4) {
89                         if(tg.statements[i] == resource) {
90                                 result.add(tg.statements[i+1]);
91                                 result.add(tg.statements[i+3]);
92                         }
93                 }
94                 return result;
95         }
96         
97         public static Collection<Identity> getChildren(TransferableGraph1 tg, Identity parent) {
98                 TreeMap<String,Identity> result = new TreeMap<>();
99                 for(Identity id : tg.identities) {
100                         if(id.definition instanceof Internal) {
101                                 Internal internal = (Internal)id.definition;
102                                 if(internal.parent == parent.resource) result.put(internal.name, id);
103                         }
104                 }
105                 Identity consistsOf = findExternal(tg, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
106                 Identity hasName = findExternal(tg, "http://www.simantics.org/Layer0-1.1/HasName");
107                 for(int i=0;i<tg.statements.length;i+=4) {
108                         if(tg.statements[i] == parent.resource) {
109                                 if(tg.statements[i+1] == consistsOf.resource) {
110                                         Identity identity = getIdentity(tg, tg.statements[i+3]);
111                                         if(identity != null) {
112                                                 if(identity.definition instanceof Internal) {
113                                                         Internal internal = (Internal)identity.definition;
114                                                         result.put(internal.name, identity);
115                                                 }
116                                         } else {
117                                                 int possibleNameResource = getPossibleObject(tg, tg.statements[i+3], hasName);
118                                                 if(possibleNameResource != 0) {
119                                                         Value value = findValue(tg, possibleNameResource);
120                                                         if(value != null) {
121                                                                 try {
122                                                                         String name = (String)value.value.getValue(Bindings.STRING);
123                                                                         result.put(name, new Identity(tg.statements[i+3], new Internal(tg.statements[i], name)));
124                                                                 } catch (AdaptException e) {
125                                                                         e.printStackTrace();
126                                                                 }
127                                                         }
128                                                 }
129                                         }
130                                 }
131                         }
132                 }
133                 return result.values();
134         }
135         
136         public static TIntArrayList getObjects(TransferableGraph1 tg, int subject, Identity predicate) {
137                 TIntArrayList result = new TIntArrayList();
138                 for(int i=0;i<tg.statements.length;i+=4) {
139                         if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
140                                 result.add(tg.statements[i+3]);
141                         }
142                 }
143                 return result;
144         }
145         
146         public static int getPossibleObject(TransferableGraph1 tg, int subject, Identity predicate) {
147                 int result = 0;
148                 for(int i=0;i<tg.statements.length;i+=4) {
149                         if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
150                                 if(result != 0) return 0;
151                                 result = tg.statements[i+3];
152                         }
153                 }
154                 return result;
155         }
156         
157         public static int getPossibleObject(TransferableGraph1 tg, Identity subject, String predicate) {
158                 Identity p = findExternal(tg, predicate);
159                 if(p == null) return 0;
160                 return getPossibleObject(tg, subject.resource, p);
161         }
162
163         public static Map<Identity, String> getNames(TransferableGraph1 tg, Collection<Identity> ids) {
164                 Map<Identity, String> result = new HashMap<Identity, String>();
165                 for(Identity id : ids) {
166                         if(id.definition instanceof Internal) {
167                                 Internal internal = (Internal)id.definition;
168                                 result.put(id, internal.name);
169                         }
170                 }
171                 return result;
172         }
173
174         public static String getName(TransferableGraph1 tg, Identity id) {
175                 return getName(id);
176         }
177
178         public static String getName(Identity id) {
179                 if(id.definition instanceof Internal) {
180                         Internal internal = (Internal)id.definition;
181                         return internal.name;
182                 } else if(id.definition instanceof External) {
183                         External external = (External)id.definition;
184                         return external.name;
185                 } else if(id.definition instanceof Root) {
186                         Root root = (Root)id.definition;
187                         return root.name;
188                 } else  {
189                         Optional optional = (Optional)id.definition;
190                         return optional.name;
191                 }
192         }
193
194         public static String getRootType(Identity id) {
195                 if(id.definition instanceof Root) {
196                         Root root = (Root)id.definition;
197                         return root.type;
198                 } else  {
199                         throw new IllegalArgumentException("Expected root, got " + id);
200                 }
201         }
202
203         public static Value findValue(TransferableGraph1 tg, int subject) {
204                 for(Value v : tg.values) {
205                         if(v.resource == subject) return v;
206                 }
207                 return null;
208         }
209         
210         public static String getURI(TransferableGraph1 tg, int id) {
211             return getURI(tg.resourceCount, tg.identities, id);
212         }
213         
214         public static String getURI(int resourceCount, Identity[] identities, int id) {
215             for(Identity identity : identities) {
216                 if(identity.resource == id) {
217                     IdentityDefinition definition = identity.definition;
218                     if(definition instanceof External) {
219                         External def = (External)definition;
220                         if(def.parent == -1) return "http:/";
221                         else return getURI(resourceCount, identities, def.parent) + "/" + def.name;
222                     } else if(definition instanceof Root) {
223                         Root def = (Root)definition;
224                         if(def.name.isEmpty()) return "http:/";
225                         return def.name;
226                     } else if (definition instanceof Internal) {
227                         Internal def = (Internal)definition;
228                         return getURI(resourceCount, identities, def.parent) + "/" + def.name;
229                     } else {
230                         return "";
231                     }
232                 }
233             }       
234             return "<internal reference " + id + ">:";
235         }
236
237         public static TIntObjectMap<Identity> mapIdentities(TransferableGraph1 tg) {
238                 return mapIdentities(tg.identities);
239         }
240
241         public static TIntObjectMap<Identity> mapIdentities(Identity[] identities) {
242                 // Integer.MIN_VALUE cannot be the value of Identity.resource
243                 TIntObjectMap<Identity> map = new TIntObjectHashMap<>(identities.length, 0.5f, Integer.MIN_VALUE);
244                 for (Identity id : identities)
245                         map.put(id.resource, id);
246                 return map;
247         }
248
249         public static String getURI(int resourceCount, TIntObjectMap<Identity> identities, int id) {
250                 Identity identity = identities.get(id);
251                 if(identity != null) {
252                         IdentityDefinition definition = identity.definition;
253                         if(definition instanceof External) {
254                                 External def = (External)definition;
255                                 if(def.parent == -1) return "http:/";
256                                 else return getURI(resourceCount, identities, def.parent) + "/" + def.name;
257                         } else if(definition instanceof Root) {
258                                 Root def = (Root)definition;
259                                 if(def.name.isEmpty()) return "http:/";
260                                 return def.name;
261                         } else if (definition instanceof Internal) {
262                                 Internal def = (Internal)definition;
263                                 return getURI(resourceCount, identities, def.parent) + "/" + def.name;
264                         } else {
265                                 return "";
266                         }
267                 }
268                 return "<internal reference " + id + ">:";
269         }
270         
271         public static String getTrueURI(int resourceCount, TIntObjectMap<Identity> identities, int id) {
272                 Identity identity = identities.get(id);
273                 if(identity != null) {
274                         IdentityDefinition definition = identity.definition;
275                         if(definition instanceof External) {
276                                 External def = (External)definition;
277                                 if(def.parent == -1) return "http:/";
278                                 else return getTrueURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name);
279                         } else if(definition instanceof Root) {
280                                 Root def = (Root)definition;
281                                 if(def.name.isEmpty()) return "http:/";
282                                 return def.name;
283                         } else if (definition instanceof Internal) {
284                                 Internal def = (Internal)definition;
285                                 return getTrueURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name);
286                         } else {
287                                 return "";
288                         }
289                 }
290                 return "<internal reference " + id + ">:";
291         }
292
293 }