From: jkauttio Date: Wed, 18 Jun 2014 11:38:55 +0000 (+0000) Subject: Make value array handling for subscript variables less zealous in Vensim import X-Git-Tag: 1.8.1~18 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=bb798b68ec1019266116d80ef3c95619a66ec0ee;p=simantics%2Fsysdyn.git Make value array handling for subscript variables less zealous in Vensim import refs #2924 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29645 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java index 5fd35182..25c77c0e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java @@ -74,7 +74,7 @@ public class MdlParser { // add the sketch label to the diagram for the sake of clarity model.addSymbol(new Comment( MdlUtil.getSysdynDimensions(offset, 0, -1, -1), - "========== VENSIM SKETCH: "+sketch.getName()+" ==========")); + "=== VENSIM SKETCH: "+sketch.getName()+" ======>")); sketch.setLocation(offset, 20); for (SketchComment comment : sketch.getComments()) { 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 ada64c8a..ef397f4e 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 @@ -109,41 +109,94 @@ public class SubscriptVariable extends MdlVariable { enumerations.add(potential.getEnumeration()); } - EnumerationExpression expr = new EnumerationExpression(enumerations); - - // populate the created expression - // first check if the expression is just a (either one or two // dimensional) list of values and just parse it if this is // the case (should only happen if there is only one expression) + // let enum x = {a,b,c} + // + // expand subscript expression over x with equations defined as + // + // foo[x] = 1,2,3 + // + // into + // + // bar[a] = 1 + // bar[b] = 2 + // bar[c] = 3 + + valuearray: if (next == null) { // number(,number)* if (enumerations.size() == 1 && Pattern.matches(MdlUtil.DBL+"(,"+MdlUtil.DBL+")*", getExpressionString())) { + EnumerationExpression expr = new EnumerationExpression(enumerations); + String[] values = getExpressionString().split(","); + if (enumerations.get(0).getValues().size() != values.length) { + // could not find a value for each enumeration index, + // attempt to parse the equation normally + break valuearray; + } for (int i = 0; i < values.length; i++) { expr.addExpression(new NormalExpression(values[i]), enumerations.get(0).getValues().get(i)); + } + return expr; } // (number(,number)*;)* else if (enumerations.size() == 2 && - Pattern.matches("("+MdlUtil.DBL+"(,"+MdlUtil.DBL+")*;)*", getExpressionString())) { + Pattern.matches("("+MdlUtil.DBL+"(,"+MdlUtil.DBL+")*;)+", getExpressionString())) { + EnumerationExpression expr = new EnumerationExpression(enumerations); + String[] rows = getExpressionString().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 + // normally + break valuearray; + } for (int i = 0; i < rows.length; i++) { String[] values = rows[i].split(","); + if (enumerations.get(1).getValues().size() != values.length) { + // could not find a value for each enumeration index + // in the second enumeration, attempt to parse the + // equation normally + break valuearray; + } for (int j = 0; j < values.length; j++) { expr.addExpression(new NormalExpression(values[j]), enumerations.get(0).getValues().get(i), enumerations.get(1).getValues().get(j)); } } + return expr; } } + // expand the expressions to one expression for each combination of + // enumeration indices; this process must take into account the + // fact that some enumerations can be subsets of other enumerations + // and must be expanded accordingly + + // let enum x = {a,b,c} and enum y = {a,b} + // + // expand subscript expression over x with equations defined as + // + // foo[y] = bar[y] + // foo[c] = 10 + // + // into: + // + // foo[a] = bar[a] + // foo[b] = bar[b] + // foo[c] = 10 + + EnumerationExpression expr = new EnumerationExpression(enumerations); + var = this; while (var != null) { @@ -174,6 +227,9 @@ public class SubscriptVariable extends MdlVariable { String expression = MdlUtil.finalize(we.expression, getMdl()); // TODO: is this check correct? if (next == null) { + // Vensim allows comparison of subscript indices which + // is not a meaningful operation in sysdyn, so attempt + // to resolve and remove them here expression = removeComparisons(expression, indices, we.indices); } expr.addExpression(parseExpression(expression), we.indices);