]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Make value array handling for subscript variables less zealous in Vensim import
authorjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 18 Jun 2014 11:38:55 +0000 (11:38 +0000)
committerjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 18 Jun 2014 11:38:55 +0000 (11:38 +0000)
refs #2924

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

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

index 5fd3518205689783026067b6dd7d7e06b5f01f98..25c77c0e48daacabdde5b32fc6a00328b26da604 100644 (file)
@@ -74,7 +74,7 @@ public class MdlParser {
                        // add the sketch label to the diagram for the sake of clarity\r
                        model.addSymbol(new Comment(\r
                                        MdlUtil.getSysdynDimensions(offset, 0, -1, -1), \r
-                                       "========== VENSIM SKETCH: "+sketch.getName()+" =========="));\r
+                                       "=== VENSIM SKETCH: "+sketch.getName()+" ======>"));\r
                        sketch.setLocation(offset, 20);\r
                        \r
                        for (SketchComment comment : sketch.getComments()) {\r
index ada64c8a6630e5d53268e7c32f8f59a70978abcc..ef397f4e1c906b4fcd23e23fddce2839b4fd0519 100644 (file)
@@ -109,41 +109,94 @@ public class SubscriptVariable extends MdlVariable {
                        enumerations.add(potential.getEnumeration());\r
                }\r
                \r
-               EnumerationExpression expr = new EnumerationExpression(enumerations);\r
-               \r
-               // populate the created expression\r
-               \r
                // first check if the expression is just a (either one or two \r
                // dimensional) list of values and just parse it if this is\r
                // the case (should only happen if there is only one expression)\r
                \r
+               // let enum x = {a,b,c}\r
+               //\r
+               // expand subscript expression over x with equations defined as\r
+               //\r
+               // foo[x] = 1,2,3\r
+               // \r
+               // into\r
+               //\r
+               // bar[a] = 1\r
+               // bar[b] = 2\r
+               // bar[c] = 3\r
+               \r
+               valuearray:\r
                if (next == null) {\r
                        // number(,number)*\r
                        if (enumerations.size() == 1 && \r
                                        Pattern.matches(MdlUtil.DBL+"(,"+MdlUtil.DBL+")*", getExpressionString())) {\r
+                               EnumerationExpression expr = new EnumerationExpression(enumerations);\r
+                               \r
                                String[] values = getExpressionString().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
+                                       break valuearray;\r
+                               }\r
                                for (int i = 0; i < values.length; i++) {\r
                                        expr.addExpression(new NormalExpression(values[i]),\r
                                                        enumerations.get(0).getValues().get(i));\r
+                               \r
                                }\r
+                               \r
                                return expr;\r
                        }\r
                        // (number(,number)*;)*\r
                        else if (enumerations.size() == 2 && \r
-                                       Pattern.matches("("+MdlUtil.DBL+"(,"+MdlUtil.DBL+")*;)*", getExpressionString())) {\r
+                                       Pattern.matches("("+MdlUtil.DBL+"(,"+MdlUtil.DBL+")*;)+", getExpressionString())) {\r
+                               EnumerationExpression expr = new EnumerationExpression(enumerations);\r
+                               \r
                                String[] rows = getExpressionString().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
+                                       // normally\r
+                                       break valuearray;\r
+                               }\r
                                for (int i = 0; i < rows.length; i++) {\r
                                        String[] values = rows[i].split(",");\r
+                                       if (enumerations.get(1).getValues().size() != values.length) {\r
+                                               // could not find a value for each enumeration index\r
+                                               // in the second enumeration, attempt to parse the \r
+                                               // equation normally\r
+                                               break valuearray;\r
+                                       }\r
                                        for (int j = 0; j < values.length; j++) {\r
                                                expr.addExpression(new NormalExpression(values[j]),\r
                                                                enumerations.get(0).getValues().get(i),\r
                                                                enumerations.get(1).getValues().get(j));\r
                                        }\r
                                }\r
+                               \r
                                return expr;\r
                        }\r
                }\r
                \r
+               // expand the expressions to one expression for each combination of \r
+               // enumeration indices; this process must take into account the \r
+               // fact that some enumerations can be subsets of other enumerations \r
+               // and must be expanded accordingly\r
+               \r
+               // let enum x = {a,b,c} and enum y = {a,b}\r
+               //\r
+               // expand subscript expression over x with equations defined as\r
+               //\r
+               // foo[y] = bar[y]\r
+               // foo[c] = 10\r
+               //\r
+               // into:\r
+               //\r
+               // foo[a] = bar[a]\r
+               // foo[b] = bar[b]\r
+               // foo[c] = 10\r
+               \r
+               EnumerationExpression expr = new EnumerationExpression(enumerations);\r
+               \r
                var = this;\r
                while (var != null) {\r
                        \r
@@ -174,6 +227,9 @@ public class SubscriptVariable extends MdlVariable {
                                String expression = MdlUtil.finalize(we.expression, getMdl());\r
                                // TODO: is this check correct?\r
                                if (next == null) {\r
+                                       // Vensim allows comparison of subscript indices which\r
+                                       // is not a meaningful operation in sysdyn, so attempt\r
+                                       // to resolve and remove them here\r
                                        expression = removeComparisons(expression, indices, we.indices);\r
                                }\r
                                expr.addExpression(parseExpression(expression), we.indices);\r