]> gerrit.simantics Code Review - simantics/platform.git/blob
6a69e8d921754fe797c134f0162eb4967b712124
[simantics/platform.git] /
1 package org.simantics.graph.db.old;
2
3 import java.io.DataOutput;
4 import java.io.DataOutputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.util.HashSet;
9 import java.util.Map;
10 import java.util.Set;
11
12 import org.simantics.databoard.Bindings;
13 import org.simantics.databoard.adapter.AdaptException;
14 import org.simantics.databoard.binding.mutable.Variant;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.VirtualGraph;
18 import org.simantics.db.WriteOnlyGraph;
19 import org.simantics.db.common.WriteBindings;
20 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
21 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
22 import org.simantics.db.common.utils.Logger;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.exception.ResourceNotFoundException;
25 import org.simantics.db.service.ClusterBuilder;
26 import org.simantics.db.service.ClusterBuilder.ResourceHandle;
27 import org.simantics.db.service.SerialisationSupport;
28 import org.simantics.db.service.TransferableGraphSupport;
29 import org.simantics.graph.db.CoreInitialization;
30 import org.simantics.graph.db.IImportAdvisor;
31 import org.simantics.graph.representation.Extensions;
32 import org.simantics.graph.representation.External;
33 import org.simantics.graph.representation.Identity;
34 import org.simantics.graph.representation.IdentityDefinition;
35 import org.simantics.graph.representation.Internal;
36 import org.simantics.graph.representation.Optional;
37 import org.simantics.graph.representation.Root;
38 import org.simantics.graph.representation.TransferableGraphUtils;
39 import org.simantics.graph.representation.old.OldTransferableGraph1;
40 import org.simantics.graph.representation.old.OldValue1;
41
42 public class OldTransferableGraphImportProcess1 {
43         
44     public static String LOG_FILE = "transferableGraphs.log";
45     final static private boolean LOG = false;
46     
47     static DataOutput log;
48
49     static {
50
51         if (LOG) {
52             try {
53                 FileOutputStream stream = new FileOutputStream(LOG_FILE);
54                 log = new DataOutputStream(stream);
55             } catch (FileNotFoundException e) {
56                 e.printStackTrace();
57             }
58         }
59
60     }
61     
62     private static void log(String line) {
63         if (LOG) {
64             try {
65                 log.writeUTF(line + "\n");
66             } catch (IOException e) {
67                 e.printStackTrace();
68             }
69         }
70     }
71     
72     OldTransferableGraph1 tg;
73     IImportAdvisor advisor;
74     
75     Resource[] resources;
76     ResourceHandle[] handles;
77     
78     Set<String> missingExternals = new HashSet<String>(); 
79     
80     // Builtins
81     Resource RootLibrary;
82     Resource String;
83     Resource Library;
84     
85     Resource InstanceOf;
86     Resource ConsistsOf;
87     Resource PartOf;
88     Resource HasName;
89     Resource NameOf;    
90                 
91     public OldTransferableGraphImportProcess1(OldTransferableGraph1 tg, IImportAdvisor advisor) {
92         this.tg = tg;
93         this.advisor = advisor;
94         /*System.out.println("Transferable graph identities:");
95         for(Identity id : tg.identities) {
96             if(id.definition instanceof Internal) {
97                 Internal def = (Internal)id.definition;
98                 System.out.println("    internal " + def.name);
99             }
100             else if(id.definition instanceof External) {
101                 External def = (External)id.definition;
102                 System.out.println("    external " + def.name);
103             }
104         }*/
105     }
106     
107     public void findBuiltins(WriteOnlyGraph g) throws DatabaseException {
108         RootLibrary = g.getBuiltin("http:/");
109         String = g.getBuiltin(CoreInitialization.LAYER0 + "String");
110         Library = g.getBuiltin(CoreInitialization.LAYER0 + "Library");
111         InstanceOf = g.getBuiltin(CoreInitialization.LAYER0 + "InstanceOf");
112         ConsistsOf = g.getBuiltin(CoreInitialization.LAYER0 + "ConsistsOf");
113         PartOf = g.getBuiltin(CoreInitialization.LAYER0 + "PartOf");
114         HasName = g.getBuiltin(CoreInitialization.LAYER0 + "HasName");
115         NameOf = g.getBuiltin(CoreInitialization.LAYER0 + "NameOf");
116     }
117     
118     public void findBuiltins(ReadGraph g) throws DatabaseException {
119         RootLibrary = g.getBuiltin("http:/");
120         String = g.getBuiltin(CoreInitialization.LAYER0 + "String");
121         Library = g.getBuiltin(CoreInitialization.LAYER0 + "Library");
122         InstanceOf = g.getBuiltin(CoreInitialization.LAYER0 + "InstanceOf");
123         ConsistsOf = g.getBuiltin(CoreInitialization.LAYER0 + "ConsistsOf");
124         PartOf = g.getBuiltin(CoreInitialization.LAYER0 + "PartOf");
125         HasName = g.getBuiltin(CoreInitialization.LAYER0 + "HasName");
126         NameOf = g.getBuiltin(CoreInitialization.LAYER0 + "NameOf");
127     }
128
129     /* Preparation that is used when the core is empty. 
130      */
131     void initialPrepare(WriteOnlyGraph graph) throws DatabaseException {
132         findBuiltins(graph);
133         
134         resources = new Resource[tg.resourceCount];
135         
136         int Root = -1;
137         int SimanticsDomain = -1;
138         int Layer0 = -1;
139         
140         for(Identity identity : tg.identities) {
141             if(identity.definition instanceof Internal) {
142                 Internal def = (Internal)identity.definition;
143                 Resource res = null;
144                 if(def.parent == Layer0) {
145                     try {
146                         res = graph.getBuiltin(CoreInitialization.LAYER0 + def.name);
147                     } catch(ResourceNotFoundException e) {                                      
148                     }
149                 }
150                 else if(def.parent == SimanticsDomain) {
151                     if(def.name.equals("Layer0-1.1"))
152                         Layer0 = identity.resource;
153                 }
154                 else if(def.parent == Root) {
155                     if(def.name.equals("www.simantics.org"))
156                         SimanticsDomain = identity.resource;
157                 }
158
159                 if(res == null)
160                     res = createChild(graph, resources[def.parent], def.name);
161                 else
162                     createChild(graph, res, resources[def.parent], def.name);
163                 resources[identity.resource] = res;
164             }
165             else if(identity.definition instanceof Root) {
166                 Root = identity.resource;
167                 resources[identity.resource] = RootLibrary;             
168             } 
169         }
170     }
171     
172     void addMissing(String external) {
173         Set<String> removals = new HashSet<String>();
174         for(String ext : missingExternals) if(ext.startsWith(external)) return;
175         for(String ext : missingExternals) if(external.startsWith(ext)) removals.add(ext);
176         missingExternals.removeAll(removals);
177         missingExternals.add(external);
178     }
179     
180     void prepare(ReadGraph graph) throws DatabaseException {
181         findBuiltins(graph);
182         
183         Resource[] resources = new Resource[tg.resourceCount];
184         
185         for(Identity identity : tg.identities) {
186             IdentityDefinition definition = identity.definition;
187             if(definition instanceof External) {
188                 External def = (External)definition;
189                 if(def.parent == -1) {
190                     resources[identity.resource] = RootLibrary;
191                 } else {
192                     if("@inverse".equals(def.name)) {
193                         Resource parent = resources[def.parent];
194                         Resource child = graph.getInverse(parent);
195                         resources[identity.resource] = child;
196                     } else {
197                         Resource parent = resources[def.parent];
198                         // TODO: escape should be removed when names become well-behaving
199                         if(parent != null) {
200                             Resource child = graph
201                             .syncRequest(new UnescapedChildMapOfResource(parent),
202                                     new TransientCacheAsyncListener<Map<String, Resource>>())
203                                     .get(def.name);
204                             if(child == null) {
205                                 addMissing(graph.getURI(parent) + "/" + def.name);
206                             }
207                             resources[identity.resource] = child;
208                         } else {
209                             addMissing(TransferableGraphUtils.getURI(tg.resourceCount, tg.identities, def.parent) + "/" + def.name);
210                         }
211                     }
212                 }
213             }
214             else if(definition instanceof Internal) {
215                 // Do not do anything for now
216             }
217             else if(definition instanceof Root) {
218                 Root root = (Root)definition;
219                 if(root.name.equals(""))
220                     resources[identity.resource] = RootLibrary;
221                 else 
222                     advisor.analyzeRoot(graph, root);                   
223             }
224             else if(definition instanceof Optional) {
225                 External def = (External)definition;
226                 Resource parent = resources[def.parent];
227                 if(parent != null)
228                     resources[identity.resource] = 
229                         graph.syncRequest(new UnescapedChildMapOfResource(parent)).get(def.name);               
230             }
231         }       
232         
233         this.resources = resources;
234         
235         if(!missingExternals.isEmpty()) throw new OldMissingDependencyException(this);
236         
237     }
238
239     Resource createChild(WriteOnlyGraph graph, Resource parent, String name) throws DatabaseException {
240         Resource child = graph.newResource();
241         graph.claim(parent, ConsistsOf, PartOf, child);
242         Resource nameResource = graph.newResource();
243         graph.claim(nameResource, InstanceOf, null, String);
244         graph.claimValue(nameResource, name, WriteBindings.STRING);
245         graph.claim(child, HasName, NameOf, nameResource);
246         return child;
247     }
248     
249     void createChild(WriteOnlyGraph graph, Resource child, Resource parent, String name) throws DatabaseException {
250         graph.claim(parent, ConsistsOf, PartOf, child);
251         Resource nameResource = graph.newResource();
252         graph.claim(nameResource, InstanceOf, null, String);
253         graph.claimValue(nameResource, name, WriteBindings.STRING);
254         graph.claim(child, HasName, NameOf, nameResource);
255     }
256     
257     int[] getClustering() {
258         Variant v = tg.extensions.get(Extensions.CLUSTERING);
259         if(v == null) return null;
260         try {
261             return (int[])v.getValue(Bindings.INT_ARRAY);
262         } catch (AdaptException e) {
263             Logger.defaultLogError(e);
264             return null;
265         }
266     }
267     
268     void write(WriteOnlyGraph graph) throws DatabaseException {
269
270         Resource[] resources = this.resources;
271
272         this.handles = new ResourceHandle[resources.length];
273         
274         ResourceHandle[] handles = this.handles; 
275
276         int[] clustering = getClustering();
277
278         // Internal identities      
279         for(Identity identity : tg.identities) {
280             IdentityDefinition definition = identity.definition;
281             if(resources[identity.resource] != null)
282                 continue;
283             if(definition instanceof External) {
284                 // Already done everything
285             }
286             else if(definition instanceof Internal) {
287                 Internal def = (Internal)definition;
288                 resources[identity.resource] = 
289                     createChild(graph, resources[def.parent], def.name);
290             }
291             else if(definition instanceof Root) {
292                 Root root = (Root)definition;               
293                 resources[identity.resource] = advisor.createRoot(graph, root);                 
294             }
295             else if(definition instanceof Optional) {
296                 Optional def = (Optional)definition;
297                 Resource child = createChild(graph, resources[def.parent], def.name);
298                 graph.claim(child, InstanceOf, null, Library); // ???
299                 resources[identity.resource] = child;                   
300             }
301         }       
302
303         ClusterBuilder builder = graph.getService(ClusterBuilder.class);
304         SerialisationSupport ss = graph.getService(SerialisationSupport.class);
305
306         if(clustering != null) {
307             
308             int i = 0;
309             for(int c : clustering) {
310                 builder.newCluster();
311                 for(int r=0;r<c;r++, i++)
312                     if(resources[i] == null)
313                         handles[i] = builder.newResource();
314                     else 
315                         handles[i] = builder.resource(resources[i]);
316
317             }
318
319             for(;i<resources.length;++i)
320                 if(resources[i] == null)
321                     handles[i] = builder.newResource();
322                 else 
323                     handles[i] = builder.resource(resources[i]);
324             
325         } else {
326         
327             // Create blank resources
328             for(int i=0;i<resources.length;++i)
329                 if(resources[i] == null)
330                     handles[i] = builder.newResource();
331                 else 
332                     handles[i] = builder.resource(resources[i]);
333
334         }
335         
336         // Write statements
337         int[] statements = tg.statements;
338         
339         int internals = tg.resourceCount - tg.identities.length;
340         
341         for(int i=0;i<statements.length;i+=4) {
342
343             int sub = statements[i];
344             int pred = statements[i+1];
345             int inv = statements[i+2];
346             int obj = statements[i+3];
347
348             ResourceHandle subject = handles[sub];
349             ResourceHandle predicate = handles[pred];
350             ResourceHandle object = handles[obj];
351
352             if(resources[sub] == null) {
353                 subject.addStatement(graph, predicate, object); 
354             } else {
355                 graph.claim(
356                         handles[sub].resource(ss),
357                         handles[pred].resource(ss),
358                         null, handles[obj].resource(ss));
359             }
360             
361             if(inv >= 0) {
362                 
363                 if(resources[obj] == null) {
364                     ResourceHandle inverse = handles[inv];
365                     object.addStatement(graph, inverse, subject);   
366                 } else {
367                     graph.claim(
368                             handles[obj].resource(ss),
369                             handles[inv].resource(ss),
370                             null, handles[sub].resource(ss));
371                 }
372                 
373             }
374             
375             if(LOG) {
376                 log("[STATEMENT] " + resources[statements[i]].getResourceId() + ", " + resources[statements[i+1]].getResourceId() + ", " + resources[statements[i+3]].getResourceId());
377             }
378             
379         }
380                 
381         // Write values
382         TransferableGraphSupport tgSupport = 
383             graph.getService(TransferableGraphSupport.class);
384         VirtualGraph vg = graph.getProvider();
385         for(OldValue1 value : tg.values) {
386             int file = value.resource & 0x80000000;
387             int resource = value.resource & 0x7FFFFFFF;
388             if (file != 0) {
389                 graph.claimValue(handles[resource].resource(ss), value.value, Bindings.BYTE_ARRAY);
390             } else {
391                 if(resource < internals) {
392                     handles[resource].addValue(graph, value.value);
393                 } else {
394                     tgSupport.setValue(graph, handles[resource].resource(ss), vg, value.value);
395                 }
396                 //tgSupport.setValue(resources[resource], vg, value.value);
397             }
398         }
399     }
400     
401     void write2(WriteOnlyGraph graph) throws DatabaseException {
402         Resource[] resources = this.resources;
403         
404         // Internal identities      
405         for(Identity identity : tg.identities) {
406             IdentityDefinition definition = identity.definition;
407             if(resources[identity.resource] != null)
408                 continue;
409             if(definition instanceof External) {
410                 // Already done everything
411             }
412             else if(definition instanceof Internal) {
413                 Internal def = (Internal)definition;
414                 resources[identity.resource] = 
415                     createChild(graph, resources[def.parent], def.name);
416             }
417             else if(definition instanceof Root) {               
418                 Root root = (Root)definition;               
419                 resources[identity.resource] = advisor.createRoot(graph, root);                 
420             }
421             else if(definition instanceof Optional) {
422                 Optional def = (Optional)definition;
423                 Resource child = createChild(graph, resources[def.parent], def.name);
424                 graph.claim(child, InstanceOf, null, Library); // ???
425                 resources[identity.resource] = child;                   
426             }
427         }       
428         
429         // Create blank resources
430         for(int i=0;i<resources.length;++i)
431             if(resources[i] == null)
432                 resources[i] = graph.newResource();
433         
434         // Write statements
435         int[] statements = tg.statements;
436         
437         for(int i=0;i<statements.length;i+=4) {
438             int inv = statements[i+2];
439             graph.claim(
440                     resources[statements[i]],
441                     resources[statements[i+1]],
442                     inv < 0 ? null : resources[inv],
443                     resources[statements[i+3]]
444                     );
445             if(LOG) {
446                 log("[STATEMENT] " + resources[statements[i]].getResourceId() + ", " + resources[statements[i+1]].getResourceId() + ", " + resources[statements[i+3]].getResourceId());
447             }
448         }
449                 
450         // Write values
451         TransferableGraphSupport tgSupport = 
452             graph.getService(TransferableGraphSupport.class);
453         VirtualGraph vg = graph.getProvider();
454         for(OldValue1 value : tg.values) {
455             int file = value.resource & 0x80000000;
456             int resource = value.resource & 0x7FFFFFFF;
457             if (file != 0) {
458                 graph.claimValue(resources[resource], value.value, Bindings.BYTE_ARRAY);
459             } else {
460                 tgSupport.setValue(graph, resources[resource], vg, value.value);
461             }
462         }
463     }
464     
465     
466     public long[] getResourceIds(SerialisationSupport serializer) throws DatabaseException {
467         final int count = resources.length;
468         long[] resourceIds = new long[count];
469         if(handles != null) {
470             for(int i=0;i<count;++i)
471                 resourceIds[i] = serializer.getRandomAccessId(handles[i].resource(serializer));
472         } else {
473             for(int i=0;i<count;++i)
474                 resourceIds[i] = serializer.getRandomAccessId(resources[i]);
475         }
476         return resourceIds;
477     }
478     
479 }