1 package org.simantics.sysdyn.modelImport.model.expression;
\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
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
18 public class EnumerationExpression extends Expression {
\r
20 private List<Enumeration> enumerations;
\r
21 private List<EnumerationSubExpression> subExpressions;
\r
23 public EnumerationExpression(List<Enumeration> enumerations) {
\r
24 this.enumerations = enumerations;
\r
25 this.subExpressions = new ArrayList<EnumerationSubExpression>();
\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
35 subExpressions.add(new EnumerationSubExpression(expression, indices));
\r
38 public List<EnumerationSubExpression> getExpressions() {
\r
39 return subExpressions;
\r
43 public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {
\r
44 SysdynResource sr = SysdynResource.getInstance(graph);
\r
46 for (EnumerationSubExpression subexpr : subExpressions) {
\r
47 StringBuilder range = new StringBuilder();
\r
49 for (int i = 0; i < subexpr.indices.length; i++) {
\r
52 range.append(subexpr.indices[i]);
\r
56 Resource res = subexpr.expression.write(graph, parent, context);
\r
58 graph.claimLiteral(res, sr.Expression_arrayRange, range.toString(), Bindings.STRING);
\r
61 ArrayList<Resource> indexlist = new ArrayList<Resource>();
\r
62 for (Enumeration enumeration : enumerations) {
\r
63 indexlist.add(enumeration.getResource());
\r
66 // write array index list
\r
67 graph.claim(parent, sr.Variable_arrayIndexesList, ListUtils.create(graph, indexlist));
\r
69 // set active expression?
\r
71 return getResource();
\r
74 private class EnumerationSubExpression {
\r
76 Expression expression;
\r
79 public EnumerationSubExpression(Expression expression, String...indices) {
\r
80 this.expression = expression;
\r
81 this.indices = indices;
\r
86 public Resource getExpressionType(ReadGraph graph) {
\r
87 return subExpressions.get(0).expression.getExpressionType(graph);
\r
91 public Resource getResource() {
\r
92 return subExpressions.get(0).expression.getResource();
\r