]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java
Import/export changes. A load.
[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
12 import gnu.trove.list.array.TIntArrayList;
13 import gnu.trove.map.TIntObjectMap;
14 import gnu.trove.map.hash.TIntObjectHashMap;
15
16 public class TransferableGraphUtils {
17
18         public static Collection<Identity> getRoots(TransferableGraph1 tg) {
19                 
20                 ArrayList<Identity> result = new ArrayList<Identity>();
21                 for(Identity id : tg.identities) {
22                         if(id.definition instanceof Root) result.add(id);
23                 }
24                 return result;
25                 
26         }
27         
28         public static Identity findRootWithName(TransferableGraph1 tg, String name) {
29                 
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;
34                         }
35                 }
36                 return null;
37                 
38         }
39
40         public static Identity findExternalWithName(TransferableGraph1 tg, String name) {
41                 
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;
46                         }
47                 }
48                 return null;
49                 
50         }
51
52         public static Identity findExternalWithNameAndParent(TransferableGraph1 tg, int parent, String name) {
53                 
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;
58                         }
59                 }
60                 return null;
61                 
62         }
63         
64         public static Identity findExternal(TransferableGraph1 tg, String uri) {
65                 
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);
73                 }
74                 return identity;
75                 
76         }
77         
78         public static Identity getIdentity(TransferableGraph1 tg, int resource) {
79                 for(Identity id : tg.identities) {
80                         if(id.resource == resource) return id;
81                 }
82                 return null;
83         }
84         
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]);
91                         }
92                 }
93                 return result;
94         }
95         
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);
102                         }
103                 }
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);
114                                                 }
115                                         } else {
116                                                 int possibleNameResource = getPossibleObject(tg, tg.statements[i+3], hasName);
117                                                 if(possibleNameResource != 0) {
118                                                         Value value = findValue(tg, possibleNameResource);
119                                                         if(value != null) {
120                                                                 try {
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) {
124                                                                         e.printStackTrace();
125                                                                 }
126                                                         }
127                                                 }
128                                         }
129                                 }
130                         }
131                 }
132                 return result.values();
133         }
134         
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]);
140                         }
141                 }
142                 return result;
143         }
144         
145         public static int getPossibleObject(TransferableGraph1 tg, int subject, Identity predicate) {
146                 int result = 0;
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];
151                         }
152                 }
153                 return result;
154         }
155         
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);
160         }
161
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);
168                         }
169                 }
170                 return result;
171         }
172
173         public static String getName(TransferableGraph1 tg, Identity id) {
174                 return getName(id);
175         }
176
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;
186                         return root.name;
187                 } else  {
188                         Optional optional = (Optional)id.definition;
189                         return optional.name;
190                 }
191         }
192
193         public static String getRootType(Identity id) {
194                 if(id.definition instanceof Root) {
195                         Root root = (Root)id.definition;
196                         return root.type;
197                 } else  {
198                         throw new IllegalArgumentException("Expected root, got " + id);
199                 }
200         }
201
202         public static Value findValue(TransferableGraph1 tg, int subject) {
203                 for(Value v : tg.values) {
204                         if(v.resource == subject) return v;
205                 }
206                 return null;
207         }
208         
209         public static String getURI(TransferableGraph1 tg, int id) {
210             return getURI(tg.resourceCount, tg.identities, id);
211         }
212         
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:/";
224                         return def.name;
225                     } else if (definition instanceof Internal) {
226                         Internal def = (Internal)definition;
227                         return getURI(resourceCount, identities, def.parent) + "/" + def.name;
228                     } else {
229                         return "";
230                     }
231                 }
232             }       
233             return "<internal reference " + id + ">:";
234         }
235
236         public static TIntObjectMap<Identity> mapIdentities(TransferableGraph1 tg) {
237                 return mapIdentities(tg.identities);
238         }
239
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);
245                 return map;
246         }
247
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:/";
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                 return "<internal reference " + id + ">:";
268         }
269
270 }