]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
refs #3842
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 2 May 2013 10:49:45 +0000 (10:49 +0000)
committerMarko Luukkainen <marko.luukkainen@vtt.fi>
Thu, 2 Feb 2017 12:01:32 +0000 (14:01 +0200)
git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@27302 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.interop.xmlio/META-INF/MANIFEST.MF
org.simantics.interop.xmlio/src/org/simantics/interop/xmlio/LoadXML.java
org.simantics.interop.xmlio/src/org/simantics/interop/xmlio/MigrateLoadRule.java [new file with mode: 0644]

index 762d755e444ab1d0e1710c5f61e0097b5640af8f..a5c223af209853ed3ca8d0e60a8b6369f6e4a220 100644 (file)
@@ -10,7 +10,8 @@ Require-Bundle: org.eclipse.ui,
  org.simantics.db;bundle-version="1.1.0",
  org.simantics.db.common;bundle-version="1.1.0",
  org.apache.xerces;bundle-version="2.9.0",
- org.simantics.db.layer0;bundle-version="1.1.0"
+ org.simantics.db.layer0;bundle-version="1.1.0",
+ org.cojen;bundle-version="2.2.1"
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 Bundle-ActivationPolicy: lazy
 Export-Package: org.simantics.interop.xmlio
index 3a6606b98d8e48a386262727bf003a09d4acee49..c79ace686e8e39bb5ee2a639e52e16f451d6478a 100644 (file)
@@ -6,14 +6,13 @@ import java.io.FileReader;
 import java.io.Reader;\r
 import java.io.Serializable;\r
 import java.util.ArrayList;\r
-import java.util.HashMap;\r
 import java.util.List;\r
-import java.util.Map;\r
 import java.util.Stack;\r
 \r
 import javax.xml.parsers.SAXParser;\r
 import javax.xml.parsers.SAXParserFactory;\r
 \r
+import org.cojen.util.IntHashMap;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.Session;\r
@@ -21,6 +20,7 @@ import org.simantics.db.WriteGraph;
 import org.simantics.db.common.request.DelayedWriteRequest;\r
 import org.simantics.db.common.request.WriteRequest;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
 import org.simantics.db.layer0.util.SessionGarbageCollection;\r
 import org.simantics.layer0.Layer0;\r
 import org.xml.sax.Attributes;\r
