]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
fd669dc6e55c4a7811d8e3f4ba7e066c579dc54f
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.modelImport.model.element;\r
2 \r
3 import java.util.List;\r
4 \r
5 import org.simantics.databoard.Bindings;\r
6 import org.simantics.db.Resource;\r
7 import org.simantics.db.WriteGraph;\r
8 import org.simantics.db.exception.DatabaseException;\r
9 import org.simantics.layer0.Layer0;\r
10 import org.simantics.layer0.utils.direct.GraphUtils;\r
11 import org.simantics.modeling.ModelingResources;\r
12 import org.simantics.sysdyn.SysdynResource;\r
13 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
14 import org.simantics.sysdyn.modelImport.model.expression.IExpression;\r
15 import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
16 import org.simantics.sysdyn.modelImport.model.support.Range;\r
17 import org.simantics.sysdyn.modelImport.model.support.Variable;\r
18 \r
19 public abstract class ModelVariable extends Element {\r
20 \r
21         protected String name;\r
22         protected IExpression expression;\r
23         protected Range range;\r
24         protected String unit;\r
25         protected String description;\r
26         \r
27         protected List<Enumeration> enumerations;\r
28 \r
29         public ModelVariable(double x, double y, double w, double h, String name, IExpression expression, Range range, String unit, String description) {\r
30                 super(x, y, w, h);\r
31                 this.name = name;\r
32                 this.expression = expression;\r
33                 this.range = range;\r
34                 this.unit = unit;\r
35                 this.description = description;\r
36         }\r
37         \r
38         public ModelVariable(double x, double y, double w, double h, Variable variable) {\r
39                 super(x, y, w, h);\r
40                 this.name = variable.getName();\r
41                 this.expression = variable.getExpression();\r
42                 this.range = variable.getRange();\r
43                 this.unit = variable.getUnit();\r
44                 this.description = variable.getDescription();\r
45         }\r
46         \r
47         public String getName() {\r
48                 return name;\r
49         }\r
50 \r
51         public Resource createVariable(WriteGraph graph, Resource type, Resource parent, WriteContext context) \r
52                         throws DatabaseException {\r
53                 Layer0 l0 = Layer0.getInstance(graph);\r
54                 ModelingResources mr = ModelingResources.getInstance(graph);\r
55                 SysdynResource sr = SysdynResource.getInstance(graph);\r
56 \r
57                 Resource variable = GraphUtils.create2(graph, type, \r
58                                 l0.HasName, name,\r
59                                 l0.PartOf, parent);\r
60                 graph.claim(variable, mr.Mapped, variable);\r
61                 \r
62                 expression.write(graph, variable, context);\r
63                 \r
64                 // TODO: write references to enumerations here?\r
65                 \r
66                 if (range != null) {\r
67                         range.write(graph, variable, context);\r
68                 }\r
69                 if (unit != null && !unit.isEmpty()) {\r
70                         graph.claimLiteral(variable, sr.Variable_unit, unit, Bindings.STRING);\r
71                 }\r
72                 if (description != null && !description.isEmpty()) {\r
73                         graph.claimLiteral(variable, l0.HasDescription, description, Bindings.STRING);\r
74                 }\r
75 \r
76                 return variable;\r
77         }\r
78 \r
79 }\r