]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Unit validation for Sysdyn functions: unit "1" in inputs to not correspond to any...
authormiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 4 Feb 2014 13:16:32 +0000 (13:16 +0000)
committermiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 4 Feb 2014 13:16:32 +0000 (13:16 +0000)
Bug fixes to unit validator (refs #4692).

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

org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/UnitUtils.java
org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNodeFactory.java
org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ComponentReferenceFull.java
org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionCall.java
org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ParenthesisExpression.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Term.java
org.simantics.sysdyn/src/org/simantics/sysdyn/utils/Function.java

index 04369f79386bf9cde3faaa1004331d42831b53ac..d74b59345a4434f61b1f54c13ae814ff4b18dd77 100644 (file)
@@ -60,9 +60,8 @@ public class UnitUtils {
             } catch (DatabaseException e) {\r
                                e.printStackTrace();\r
                        }\r
-            \r
         } catch (ParseException e) {\r
-            e.printStackTrace();\r
+               return "Cannot validate units: Syntax error in expression.";\r
         }\r
         return null;\r
     }\r
@@ -94,38 +93,40 @@ public class UnitUtils {
     public static String matchUnits(ReadGraph graph, SysdynModel model, Configuration configuration, String unit, String expression) {\r
         if(unit == null)\r
             return "Unit not defined";\r
+        \r
         try {\r
-            \r
             StringReader rightReader = new StringReader(expression);\r
             UnitParser rightParser = new UnitParser(rightReader);\r
             UnitCheckingNode right = (UnitCheckingNode) rightParser.expr();\r
             rightReader.close();\r
             Set<String> components = findComponents(right);\r
             HashMap<String, String> units = findUnits(graph, model, configuration, components);\r
-            \r
-            StringReader leftReader = new StringReader(unit);\r
-            UnitParser leftParser = new UnitParser(leftReader);\r
-            UnitCheckingNode left = (UnitCheckingNode) leftParser.expr();\r
-            leftReader.close();\r
-            \r
+       \r
             try {\r
-               ArrayList<Function> functions = Function.getAllBuiltInFunctions(graph);\r
-               boolean allowEquivalents = allowEquivalents(graph, model);\r
-                UnitResult rightUnits = right.getUnits(units, functions, allowEquivalents);\r
-                UnitResult leftUnits = left.getUnits(null, functions, allowEquivalents);\r
-                \r
-                if(!rightUnits.equals(leftUnits))\r
-                    return leftUnits.getCleanFullUnit() + " != " + rightUnits.getCleanFullUnit();\r
-            } catch (UnitCheckingException e) {\r
-                return e.getMessage();\r
-            } catch (DatabaseException e) {\r
-                               e.printStackTrace();\r
-                       }\r
-            \r
+                StringReader leftReader = new StringReader(unit);\r
+                   UnitParser leftParser = new UnitParser(leftReader);\r
+                   UnitCheckingNode left = (UnitCheckingNode) leftParser.expr();\r
+                   leftReader.close();\r
+                   \r
+                   try {\r
+                       ArrayList<Function> functions = Function.getAllBuiltInFunctions(graph);\r
+                       boolean allowEquivalents = allowEquivalents(graph, model);\r
+                       UnitResult rightUnits = right.getUnits(units, functions, allowEquivalents);\r
+                       UnitResult leftUnits = left.getUnits(null, functions, allowEquivalents);\r
+                       \r
+                       if(!rightUnits.equals(leftUnits))\r
+                           return leftUnits.getCleanFullUnit() + " != " + rightUnits.getCleanFullUnit();\r
+                   } catch (UnitCheckingException e) {\r
+                       return e.getMessage();\r
+                   } catch (DatabaseException e) {\r
+                                       e.printStackTrace();\r
+                               }\r
+            } catch (ParseException e) {\r
+               return "Syntax error in defined unit.";\r
+            }\r
         } catch (ParseException e) {\r
-            e.printStackTrace();\r
+               return "Cannot validate units: Syntax error in expression.";\r
         }\r
-        \r
         return null;\r
     }\r
     \r