@@ -38,7 +38,9 @@ public class LoadXML {
        \r
        private File file;\r
        \r
-       private Map<Integer,Resource> idMap = new HashMap<Integer, Resource>();\r
+       \r
+       //private Map<Integer,Resource> idMap = new HashMap<Integer, Resource>();\r
+       private IntHashMap<Resource> idMap = new IntHashMap<Resource>();\r
        \r
        //private Map<String,Resource> uriMap = new HashMap<String, Resource>();\r
        \r
@@ -49,6 +51,8 @@ public class LoadXML {
        private int resources;\r
        private int statements;\r
        \r
+       private static final boolean USE_DELAYED = true;\r
+       \r
        public LoadXML(Session session, File file) {\r
                this.session = session;\r
                this.file = file;\r
@@ -59,33 +63,68 @@ public class LoadXML {
                this.rule = rule;\r
        }\r
        \r
-       public void load(final Resource subj, final Resource pred) throws Exception {\r
+       public void load(Resource subj, Resource pred) throws Exception {\r
                resources = 0;\r
                statements = 0;\r
-               // TODO : switch to Delayed if possible\r
-               //        Delayed Write  Graph in 1.6 does not seem to support converting virtual Resources to real Resources!\r
                \r
-               session.syncRequest(new DelayedWriteRequest() {\r
-               //session.syncRequest(new WriteRequest() {\r
-                       \r
-                       @Override\r
-                       public void perform(WriteGraph graph) throws DatabaseException {\r
-                               LoadXML.this.graph = graph;\r
-                               l0 = Layer0.getInstance(graph);\r
-                               try {\r
-                                       XMLParser parser = new XMLParser();\r
-                                       parser.parse(file.getAbsolutePath());\r
-                               } catch (Exception e) {\r
-                                       throw new DatabaseException(e);\r
-                               } finally {\r
-                                       resultRoot = idMap.get(0);\r
-                                       graph.claim(subj, pred, resultRoot);\r
-                                       idMap.clear();\r
-                               }\r
-                       }\r
-               });\r
+               if (USE_DELAYED) {\r
+                       session.syncRequest(new ImportDelayedRequest(subj,pred));       \r
+               } else {\r
+                       session.syncRequest(new ImportRequest(subj,pred));      \r
+               }\r
+               //session.syncRequest(new DelayedWriteRequest() {\r
+               \r
+               \r
+               System.out.println("Import done.");\r
+       }\r
+       \r
+       private class ImportRequest extends WriteRequest {\r
+               Resource subj;Resource pred;\r
+               \r
+               \r
+               public ImportRequest(Resource subj, Resource pred) {\r
+                       super();\r
+                       this.subj = subj;\r
+                       this.pred = pred;\r
+               }\r
+\r
+               @Override\r
+               public void perform(WriteGraph graph) throws DatabaseException {\r
+                       doImport(graph, subj, pred);\r
+               }\r
+       }\r
+       \r
+       private class ImportDelayedRequest extends DelayedWriteRequest {\r
+               Resource subj;Resource pred;\r
                \r
-               System.out.println("Imported " + statements + " Statements, " + resources + " Resources.");\r
+               \r
+               public ImportDelayedRequest(Resource subj, Resource pred) {\r
+                       super();\r
+                       this.subj = subj;\r
+                       this.pred = pred;\r
+               }\r
+\r
+               @Override\r
+               public void perform(WriteGraph graph) throws DatabaseException {\r
+                       doImport(graph, subj, pred);\r
+               }\r
+       }\r
+       \r
+       private void doImport(WriteGraph graph, Resource subj, Resource pred) throws DatabaseException {\r
+               LoadXML.this.graph = graph;\r
+               l0 = Layer0.getInstance(graph);\r
+               Layer0Utils.setDependenciesIndexingDisabled(graph, true);\r
+               try {\r
+                       XMLParser parser = new XMLParser();\r
+                       parser.parse(file.getAbsolutePath());\r
+                       System.out.println("Imported " + statements + " Statements, " + resources + " Resources.");\r
+               } catch (Exception e) {\r
+                       throw new DatabaseException(e);\r
+               } finally {\r
+                       resultRoot = idMap.get(0);\r
+                       graph.claim(subj, pred, resultRoot);\r
+                       idMap.clear();\r
+               }\r
        }\r
        \r
        \r
@@ -242,6 +281,11 @@ public class LoadXML {
                                        System.out.println("Imported " + statements + " Statements, " + resources + " Resources..");\r
                                        //SessionGarbageCollection.gc(graph, 0, -1);\r
                                }\r
+                               if (!USE_DELAYED && statements % 100000 == 0) {\r
+                                       SessionGarbageCollection.gc(graph, 0, -1);\r
+                                       System.gc();\r
+                               }\r
+                               \r
                                return;\r
                        }\r
                        \r
diff --git a/org.simantics.interop.xmlio/src/org/simantics/interop/xmlio/MigrateLoadRule.java b/org.simantics.interop.xmlio/src/org/simantics/interop/xmlio/MigrateLoadRule.java
new file mode 100644 (file)
index 0000000..e0d5fdc
--- /dev/null
@@ -0,0 +1,68 @@
+package org.simantics.interop.xmlio;\r
+\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class MigrateLoadRule extends DefaultLoadRule {\r
+       \r
+       Set<String> ontologies = new HashSet<String>();\r
+       Map<String,String> ontologyVersions = new HashMap<String, String>();\r
+       \r
+       public MigrateLoadRule(ReadGraph graph) throws DatabaseException{\r
+               Resource root = graph.getSession().getRootLibrary();\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+               for (Resource lib : graph.getObjects(root, l0.ConsistsOf)) {\r
+                       if (!graph.isInstanceOf(lib, l0.Library))\r
+                               continue;\r
+                       for (Resource ontology : graph.getObjects(lib, l0.ConsistsOf)) {\r
+                               if (!graph.isInstanceOf(ontology, l0.Ontology))\r
+                                       continue;\r
+                               String uri = graph.getURI(ontology);\r
+                               String parsed[] = parse(uri);\r
+                               ontologies.add(parsed[0]);\r
+                               ontologyVersions.put(parsed[0], parsed[1]);\r
+                       }\r
+               }\r
+               \r
+       }\r
+       @Override\r
+       public Resource getResource(ReadGraph graph, String uri) throws DatabaseException {\r
+               return super.getResource(graph, getUri(uri));\r
+       }\r
+       \r
+       //Map<String,String> mappedUris = new HashMap<String, String>();\r
+       \r
+       private String getUri(String uri) {\r
+               \r
+               String parsed[] = parse(uri);\r
+               String currentUri = parsed[0] + "-" + ontologyVersions.get(parsed[0]) + "/" + parsed[2];\r
+               return currentUri;\r
+       }\r
+       \r
+       private String[] parse(String uri) {\r
+               int startVer = uri.indexOf("-");\r
+               int endVer = uri.indexOf("/", startVer);\r
+               if (endVer == -1) {\r
+                       return new String[]{uri.substring(0,startVer),uri.substring(startVer+1)};\r
+               } else {\r
+                       return new String[]{uri.substring(0,startVer),uri.substring(startVer+1,endVer),uri.substring(endVer+1)};\r
+               }\r
+               \r
+       }\r
+       \r
+       @Override\r
+       public Object createValue(ReadGraph graph, List<String> value, List<String> types) throws DatabaseException {\r
+               for (int i = 0; i < types.size(); i++) {\r
+                       types.set(i, getUri(types.get(i)));\r
+               }\r
+               return super.createValue(graph, value, types);\r
+       }\r
+}\r