]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.layer0.utils.writer;\r
13 \r
14 import org.simantics.db.Resource;\r
15 import org.simantics.db.WriteGraph;\r
16 import org.simantics.db.exception.DatabaseException;\r
17 import org.simantics.layer0.Layer0;\r
18 \r
19 public class DirectGraphWriter extends GraphWriterPartial {\r
20 \r
21         Resource current;\r
22         WriteGraph wg;\r
23                 \r
24         public DirectGraphWriter(WriteGraph graph) {\r
25                 super(graph);\r
26                 this.wg = graph;\r
27                 this.current = null;\r
28         }\r
29 \r
30     @Override\r
31     public GraphWriter create() throws DatabaseException {\r
32         current = wg.newResource();\r
33         return this;\r
34     }\r
35 \r
36         @Override\r
37         public GraphWriter create(Resource type) throws DatabaseException {\r
38                 current = wg.newResource();\r
39         Layer0 b = Layer0.getInstance(wg);\r
40                 wg.claim(current, b.InstanceOf, null, type);\r
41                 return this;\r
42         }\r
43 \r
44         @Override\r
45         public Resource get() {\r
46                 return current;\r
47         }\r
48 \r
49         @Override\r
50         public GraphWriter handle(Resource s) {\r
51                 current = s;\r
52                 return this;\r
53         }\r
54 \r
55         @Override\r
56         public GraphWriter let(Resource p, Resource o) throws DatabaseException {\r
57                 if(current == null)\r
58                         throw new RuntimeException("Should call create or handle before let.");\r
59                 wg.claim(current, p, o);\r
60                 return this;\r
61         }\r
62 \r
63         @Override\r
64         public GraphWriter let(Resource p, Object value, Resource dataType) throws DatabaseException {\r
65                 if(current == null)\r
66                         throw new RuntimeException("Should call create or handle before let.");\r
67                 Resource temp = wg.newResource();               \r
68                 wg.claim(temp, l0.InstanceOf, null, dataType);\r
69                 wg.claimValue(temp, value);\r
70                 wg.claim(current, p, temp);\r
71                 return this;\r
72         }\r
73 \r
74         @Override\r
75         public GraphWriter flush() throws DatabaseException {\r
76                 wg.flushCluster();\r
77                 return this;\r
78         }\r
79         \r
80     @Override\r
81     public GraphWriter createLiteral(Object value, Resource dataType) throws DatabaseException {\r
82         current = wg.newResource();\r
83         wg.claim(current, l0.InstanceOf, null, dataType);\r
84         wg.claimValue(current, value);\r
85         return this;\r
86     }\r
87 \r
88     @Override\r
89     public GraphWriter createInverse(Resource r) throws DatabaseException {\r
90         current = wg.newResource();\r
91         wg.claim(r, l0.InverseOf, current);\r
92         return this;\r
93     }\r
94 \r
95 }\r