--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.layer0.utils.writer;\r
+\r
+import gnu.trove.list.array.TIntArrayList;\r
+import gnu.trove.map.hash.TIntObjectHashMap;\r
+import gnu.trove.map.hash.TObjectIntHashMap;\r
+\r
+import java.util.ArrayList;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.WriteOnlyGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+\r
+public abstract class AbstractDelayedGraphWriter extends GraphWriterPartial {\r
+\r
+ protected int current = 0;\r
+ int externalCount = 0;\r
+ protected int internalCount = 0;\r
+ ArrayList<Resource> externals = new ArrayList<Resource>();\r
+ protected TObjectIntHashMap<Resource> externalsInv = new TObjectIntHashMap<Resource>();\r
+ protected TIntObjectHashMap<Resource> inverses = new TIntObjectHashMap<Resource>();\r
+ TIntArrayList timestamps = new TIntArrayList();\r
+ long[] resourceIds;\r
+ int time = 0;\r
+ \r
+ public long getResourceId(Session session, Resource r) {\r
+ if(r instanceof InternalResource) {\r
+ if(resourceIds == null) {\r
+ System.out.println("ERROR. Requesting resource id for new resource in writer before ids have been assigned.");\r
+ return 0;\r
+ }\r
+ return resourceIds[((InternalResource)r).id-1];\r
+ } else {\r
+ return r.getResourceId();\r
+ }\r
+ }\r
+\r
+ protected static class InternalResource implements Resource {\r
+ \r
+ int id;\r
+ \r
+ public InternalResource(int id) {\r
+ this.id = id;\r
+ }\r
+ \r
+ @Override\r
+ public long getResourceId() {\r
+ return (long)id;\r
+ }\r
+ \r
+ @Override\r
+ public Resource get() {\r
+ return this;\r
+ }\r
+ \r
+ @Override\r
+ public boolean isPersistent() {\r
+ return false;\r
+ }\r
+ \r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ return this==obj || \r
+ (obj instanceof InternalResource && \r
+ ((InternalResource)obj).id == id);\r
+ }\r
+\r
+ @Override\r
+ public boolean equalsResource(Resource other) {\r
+ return equals(other);\r
+ }\r
+ \r
+ @Override\r
+ public int hashCode() {\r
+ return id;\r
+ }\r
+ \r
+ @Override\r
+ public int getThreadHash() {\r
+ return id>>>16;\r
+ }\r
+\r
+ @Override\r
+ public int compareTo(Resource o) {\r
+ if(this==o)\r
+ return 0;\r
+ if(o instanceof InternalResource)\r
+ return id - ((InternalResource)o).id;\r
+ else\r
+ return -1;\r
+ }\r
+ \r
+ }\r
+\r
+ public AbstractDelayedGraphWriter(ReadGraph graph) {\r
+ super(graph);\r
+ }\r
+\r
+ @Override\r
+ public GraphWriter create() {\r
+ current = ++internalCount;\r
+ timestamps.add(0);\r
+ return this;\r
+ }\r
+\r
+ @Override\r
+ public Resource get() {\r
+ if(current > 0)\r
+ return new InternalResource(current);\r
+ else if(current < 0)\r
+ return externals.get(-1-current);\r
+ else\r
+ return null;\r
+ }\r
+\r
+ protected int getId(Resource r) {\r
+ if(r instanceof InternalResource)\r
+ return ((InternalResource)r).id;\r
+ int id = externalsInv.get(r);\r
+ if(id==0) {\r
+ id = -(++externalCount);\r
+ externals.add(r);\r
+ externalsInv.put(r, id);\r
+ }\r
+ return id; \r
+ } \r
+\r
+ @Override\r
+ public GraphWriter handle(Resource s) {\r
+ current = getId(s);\r
+ return this;\r
+ }\r
+\r
+ protected int getPredicateId(Resource p) throws DatabaseException {\r
+ int pId = getId(p);\r
+ if(!inverses.contains(pId))\r
+ if(!(p instanceof InternalResource))\r
+ inverses.put(pId, graph.getInverse(p));\r
+ return pId;\r
+ }\r
+ \r
+ public abstract void commit(IProgressMonitor monitor, WriteOnlyGraph wg) throws DatabaseException;\r
+\r
+}
\ No newline at end of file