]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Fix subscript variable name parsing and error reporting in Vensim import.
authorjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 30 Jan 2015 13:46:05 +0000 (13:46 +0000)
committerjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 30 Jan 2015 13:46:05 +0000 (13:46 +0000)
refs #5644

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

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportMdlHandler.java
org.simantics.sysdyn.ui/sysdyn.product
org.simantics.sysdyn/META-INF/MANIFEST.MF
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchConnection.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve.java

index e1230b8a0679869a6a46dd1ed921af0d5996ff47..a42b66b02be196e1a610d67b24c9c05486774cbe 100644 (file)
@@ -30,6 +30,7 @@ import org.simantics.sysdyn.modelImport.model.Model;
 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
 import org.simantics.sysdyn.ui.Activator;\r
 import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
 \r
 /**\r
  * Class for importing Vensim models (.mdl) to Simantics SysDyn using MdlParser \r
@@ -71,7 +72,7 @@ public class ImportMdlHandler extends AbstractHandler {
                        model = new MdlParser().parse(file);\r
                }\r
                catch (Exception e) {\r
-                       e.printStackTrace();\r
+                       ExceptionUtils.logAndShowError(e);\r
                        return null;\r
                }\r
                \r
index faf45d982432da42bdf06ab69735449f0973dd3f..6fbc128d3d2be0e932345807bb7ac69426d908b4 100644 (file)
@@ -38,7 +38,7 @@
    </launcher>\r
 \r
    <vm>\r
-      <windows include="true">org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6</windows>\r
+      <windows include="true">org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7</windows>\r
    </vm>\r
 \r
    <license>\r
@@ -122,6 +122,7 @@ This Agreement is governed by the laws of the State of New York and the intellec
 \r
    <features>\r
       <feature id="org.simantics.sysdyn" version="1.8.0.qualifier"/>\r
+      <feature id="fi.semantum.simupedia" version="1.0.0.qualifier"/>\r
    </features>\r
 \r
 \r
index 66b45852df55829373d27974e24738bcfb9f8a64..218edabe3677307e493b544e0429ecc5954a4c57 100644 (file)
@@ -42,6 +42,7 @@ Export-Package: org.simantics.sysdyn,
  org.simantics.sysdyn.modelExport,
  org.simantics.sysdyn.modelImport,
  org.simantics.sysdyn.modelImport.model,
+ org.simantics.sysdyn.modelImport.model.expression,
  org.simantics.sysdyn.modelParser,
  org.simantics.sysdyn.modelica,
  org.simantics.sysdyn.representation,
index 980948d03808fa179e8cdd9d9181cb591cf25f08..c1cf7fb341e94ee066917ba1ce7e8a955a0052b5 100644 (file)
@@ -37,9 +37,9 @@ public class MdlUtil {
        // by an open parenthesis or a special name)\r
        public static final String VARIABLE_EXPRESSION =\r
                        "([A-Za-z](?![\\s\\w]*\\()\\w*(?:\\s+\\w+)*|"+SPECIAL_NAME+")";\r
-       // matches a vensim subscript variable (basic name with a list of indices)\r
+       // matches a vensim subscript variable (variable with a list of indices)\r
        public static final String SUBSCRIPT =\r
-                       "("+BASIC_NAME+")\\[("+BASIC_NAME+"\\!?(?:,"+BASIC_NAME+"\\!?)*)\\]";\r
+                       VARIABLE+"\\[("+BASIC_NAME+"\\!?(?:,"+BASIC_NAME+"\\!?)*)\\]";\r
        // matches a vensim function (basic name followed by an open parenthesis)\r
        public static final String FUNCTION =\r
                        "("+BASIC_NAME+")\\s*\\(";\r
@@ -90,9 +90,13 @@ public class MdlUtil {
                                // replace double quotes with single quotes in quoted variable\r
                                // names as per modelica syntax (currently useless and might \r
                                // actually break something)\r
-                               result.append('\'');\r
-                               result.append(variable.substring(1, variable.length() - 1));\r
-                               result.append('\'');\r
+//                             result.append('\'');\r
+//                             result.append(variable.substring(1, variable.length() - 1));\r
+//                             result.append('\'');\r
+                               // try this: replace quoted variable names with normalized versions\r
+                               String foobar = variable.replace('"', ' ');\r
+                               foobar = foobar.replace('-', ' ');\r
+                               result.append(variable);\r
                        }\r
                        else {\r
                                // in all other cases, manipulate variable names slightly to \r
@@ -137,6 +141,7 @@ public class MdlUtil {
                                // are replaced with dot-operations (e.g. * with .*) inside\r
                                // the parameters\r
                                result.append("sum(");\r
+                               // TODO: notice that this does not work correctly, should be replaced with a proper parser\r
                                int closing = expression.indexOf(')', matcher.end());\r
                                String parameters = expression.substring(matcher.end(), closing);\r
                                parameters = parameters.replaceAll("\\*", ".*");\r
@@ -145,6 +150,7 @@ public class MdlUtil {
                                parameters = parameters.replaceAll("-", ".-");\r
                                result.append(parameters);\r
                                offset = closing;\r
+                               offset = matcher.end();\r
                        }\r
                        else if (function.equalsIgnoreCase("if then else")) {\r
                                result.append("IFTHENELSE(");\r
index e1fe384a77b289e7865facf723805518ada57a0b..e4b292c1834fd81cd3a59eb5bfd66d16e84488cf 100644 (file)
@@ -144,8 +144,6 @@ public class SketchConnection extends SketchObject {
                double x1 = (double)points[0];\r
                double y1 = (double)points[1];\r
                \r
-               //System.err.println("("+x0+","+y0+") -> ("+x1+","+y1+") -> ("+x2+","+y2+")");\r
-\r
                double dx0 = x1 - x0;\r
                double dy0 = y1 - y0;\r
                double dx1 = x1 - x2;\r
index c6b4a940d24717380bed75b23217dcbd64adb7fc..dc3bdf2c31fbf26e4200413ada6d459b47f43083 100644 (file)
@@ -45,10 +45,12 @@ public class Subscript extends Declaration {
                String expression = MdlUtil.normalize(matcher.group(subscriptExpression));\r
                \r
                Subscript subscript;\r
-               if (equivalent)\r
+               if (equivalent) {\r
                        subscript = new Subscript(name, mdl, expression);\r
-               else\r
+               }\r
+               else {\r
                        subscript = new Subscript(name, mdl, Arrays.asList(expression.split(",")));\r
+               }\r
                subscript.parseSuffix(matcher.group(subscriptSuffix));\r
                return subscript;\r
        }\r
index 5bfd82ae944db5215df9081c4fc0611d1c067d86..c9837bcf5426c8227e600ea3cb9d854a88693c91 100644 (file)
@@ -125,6 +125,46 @@ public class Model implements IWriteableObject {
                connections.add(connection);\r
        }\r
        \r
+       public List<Flow> getInFlows(Variable variable) {\r
+               List<Flow> flows = new ArrayList<Flow>();\r
+               for (Connection connection : connections) {\r
+                       if (connection instanceof Flow && connection.getHead().equals(variable)) {\r
+                               flows.add((Flow)connection);\r
+                       }\r
+               }\r
+               return flows;\r
+       }\r
+       \r
+       public List<Flow> getOutFlows(Variable variable) {\r
+               List<Flow> flows = new ArrayList<Flow>();\r
+               for (Connection connection : connections) {\r
+                       if (connection instanceof Flow && connection.getTail().equals(variable)) {\r
+                               flows.add((Flow)connection);\r
+                       }\r
+               }\r
+               return flows;\r
+       }\r
+       \r
+       public List<Dependency> getInDependencies(Variable variable) {\r
+               List<Dependency> deps = new ArrayList<Dependency>();\r
+               for (Connection connection : connections) {\r
+                       if (connection instanceof Dependency && connection.getHead().equals(variable)) {\r
+                               deps.add((Dependency)connection);\r
+                       }\r
+               }\r
+               return deps;\r
+       }\r
+       \r
+       public List<Dependency> getOutDependencies(Variable variable) {\r
+               List<Dependency> deps = new ArrayList<Dependency>();\r
+               for (Connection connection : connections) {\r
+                       if (connection instanceof Dependency && connection.getTail().equals(variable)) {\r
+                               deps.add((Dependency)connection);\r
+                       }\r
+               }\r
+               return deps;\r
+       }\r
+       \r
        public void addEnumeration(Enumeration enumeration) {\r
                if (enumerations.get(enumeration.getName()) != null) {\r
                        System.err.println("enumeration "+enumeration.getName()+" already defined");\r
index 011b3c1508fc3096269ba054a2d7a49094c713fd..1c9de12059faf69adf25d39e2507cb417ab6374d 100644 (file)
@@ -22,7 +22,9 @@ public class Valve extends Variable {
        private TextPosition position;\r
        \r
        public Valve() {\r
-               \r
+               // TODO: should this really be done here?\r
+               this.orientation = Orientation.HORIZONTAL;\r
+               this.position = TextPosition.UNSET;\r
        }\r
        \r
        public Valve(Orientation orientation, TextPosition position) {\r