]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Make regular expressions handling variable ranges more general in vensim import
authorjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 15 Aug 2014 08:03:41 +0000 (08:03 +0000)
committerjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 15 Aug 2014 08:03:41 +0000 (08:03 +0000)
refs #5176

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

org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlVariable.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java

index ad63d0034f96aec58be9728a486850ceb1b8eaf2..2be6234efad03b187520a577b7f3839fac78e063 100644 (file)
@@ -158,22 +158,20 @@ public class MdlVariable extends Declaration {
        \r
        private static Range parseRange(String unit) {\r
                Matcher matcher = Pattern.compile(\r
-                       "\\["+MdlUtil.DBL_G+",(\\?|"+MdlUtil.DBL_G+"|"+MdlUtil.DBL_G+","+MdlUtil.DBL_G+")\\]"\r
+                       "\\[(\\?|"+MdlUtil.DBL+"),(\\?|"+MdlUtil.DBL+")(,(\\?|"+MdlUtil.DBL+"))?\\]"\r
                                ).matcher(unit);\r
                if (matcher.find()) {\r
-                       Double start, end, step;\r
-                       start = Double.parseDouble(matcher.group(1));\r
-                       if (matcher.group(2).equals("?")) {\r
-                               end = null;\r
-                               step = null;\r
+                       Double start = null;\r
+                       if (!matcher.group(1).equals("?")) {\r
+                               start = Double.parseDouble(matcher.group(1));\r
                        }\r
-                       else if (matcher.group(2).contains(",")){\r
-                               end = Double.parseDouble(matcher.group(4));\r
-                               step = Double.parseDouble(matcher.group(5));\r
+                       Double end = null;\r
+                       if (!matcher.group(2).equals("?")) {\r
+                               end = Double.parseDouble(matcher.group(2));\r
                        }\r
-                       else {\r
-                               end = Double.parseDouble(matcher.group(3));\r
-                               step = null;    \r
+                       Double step = null;\r
+                       if (matcher.group(3) != null && !matcher.group(4).equals("?")) {\r
+                               step = Double.parseDouble(matcher.group(4));\r
                        }\r
                        return new Range(start, end, step);\r
                }\r
index ef397f4e1c906b4fcd23e23fddce2839b4fd0519..156ec931bfd7a6c4c8179caaeed8ecb1e41c18d6 100644 (file)
@@ -125,14 +125,17 @@ public class SubscriptVariable extends MdlVariable {
                // bar[b] = 2\r
                // bar[c] = 3\r
                \r
+               // remove spaces, should maybe remove all whitespace\r
+               String exprstr = getExpressionString().replaceAll(" ", "");\r
+               \r
                valuearray:\r
                if (next == null) {\r
                        // number(,number)*\r
                        if (enumerations.size() == 1 && \r
-                                       Pattern.matches(MdlUtil.DBL+"(,"+MdlUtil.DBL+")*", getExpressionString())) {\r
+                                       Pattern.matches(MdlUtil.DBL+"(,"+MdlUtil.DBL+")*", exprstr)) {\r
                                EnumerationExpression expr = new EnumerationExpression(enumerations);\r
                                \r
-                               String[] values = getExpressionString().split(",");\r
+                               String[] values = exprstr.split(",");\r
                                if (enumerations.get(0).getValues().size() != values.length) {\r
                                        // could not find a value for each enumeration index, \r
                                        // attempt to parse the equation normally\r
@@ -148,10 +151,10 @@ public class SubscriptVariable extends MdlVariable {
                        }\r
                        // (number(,number)*;)*\r
                        else if (enumerations.size() == 2 && \r
-                                       Pattern.matches("("+MdlUtil.DBL+"(,"+MdlUtil.DBL+")*;)+", getExpressionString())) {\r
+                                       Pattern.matches("("+MdlUtil.DBL+"(,"+MdlUtil.DBL+")*;)+", exprstr)) {\r
                                EnumerationExpression expr = new EnumerationExpression(enumerations);\r
                                \r
-                               String[] rows = getExpressionString().split(";");\r
+                               String[] rows = exprstr.split(";");\r
                                if (enumerations.get(0).getValues().size() != rows.length) {\r
                                        // could not find a value for each enumeration index \r
                                        // in the first enumeration, attempt to parse the equation \r