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