]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
cd2f452f28434d45f6da8ba87cbcc073636c5142
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.modelImport.model.element;\r
2 \r
3 import org.simantics.databoard.Bindings;\r
4 import org.simantics.db.ReadGraph;\r
5 import org.simantics.db.Resource;\r
6 import org.simantics.db.WriteGraph;\r
7 import org.simantics.db.exception.DatabaseException;\r
8 import org.simantics.layer0.Layer0;\r
9 import org.simantics.layer0.utils.direct.GraphUtils;\r
10 import org.simantics.modeling.ModelingResources;\r
11 import org.simantics.sysdyn.SysdynResource;\r
12 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
13 import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
14 import org.simantics.sysdyn.modelImport.model.support.Range;\r
15 import org.simantics.sysdyn.modelImport.model.support.Variable;\r
16 \r
17 public abstract class ModelVariable extends Symbol {\r
18 \r
19         private String name;\r
20         private Expression expression;\r
21         private Range range;\r
22         private String unit;\r
23         private String description;\r
24         \r
25         private Resource variable;\r
26 \r
27         public ModelVariable(double x, double y, double w, double h,\r
28                         String name, Expression expression, Range range, String unit, String description) {\r
29                 super(x, y, w, h);\r
30                 this.name = name;\r
31                 this.expression = expression;\r
32                 this.range = range;\r
33                 this.unit = unit;\r
34                 this.description = description;\r
35         }\r
36         \r
37         public ModelVariable(double x, double y, double w, double h, Variable variable) {\r
38                 super(x, y, w, h);\r
39                 this.name = variable.getName();\r
40                 this.expression = variable.getExpression();\r
41                 this.range = variable.getRange();\r
42                 this.unit = variable.getUnit();\r
43                 this.description = variable.getDescription();\r
44         }\r
45         \r
46         public String getName() {\r
47                 return name;\r
48         }\r
49 \r
50         @Override\r
51         public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
52                 Layer0 l0 = Layer0.getInstance(graph);\r
53                 ModelingResources mr = ModelingResources.getInstance(graph);\r
54                 SysdynResource sr = SysdynResource.getInstance(graph);\r
55 \r
56                 variable = GraphUtils.create2(graph, getVariableType(graph), \r
57                                 l0.HasName, name,\r
58                                 l0.PartOf, parent);\r
59                 graph.claim(variable, mr.Mapped, variable);\r
60                 \r
61                 // TODO: write references to enumerations here?\r
62                 \r
63                 if (expression != null) {\r
64                         Resource expressionList = expression.write(graph, variable, context);\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                 Resource symbol = super.write(graph, parent, context);\r
77                 graph.claim(symbol, mr.ElementToComponent, variable);\r
78 \r
79                 return variable;\r
80         }\r
81         \r
82         public abstract Resource getVariableType(ReadGraph graph);\r
83         \r
84         @Override\r
85         public Resource getResource() {\r
86                 return variable;\r
87         }\r
88 \r
89 }\r