]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java
Merge "Revert "Default property editing restores assertions""
[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     /**
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
152      */
153         @Deprecated
154         public static int getPossibleObject(TransferableGraph1 tg, int subject, Identity predicate) {
155                 int result = 0;
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];
160                         }
161                 }
162                 return result;
163         }
164
165         public static final int NOT_FOUND = -2;
166
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)
172                     return NOT_FOUND;
173                 result = tg.statements[i+3];
174             }
175         }
176         return result;
177         }
178         
179         /**
180          * @return 0 for presenting not found which is BAD
181          * @see getPossibleObject2
182          */
183         @Deprecated
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);
188         }
189         
190     public static int getPossibleObject2(TransferableGraph1 tg, Identity subject, String predicate) {
191         Identity p = findExternal(tg, predicate);
192         if (p == null)
193             return NOT_FOUND;
194         return getPossibleObject2(tg, subject.resource, p);
195     }
196
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);
203                         }
204                 }
205                 return result;
206         }
207
208         public static String getName(TransferableGraph1 tg, Identity id) {
209                 return getName(id);
210         }
211
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;
221                         return root.name;
222                 } else  {
223                         Optional optional = (Optional)id.definition;
224                         return optional.name;
225                 }
226         }
227
228         public static String getRootType(Identity id) {
229                 if(id.definition instanceof Root) {
230                         Root root = (Root)id.definition;
231                         return root.type;
232                 } else  {
233                         throw new IllegalArgumentException("Expected root, got " + id);
234                 }
235         }
236
237         public static Value findValue(TransferableGraph1 tg, int subject) {
238                 for(Value v : tg.values) {
239                         if(v.resource == subject) return v;
240                 }
241                 return null;
242         }
243         
244         public static String getURI(TransferableGraph1 tg, int id) {
245             return getURI(tg.resourceCount, tg.identities, id);
246         }
247         
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:/";
259                         return def.name;
260                     } else if (definition instanceof Internal) {
261                         Internal def = (Internal)definition;
262                         return getURI(resourceCount, identities, def.parent) + "/" + def.name;
263                     } else {
264                         return "";
265                     }
266                 }
267             }       
268             return "<internal reference " + id + ">:";
269         }
270
271         public static TIntObjectMap<Identity> mapIdentities(TransferableGraph1 tg) {
272                 return mapIdentities(tg.identities);
273         }
274
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);
280                 return map;
281         }
282
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:/";
294                                 return def.name;
295                         } else if (definition instanceof Internal) {
296                                 Internal def = (Internal)definition;
297                                 return getURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name);
298                         } else {
299                                 return "";
300                         }
301                 }
302                 return "<internal reference " + id + ">:";
303         }
304
305 }