From: lempinen Date: Thu, 2 Dec 2010 14:42:47 +0000 (+0000) Subject: Simple models with array variables are working. No equation, variable name or other... X-Git-Tag: 2011-04-05-db-merge-trunk~64 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=367b8fbd9c8e1a9528026885c9e659c6d8853626;p=simantics%2Fsysdyn.git Simple models with array variables are working. No equation, variable name or other validations are available. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18913 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java index 163f9abb..4cc2b226 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -443,7 +443,9 @@ public class SysdynModel implements IMappingListener, IModel { //FIXME: more general solution? IExpression expression = variable.getExpressions().getExpressions().get(0); if (expression instanceof ParameterExpression) { - inits.put(prefix + variable.getName(), "" +((ParameterExpression)expression).getValue()); + Double value = ((ParameterExpression)expression).getValue(); + if(value != null) + inits.put(prefix + variable.getName(), "" + value); } } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java index 96aed4cc..33dc5760 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java @@ -34,16 +34,5 @@ public class Enumeration extends Variable { public ArrayList getEnumerationIndexes() { return enumerationIndexes.getEnumerationIndexes(); } - - public int indexOf(String index) { - int result = -1; - ArrayList indexes = getEnumerationIndexes(); - for(int i = 0; i < indexes.size(); i++) { - if(indexes.get(i).equals(index)) { - result = i +1; - break; - } - } - return result; - } + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndexUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndexUtils.java new file mode 100644 index 00000000..d7eaf9ae --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndexUtils.java @@ -0,0 +1,88 @@ +package org.simantics.sysdyn.representation; + +import java.util.ArrayList; +import java.util.StringTokenizer; + +public class IndexUtils { + + public static int indexOf(Enumeration enumeration, String index) { + int result = -1; + ArrayList indexes = enumeration.getEnumerationIndexes(); + for(int i = 0; i < indexes.size(); i++) { + if(indexes.get(i).getName().equals(index)) { + result = i +1; + break; + } + } + return result; + } + + + public static String rangeToIndexes(Variable variable, String range) { + StringBuilder sb = new StringBuilder(); + ArrayIndexes arrayIndexes = variable.getArrayIndexes(); + if(arrayIndexes == null || range == null) + return ""; + ArrayList enumerations = arrayIndexes.getEnumerations(); + StringTokenizer st = new StringTokenizer(range, "[]:,", true); + int index = 0; + while(st.hasMoreTokens()) { + String rangeToken = st.nextToken().trim(); + if(rangeToken.matches("[\\[\\]:]")) { + sb.append(rangeToken); + } else if (rangeToken.equals(",")) { + sb.append(rangeToken); + index++; + } else if(rangeToken.equals(enumerations.get(index).getName())) { + sb.append(":"); + } else { + int i = indexOf(enumerations.get(index), rangeToken); + sb.append(i); + } + + } + return sb.toString(); + } + + public static String equationRangesToIndexes(Variable variable, String equation) { + if(equation == null || !equation.contains("[")) return equation; + + StringBuilder result = new StringBuilder(); + String delimiters = "+-*/(){}[],. "; + StringTokenizer st = new StringTokenizer(equation, delimiters, true); + String prevToken = st.nextToken(); + result.append(prevToken); + while (st.hasMoreTokens()) { + String nextToken = st.nextToken(); + if (nextToken.equals("[")) { + StringBuilder range = new StringBuilder(); + range.append("["); + String rangeToken = st.nextToken(); + while(!rangeToken.equals("]")) { + range.append(rangeToken); + rangeToken = st.nextToken(); + } + range.append("]"); + + Variable prevVar = getVariable(variable.getParentConfiguration(), prevToken); + result.append(rangeToIndexes(prevVar, range.toString())); + } else { + result.append(nextToken); + } + prevToken = nextToken; + } + return result.toString(); + } + + private static Variable getVariable(Configuration configuration, String name) { + for(IElement element : configuration.getElements()) { + if(element instanceof Variable) { + Variable variable = (Variable) element; + if(variable.getName().equals(name)) { + return variable; + } + } + } + return null; + } +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java index 09949c58..613feb3f 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java @@ -14,6 +14,7 @@ package org.simantics.sysdyn.representation.expressions; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.representation.IndependentVariable; +import org.simantics.sysdyn.representation.IndexUtils; @GraphType("http://www.simantics.org/Sysdyn-1.0/NormalExpression") public class NormalExpression extends Expression { @@ -28,7 +29,8 @@ public class NormalExpression extends Expression { @Override public String getEquation(IndependentVariable variable) { - return " " + variable.getName() + this.getArrayRange() + " = " + equation + ";\n"; + String equation = IndexUtils.equationRangesToIndexes(variable, this.equation); + return " " + variable.getName() + IndexUtils.rangeToIndexes(variable, this.getArrayRange()) + " = " + equation + ";\n"; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java index 91ec0c01..d09964fe 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java @@ -14,6 +14,7 @@ package org.simantics.sysdyn.representation.expressions; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.representation.IndependentVariable; +import org.simantics.sysdyn.representation.IndexUtils; @GraphType("http://www.simantics.org/Sysdyn-1.0/ParameterExpression") public class ParameterExpression extends Expression { @@ -35,10 +36,14 @@ public class ParameterExpression extends Expression { */ @Override public String getEquation(IndependentVariable variable) { - return " " + variable.getName() + this.getArrayRange() + " = " + equation + ";\n"; + return " " + variable.getName() + IndexUtils.rangeToIndexes(variable, this.getArrayRange()) + " = " + equation + ";\n"; }; public Double getValue() { - return Double.parseDouble(equation); + try { + return Double.parseDouble(equation); + } catch(NumberFormatException e) { + return null; + } } }