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
17 public class EnumerationExpression extends Expression {
\r
19 private List<SubscriptSubExpression> subExpressions;
\r
21 public EnumerationExpression(Expression expression, String...indices) {
\r
22 subExpressions = new ArrayList<SubscriptSubExpression>();
\r
23 addExpression(expression, indices);
\r
26 public void addExpression(Expression expression, String...indices) {
\r
27 subExpressions.add(new SubscriptSubExpression(expression, indices));
\r
30 public void addExpressions(List<SubscriptSubExpression> expressions) {
\r
31 subExpressions.addAll(expressions);
\r
34 public List<SubscriptSubExpression> getExpressions() {
\r
35 return subExpressions;
\r
39 public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {
\r
40 SysdynResource sr = SysdynResource.getInstance(graph);
\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
47 for (SubscriptSubExpression subexpr : subExpressions) {
\r
48 StringBuilder range = new StringBuilder();
\r
50 for (int i = 0; i < subexpr.indices.length; i++) {
\r
53 range.append(subexpr.indices[i]);
\r
55 enumerationIndices.get(i).add(subexpr.indices[i]);
\r
59 Resource res = subexpr.expression.write(graph, parent, context);
\r
61 graph.claimLiteral(res, sr.Expression_arrayRange, range.toString(), Bindings.STRING);
\r
64 ArrayList<Resource> enumerations = new ArrayList<Resource>();
\r
65 for (Set<String> set : enumerationIndices) {
\r
66 enumerations.add(context.getEnumeration(set).getResource());
\r
69 // write array index list
\r
70 graph.claim(parent, sr.Variable_arrayIndexesList, ListUtils.create(graph, enumerations));
\r
72 // set active expression?
\r
74 return getResource();
\r
77 private class SubscriptSubExpression {
\r
79 Expression expression;
\r
82 public SubscriptSubExpression(Expression expression, String...indices) {
\r
83 this.expression = expression;
\r
84 this.indices = indices;
\r
89 public Resource getExpressionType(ReadGraph graph) {
\r
90 return subExpressions.get(0).expression.getExpressionType(graph);
\r
94 public Resource getResource() {
\r
95 return subExpressions.get(0).expression.getResource();
\r