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
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
\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
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
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
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
--- /dev/null
+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