]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graph.db/src/org/simantics/graph/db/old/OldTransferableGraphImportProcess1.java
Enhancements to modelled STS-tests
[simantics/platform.git] / bundles / org.simantics.graph.db / src / org / simantics / graph / db / old / OldTransferableGraphImportProcess1.java
index d756cb1d8d30514bdbfb1068494454e176b557f6..6a69e8d921754fe797c134f0162eb4967b712124 100644 (file)
-package org.simantics.graph.db.old;\r
-\r
-import java.io.DataOutput;\r
-import java.io.DataOutputStream;\r
-import java.io.FileNotFoundException;\r
-import java.io.FileOutputStream;\r
-import java.io.IOException;\r
-import java.util.HashSet;\r
-import java.util.Map;\r
-import java.util.Set;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.adapter.AdaptException;\r
-import org.simantics.databoard.binding.mutable.Variant;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.VirtualGraph;\r
-import org.simantics.db.WriteOnlyGraph;\r
-import org.simantics.db.common.WriteBindings;\r
-import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
-import org.simantics.db.common.uri.UnescapedChildMapOfResource;\r
-import org.simantics.db.common.utils.Logger;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.ResourceNotFoundException;\r
-import org.simantics.db.service.ClusterBuilder;\r
-import org.simantics.db.service.ClusterBuilder.ResourceHandle;\r
-import org.simantics.db.service.SerialisationSupport;\r
-import org.simantics.db.service.TransferableGraphSupport;\r
-import org.simantics.graph.db.CoreInitialization;\r
-import org.simantics.graph.db.IImportAdvisor;\r
-import org.simantics.graph.representation.Extensions;\r
-import org.simantics.graph.representation.External;\r
-import org.simantics.graph.representation.Identity;\r
-import org.simantics.graph.representation.IdentityDefinition;\r
-import org.simantics.graph.representation.Internal;\r
-import org.simantics.graph.representation.Optional;\r
-import org.simantics.graph.representation.Root;\r
-import org.simantics.graph.representation.TransferableGraphUtils;\r
-import org.simantics.graph.representation.old.OldTransferableGraph1;\r
-import org.simantics.graph.representation.old.OldValue1;\r
-\r
-public class OldTransferableGraphImportProcess1 {\r
-       \r
-    public static String LOG_FILE = "transferableGraphs.log";\r
-    final static private boolean LOG = false;\r
-    \r
-    static DataOutput log;\r
-\r
-    static {\r
-\r
-        if (LOG) {\r
-            try {\r
-                FileOutputStream stream = new FileOutputStream(LOG_FILE);\r
-                log = new DataOutputStream(stream);\r
-            } catch (FileNotFoundException e) {\r
-                e.printStackTrace();\r
-            }\r
-        }\r
-\r
-    }\r
-    \r
-    private static void log(String line) {\r
-        if (LOG) {\r
-            try {\r
-                log.writeUTF(line + "\n");\r
-            } catch (IOException e) {\r
-                e.printStackTrace();\r
-            }\r
-        }\r
-    }\r
-    \r
-    OldTransferableGraph1 tg;\r
-    IImportAdvisor advisor;\r
-    \r
-    Resource[] resources;\r
-    ResourceHandle[] handles;\r
-    \r
-    Set<String> missingExternals = new HashSet<String>(); \r
-    \r
-    // Builtins\r
-    Resource RootLibrary;\r
-    Resource String;\r
-    Resource Library;\r
-    \r
-    Resource InstanceOf;\r
-    Resource ConsistsOf;\r
-    Resource PartOf;\r
-    Resource HasName;\r
-    Resource NameOf;    \r
-                \r
-    public OldTransferableGraphImportProcess1(OldTransferableGraph1 tg, IImportAdvisor advisor) {\r
-        this.tg = tg;\r
-        this.advisor = advisor;\r
-        /*System.out.println("Transferable graph identities:");\r
-        for(Identity id : tg.identities) {\r
-            if(id.definition instanceof Internal) {\r
-                Internal def = (Internal)id.definition;\r
-                System.out.println("    internal " + def.name);\r
-            }\r
-            else if(id.definition instanceof External) {\r
-                External def = (External)id.definition;\r
-                System.out.println("    external " + def.name);\r
-            }\r
-        }*/\r
-    }\r
-    \r
-    public void findBuiltins(WriteOnlyGraph g) throws DatabaseException {\r
-        RootLibrary = g.getBuiltin("http:/");\r
-        String = g.getBuiltin(CoreInitialization.LAYER0 + "String");\r
-        Library = g.getBuiltin(CoreInitialization.LAYER0 + "Library");\r
-        InstanceOf = g.getBuiltin(CoreInitialization.LAYER0 + "InstanceOf");\r
-        ConsistsOf = g.getBuiltin(CoreInitialization.LAYER0 + "ConsistsOf");\r
-        PartOf = g.getBuiltin(CoreInitialization.LAYER0 + "PartOf");\r
-        HasName = g.getBuiltin(CoreInitialization.LAYER0 + "HasName");\r
-        NameOf = g.getBuiltin(CoreInitialization.LAYER0 + "NameOf");\r
-    }\r
-    \r
-    public void findBuiltins(ReadGraph g) throws DatabaseException {\r
-        RootLibrary = g.getBuiltin("http:/");\r
-        String = g.getBuiltin(CoreInitialization.LAYER0 + "String");\r
-        Library = g.getBuiltin(CoreInitialization.LAYER0 + "Library");\r
-        InstanceOf = g.getBuiltin(CoreInitialization.LAYER0 + "InstanceOf");\r
-        ConsistsOf = g.getBuiltin(CoreInitialization.LAYER0 + "ConsistsOf");\r
-        PartOf = g.getBuiltin(CoreInitialization.LAYER0 + "PartOf");\r
-        HasName = g.getBuiltin(CoreInitialization.LAYER0 + "HasName");\r
-        NameOf = g.getBuiltin(CoreInitialization.LAYER0 + "NameOf");\r
-    }\r
-\r
-    /* Preparation that is used when the core is empty. \r
-     */\r
-    void initialPrepare(WriteOnlyGraph graph) throws DatabaseException {\r
-        findBuiltins(graph);\r
-        \r
-        resources = new Resource[tg.resourceCount];\r
-        \r
-        int Root = -1;\r
-        int SimanticsDomain = -1;\r
-        int Layer0 = -1;\r
-        \r
-        for(Identity identity : tg.identities) {\r
-            if(identity.definition instanceof Internal) {\r
-                Internal def = (Internal)identity.definition;\r
-                Resource res = null;\r
-                if(def.parent == Layer0) {\r
-                    try {\r
-                        res = graph.getBuiltin(CoreInitialization.LAYER0 + def.name);\r
-                    } catch(ResourceNotFoundException e) {                                      \r
-                    }\r
-                }\r
-                else if(def.parent == SimanticsDomain) {\r
-                    if(def.name.equals("Layer0-1.1"))\r
-                        Layer0 = identity.resource;\r
-                }\r
-                else if(def.parent == Root) {\r
-                    if(def.name.equals("www.simantics.org"))\r
-                        SimanticsDomain = identity.resource;\r
-                }\r
-\r
-                if(res == null)\r
-                    res = createChild(graph, resources[def.parent], def.name);\r
-                else\r
-                    createChild(graph, res, resources[def.parent], def.name);\r
-                resources[identity.resource] = res;\r
-            }\r
-            else if(identity.definition instanceof Root) {\r
-                Root = identity.resource;\r
-                resources[identity.resource] = RootLibrary;             \r
-            } \r
-        }\r
-    }\r
-    \r
-    void addMissing(String external) {\r
-        Set<String> removals = new HashSet<String>();\r
-        for(String ext : missingExternals) if(ext.startsWith(external)) return;\r
-        for(String ext : missingExternals) if(external.startsWith(ext)) removals.add(ext);\r
-        missingExternals.removeAll(removals);\r
-        missingExternals.add(external);\r
-    }\r
-    \r
-    void prepare(ReadGraph graph) throws DatabaseException {\r
-        findBuiltins(graph);\r
-        \r
-        Resource[] resources = new Resource[tg.resourceCount];\r
-        \r
-        for(Identity identity : tg.identities) {\r
-            IdentityDefinition definition = identity.definition;\r
-            if(definition instanceof External) {\r
-                External def = (External)definition;\r
-                if(def.parent == -1) {\r
-                    resources[identity.resource] = RootLibrary;\r
-                } else {\r
-                    if("@inverse".equals(def.name)) {\r
-                        Resource parent = resources[def.parent];\r
-                        Resource child = graph.getInverse(parent);\r
-                        resources[identity.resource] = child;\r
-                    } else {\r
-                        Resource parent = resources[def.parent];\r
-                        // TODO: escape should be removed when names become well-behaving\r
-                        if(parent != null) {\r
-                            Resource child = graph\r
-                            .syncRequest(new UnescapedChildMapOfResource(parent),\r
-                                    new TransientCacheAsyncListener<Map<String, Resource>>())\r
-                                    .get(def.name);\r
-                            if(child == null) {\r
-                                addMissing(graph.getURI(parent) + "/" + def.name);\r
-                            }\r
-                            resources[identity.resource] = child;\r
-                        } else {\r
-                            addMissing(TransferableGraphUtils.getURI(tg.resourceCount, tg.identities, def.parent) + "/" + def.name);\r
-                        }\r
-                    }\r
-                }\r
-            }\r
-            else if(definition instanceof Internal) {\r
-                // Do not do anything for now\r
-            }\r
-            else if(definition instanceof Root) {\r
-                Root root = (Root)definition;\r
-                if(root.name.equals(""))\r
-                    resources[identity.resource] = RootLibrary;\r
-                else \r
-                    advisor.analyzeRoot(graph, root);                   \r
-            }\r
-            else if(definition instanceof Optional) {\r
-                External def = (External)definition;\r
-                Resource parent = resources[def.parent];\r
-                if(parent != null)\r
-                    resources[identity.resource] = \r
-                        graph.syncRequest(new UnescapedChildMapOfResource(parent)).get(def.name);               \r
-            }\r
-        }       \r
-        \r
-        this.resources = resources;\r
-        \r
-        if(!missingExternals.isEmpty()) throw new OldMissingDependencyException(this);\r
-        \r
-    }\r
-\r
-    Resource createChild(WriteOnlyGraph graph, Resource parent, String name) throws DatabaseException {\r
-        Resource child = graph.newResource();\r
-        graph.claim(parent, ConsistsOf, PartOf, child);\r
-        Resource nameResource = graph.newResource();\r
-        graph.claim(nameResource, InstanceOf, null, String);\r
-        graph.claimValue(nameResource, name, WriteBindings.STRING);\r
-        graph.claim(child, HasName, NameOf, nameResource);\r
-        return child;\r
-    }\r
-    \r
-    void createChild(WriteOnlyGraph graph, Resource child, Resource parent, String name) throws DatabaseException {\r
-        graph.claim(parent, ConsistsOf, PartOf, child);\r
-        Resource nameResource = graph.newResource();\r
-        graph.claim(nameResource, InstanceOf, null, String);\r
-        graph.claimValue(nameResource, name, WriteBindings.STRING);\r
-        graph.claim(child, HasName, NameOf, nameResource);\r
-    }\r
-    \r
-    int[] getClustering() {\r
-        Variant v = tg.extensions.get(Extensions.CLUSTERING);\r
-        if(v == null) return null;\r
-        try {\r
-            return (int[])v.getValue(Bindings.INT_ARRAY);\r
-        } catch (AdaptException e) {\r
-            Logger.defaultLogError(e);\r
-            return null;\r
-        }\r
-    }\r
-    \r
-    void write(WriteOnlyGraph graph) throws DatabaseException {\r
-\r
-        Resource[] resources = this.resources;\r
-\r
-        this.handles = new ResourceHandle[resources.length];\r
-        \r
-        ResourceHandle[] handles = this.handles; \r
-\r
-        int[] clustering = getClustering();\r
-\r
-        // Internal identities      \r
-        for(Identity identity : tg.identities) {\r
-            IdentityDefinition definition = identity.definition;\r
-            if(resources[identity.resource] != null)\r
-                continue;\r
-            if(definition instanceof External) {\r
-                // Already done everything\r
-            }\r
-            else if(definition instanceof Internal) {\r
-                Internal def = (Internal)definition;\r
-                resources[identity.resource] = \r
-                    createChild(graph, resources[def.parent], def.name);\r
-            }\r
-            else if(definition instanceof Root) {\r
-                Root root = (Root)definition;               \r
-                resources[identity.resource] = advisor.createRoot(graph, root);                 \r
-            }\r
-            else if(definition instanceof Optional) {\r
-                Optional def = (Optional)definition;\r
-                Resource child = createChild(graph, resources[def.parent], def.name);\r
-                graph.claim(child, InstanceOf, null, Library); // ???\r
-                resources[identity.resource] = child;                   \r
-            }\r
-        }       \r
-\r
-        ClusterBuilder builder = graph.getService(ClusterBuilder.class);\r
-        SerialisationSupport ss = graph.getService(SerialisationSupport.class);\r
-\r
-        if(clustering != null) {\r
-            \r
-            int i = 0;\r
-            for(int c : clustering) {\r
-                builder.newCluster();\r
-                for(int r=0;r<c;r++, i++)\r
-                    if(resources[i] == null)\r
-                        handles[i] = builder.newResource();\r
-                    else \r
-                        handles[i] = builder.resource(resources[i]);\r
-\r
-            }\r
-\r
-            for(;i<resources.length;++i)\r
-                if(resources[i] == null)\r
-                    handles[i] = builder.newResource();\r
-                else \r
-                    handles[i] = builder.resource(resources[i]);\r
-            \r
-        } else {\r
-        \r
-            // Create blank resources\r
-            for(int i=0;i<resources.length;++i)\r
-                if(resources[i] == null)\r
-                    handles[i] = builder.newResource();\r
-                else \r
-                    handles[i] = builder.resource(resources[i]);\r
-\r
-        }\r
-        \r
-        // Write statements\r
-        int[] statements = tg.statements;\r
-        \r
-        int internals = tg.resourceCount - tg.identities.length;\r
-        \r
-        for(int i=0;i<statements.length;i+=4) {\r
-\r
-            int sub = statements[i];\r
-            int pred = statements[i+1];\r
-            int inv = statements[i+2];\r
-            int obj = statements[i+3];\r
-\r
-            ResourceHandle subject = handles[sub];\r
-            ResourceHandle predicate = handles[pred];\r
-            ResourceHandle object = handles[obj];\r
-\r
-            if(resources[sub] == null) {\r
-                subject.addStatement(graph, predicate, object); \r
-            } else {\r
-                graph.claim(\r
-                        handles[sub].resource(ss),\r
-                        handles[pred].resource(ss),\r
-                        null, handles[obj].resource(ss));\r
-            }\r
-            \r
-            if(inv >= 0) {\r
-                \r
-                if(resources[obj] == null) {\r
-                    ResourceHandle inverse = handles[inv];\r
-                    object.addStatement(graph, inverse, subject);   \r
-                } else {\r
-                    graph.claim(\r
-                            handles[obj].resource(ss),\r
-                            handles[inv].resource(ss),\r
-                            null, handles[sub].resource(ss));\r
-                }\r
-                \r
-            }\r
-            \r
-            if(LOG) {\r
-                log("[STATEMENT] " + resources[statements[i]].getResourceId() + ", " + resources[statements[i+1]].getResourceId() + ", " + resources[statements[i+3]].getResourceId());\r
-            }\r
-            \r
-        }\r
-                \r
-        // Write values\r
-        TransferableGraphSupport tgSupport = \r
-            graph.getService(TransferableGraphSupport.class);\r
-        VirtualGraph vg = graph.getProvider();\r
-        for(OldValue1 value : tg.values) {\r
-            int file = value.resource & 0x80000000;\r
-            int resource = value.resource & 0x7FFFFFFF;\r
-            if (file != 0) {\r
-                graph.claimValue(handles[resource].resource(ss), value.value, Bindings.BYTE_ARRAY);\r
-            } else {\r
-                if(resource < internals) {\r
-                    handles[resource].addValue(graph, value.value);\r
-                } else {\r
-                    tgSupport.setValue(graph, handles[resource].resource(ss), vg, value.value);\r
-                }\r
-                //tgSupport.setValue(resources[resource], vg, value.value);\r
-            }\r
-        }\r
-    }\r
-    \r
-    void write2(WriteOnlyGraph graph) throws DatabaseException {\r
-        Resource[] resources = this.resources;\r
-        \r
-        // Internal identities      \r
-        for(Identity identity : tg.identities) {\r
-            IdentityDefinition definition = identity.definition;\r
-            if(resources[identity.resource] != null)\r
-                continue;\r
-            if(definition instanceof External) {\r
-                // Already done everything\r
-            }\r
-            else if(definition instanceof Internal) {\r
-                Internal def = (Internal)definition;\r
-                resources[identity.resource] = \r
-                    createChild(graph, resources[def.parent], def.name);\r
-            }\r
-            else if(definition instanceof Root) {               \r
-                Root root = (Root)definition;               \r
-                resources[identity.resource] = advisor.createRoot(graph, root);                 \r
-            }\r
-            else if(definition instanceof Optional) {\r
-                Optional def = (Optional)definition;\r
-                Resource child = createChild(graph, resources[def.parent], def.name);\r
-                graph.claim(child, InstanceOf, null, Library); // ???\r
-                resources[identity.resource] = child;                   \r
-            }\r
-        }       \r
-        \r
-        // Create blank resources\r
-        for(int i=0;i<resources.length;++i)\r
-            if(resources[i] == null)\r
-                resources[i] = graph.newResource();\r
-        \r
-        // Write statements\r
-        int[] statements = tg.statements;\r
-        \r
-        for(int i=0;i<statements.length;i+=4) {\r
-            int inv = statements[i+2];\r
-            graph.claim(\r
-                    resources[statements[i]],\r
-                    resources[statements[i+1]],\r
-                    inv < 0 ? null : resources[inv],\r
-                    resources[statements[i+3]]\r
-                    );\r
-            if(LOG) {\r
-                log("[STATEMENT] " + resources[statements[i]].getResourceId() + ", " + resources[statements[i+1]].getResourceId() + ", " + resources[statements[i+3]].getResourceId());\r
-            }\r
-        }\r
-                \r
-        // Write values\r
-        TransferableGraphSupport tgSupport = \r
-            graph.getService(TransferableGraphSupport.class);\r
-        VirtualGraph vg = graph.getProvider();\r
-        for(OldValue1 value : tg.values) {\r
-            int file = value.resource & 0x80000000;\r
-            int resource = value.resource & 0x7FFFFFFF;\r
-            if (file != 0) {\r
-                graph.claimValue(resources[resource], value.value, Bindings.BYTE_ARRAY);\r
-            } else {\r
-                tgSupport.setValue(graph, resources[resource], vg, value.value);\r
-            }\r
-        }\r
-    }\r
-    \r
-    \r
-    public long[] getResourceIds(SerialisationSupport serializer) throws DatabaseException {\r
-        final int count = resources.length;\r
-        long[] resourceIds = new long[count];\r
-        if(handles != null) {\r
-            for(int i=0;i<count;++i)\r
-                resourceIds[i] = serializer.getRandomAccessId(handles[i].resource(serializer));\r
-        } else {\r
-            for(int i=0;i<count;++i)\r
-                resourceIds[i] = serializer.getRandomAccessId(resources[i]);\r
-        }\r
-        return resourceIds;\r
-    }\r
-    \r
+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<String> missingExternals = new HashSet<String>(); 
+    
+    // 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<String> removals = new HashSet<String>();
+        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<Map<String, Resource>>())
+                                    .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<c;r++, i++)
+                    if(resources[i] == null)
+                        handles[i] = builder.newResource();
+                    else 
+                        handles[i] = builder.resource(resources[i]);
+
+            }
+
+            for(;i<resources.length;++i)
+                if(resources[i] == null)
+                    handles[i] = builder.newResource();
+                else 
+                    handles[i] = builder.resource(resources[i]);
+            
+        } else {
+        
+            // Create blank resources
+            for(int i=0;i<resources.length;++i)
+                if(resources[i] == null)
+                    handles[i] = builder.newResource();
+                else 
+                    handles[i] = builder.resource(resources[i]);
+
+        }
+        
+        // Write statements
+        int[] statements = tg.statements;
+        
+        int internals = tg.resourceCount - tg.identities.length;
+        
+        for(int i=0;i<statements.length;i+=4) {
+
+            int sub = statements[i];
+            int pred = statements[i+1];
+            int inv = statements[i+2];
+            int obj = statements[i+3];
+
+            ResourceHandle subject = handles[sub];
+            ResourceHandle predicate = handles[pred];
+            ResourceHandle object = handles[obj];
+
+            if(resources[sub] == null) {
+                subject.addStatement(graph, predicate, object); 
+            } else {
+                graph.claim(
+                        handles[sub].resource(ss),
+                        handles[pred].resource(ss),
+                        null, handles[obj].resource(ss));
+            }
+            
+            if(inv >= 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<resources.length;++i)
+            if(resources[i] == null)
+                resources[i] = graph.newResource();
+        
+        // Write statements
+        int[] statements = tg.statements;
+        
+        for(int i=0;i<statements.length;i+=4) {
+            int inv = statements[i+2];
+            graph.claim(
+                    resources[statements[i]],
+                    resources[statements[i+1]],
+                    inv < 0 ? null : resources[inv],
+                    resources[statements[i+3]]
+                    );
+            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(resources[resource], value.value, Bindings.BYTE_ARRAY);
+            } else {
+                tgSupport.setValue(graph, resources[resource], vg, value.value);
+            }
+        }
+    }
+    
+    
+    public long[] getResourceIds(SerialisationSupport serializer) throws DatabaseException {
+        final int count = resources.length;
+        long[] resourceIds = new long[count];
+        if(handles != null) {
+            for(int i=0;i<count;++i)
+                resourceIds[i] = serializer.getRandomAccessId(handles[i].resource(serializer));
+        } else {
+            for(int i=0;i<count;++i)
+                resourceIds[i] = serializer.getRandomAccessId(resources[i]);
+        }
+        return resourceIds;
+    }
+    
 }
\ No newline at end of file