]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Simple models with array variables are working. No equation, variable name or other...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 2 Dec 2010 14:42:47 +0000 (14:42 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 2 Dec 2010 14:42:47 +0000 (14:42 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18913 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndexUtils.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java

index 163f9abb4048063e481ae4cc22a4a55773e5b60a..4cc2b2264c75f827c5fd2d64795b41f8c5afdc72 100644 (file)
@@ -443,7 +443,9 @@ public class SysdynModel implements IMappingListener, IModel {
                                //FIXME: more general solution?\r
                                IExpression expression = variable.getExpressions().getExpressions().get(0);\r
                                if (expression instanceof ParameterExpression) {\r
-                                       inits.put(prefix + variable.getName(), "" +((ParameterExpression)expression).getValue());\r
+                                       Double value = ((ParameterExpression)expression).getValue();\r
+                                       if(value != null)\r
+                                               inits.put(prefix + variable.getName(), "" + value);\r
                                }\r
                        }\r
                }\r
index 96aed4cc459f995160dd342188d74bd72f301fef..33dc5760d3e80e5c301de127e98d84c2691c5af1 100644 (file)
@@ -34,16 +34,5 @@ public class Enumeration extends Variable {
        public ArrayList<EnumerationIndex> getEnumerationIndexes() {\r
        return enumerationIndexes.getEnumerationIndexes();\r
        }\r
-       \r
-       public int indexOf(String index) {\r
-               int result = -1;\r
-               ArrayList<EnumerationIndex> indexes = getEnumerationIndexes();\r
-               for(int i = 0; i < indexes.size(); i++) {\r
-                       if(indexes.get(i).equals(index)) {\r
-                               result = i  +1;\r
-                               break;\r
-                       }\r
-               }\r
-               return result;\r
-       }\r
+\r
 }\r
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 (file)
index 0000000..d7eaf9a
--- /dev/null
@@ -0,0 +1,88 @@
+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
index 09949c5828eb607f16c3aaf251397737f9f50fae..613feb3fcebbc4b5de2fef9941686250a82c7d95 100644 (file)
@@ -14,6 +14,7 @@ package org.simantics.sysdyn.representation.expressions;
 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
@@ -28,7 +29,8 @@ public class NormalExpression extends Expression {
 \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
index 91ec0c018b8868cffe9250f7709397fb70b0d22a..d09964fe14e9dde9857cac1d02da1078c1a71d29 100644 (file)
@@ -14,6 +14,7 @@ package org.simantics.sysdyn.representation.expressions;
 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
@@ -35,10 +36,14 @@ public class ParameterExpression extends Expression {
      */\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