@@ -221,29 +222,30 @@ public class UnitUtils {
             return "No unit defined";\r
         \r
         try {\r
-\r
             StringReader leftReader = new StringReader(left);\r
             UnitParser leftParser = new UnitParser(leftReader);\r
             UnitCheckingNode leftNode = (UnitCheckingNode) leftParser.expr();\r
             leftReader.close();\r
-\r
-            StringReader rightReader = new StringReader(right);\r
-            UnitParser rightParser = new UnitParser(rightReader);\r
-            UnitCheckingNode rightNode = (UnitCheckingNode) rightParser.expr();\r
-            rightReader.close();\r
-\r
             try {\r
-                UnitResult leftUnits = leftNode.getUnits(null, functions, allowEquivalents);\r
-                UnitResult rightUnits = rightNode.getUnits(null, functions, allowEquivalents);\r
-\r
-                if(!rightUnits.equals(leftUnits))\r
-                    return leftUnits.getCleanFullUnit() + " != " + rightUnits.getCleanFullUnit();\r
-            } catch (UnitCheckingException e) {\r
-                return e.getMessage();\r
+                   StringReader rightReader = new StringReader(right);\r
+                   UnitParser rightParser = new UnitParser(rightReader);\r
+                   UnitCheckingNode rightNode = (UnitCheckingNode) rightParser.expr();\r
+                   rightReader.close();\r
+       \r
+                   try {\r
+                       UnitResult leftUnits = leftNode.getUnits(null, functions, allowEquivalents);\r
+                       UnitResult rightUnits = rightNode.getUnits(null, functions, allowEquivalents);\r
+       \r
+                       if(!rightUnits.equals(leftUnits))\r
+                           return leftUnits.getCleanFullUnit() + " != " + rightUnits.getCleanFullUnit();\r
+                   } catch (UnitCheckingException e) {\r
+                       return e.getMessage();\r
+                   }\r
+            } catch (ParseException e) {\r
+               return "Cannot validate units: Syntax error in expression.";\r
             }\r
-\r
         } catch (ParseException e) {\r
-            e.printStackTrace();\r
+               return "Syntax error in defined unit.";\r
         }\r
         return null;\r
     }\r
index 798659e5943139256a3078871cca40c3ed241312..35f22dfb06252e233e7e4323e7ab5d23188da132 100644 (file)
@@ -30,6 +30,7 @@ import org.simantics.sysdyn.unitParser.nodes.FunctionCall;
 import org.simantics.sysdyn.unitParser.nodes.IfThenElse;\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
 import org.simantics.sysdyn.unitParser.nodes.Power;\r
 import org.simantics.sysdyn.unitParser.nodes.RelOp;\r
 import org.simantics.sysdyn.unitParser.nodes.Relation;\r
