]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Sysdyn unit validation: correct handling for relations and boolean operators (not...
authormiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 24 Jun 2014 07:05:46 +0000 (07:05 +0000)
committermiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 24 Jun 2014 07:05:46 +0000 (07:05 +0000)
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

org.simantics.sysdyn.feature/rootfiles/sampleModels/LogisticFunc.sysdyn [new file with mode: 0644]
org.simantics.sysdyn.feature/rootfiles/sampleModels/LogisticFunctions.sysdynFunctions [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNodeFactory.java
org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/LogicalExpression.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/LogicalFactor.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/LogicalTerm.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Relation.java

diff --git a/org.simantics.sysdyn.feature/rootfiles/sampleModels/LogisticFunc.sysdyn b/org.simantics.sysdyn.feature/rootfiles/sampleModels/LogisticFunc.sysdyn
new file mode 100644 (file)
index 0000000..683e59d
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 (file)
index 0000000..8475b3f
Binary files /dev/null and b/org.simantics.sysdyn.feature/rootfiles/sampleModels/LogisticFunctions.sysdynFunctions differ
index 35f22dfb06252e233e7e4323e7ab5d23188da132..b24af37aa15d5e112b766d83a3b20552a6f15903 100644 (file)
@@ -8,6 +8,7 @@
  *\r
  * Contributors:\r
  *     Semantum Oy - initial API and implementation\r
+ *     VTT Technical Research Centre of Finland\r
  *******************************************************************************/\r
 package org.simantics.sysdyn.unitParser;\r
 \r
@@ -28,6 +29,9 @@ import org.simantics.sysdyn.unitParser.nodes.ForIndex;
 import org.simantics.sysdyn.unitParser.nodes.FunctionArguments;\r
 import org.simantics.sysdyn.unitParser.nodes.FunctionCall;\r
 import org.simantics.sysdyn.unitParser.nodes.IfThenElse;\r
+import org.simantics.sysdyn.unitParser.nodes.LogicalExpression;\r
+import org.simantics.sysdyn.unitParser.nodes.LogicalFactor;\r
+import org.simantics.sysdyn.unitParser.nodes.LogicalTerm;\r
 import org.simantics.sysdyn.unitParser.nodes.Multiplication;\r
 import org.simantics.sysdyn.unitParser.nodes.NamedArguments;\r
 import org.simantics.sysdyn.unitParser.nodes.ParenthesisExpression;\r
@@ -42,8 +46,11 @@ public class UnitCheckingNodeFactory {
     private static HashMap<String, Class<?>> constructors = new HashMap<String, Class<?>>();\r
     \r
     static {\r
+       constructors.put("logical_expression", LogicalExpression.class);\r
+        constructors.put("logical_term", LogicalTerm.class);\r
+        constructors.put("logical_factor", LogicalFactor.class);\r
         constructors.put("relation", Relation.class);\r
-        constructors.put("term", Term.class);\r
+       constructors.put("term", Term.class);\r
         constructors.put("factor", Factor.class);\r
         constructors.put("arithmetic_expression", Arithmetic.class);\r
         constructors.put("ifthenelse", IfThenElse.class);\r
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 (file)
index 0000000..d154ec9
--- /dev/null
@@ -0,0 +1,49 @@
+/*******************************************************************************\r
+ * Copyright (c) 2014 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\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
+ *     VTT Technical Research Centre of Finland\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.unitParser.nodes;\r
+\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+\r
+import org.simantics.sysdyn.unitParser.UnitCheckingException;\r
+import org.simantics.sysdyn.unitParser.UnitCheckingNode;\r
+import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType;\r
+import org.simantics.sysdyn.utils.Function;\r
+\r
+/**\r
+ * See UnitCheckingNodeFactory for mapping\r
+ * @author Tuomas Miettinen\r
+ *\r
+ */\r
+public class LogicalExpression extends UnitCheckingNode {\r
+\r
+    public LogicalExpression(int id) {\r
+        super(id);\r
+    }\r
+\r
+    @Override\r
+    public UnitResult getUnits(HashMap<String, String> units, ArrayList<Function> functions, boolean allowEquivalents) throws UnitCheckingException {\r
+       UnitResult result;\r
+       \r
+       if (jjtGetNumChildren() == 1) {\r
+               result = ((UnitCheckingNode)jjtGetChild(0)).getUnits(units, functions, allowEquivalents);\r
+        } else {\r
+               // If the logical expression is of type logical_term or logical_term, allow\r
+               // all units as input and output unit is scalar\r
+               result = new UnitResult(allowEquivalents);\r
+            result.setUnitType(UnitType.SCALAR);\r
+        }\r
+    \r
+        return result;\r
+    }\r
+\r
+}\r
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 (file)
index 0000000..ca304bb
--- /dev/null
@@ -0,0 +1,49 @@
+/*******************************************************************************\r
+ * Copyright (c) 2014 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\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
+ *     VTT Technical Research Centre of Finland\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.unitParser.nodes;\r
+\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+\r
+import org.simantics.sysdyn.unitParser.UnitCheckingException;\r
+import org.simantics.sysdyn.unitParser.UnitCheckingNode;\r
+import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType;\r
+import org.simantics.sysdyn.utils.Function;\r
+\r
+/**\r
+ * See UnitCheckingNodeFactory for mapping\r
+ * @author Tuomas Miettinen\r
+ *\r
+ */\r
+public class LogicalFactor extends UnitCheckingNode {\r
+\r
+    public LogicalFactor(int id) {\r
+        super(id);\r
+    }\r
+\r
+    @Override\r
+    public UnitResult getUnits(HashMap<String, String> units, ArrayList<Function> functions, boolean allowEquivalents) throws UnitCheckingException {\r
+       UnitResult result;\r
+       \r
+       if (jjtGetNumChildren() == 1) {\r
+               result = ((UnitCheckingNode)jjtGetChild(0)).getUnits(units, functions, allowEquivalents);\r
+        } else {\r
+               // If the logical factor is of type not relation, allow\r
+               // all units as input and output unit is scalar\r
+               result = new UnitResult(allowEquivalents);\r
+            result.setUnitType(UnitType.SCALAR);\r
+        }\r
+    \r
+        return result;\r
+    }\r
+\r
+}\r
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 (file)
index 0000000..288a914
--- /dev/null
@@ -0,0 +1,49 @@
+/*******************************************************************************\r
+ * Copyright (c) 2014 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\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
+ *     VTT Technical Research Centre of Finland\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.unitParser.nodes;\r
+\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+\r
+import org.simantics.sysdyn.unitParser.UnitCheckingException;\r
+import org.simantics.sysdyn.unitParser.UnitCheckingNode;\r
+import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType;\r
+import org.simantics.sysdyn.utils.Function;\r
+\r
+/**\r
+ * See UnitCheckingNodeFactory for mapping\r
+ * @author Tuomas Miettinen\r
+ *\r
+ */\r
+public class LogicalTerm extends UnitCheckingNode {\r
+\r
+    public LogicalTerm(int id) {\r
+        super(id);\r
+    }\r
+\r
+    @Override\r
+    public UnitResult getUnits(HashMap<String, String> units, ArrayList<Function> functions, boolean allowEquivalents) throws UnitCheckingException {\r
+       UnitResult result;\r
+       \r
+       if (jjtGetNumChildren() == 1) {\r
+               result = ((UnitCheckingNode)jjtGetChild(0)).getUnits(units, functions, allowEquivalents);\r
+        } else {\r
+               // If the logical term is of type logical_factor and logical_factor, allow\r
+               // all units as input and output unit is scalar\r
+               result = new UnitResult(allowEquivalents);\r
+            result.setUnitType(UnitType.SCALAR);\r
+        }\r
+    \r
+        return result;\r
+    }\r
+\r
+}\r
index 5831092bab57b1e7b2142c9bd830795d737ccca4..878b53e1abb5fbc819573aa42c72ee8b4b526b2e 100644 (file)
@@ -17,6 +17,7 @@ import java.util.HashMap;
 \r
 import org.simantics.sysdyn.unitParser.UnitCheckingException;\r
 import org.simantics.sysdyn.unitParser.UnitCheckingNode;\r
+import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType;\r
 import org.simantics.sysdyn.utils.Function;\r
 \r
 /**\r
@@ -40,8 +41,9 @@ public class Relation extends UnitCheckingNode {
         UnitCheckingNode candidateNode = null;\r
         \r
         base = ((UnitCheckingNode)jjtGetChild(0));\r
-        if(!(base instanceof Arithmetic) && jjtGetNumChildren() > 1)\r
+        if(!(base instanceof Arithmetic) && jjtGetNumChildren() > 1) {\r
             base = ((UnitCheckingNode)jjtGetChild(1));\r
+        }\r
             \r
         if(base instanceof Arithmetic) {\r
             result.appendResult(base.getUnits(units, functions, allowEquivalents));\r
@@ -53,15 +55,21 @@ public class Relation extends UnitCheckingNode {
                     operator = ((UnitCheckingNode)jjtGetChild(i-1));\r
                     UnitResult candidateUnits = candidateNode.getUnits(units, functions, allowEquivalents);\r
                     if(!result.equals(candidateUnits)) {\r
-                        result.equals(candidateUnits);\r
                         throw new UnitCheckingException("Not equals exception: " +\r
                                 base.printNode() + " [" + result.getFullUnit() + "] " + operator.printNode() + " " +\r
                                 candidateNode.printNode() + " [" + candidateUnits.getFullUnit() + "]"\r
                                 );\r
                     }\r
                 }\r
-\r
             }\r
+            \r
+            if (jjtGetNumChildren() > 1) {\r
+               // If there is an actual relation (e.g. a > b), the output\r
+               // unit is a scalar.\r
+               result = new UnitResult(allowEquivalents);\r
+                result.setUnitType(UnitType.SCALAR);\r
+            }\r
+\r
         }\r
         \r
         return result;\r