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