@@ -64,6 +65,7 @@ public class UnitCheckingNodeFactory {
         constructors.put("component_identity", ComponentIdentity.class);\r
         constructors.put("condition", Condition.class);\r
         constructors.put("for_index", ForIndex.class);\r
+        constructors.put("parenthesis_expression", ParenthesisExpression.class);\r
         constructors.put("function_call", FunctionCall.class);\r
         constructors.put("function_arguments", FunctionArguments.class);\r
         constructors.put("named_arguments", NamedArguments.class);\r
index 21b016481e03b3282b57963e39efb56e0f57f82d..fa8e959bad3c3134f94f38fca6ee9ec5784868dc 100644 (file)
@@ -35,16 +35,16 @@ public class ComponentReferenceFull extends UnitCheckingNode {
         super(id);\r
     }\r
 \r
-    protected UnitResult parseUnits(String units, ArrayList<Function> functions, boolean allowEquivalents) {\r
+    protected UnitResult parseUnits(String units, ArrayList<Function> functions, boolean allowEquivalents) throws UnitCheckingException {\r
         StringReader sr = new StringReader(units);\r
         UnitParser parser = new UnitParser(sr);\r
         try {\r
             UnitCheckingNode node = (UnitCheckingNode) parser.expr();\r
             return node.getUnits(null, functions, allowEquivalents);\r
-        } catch (ParseException e) {\r
-            e.printStackTrace();\r
         } catch (UnitCheckingException e) {\r
             e.printStackTrace();\r
+        } catch (ParseException e) {\r
+            throw new UnitCheckingException("Cannot validate units: Syntax error in expression.");\r
         }\r
         return null;\r
     }\r
index eb07940d90b337aad4d00c54a664ee73c57e768a..0832b0dfe8d6aaef897a914862501b14bf9d822e 100644 (file)
@@ -104,6 +104,11 @@ public class FunctionCall extends UnitCheckingNode {
 \r
        private static void addPossibleCorrespondence(HashMap<String, String> correspondences,\r
                        Input input, UnitResult argumentUnit) {\r
+               if (correspondences.containsKey(input.unit)\r
+                               && argumentUnit.getUnitType() == UnitType.SCALAR) {\r
+                       // Don't overwrite if the correspondence already exists and the new argument is scalar. \r
+                       return;\r
+               }\r
                if (input.unit.matches("'[a-zA-Z]")) {\r
                        String fullUnit = argumentUnit.getCleanFullUnit();\r
                        if ("".equals(fullUnit))\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ParenthesisExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ParenthesisExpression.java
new file mode 100644 (file)
index 0000000..93d2872
--- /dev/null
@@ -0,0 +1,39 @@
+/*******************************************************************************\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.utils.Function;\r
+\r
+/**\r
+ * See UnitCheckingNodeFactory for mapping\r
+ * @author Tuomas Miettinen\r
+ *\r
+ */\r
+public class ParenthesisExpression extends UnitCheckingNode {\r
+\r
+    public ParenthesisExpression(int id) {\r
+        super(id);\r
+    }\r
+    \r
+    public UnitResult getUnits(HashMap<String, String> units, ArrayList<Function> functions, boolean allowEquivalents) throws UnitCheckingException{\r
+        UnitResult result = new UnitResult(allowEquivalents);\r
+        // Get the Expression between the parenthesis.\r
+        result.appendResult(((UnitCheckingNode)jjtGetChild(1)).getUnits(units, functions, allowEquivalents));\r
+        return result; \r
+    }\r
+\r
+}\r
index dfdcc54eb60d2f5efdfde55f373fc57f931ab495..baa4a1cee65d48f50e25cce20693a9fa2fd12a58 100644 (file)
@@ -69,11 +69,12 @@ public class Term extends UnitCheckingNode {
                     result.addAllDividents(currentUnits.getDividers());\r
                     result.addAllDividers(currentUnits.getDividents());\r
                     UnitType unitType = currentUnits.getUnitType();\r
-                    result.setUnitType(unitType);\r
-                    if(unitType == UnitType.SCALAR || unitType == UnitType.DMNL)\r
+                    if(unitType == UnitType.SCALAR || unitType == UnitType.DMNL) {\r
                         result.append("1");\r
-                    else\r
+                    } else {\r
                         result.append(currentUnits.getFullUnit());\r
+                        result.setUnitType(unitType);\r
+                    }\r
                 }\r
             }\r
         }\r
index 8fb00e5aee18ab985076234487a47506ccf16443..bedd3247a46ce04f6e8409acadc91bd75ce4b102 100644 (file)
@@ -73,10 +73,10 @@ public class Function implements Comparable<Function>{
                                outputReader.close();\r
                    result.appendResult(output.getUnits(null, functions, allowEquivalents));\r
                                result.setUnitType(UnitType.NORMAL);\r
-                       } catch (ParseException e) {\r
-                               e.printStackTrace();\r
                        } catch (UnitCheckingException e) {\r
                                e.printStackTrace();\r
+                       } catch (ParseException e) {\r
+                               throw new UnitCheckingException("Cannot validate units: Syntax error in expression.");\r
                        }\r
                }\r
                \r
@@ -541,6 +541,9 @@ public class Function implements Comparable<Function>{
                                } else {\r
                                        // Match input unit to argument unit\r
                                        UnitResult inputUnit = inputList.get(i).getUnitResult(units, this, functions, allowEquivalents, correspondences);\r
+                                       if (inputUnit.getUnitType() == UnitType.SCALAR\r
+                                                       && !(argumentUnits.get(i).first.getUnitType() == UnitType.SCALAR))\r
+                                               return false; // Here we don't accept a "1" replaced with a NORMAL expression, there's "ANY" for that.\r
                                        if (!inputUnit.equals(argumentUnits.get(i).first))\r
                                                return false;\r
                                        inputMatches.set(i, Boolean.TRUE);\r