]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
c297535f851167d883538efbb761166c81ef0936
[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[] dimensions, Variable variable) {\r
38                 super(dimensions);\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 ModelVariable(double x, double y, double w, double h, Variable variable) {\r
47                 super(x, y, w, h);\r
48                 this.name = variable.getName();\r
49                 this.expression = variable.getExpression();\r
50                 this.range = variable.getRange();\r
51                 this.unit = variable.getUnit();\r
52                 this.description = variable.getDescription();\r
53         }\r
54         \r
55         public String getName() {\r
56                 return name;\r
57         }\r
58 \r
59         @Override\r
60         public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
61                 Layer0 l0 = Layer0.getInstance(graph);\r
62                 ModelingResources mr = ModelingResources.getInstance(graph);\r
63                 SysdynResource sr = SysdynResource.getInstance(graph);\r
64 \r
65                 variable = GraphUtils.create2(graph, getVariableType(graph), \r
66                                 l0.HasName, name,\r
67                                 l0.PartOf, parent);\r
68                 graph.claim(variable, mr.Mapped, variable);\r
69                 \r
70                 // TODO: write references to enumerations here?\r
71                 \r
72                 if (expression != null) {\r
73                         expression.write(graph, variable, context);\r
74                 }\r
75                 if (range != null) {\r
76                         range.write(graph, variable, context);\r
77                 }\r
78                 if (unit != null && !unit.isEmpty()) {\r
79                         graph.claimLiteral(variable, sr.Variable_unit, unit, Bindings.STRING);\r
80                 }\r
81                 if (description != null && !description.isEmpty()) {\r
82                         graph.claimLiteral(variable, l0.HasDescription, description, Bindings.STRING);\r
83                 }\r
84                 \r
85                 Resource symbol = super.write(graph, parent, context);\r
86                 graph.claim(symbol, mr.ElementToComponent, variable);\r
87 \r
88                 return variable;\r
89         }\r
90         \r
91         public abstract Resource getVariableType(ReadGraph graph);\r
92         \r
93         @Override\r
94         public Resource getResource() {\r
95                 return variable;\r
96         }\r
97 \r
98 }\r