]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Better error for array assignment into scalar.
authorvillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 7 Oct 2014 12:39:41 +0000 (12:39 +0000)
committervillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 7 Oct 2014 12:39:41 +0000 (12:39 +0000)
Fixed handling of '.+' and '.-'
refs #5224

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30385 ac1ea38d-2e2b-0410-8846-a27921b304fc

fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ElementwiseAddition.java [new file with mode: 0644]
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ElementwiseSubtraction.java [new file with mode: 0644]
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Variable.java

diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ElementwiseAddition.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ElementwiseAddition.java
new file mode 100644 (file)
index 0000000..ff08ca6
--- /dev/null
@@ -0,0 +1,79 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 Semantum Oy.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package fi.semantum.sysdyn.solver;\r
+\r
+import java.util.Map;\r
+\r
+import fi.semantum.sysdyn.solver.Array.Modifier2;\r
+\r
+public class ElementwiseAddition implements IExpression {\r
+       \r
+    public IExpression exp1;\r
+    public IExpression exp2;\r
+    \r
+    public ElementwiseAddition(IExpression exp1, IExpression exp2) {\r
+       this.exp1 = exp1;\r
+       this.exp2 = exp2;\r
+    }\r
+    \r
+    @Override\r
+    public String toString() {\r
+       return exp1 + " + " + exp2;\r
+    }\r
+    \r
+       @Override\r
+       public Object evaluate(IEnvironment environment) {\r
+               Object o1 = exp1.evaluate(environment);\r
+               Object o2 = exp2.evaluate(environment);\r
+               if(o1 == null || o2 == null) return null; \r
+               if(o1 instanceof Double && o2 instanceof Double) {\r
+                       return (Double)o1 + (Double)o2;\r
+               }\r
+               if(o1 instanceof Array && o2 instanceof Array) {\r
+                       Array la = (Array)o1;\r
+                       Array ra = (Array)o2;\r
+                       return la.copy2(ra, new Modifier2() {\r
+                               \r
+                               @Override\r
+                               public Object modify(Object o1, Object o2) {\r
+                                       if(o1 instanceof Double && o2 instanceof Double) return ((Double)o1)+((Double)o2);\r
+                                       throw new IllegalStateException("Tried to add a non-numerical array");\r
+                               }\r
+                       });\r
+               }\r
+               throw new IllegalStateException();\r
+       }\r
+       \r
+       @Override\r
+       public IExpression withBase(IFrame frame, String prefix) {\r
+               return new ElementwiseAddition(exp1.withBase(frame, prefix), exp2.withBase(frame, prefix));\r
+       }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               exp1 = exp1.rewrite(frame, copies);\r
+               exp2 = exp2.rewrite(frame, copies);\r
+               return this;\r
+       }\r
+       \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               exp1.accept(visitor);\r
+               exp2.accept(visitor);\r
+       }\r
+       \r
+}\r
diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ElementwiseSubtraction.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ElementwiseSubtraction.java
new file mode 100644 (file)
index 0000000..55a3662
--- /dev/null
@@ -0,0 +1,80 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 Semantum Oy.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package fi.semantum.sysdyn.solver;\r
+\r
+import java.util.Map;\r
+\r
+import fi.semantum.sysdyn.solver.Array.Modifier2;\r
+\r
+public class ElementwiseSubtraction implements IExpression {\r
+       \r
+    public IExpression exp1;\r
+    public IExpression exp2;\r
+    \r
+    public ElementwiseSubtraction(IExpression exp1, IExpression exp2) {\r
+       if(exp1 == null) exp1 = new Constant("0");\r
+       this.exp1 = exp1;\r
+       this.exp2 = exp2;\r
+    }\r
+    \r
+    @Override\r
+    public String toString() {\r
+       return exp1 + " - " + exp2;\r
+    }\r
+    \r
+       @Override\r
+       public Object evaluate(IEnvironment environment) {\r
+               Object o1 = exp1.evaluate(environment);\r
+               Object o2 = exp2.evaluate(environment);\r
+               if(o1 == null || o2 == null) return null; \r
+               if(o1 instanceof Double && o2 instanceof Double) {\r
+                       return (Double)o1 - (Double)o2;\r
+               }\r
+               if(o1 instanceof Array && o2 instanceof Array) {\r
+                       Array la = (Array)o1;\r
+                       Array ra = (Array)o2;\r
+                       return la.copy2(ra, new Modifier2() {\r
+                               \r
+                               @Override\r
+                               public Object modify(Object o1, Object o2) {\r
+                                       if(o1 instanceof Double && o2 instanceof Double) return ((Double)o1)-((Double)o2);\r
+                                       throw new IllegalStateException("Tried to subtract a non-numerical array");\r
+                               }\r
+                       });\r
+               }\r
+               throw new IllegalStateException();\r
+       }\r
+       \r
+       @Override\r
+       public IExpression withBase(IFrame frame, String prefix) {\r
+               return new ElementwiseSubtraction(exp1.withBase(frame, prefix), exp2.withBase(frame, prefix));\r
+       }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+    \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               exp1 = exp1.rewrite(frame, copies);\r
+               exp2 = exp2.rewrite(frame, copies);\r
+               return this;\r
+       }\r
+       \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               exp1.accept(visitor);\r
+               exp2.accept(visitor);\r
+       }\r
+       \r
+}\r
index 14c73e870c23b82eb0df8bfc28518b5e2710e2d2..32695d7417049f5454c02d48c93c459925b87dd5 100644 (file)
@@ -552,7 +552,9 @@ public class Parser {
                                String op = ((String)walk((SimpleNode)n.jjtGetChild(i), indent+2, model)).trim();\r
                                IExpression exp2 = (IExpression)walk((SimpleNode)n.jjtGetChild(i+1), indent+2, frame);\r
                                if("+".equals(op)) left = new Addition(left, exp2);\r
+                               else if(".+".equals(op)) left = new ElementwiseAddition(left, exp2);\r
                                else if("-".equals(op)) left = new Subtraction(left, exp2);\r
+                               else if(".-".equals(op)) left = new ElementwiseSubtraction(left, exp2);\r
                        }\r
 \r
                        return left;\r
index fe222fc86bbbf5b79186ef20c6a0502e94ac708d..581ccecb8815a105be407b8ec3826b86330d6658 100644 (file)
@@ -136,8 +136,9 @@ public class Variable implements IExpression {
                if(fullDimension == 1) {\r
                        if(!(value instanceof Double)) {\r
                                Array arr = (Array)value;\r
-                               if(arr.size(0) != 1)\r
-                                       throw new IllegalStateException();\r
+                               int arrSize = arr.dimension(); \r
+                               if(arrSize != 1)\r
+                                       throw new ExecutionException("Trying to assign an array into a scalar variable");\r
                        }\r
                } else {\r
                        if(subscripts == null) {\r