]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Forgotten files for unit parser. (refs #4263)
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 2 May 2013 05:13:55 +0000 (05:13 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 2 May 2013 05:13:55 +0000 (05:13 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27301 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ArrayDefinition.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Expression.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionArguments.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionCall.java [new file with mode: 0644]

diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ArrayDefinition.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ArrayDefinition.java
new file mode 100644 (file)
index 0000000..4b46e71
--- /dev/null
@@ -0,0 +1,102 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 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
+ *     Semantum Oy - initial API and implementation\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
+\r
+/**\r
+ * See UnitCheckingNodeFactory for mapping\r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public class ArrayDefinition extends UnitCheckingNode {\r
+\r
+    public ArrayDefinition(int id) {\r
+        super(id);\r
+    }\r
+\r
+\r
+    @Override\r
+    public UnitResult getUnits(HashMap<String, String> units) throws UnitCheckingException {\r
+        UnitResult result = new UnitResult();\r
+        result.setUnitType(UnitType.SCALAR);\r
+\r
+        UnitCheckingNode base = null;\r
+\r
+        if(jjtGetNumChildren() > 0 && jjtGetChild(0) instanceof FunctionArguments) {\r
+            FunctionArguments functionArguments = (FunctionArguments) jjtGetChild(0);\r
+\r
+            for(UnitCheckingNode candidateNode : gatherExpressions(functionArguments, new ArrayList<UnitCheckingNode>())) {\r
+                UnitResult candidateUnits = candidateNode.getUnits(units);\r
+                if(candidateUnits.getUnitType() == UnitType.SCALAR) {\r
+                    continue;\r
+                } else if(base == null) {\r
+                    base = candidateNode;\r
+                    UnitType oldUnitType = result.getUnitType();\r
+                    result.appendResult(base.getUnits(units));\r
+\r
+                    /*\r
+                     * Make sure unit type persist\r
+                     *  {Var1, Var2, 1} would result in UnitType.SCALAR even though it should be UnitType.NORMAL \r
+                     *  because the last element has UnitType.SCALAR  \r
+                     */\r
+                    UnitType newUnitType = result.getUnitType();\r
+                    if(newUnitType == UnitType.SCALAR || newUnitType == UnitType.ANY || newUnitType == UnitType.DMNL)\r
+                        result.setUnitType(oldUnitType);\r
+                    continue;\r
+                } else {\r
+                    if(!result.equals(candidateUnits)) {\r
+                        throw new UnitCheckingException("Array elements do not have same units: " +\r
+                                base.printNode() + " [" + result.getCleanFullUnit() + "] =/= " +\r
+                                candidateNode.printNode() + " [" + candidateUnits.getCleanFullUnit() + "]"\r
+                                );\r
+                    }\r
+                }\r
+            }\r
+        }\r
+        \r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * Gather all expressions of array definitions.\r
+     * \r
+     * void function_arguments() : {\r
+     * } {\r
+     *     LOOKAHEAD(2) named_argument()  ( "," function_arguments() )?\r
+     *     | expression() ( "," function_arguments() | "for" for_indices() )?\r
+     *     \r
+     * }\r
+     * @param functionArguments\r
+     * @param expressions\r
+     * @return\r
+     */\r
+    private ArrayList<UnitCheckingNode> gatherExpressions(UnitCheckingNode functionArguments, ArrayList<UnitCheckingNode> expressions) {\r
+        if(functionArguments.jjtGetNumChildren() > 0) {\r
+            if(functionArguments.jjtGetChild(0) instanceof Expression) {\r
+                expressions.add(((UnitCheckingNode)functionArguments.jjtGetChild(0)));\r
+            }\r
+        }\r
+\r
+        if(functionArguments.jjtGetNumChildren() > 1) {\r
+            gatherExpressions((UnitCheckingNode)functionArguments.jjtGetChild(1), expressions);\r
+        }\r
+\r
+        return expressions;\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Expression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Expression.java
new file mode 100644 (file)
index 0000000..eb3ec7f
--- /dev/null
@@ -0,0 +1,27 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 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
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.unitParser.nodes;\r
+\r
+import org.simantics.sysdyn.unitParser.UnitCheckingNode;\r
+\r
+/**\r
+ * See UnitCheckingNodeFactory for mapping\r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public class Expression extends UnitCheckingNode {\r
+\r
+    public Expression(int id) {\r
+        super(id);\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionArguments.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionArguments.java
new file mode 100644 (file)
index 0000000..3921d3c
--- /dev/null
@@ -0,0 +1,27 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 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
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.unitParser.nodes;\r
+\r
+import org.simantics.sysdyn.unitParser.UnitCheckingNode;\r
+\r
+/**\r
+ * See UnitCheckingNodeFactory for mapping\r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public class FunctionArguments extends UnitCheckingNode {\r
+\r
+    public FunctionArguments(int id) {\r
+        super(id);\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionCall.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionCall.java
new file mode 100644 (file)
index 0000000..e165cc5
--- /dev/null
@@ -0,0 +1,38 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 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
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.unitParser.nodes;\r
+\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
+\r
+/**\r
+ * See UnitCheckingNodeFactory for mapping\r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public class FunctionCall extends UnitCheckingNode {\r
+\r
+    public FunctionCall(int id) {\r
+        super(id);\r
+    }\r
+    \r
+    @Override\r
+    public UnitResult getUnits(HashMap<String, String> units) throws UnitCheckingException {\r
+        UnitResult result = new UnitResult();\r
+        result.setUnitType(UnitType.ANY);\r
+        return result;\r
+    }\r
+\r
+}\r