1 package org.simantics.graph.db.old;
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;
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;
42 public class OldTransferableGraphImportProcess1 {
44 public static String LOG_FILE = "transferableGraphs.log";
45 final static private boolean LOG = false;
47 static DataOutput log;
53 FileOutputStream stream = new FileOutputStream(LOG_FILE);
54 log = new DataOutputStream(stream);
55 } catch (FileNotFoundException e) {
62 private static void log(String line) {
65 log.writeUTF(line + "\n");
66 } catch (IOException e) {
72 OldTransferableGraph1 tg;
73 IImportAdvisor advisor;
76 ResourceHandle[] handles;
78 Set<String> missingExternals = new HashSet<String>();
91 public OldTransferableGraphImportProcess1(OldTransferableGraph1 tg, IImportAdvisor advisor) {
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);
100 else if(id.definition instanceof External) {
101 External def = (External)id.definition;
102 System.out.println(" external " + def.name);
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");
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");
129 /* Preparation that is used when the core is empty.
131 void initialPrepare(WriteOnlyGraph graph) throws DatabaseException {
134 resources = new Resource[tg.resourceCount];
137 int SimanticsDomain = -1;
140 for(Identity identity : tg.identities) {
141 if(identity.definition instanceof Internal) {
142 Internal def = (Internal)identity.definition;
144 if(def.parent == Layer0) {
146 res = graph.getBuiltin(CoreInitialization.LAYER0 + def.name);
147 } catch(ResourceNotFoundException e) {
150 else if(def.parent == SimanticsDomain) {
151 if(def.name.equals("Layer0-1.1"))
152 Layer0 = identity.resource;
154 else if(def.parent == Root) {
155 if(def.name.equals("www.simantics.org"))
156 SimanticsDomain = identity.resource;
160 res = createChild(graph, resources[def.parent], def.name);
162 createChild(graph, res, resources[def.parent], def.name);
163 resources[identity.resource] = res;
165 else if(identity.definition instanceof Root) {
166 Root = identity.resource;
167 resources[identity.resource] = RootLibrary;
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);
180 void prepare(ReadGraph graph) throws DatabaseException {
183 Resource[] resources = new Resource[tg.resourceCount];
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;
192 if("@inverse".equals(def.name)) {
193 Resource parent = resources[def.parent];
194 Resource child = graph.getInverse(parent);
195 resources[identity.resource] = child;
197 Resource parent = resources[def.parent];
198 // TODO: escape should be removed when names become well-behaving
200 Resource child = graph
201 .syncRequest(new UnescapedChildMapOfResource(parent),
202 new TransientCacheAsyncListener<Map<String, Resource>>())
205 addMissing(graph.getURI(parent) + "/" + def.name);
207 resources[identity.resource] = child;
209 addMissing(TransferableGraphUtils.getURI(tg.resourceCount, tg.identities, def.parent) + "/" + def.name);
214 else if(definition instanceof Internal) {
215 // Do not do anything for now
217 else if(definition instanceof Root) {
218 Root root = (Root)definition;
219 if(root.name.equals(""))
220 resources[identity.resource] = RootLibrary;
222 advisor.analyzeRoot(graph, root);
224 else if(definition instanceof Optional) {
225 External def = (External)definition;
226 Resource parent = resources[def.parent];
228 resources[identity.resource] =
229 graph.syncRequest(new UnescapedChildMapOfResource(parent)).get(def.name);
233 this.resources = resources;
235 if(!missingExternals.isEmpty()) throw new OldMissingDependencyException(this);
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);
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);
257 int[] getClustering() {
258 Variant v = tg.extensions.get(Extensions.CLUSTERING);
259 if(v == null) return null;
261 return (int[])v.getValue(Bindings.INT_ARRAY);
262 } catch (AdaptException e) {
263 Logger.defaultLogError(e);
268 void write(WriteOnlyGraph graph) throws DatabaseException {
270 Resource[] resources = this.resources;
272 this.handles = new ResourceHandle[resources.length];
274 ResourceHandle[] handles = this.handles;
276 int[] clustering = getClustering();
278 // Internal identities
279 for(Identity identity : tg.identities) {
280 IdentityDefinition definition = identity.definition;
281 if(resources[identity.resource] != null)
283 if(definition instanceof External) {
284 // Already done everything
286 else if(definition instanceof Internal) {
287 Internal def = (Internal)definition;
288 resources[identity.resource] =
289 createChild(graph, resources[def.parent], def.name);
291 else if(definition instanceof Root) {
292 Root root = (Root)definition;
293 resources[identity.resource] = advisor.createRoot(graph, root);
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;
303 ClusterBuilder builder = graph.getService(ClusterBuilder.class);
304 SerialisationSupport ss = graph.getService(SerialisationSupport.class);
306 if(clustering != null) {
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();
315 handles[i] = builder.resource(resources[i]);
319 for(;i<resources.length;++i)
320 if(resources[i] == null)
321 handles[i] = builder.newResource();
323 handles[i] = builder.resource(resources[i]);
327 // Create blank resources
328 for(int i=0;i<resources.length;++i)
329 if(resources[i] == null)
330 handles[i] = builder.newResource();
332 handles[i] = builder.resource(resources[i]);
337 int[] statements = tg.statements;
339 int internals = tg.resourceCount - tg.identities.length;
341 for(int i=0;i<statements.length;i+=4) {
343 int sub = statements[i];
344 int pred = statements[i+1];
345 int inv = statements[i+2];
346 int obj = statements[i+3];
348 ResourceHandle subject = handles[sub];
349 ResourceHandle predicate = handles[pred];
350 ResourceHandle object = handles[obj];
352 if(resources[sub] == null) {
353 subject.addStatement(graph, predicate, object);
356 handles[sub].resource(ss),
357 handles[pred].resource(ss),
358 null, handles[obj].resource(ss));
363 if(resources[obj] == null) {
364 ResourceHandle inverse = handles[inv];
365 object.addStatement(graph, inverse, subject);
368 handles[obj].resource(ss),
369 handles[inv].resource(ss),
370 null, handles[sub].resource(ss));
376 log("[STATEMENT] " + resources[statements[i]].getResourceId() + ", " + resources[statements[i+1]].getResourceId() + ", " + resources[statements[i+3]].getResourceId());
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;
389 graph.claimValue(handles[resource].resource(ss), value.value, Bindings.BYTE_ARRAY);
391 if(resource < internals) {
392 handles[resource].addValue(graph, value.value);
394 tgSupport.setValue(graph, handles[resource].resource(ss), vg, value.value);
396 //tgSupport.setValue(resources[resource], vg, value.value);
401 void write2(WriteOnlyGraph graph) throws DatabaseException {
402 Resource[] resources = this.resources;
404 // Internal identities
405 for(Identity identity : tg.identities) {
406 IdentityDefinition definition = identity.definition;
407 if(resources[identity.resource] != null)
409 if(definition instanceof External) {
410 // Already done everything
412 else if(definition instanceof Internal) {
413 Internal def = (Internal)definition;
414 resources[identity.resource] =
415 createChild(graph, resources[def.parent], def.name);
417 else if(definition instanceof Root) {
418 Root root = (Root)definition;
419 resources[identity.resource] = advisor.createRoot(graph, root);
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;
429 // Create blank resources
430 for(int i=0;i<resources.length;++i)
431 if(resources[i] == null)
432 resources[i] = graph.newResource();
435 int[] statements = tg.statements;
437 for(int i=0;i<statements.length;i+=4) {
438 int inv = statements[i+2];
440 resources[statements[i]],
441 resources[statements[i+1]],
442 inv < 0 ? null : resources[inv],
443 resources[statements[i+3]]
446 log("[STATEMENT] " + resources[statements[i]].getResourceId() + ", " + resources[statements[i+1]].getResourceId() + ", " + resources[statements[i+3]].getResourceId());
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;
458 graph.claimValue(resources[resource], value.value, Bindings.BYTE_ARRAY);
460 tgSupport.setValue(graph, resources[resource], vg, value.value);
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));
473 for(int i=0;i<count;++i)
474 resourceIds[i] = serializer.getRandomAccessId(resources[i]);