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