X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.graph.db%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fdb%2Fold%2FOldTransferableGraphImportProcess1.java;fp=bundles%2Forg.simantics.graph.db%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fdb%2Fold%2FOldTransferableGraphImportProcess1.java;h=d756cb1d8d30514bdbfb1068494454e176b557f6;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.graph.db/src/org/simantics/graph/db/old/OldTransferableGraphImportProcess1.java b/bundles/org.simantics.graph.db/src/org/simantics/graph/db/old/OldTransferableGraphImportProcess1.java new file mode 100644 index 000000000..d756cb1d8 --- /dev/null +++ b/bundles/org.simantics.graph.db/src/org/simantics/graph/db/old/OldTransferableGraphImportProcess1.java @@ -0,0 +1,479 @@ +package org.simantics.graph.db.old; + +import java.io.DataOutput; +import java.io.DataOutputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.adapter.AdaptException; +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.VirtualGraph; +import org.simantics.db.WriteOnlyGraph; +import org.simantics.db.common.WriteBindings; +import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener; +import org.simantics.db.common.uri.UnescapedChildMapOfResource; +import org.simantics.db.common.utils.Logger; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ResourceNotFoundException; +import org.simantics.db.service.ClusterBuilder; +import org.simantics.db.service.ClusterBuilder.ResourceHandle; +import org.simantics.db.service.SerialisationSupport; +import org.simantics.db.service.TransferableGraphSupport; +import org.simantics.graph.db.CoreInitialization; +import org.simantics.graph.db.IImportAdvisor; +import org.simantics.graph.representation.Extensions; +import org.simantics.graph.representation.External; +import org.simantics.graph.representation.Identity; +import org.simantics.graph.representation.IdentityDefinition; +import org.simantics.graph.representation.Internal; +import org.simantics.graph.representation.Optional; +import org.simantics.graph.representation.Root; +import org.simantics.graph.representation.TransferableGraphUtils; +import org.simantics.graph.representation.old.OldTransferableGraph1; +import org.simantics.graph.representation.old.OldValue1; + +public class OldTransferableGraphImportProcess1 { + + public static String LOG_FILE = "transferableGraphs.log"; + final static private boolean LOG = false; + + static DataOutput log; + + static { + + if (LOG) { + try { + FileOutputStream stream = new FileOutputStream(LOG_FILE); + log = new DataOutputStream(stream); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + } + + private static void log(String line) { + if (LOG) { + try { + log.writeUTF(line + "\n"); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + OldTransferableGraph1 tg; + IImportAdvisor advisor; + + Resource[] resources; + ResourceHandle[] handles; + + Set missingExternals = new HashSet(); + + // Builtins + Resource RootLibrary; + Resource String; + Resource Library; + + Resource InstanceOf; + Resource ConsistsOf; + Resource PartOf; + Resource HasName; + Resource NameOf; + + public OldTransferableGraphImportProcess1(OldTransferableGraph1 tg, IImportAdvisor advisor) { + this.tg = tg; + this.advisor = advisor; + /*System.out.println("Transferable graph identities:"); + for(Identity id : tg.identities) { + if(id.definition instanceof Internal) { + Internal def = (Internal)id.definition; + System.out.println(" internal " + def.name); + } + else if(id.definition instanceof External) { + External def = (External)id.definition; + System.out.println(" external " + def.name); + } + }*/ + } + + public void findBuiltins(WriteOnlyGraph g) throws DatabaseException { + RootLibrary = g.getBuiltin("http:/"); + String = g.getBuiltin(CoreInitialization.LAYER0 + "String"); + Library = g.getBuiltin(CoreInitialization.LAYER0 + "Library"); + InstanceOf = g.getBuiltin(CoreInitialization.LAYER0 + "InstanceOf"); + ConsistsOf = g.getBuiltin(CoreInitialization.LAYER0 + "ConsistsOf"); + PartOf = g.getBuiltin(CoreInitialization.LAYER0 + "PartOf"); + HasName = g.getBuiltin(CoreInitialization.LAYER0 + "HasName"); + NameOf = g.getBuiltin(CoreInitialization.LAYER0 + "NameOf"); + } + + public void findBuiltins(ReadGraph g) throws DatabaseException { + RootLibrary = g.getBuiltin("http:/"); + String = g.getBuiltin(CoreInitialization.LAYER0 + "String"); + Library = g.getBuiltin(CoreInitialization.LAYER0 + "Library"); + InstanceOf = g.getBuiltin(CoreInitialization.LAYER0 + "InstanceOf"); + ConsistsOf = g.getBuiltin(CoreInitialization.LAYER0 + "ConsistsOf"); + PartOf = g.getBuiltin(CoreInitialization.LAYER0 + "PartOf"); + HasName = g.getBuiltin(CoreInitialization.LAYER0 + "HasName"); + NameOf = g.getBuiltin(CoreInitialization.LAYER0 + "NameOf"); + } + + /* Preparation that is used when the core is empty. + */ + void initialPrepare(WriteOnlyGraph graph) throws DatabaseException { + findBuiltins(graph); + + resources = new Resource[tg.resourceCount]; + + int Root = -1; + int SimanticsDomain = -1; + int Layer0 = -1; + + for(Identity identity : tg.identities) { + if(identity.definition instanceof Internal) { + Internal def = (Internal)identity.definition; + Resource res = null; + if(def.parent == Layer0) { + try { + res = graph.getBuiltin(CoreInitialization.LAYER0 + def.name); + } catch(ResourceNotFoundException e) { + } + } + else if(def.parent == SimanticsDomain) { + if(def.name.equals("Layer0-1.1")) + Layer0 = identity.resource; + } + else if(def.parent == Root) { + if(def.name.equals("www.simantics.org")) + SimanticsDomain = identity.resource; + } + + if(res == null) + res = createChild(graph, resources[def.parent], def.name); + else + createChild(graph, res, resources[def.parent], def.name); + resources[identity.resource] = res; + } + else if(identity.definition instanceof Root) { + Root = identity.resource; + resources[identity.resource] = RootLibrary; + } + } + } + + void addMissing(String external) { + Set removals = new HashSet(); + for(String ext : missingExternals) if(ext.startsWith(external)) return; + for(String ext : missingExternals) if(external.startsWith(ext)) removals.add(ext); + missingExternals.removeAll(removals); + missingExternals.add(external); + } + + void prepare(ReadGraph graph) throws DatabaseException { + findBuiltins(graph); + + Resource[] resources = new Resource[tg.resourceCount]; + + for(Identity identity : tg.identities) { + IdentityDefinition definition = identity.definition; + if(definition instanceof External) { + External def = (External)definition; + if(def.parent == -1) { + resources[identity.resource] = RootLibrary; + } else { + if("@inverse".equals(def.name)) { + Resource parent = resources[def.parent]; + Resource child = graph.getInverse(parent); + resources[identity.resource] = child; + } else { + Resource parent = resources[def.parent]; + // TODO: escape should be removed when names become well-behaving + if(parent != null) { + Resource child = graph + .syncRequest(new UnescapedChildMapOfResource(parent), + new TransientCacheAsyncListener>()) + .get(def.name); + if(child == null) { + addMissing(graph.getURI(parent) + "/" + def.name); + } + resources[identity.resource] = child; + } else { + addMissing(TransferableGraphUtils.getURI(tg.resourceCount, tg.identities, def.parent) + "/" + def.name); + } + } + } + } + else if(definition instanceof Internal) { + // Do not do anything for now + } + else if(definition instanceof Root) { + Root root = (Root)definition; + if(root.name.equals("")) + resources[identity.resource] = RootLibrary; + else + advisor.analyzeRoot(graph, root); + } + else if(definition instanceof Optional) { + External def = (External)definition; + Resource parent = resources[def.parent]; + if(parent != null) + resources[identity.resource] = + graph.syncRequest(new UnescapedChildMapOfResource(parent)).get(def.name); + } + } + + this.resources = resources; + + if(!missingExternals.isEmpty()) throw new OldMissingDependencyException(this); + + } + + Resource createChild(WriteOnlyGraph graph, Resource parent, String name) throws DatabaseException { + Resource child = graph.newResource(); + graph.claim(parent, ConsistsOf, PartOf, child); + Resource nameResource = graph.newResource(); + graph.claim(nameResource, InstanceOf, null, String); + graph.claimValue(nameResource, name, WriteBindings.STRING); + graph.claim(child, HasName, NameOf, nameResource); + return child; + } + + void createChild(WriteOnlyGraph graph, Resource child, Resource parent, String name) throws DatabaseException { + graph.claim(parent, ConsistsOf, PartOf, child); + Resource nameResource = graph.newResource(); + graph.claim(nameResource, InstanceOf, null, String); + graph.claimValue(nameResource, name, WriteBindings.STRING); + graph.claim(child, HasName, NameOf, nameResource); + } + + int[] getClustering() { + Variant v = tg.extensions.get(Extensions.CLUSTERING); + if(v == null) return null; + try { + return (int[])v.getValue(Bindings.INT_ARRAY); + } catch (AdaptException e) { + Logger.defaultLogError(e); + return null; + } + } + + void write(WriteOnlyGraph graph) throws DatabaseException { + + Resource[] resources = this.resources; + + this.handles = new ResourceHandle[resources.length]; + + ResourceHandle[] handles = this.handles; + + int[] clustering = getClustering(); + + // Internal identities + for(Identity identity : tg.identities) { + IdentityDefinition definition = identity.definition; + if(resources[identity.resource] != null) + continue; + if(definition instanceof External) { + // Already done everything + } + else if(definition instanceof Internal) { + Internal def = (Internal)definition; + resources[identity.resource] = + createChild(graph, resources[def.parent], def.name); + } + else if(definition instanceof Root) { + Root root = (Root)definition; + resources[identity.resource] = advisor.createRoot(graph, root); + } + else if(definition instanceof Optional) { + Optional def = (Optional)definition; + Resource child = createChild(graph, resources[def.parent], def.name); + graph.claim(child, InstanceOf, null, Library); // ??? + resources[identity.resource] = child; + } + } + + ClusterBuilder builder = graph.getService(ClusterBuilder.class); + SerialisationSupport ss = graph.getService(SerialisationSupport.class); + + if(clustering != null) { + + int i = 0; + for(int c : clustering) { + builder.newCluster(); + for(int r=0;r= 0) { + + if(resources[obj] == null) { + ResourceHandle inverse = handles[inv]; + object.addStatement(graph, inverse, subject); + } else { + graph.claim( + handles[obj].resource(ss), + handles[inv].resource(ss), + null, handles[sub].resource(ss)); + } + + } + + if(LOG) { + log("[STATEMENT] " + resources[statements[i]].getResourceId() + ", " + resources[statements[i+1]].getResourceId() + ", " + resources[statements[i+3]].getResourceId()); + } + + } + + // Write values + TransferableGraphSupport tgSupport = + graph.getService(TransferableGraphSupport.class); + VirtualGraph vg = graph.getProvider(); + for(OldValue1 value : tg.values) { + int file = value.resource & 0x80000000; + int resource = value.resource & 0x7FFFFFFF; + if (file != 0) { + graph.claimValue(handles[resource].resource(ss), value.value, Bindings.BYTE_ARRAY); + } else { + if(resource < internals) { + handles[resource].addValue(graph, value.value); + } else { + tgSupport.setValue(graph, handles[resource].resource(ss), vg, value.value); + } + //tgSupport.setValue(resources[resource], vg, value.value); + } + } + } + + void write2(WriteOnlyGraph graph) throws DatabaseException { + Resource[] resources = this.resources; + + // Internal identities + for(Identity identity : tg.identities) { + IdentityDefinition definition = identity.definition; + if(resources[identity.resource] != null) + continue; + if(definition instanceof External) { + // Already done everything + } + else if(definition instanceof Internal) { + Internal def = (Internal)definition; + resources[identity.resource] = + createChild(graph, resources[def.parent], def.name); + } + else if(definition instanceof Root) { + Root root = (Root)definition; + resources[identity.resource] = advisor.createRoot(graph, root); + } + else if(definition instanceof Optional) { + Optional def = (Optional)definition; + Resource child = createChild(graph, resources[def.parent], def.name); + graph.claim(child, InstanceOf, null, Library); // ??? + resources[identity.resource] = child; + } + } + + // Create blank resources + for(int i=0;i