--- /dev/null
+/*******************************************************************************\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
--- /dev/null
+/*******************************************************************************\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
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
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