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