From: miettinen Date: Tue, 24 Jun 2014 07:05:46 +0000 (+0000) Subject: Sysdyn unit validation: correct handling for relations and boolean operators (not... X-Git-Tag: v1.29.0~263 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=598adbc50b0a26cdd9245979f0b4ce8648818129;p=simantics%2Fsysdyn.git Sysdyn unit validation: correct handling for relations and boolean operators (not, and, or) (refs #4987). Added a sample model and a function library. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29707 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.feature/rootfiles/sampleModels/LogisticFunc.sysdyn b/org.simantics.sysdyn.feature/rootfiles/sampleModels/LogisticFunc.sysdyn new file mode 100644 index 00000000..683e59d0 Binary files /dev/null and b/org.simantics.sysdyn.feature/rootfiles/sampleModels/LogisticFunc.sysdyn differ diff --git a/org.simantics.sysdyn.feature/rootfiles/sampleModels/LogisticFunctions.sysdynFunctions b/org.simantics.sysdyn.feature/rootfiles/sampleModels/LogisticFunctions.sysdynFunctions new file mode 100644 index 00000000..8475b3f7 Binary files /dev/null and b/org.simantics.sysdyn.feature/rootfiles/sampleModels/LogisticFunctions.sysdynFunctions differ diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNodeFactory.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNodeFactory.java index 35f22dfb..b24af37a 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNodeFactory.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNodeFactory.java @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser; @@ -28,6 +29,9 @@ import org.simantics.sysdyn.unitParser.nodes.ForIndex; import org.simantics.sysdyn.unitParser.nodes.FunctionArguments; import org.simantics.sysdyn.unitParser.nodes.FunctionCall; import org.simantics.sysdyn.unitParser.nodes.IfThenElse; +import org.simantics.sysdyn.unitParser.nodes.LogicalExpression; +import org.simantics.sysdyn.unitParser.nodes.LogicalFactor; +import org.simantics.sysdyn.unitParser.nodes.LogicalTerm; import org.simantics.sysdyn.unitParser.nodes.Multiplication; import org.simantics.sysdyn.unitParser.nodes.NamedArguments; import org.simantics.sysdyn.unitParser.nodes.ParenthesisExpression; @@ -42,8 +46,11 @@ public class UnitCheckingNodeFactory { private static HashMap> constructors = new HashMap>(); static { + constructors.put("logical_expression", LogicalExpression.class); + constructors.put("logical_term", LogicalTerm.class); + constructors.put("logical_factor", LogicalFactor.class); constructors.put("relation", Relation.class); - constructors.put("term", Term.class); + constructors.put("term", Term.class); constructors.put("factor", Factor.class); constructors.put("arithmetic_expression", Arithmetic.class); constructors.put("ifthenelse", IfThenElse.class); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/LogicalExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/LogicalExpression.java new file mode 100644 index 00000000..d154ec9d --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/LogicalExpression.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2014 Association for Decentralized Information Management in + * Industry THTH ry. + * 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: + * VTT Technical Research Centre of Finland + *******************************************************************************/ +package org.simantics.sysdyn.unitParser.nodes; + +import java.util.ArrayList; +import java.util.HashMap; + +import org.simantics.sysdyn.unitParser.UnitCheckingException; +import org.simantics.sysdyn.unitParser.UnitCheckingNode; +import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; +import org.simantics.sysdyn.utils.Function; + +/** + * See UnitCheckingNodeFactory for mapping + * @author Tuomas Miettinen + * + */ +public class LogicalExpression extends UnitCheckingNode { + + public LogicalExpression(int id) { + super(id); + } + + @Override + public UnitResult getUnits(HashMap units, ArrayList functions, boolean allowEquivalents) throws UnitCheckingException { + UnitResult result; + + if (jjtGetNumChildren() == 1) { + result = ((UnitCheckingNode)jjtGetChild(0)).getUnits(units, functions, allowEquivalents); + } else { + // If the logical expression is of type logical_term or logical_term, allow + // all units as input and output unit is scalar + result = new UnitResult(allowEquivalents); + result.setUnitType(UnitType.SCALAR); + } + + return result; + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/LogicalFactor.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/LogicalFactor.java new file mode 100644 index 00000000..ca304bbd --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/LogicalFactor.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2014 Association for Decentralized Information Management in + * Industry THTH ry. + * 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: + * VTT Technical Research Centre of Finland + *******************************************************************************/ +package org.simantics.sysdyn.unitParser.nodes; + +import java.util.ArrayList; +import java.util.HashMap; + +import org.simantics.sysdyn.unitParser.UnitCheckingException; +import org.simantics.sysdyn.unitParser.UnitCheckingNode; +import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; +import org.simantics.sysdyn.utils.Function; + +/** + * See UnitCheckingNodeFactory for mapping + * @author Tuomas Miettinen + * + */ +public class LogicalFactor extends UnitCheckingNode { + + public LogicalFactor(int id) { + super(id); + } + + @Override + public UnitResult getUnits(HashMap units, ArrayList functions, boolean allowEquivalents) throws UnitCheckingException { + UnitResult result; + + if (jjtGetNumChildren() == 1) { + result = ((UnitCheckingNode)jjtGetChild(0)).getUnits(units, functions, allowEquivalents); + } else { + // If the logical factor is of type not relation, allow + // all units as input and output unit is scalar + result = new UnitResult(allowEquivalents); + result.setUnitType(UnitType.SCALAR); + } + + return result; + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/LogicalTerm.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/LogicalTerm.java new file mode 100644 index 00000000..288a9142 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/LogicalTerm.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2014 Association for Decentralized Information Management in + * Industry THTH ry. + * 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: + * VTT Technical Research Centre of Finland + *******************************************************************************/ +package org.simantics.sysdyn.unitParser.nodes; + +import java.util.ArrayList; +import java.util.HashMap; + +import org.simantics.sysdyn.unitParser.UnitCheckingException; +import org.simantics.sysdyn.unitParser.UnitCheckingNode; +import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; +import org.simantics.sysdyn.utils.Function; + +/** + * See UnitCheckingNodeFactory for mapping + * @author Tuomas Miettinen + * + */ +public class LogicalTerm extends UnitCheckingNode { + + public LogicalTerm(int id) { + super(id); + } + + @Override + public UnitResult getUnits(HashMap units, ArrayList functions, boolean allowEquivalents) throws UnitCheckingException { + UnitResult result; + + if (jjtGetNumChildren() == 1) { + result = ((UnitCheckingNode)jjtGetChild(0)).getUnits(units, functions, allowEquivalents); + } else { + // If the logical term is of type logical_factor and logical_factor, allow + // all units as input and output unit is scalar + result = new UnitResult(allowEquivalents); + result.setUnitType(UnitType.SCALAR); + } + + return result; + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Relation.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Relation.java index 5831092b..878b53e1 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Relation.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Relation.java @@ -17,6 +17,7 @@ import java.util.HashMap; import org.simantics.sysdyn.unitParser.UnitCheckingException; import org.simantics.sysdyn.unitParser.UnitCheckingNode; +import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; import org.simantics.sysdyn.utils.Function; /** @@ -40,8 +41,9 @@ public class Relation extends UnitCheckingNode { UnitCheckingNode candidateNode = null; base = ((UnitCheckingNode)jjtGetChild(0)); - if(!(base instanceof Arithmetic) && jjtGetNumChildren() > 1) + if(!(base instanceof Arithmetic) && jjtGetNumChildren() > 1) { base = ((UnitCheckingNode)jjtGetChild(1)); + } if(base instanceof Arithmetic) { result.appendResult(base.getUnits(units, functions, allowEquivalents)); @@ -53,15 +55,21 @@ public class Relation extends UnitCheckingNode { operator = ((UnitCheckingNode)jjtGetChild(i-1)); UnitResult candidateUnits = candidateNode.getUnits(units, functions, allowEquivalents); if(!result.equals(candidateUnits)) { - result.equals(candidateUnits); throw new UnitCheckingException("Not equals exception: " + base.printNode() + " [" + result.getFullUnit() + "] " + operator.printNode() + " " + candidateNode.printNode() + " [" + candidateUnits.getFullUnit() + "]" ); } } - } + + if (jjtGetNumChildren() > 1) { + // If there is an actual relation (e.g. a > b), the output + // unit is a scalar. + result = new UnitResult(allowEquivalents); + result.setUnitType(UnitType.SCALAR); + } + } return result;