--- /dev/null
+package org.simantics.sysdyn.representation;\r
+\r
+import java.util.ArrayList;\r
+import java.util.StringTokenizer;\r
+\r
+public class IndexUtils {\r
+\r
+ public static int indexOf(Enumeration enumeration, String index) {\r
+ int result = -1;\r
+ ArrayList<EnumerationIndex> indexes = enumeration.getEnumerationIndexes();\r
+ for(int i = 0; i < indexes.size(); i++) {\r
+ if(indexes.get(i).getName().equals(index)) {\r
+ result = i +1;\r
+ break;\r
+ }\r
+ }\r
+ return result;\r
+ }\r
+ \r
+ \r
+ public static String rangeToIndexes(Variable variable, String range) {\r
+ StringBuilder sb = new StringBuilder();\r
+ ArrayIndexes arrayIndexes = variable.getArrayIndexes();\r
+ if(arrayIndexes == null || range == null)\r
+ return "";\r
+ ArrayList<Enumeration> enumerations = arrayIndexes.getEnumerations();\r
+ StringTokenizer st = new StringTokenizer(range, "[]:,", true);\r
+ int index = 0;\r
+ while(st.hasMoreTokens()) {\r
+ String rangeToken = st.nextToken().trim();\r
+ if(rangeToken.matches("[\\[\\]:]")) {\r
+ sb.append(rangeToken);\r
+ } else if (rangeToken.equals(",")) {\r
+ sb.append(rangeToken);\r
+ index++;\r
+ } else if(rangeToken.equals(enumerations.get(index).getName())) {\r
+ sb.append(":");\r
+ } else {\r
+ int i = indexOf(enumerations.get(index), rangeToken);\r
+ sb.append(i);\r
+ }\r
+ \r
+ }\r
+ return sb.toString();\r
+ }\r
+ \r
+ public static String equationRangesToIndexes(Variable variable, String equation) {\r
+ if(equation == null || !equation.contains("[")) return equation;\r
+ \r
+ StringBuilder result = new StringBuilder();\r
+ String delimiters = "+-*/(){}[],. ";\r
+ StringTokenizer st = new StringTokenizer(equation, delimiters, true);\r
+ String prevToken = st.nextToken();\r
+ result.append(prevToken);\r
+ while (st.hasMoreTokens()) {\r
+ String nextToken = st.nextToken();\r
+ if (nextToken.equals("[")) {\r
+ StringBuilder range = new StringBuilder();\r
+ range.append("[");\r
+ String rangeToken = st.nextToken();\r
+ while(!rangeToken.equals("]")) {\r
+ range.append(rangeToken);\r
+ rangeToken = st.nextToken();\r
+ }\r
+ range.append("]");\r
+ \r
+ Variable prevVar = getVariable(variable.getParentConfiguration(), prevToken);\r
+ result.append(rangeToIndexes(prevVar, range.toString()));\r
+ } else {\r
+ result.append(nextToken);\r
+ }\r
+ prevToken = nextToken;\r
+ }\r
+ return result.toString();\r
+ }\r
+ \r
+ private static Variable getVariable(Configuration configuration, String name) {\r
+ for(IElement element : configuration.getElements()) {\r
+ if(element instanceof Variable) {\r
+ Variable variable = (Variable) element;\r
+ if(variable.getName().equals(name)) {\r
+ return variable;\r
+ }\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+}\r
import org.simantics.objmap.annotations.GraphType;\r
import org.simantics.objmap.annotations.RelatedValue;\r
import org.simantics.sysdyn.representation.IndependentVariable;\r
+import org.simantics.sysdyn.representation.IndexUtils;\r
\r
@GraphType("http://www.simantics.org/Sysdyn-1.0/NormalExpression")\r
public class NormalExpression extends Expression {\r
\r
@Override\r
public String getEquation(IndependentVariable variable) {\r
- return " " + variable.getName() + this.getArrayRange() + " = " + equation + ";\n";\r
+ String equation = IndexUtils.equationRangesToIndexes(variable, this.equation);\r
+ return " " + variable.getName() + IndexUtils.rangeToIndexes(variable, this.getArrayRange()) + " = " + equation + ";\n";\r
}\r
\r
}\r
import org.simantics.objmap.annotations.GraphType;\r
import org.simantics.objmap.annotations.RelatedValue;\r
import org.simantics.sysdyn.representation.IndependentVariable;\r
+import org.simantics.sysdyn.representation.IndexUtils;\r
\r
@GraphType("http://www.simantics.org/Sysdyn-1.0/ParameterExpression")\r
public class ParameterExpression extends Expression {\r
*/\r
@Override\r
public String getEquation(IndependentVariable variable) {\r
- return " " + variable.getName() + this.getArrayRange() + " = " + equation + ";\n";\r
+ return " " + variable.getName() + IndexUtils.rangeToIndexes(variable, this.getArrayRange()) + " = " + equation + ";\n";\r
};\r
\r
public Double getValue() {\r
- return Double.parseDouble(equation);\r
+ try {\r
+ return Double.parseDouble(equation);\r
+ } catch(NumberFormatException e) {\r
+ return null;\r
+ }\r
}\r
}\r