From: jkauttio Date: Fri, 15 Aug 2014 08:03:41 +0000 (+0000) Subject: Make regular expressions handling variable ranges more general in vensim import X-Git-Tag: v1.29.0~230 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=68fe5d0ed2eb3e38e2fc214e81db64fba4b8fd97;p=simantics%2Fsysdyn.git Make regular expressions handling variable ranges more general in vensim import refs #5176 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30073 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlVariable.java index ad63d003..2be6234e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlVariable.java @@ -158,22 +158,20 @@ public class MdlVariable extends Declaration { private static Range parseRange(String unit) { Matcher matcher = Pattern.compile( - "\\["+MdlUtil.DBL_G+",(\\?|"+MdlUtil.DBL_G+"|"+MdlUtil.DBL_G+","+MdlUtil.DBL_G+")\\]" + "\\[(\\?|"+MdlUtil.DBL+"),(\\?|"+MdlUtil.DBL+")(,(\\?|"+MdlUtil.DBL+"))?\\]" ).matcher(unit); if (matcher.find()) { - Double start, end, step; - start = Double.parseDouble(matcher.group(1)); - if (matcher.group(2).equals("?")) { - end = null; - step = null; + Double start = null; + if (!matcher.group(1).equals("?")) { + start = Double.parseDouble(matcher.group(1)); } - else if (matcher.group(2).contains(",")){ - end = Double.parseDouble(matcher.group(4)); - step = Double.parseDouble(matcher.group(5)); + Double end = null; + if (!matcher.group(2).equals("?")) { + end = Double.parseDouble(matcher.group(2)); } - else { - end = Double.parseDouble(matcher.group(3)); - step = null; + Double step = null; + if (matcher.group(3) != null && !matcher.group(4).equals("?")) { + step = Double.parseDouble(matcher.group(4)); } return new Range(start, end, step); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java index ef397f4e..156ec931 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java @@ -125,14 +125,17 @@ public class SubscriptVariable extends MdlVariable { // bar[b] = 2 // bar[c] = 3 + // remove spaces, should maybe remove all whitespace + String exprstr = getExpressionString().replaceAll(" ", ""); + valuearray: if (next == null) { // number(,number)* if (enumerations.size() == 1 && - Pattern.matches(MdlUtil.DBL+"(,"+MdlUtil.DBL+")*", getExpressionString())) { + Pattern.matches(MdlUtil.DBL+"(,"+MdlUtil.DBL+")*", exprstr)) { EnumerationExpression expr = new EnumerationExpression(enumerations); - String[] values = getExpressionString().split(","); + String[] values = exprstr.split(","); if (enumerations.get(0).getValues().size() != values.length) { // could not find a value for each enumeration index, // attempt to parse the equation normally @@ -148,10 +151,10 @@ public class SubscriptVariable extends MdlVariable { } // (number(,number)*;)* else if (enumerations.size() == 2 && - Pattern.matches("("+MdlUtil.DBL+"(,"+MdlUtil.DBL+")*;)+", getExpressionString())) { + Pattern.matches("("+MdlUtil.DBL+"(,"+MdlUtil.DBL+")*;)+", exprstr)) { EnumerationExpression expr = new EnumerationExpression(enumerations); - String[] rows = getExpressionString().split(";"); + String[] rows = exprstr.split(";"); if (enumerations.get(0).getValues().size() != rows.length) { // could not find a value for each enumeration index // in the first enumeration, attempt to parse the equation