]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
c3246adc35343cca9c0090d8bcf4c34dc07c5499
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.modelImport.model.expression;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.List;\r
5 \r
6 import org.simantics.databoard.Bindings;\r
7 import org.simantics.db.ReadGraph;\r
8 import org.simantics.db.Resource;\r
9 import org.simantics.db.WriteGraph;\r
10 import org.simantics.db.common.utils.ListUtils;\r
11 import org.simantics.db.exception.DatabaseException;\r
12 import org.simantics.sysdyn.SysdynResource;\r
13 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
14 import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
15 \r
16 public class EnumerationExpression extends Expression {\r
17         \r
18         private List<Enumeration> enumerations;\r
19         private List<EnumerationSubExpression> subExpressions;\r
20         \r
21         public EnumerationExpression(List<Enumeration> enumerations) {\r
22                 this.enumerations = enumerations;\r
23                 this.subExpressions = new ArrayList<EnumerationSubExpression>();\r
24         }\r
25         \r
26         public void addExpression(Expression expression, String...indices) {\r
27                 subExpressions.add(new EnumerationSubExpression(expression, indices));\r
28         }\r
29         \r
30         @Override\r
31         public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
32                 SysdynResource sr = SysdynResource.getInstance(graph);\r
33                 \r
34                 for (EnumerationSubExpression subexpr : subExpressions) {\r
35                         StringBuilder range = new StringBuilder();\r
36                         range.append('[');\r
37                         for (int i = 0; i < subexpr.indices.length; i++) {\r
38                                 if (i > 0)\r
39                                         range.append(',');\r
40                                 range.append(subexpr.indices[i]);\r
41                         }\r
42                         range.append(']');\r
43                         \r
44                         Resource res = subexpr.expression.write(graph, parent, context);\r
45                         \r
46                         graph.claimLiteral(res, sr.Expression_arrayRange, range.toString(), Bindings.STRING);\r
47                 }\r
48                 \r
49                 ArrayList<Resource> indexlist = new ArrayList<Resource>();\r
50                 for (Enumeration enumeration : enumerations) {\r
51                         indexlist.add(enumeration.getResource());\r
52                 }\r
53                 \r
54                 // write array index list\r
55                 graph.claim(parent, sr.Variable_arrayIndexesList, ListUtils.create(graph, indexlist));\r
56                 \r
57                 // set active expression?\r
58                 \r
59                 return getResource();\r
60         }\r
61 \r
62         @Override\r
63         public Resource getExpressionType(ReadGraph graph) {\r
64                 return subExpressions.get(0).expression.getExpressionType(graph);\r
65         }\r
66         \r
67         @Override\r
68         public Resource getResource() {\r
69                 return subExpressions.get(0).expression.getResource();\r
70         }\r
71         \r
72         private class EnumerationSubExpression {\r
73                 \r
74                 Expression expression;\r
75                 String[] indices;\r
76                 \r
77                 public EnumerationSubExpression(Expression expression, String...indices) {\r
78                         this.expression = expression;\r
79                         this.indices = indices;\r
80                 }\r
81         }\r
82 }\r