]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/writer/DirectGraphWriter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / writer / DirectGraphWriter.java
diff --git a/bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/writer/DirectGraphWriter.java b/bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/writer/DirectGraphWriter.java
new file mode 100644 (file)
index 0000000..c0e8e2d
--- /dev/null
@@ -0,0 +1,95 @@
+/*******************************************************************************\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 org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class DirectGraphWriter extends GraphWriterPartial {\r
+\r
+       Resource current;\r
+       WriteGraph wg;\r
+               \r
+       public DirectGraphWriter(WriteGraph graph) {\r
+               super(graph);\r
+               this.wg = graph;\r
+               this.current = null;\r
+       }\r
+\r
+    @Override\r
+    public GraphWriter create() throws DatabaseException {\r
+        current = wg.newResource();\r
+        return this;\r
+    }\r
+\r
+       @Override\r
+       public GraphWriter create(Resource type) throws DatabaseException {\r
+               current = wg.newResource();\r
+       Layer0 b = Layer0.getInstance(wg);\r
+               wg.claim(current, b.InstanceOf, null, type);\r
+               return this;\r
+       }\r
+\r
+       @Override\r
+       public Resource get() {\r
+               return current;\r
+       }\r
+\r
+       @Override\r
+       public GraphWriter handle(Resource s) {\r
+               current = s;\r
+               return this;\r
+       }\r
+\r
+       @Override\r
+       public GraphWriter let(Resource p, Resource o) throws DatabaseException {\r
+               if(current == null)\r
+                       throw new RuntimeException("Should call create or handle before let.");\r
+               wg.claim(current, p, o);\r
+               return this;\r
+       }\r
+\r
+       @Override\r
+       public GraphWriter let(Resource p, Object value, Resource dataType) throws DatabaseException {\r
+               if(current == null)\r
+                       throw new RuntimeException("Should call create or handle before let.");\r
+               Resource temp = wg.newResource();               \r
+               wg.claim(temp, l0.InstanceOf, null, dataType);\r
+               wg.claimValue(temp, value);\r
+               wg.claim(current, p, temp);\r
+               return this;\r
+       }\r
+\r
+       @Override\r
+       public GraphWriter flush() throws DatabaseException {\r
+               wg.flushCluster();\r
+               return this;\r
+       }\r
+       \r
+    @Override\r
+    public GraphWriter createLiteral(Object value, Resource dataType) throws DatabaseException {\r
+        current = wg.newResource();\r
+        wg.claim(current, l0.InstanceOf, null, dataType);\r
+        wg.claimValue(current, value);\r
+        return this;\r
+    }\r
+\r
+    @Override\r
+    public GraphWriter createInverse(Resource r) throws DatabaseException {\r
+        current = wg.newResource();\r
+        wg.claim(r, l0.InverseOf, current);\r
+        return this;\r
+    }\r
+\r
+}\r