From: villberg Date: Tue, 7 Oct 2014 12:39:41 +0000 (+0000) Subject: Better error for array assignment into scalar. X-Git-Tag: v1.29.0~167 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=46772ada3225444546d045db8733ba6167eb975e;p=simantics%2Fsysdyn.git Better error for array assignment into scalar. Fixed handling of '.+' and '.-' refs #5224 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30385 ac1ea38d-2e2b-0410-8846-a27921b304fc --- 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 index 00000000..ff08ca61 --- /dev/null +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ElementwiseAddition.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2013 Semantum Oy. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package fi.semantum.sysdyn.solver; + +import java.util.Map; + +import fi.semantum.sysdyn.solver.Array.Modifier2; + +public class ElementwiseAddition implements IExpression { + + public IExpression exp1; + public IExpression exp2; + + public ElementwiseAddition(IExpression exp1, IExpression exp2) { + this.exp1 = exp1; + this.exp2 = exp2; + } + + @Override + public String toString() { + return exp1 + " + " + exp2; + } + + @Override + public Object evaluate(IEnvironment environment) { + Object o1 = exp1.evaluate(environment); + Object o2 = exp2.evaluate(environment); + if(o1 == null || o2 == null) return null; + if(o1 instanceof Double && o2 instanceof Double) { + return (Double)o1 + (Double)o2; + } + if(o1 instanceof Array && o2 instanceof Array) { + Array la = (Array)o1; + Array ra = (Array)o2; + return la.copy2(ra, new Modifier2() { + + @Override + public Object modify(Object o1, Object o2) { + if(o1 instanceof Double && o2 instanceof Double) return ((Double)o1)+((Double)o2); + throw new IllegalStateException("Tried to add a non-numerical array"); + } + }); + } + throw new IllegalStateException(); + } + + @Override + public IExpression withBase(IFrame frame, String prefix) { + return new ElementwiseAddition(exp1.withBase(frame, prefix), exp2.withBase(frame, prefix)); + } + + @Override + public Object getPossibleConstant() { + return null; + } + + @Override + public IExpression rewrite(IFrame frame, Map copies) { + exp1 = exp1.rewrite(frame, copies); + exp2 = exp2.rewrite(frame, copies); + return this; + } + + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + exp1.accept(visitor); + exp2.accept(visitor); + } + +} 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 index 00000000..55a36627 --- /dev/null +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ElementwiseSubtraction.java @@ -0,0 +1,80 @@ +/******************************************************************************* + * Copyright (c) 2013 Semantum Oy. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package fi.semantum.sysdyn.solver; + +import java.util.Map; + +import fi.semantum.sysdyn.solver.Array.Modifier2; + +public class ElementwiseSubtraction implements IExpression { + + public IExpression exp1; + public IExpression exp2; + + public ElementwiseSubtraction(IExpression exp1, IExpression exp2) { + if(exp1 == null) exp1 = new Constant("0"); + this.exp1 = exp1; + this.exp2 = exp2; + } + + @Override + public String toString() { + return exp1 + " - " + exp2; + } + + @Override + public Object evaluate(IEnvironment environment) { + Object o1 = exp1.evaluate(environment); + Object o2 = exp2.evaluate(environment); + if(o1 == null || o2 == null) return null; + if(o1 instanceof Double && o2 instanceof Double) { + return (Double)o1 - (Double)o2; + } + if(o1 instanceof Array && o2 instanceof Array) { + Array la = (Array)o1; + Array ra = (Array)o2; + return la.copy2(ra, new Modifier2() { + + @Override + public Object modify(Object o1, Object o2) { + if(o1 instanceof Double && o2 instanceof Double) return ((Double)o1)-((Double)o2); + throw new IllegalStateException("Tried to subtract a non-numerical array"); + } + }); + } + throw new IllegalStateException(); + } + + @Override + public IExpression withBase(IFrame frame, String prefix) { + return new ElementwiseSubtraction(exp1.withBase(frame, prefix), exp2.withBase(frame, prefix)); + } + + @Override + public Object getPossibleConstant() { + return null; + } + + @Override + public IExpression rewrite(IFrame frame, Map copies) { + exp1 = exp1.rewrite(frame, copies); + exp2 = exp2.rewrite(frame, copies); + return this; + } + + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + exp1.accept(visitor); + exp2.accept(visitor); + } + +} diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java index 14c73e87..32695d74 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java @@ -552,7 +552,9 @@ public class Parser { String op = ((String)walk((SimpleNode)n.jjtGetChild(i), indent+2, model)).trim(); IExpression exp2 = (IExpression)walk((SimpleNode)n.jjtGetChild(i+1), indent+2, frame); if("+".equals(op)) left = new Addition(left, exp2); + else if(".+".equals(op)) left = new ElementwiseAddition(left, exp2); else if("-".equals(op)) left = new Subtraction(left, exp2); + else if(".-".equals(op)) left = new ElementwiseSubtraction(left, exp2); } return left; diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Variable.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Variable.java index fe222fc8..581ccecb 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Variable.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Variable.java @@ -136,8 +136,9 @@ public class Variable implements IExpression { if(fullDimension == 1) { if(!(value instanceof Double)) { Array arr = (Array)value; - if(arr.size(0) != 1) - throw new IllegalStateException(); + int arrSize = arr.dimension(); + if(arrSize != 1) + throw new ExecutionException("Trying to assign an array into a scalar variable"); } } else { if(subscripts == null) {