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