1 package org.simantics.sysdyn.modelImport.model.expression;
\r
3 import java.util.ArrayList;
\r
4 import java.util.List;
\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
16 public class EnumerationExpression extends Expression {
\r
18 private List<Enumeration> enumerations;
\r
19 private List<EnumerationSubExpression> subExpressions;
\r
21 public EnumerationExpression(List<Enumeration> enumerations) {
\r
22 this.enumerations = enumerations;
\r
23 this.subExpressions = new ArrayList<EnumerationSubExpression>();
\r
26 public void addExpression(Expression expression, String...indices) {
\r
27 subExpressions.add(new EnumerationSubExpression(expression, indices));
\r
31 public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {
\r
32 SysdynResource sr = SysdynResource.getInstance(graph);
\r
34 for (EnumerationSubExpression subexpr : subExpressions) {
\r
35 StringBuilder range = new StringBuilder();
\r
37 for (int i = 0; i < subexpr.indices.length; i++) {
\r
40 range.append(subexpr.indices[i]);
\r
44 Resource res = subexpr.expression.write(graph, parent, context);
\r
46 graph.claimLiteral(res, sr.Expression_arrayRange, range.toString(), Bindings.STRING);
\r
49 ArrayList<Resource> indexlist = new ArrayList<Resource>();
\r
50 for (Enumeration enumeration : enumerations) {
\r
51 indexlist.add(enumeration.getResource());
\r
54 // write array index list
\r
55 graph.claim(parent, sr.Variable_arrayIndexesList, ListUtils.create(graph, indexlist));
\r
57 // set active expression?
\r
59 return getResource();
\r
63 public Resource getExpressionType(ReadGraph graph) {
\r
64 return subExpressions.get(0).expression.getExpressionType(graph);
\r
68 public Resource getResource() {
\r
69 return subExpressions.get(0).expression.getResource();
\r
72 private class EnumerationSubExpression {
\r
74 Expression expression;
\r
77 public EnumerationSubExpression(Expression expression, String...indices) {
\r
78 this.expression = expression;
\r
79 this.indices = indices;
\r