]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
00d99797934ab3273afbb29060e3c694c42884c1
[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 import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
17 \r
18 public class EnumerationExpression extends Expression {\r
19         \r
20         private List<Enumeration> enumerations;\r
21         private List<EnumerationSubExpression> subExpressions;\r
22         \r
23         public EnumerationExpression(List<Enumeration> enumerations) {\r
24                 this.enumerations = enumerations;\r
25                 this.subExpressions = new ArrayList<EnumerationSubExpression>();\r
26         }\r
27         \r
28         public void addExpression(Expression expression, String...indices) {\r
29 //              for (int i = 0; i < indices.length; i++) {\r
30 //                      if (!enumerations.get(i).getValues().contains(indices)) {\r
31 //                              System.err.println("tried adding an enumeration expression with incorrect indices");\r
32 //                              return;\r
33 //                      }\r
34 //              }\r
35                 subExpressions.add(new EnumerationSubExpression(expression, indices));\r
36         }\r
37         \r
38         public List<EnumerationSubExpression> getExpressions() {\r
39                 return subExpressions;\r
40         }\r
41         \r
42         @Override\r
43         public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
44                 SysdynResource sr = SysdynResource.getInstance(graph);\r
45                 \r
46                 for (EnumerationSubExpression subexpr : subExpressions) {\r
47                         StringBuilder range = new StringBuilder();\r
48                         range.append('[');\r
49                         for (int i = 0; i < subexpr.indices.length; i++) {\r
50                                 if (i > 0)\r
51                                         range.append(',');\r
52                                 range.append(subexpr.indices[i]);\r
53                         }\r
54                         range.append(']');\r
55                         \r
56                         Resource res = subexpr.expression.write(graph, parent, context);\r
57                         \r
58                         graph.claimLiteral(res, sr.Expression_arrayRange, range.toString(), Bindings.STRING);\r
59                 }\r
60                 \r
61                 ArrayList<Resource> indexlist = new ArrayList<Resource>();\r
62                 for (Enumeration enumeration : enumerations) {\r
63                         indexlist.add(enumeration.getResource());\r
64                 }\r
65                 \r
66                 // write array index list\r
67                 graph.claim(parent, sr.Variable_arrayIndexesList, ListUtils.create(graph, indexlist));\r
68                 \r
69                 // set active expression?\r
70                 \r
71                 return getResource();\r
72         }\r
73         \r
74         private class EnumerationSubExpression {\r
75                 \r
76                 Expression expression;\r
77                 String[] indices;\r
78                 \r
79                 public EnumerationSubExpression(Expression expression, String...indices) {\r
80                         this.expression = expression;\r
81                         this.indices = indices;\r
82                 }\r
83         }\r
84 \r
85         @Override\r
86         public Resource getExpressionType(ReadGraph graph) {\r
87                 return subExpressions.get(0).expression.getExpressionType(graph);\r
88         }\r
89         \r
90         @Override\r
91         public Resource getResource() {\r
92                 return subExpressions.get(0).expression.getResource();\r
93         }\r
94 }\r