From 08f39ab8a8526eaf8dae75342472da7ac8b9b3b0 Mon Sep 17 00:00:00 2001 From: jkauttio Date: Fri, 30 Jan 2015 13:46:05 +0000 Subject: [PATCH] Fix subscript variable name parsing and error reporting in Vensim import. refs #5644 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30864 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../ui/handlers/imports/ImportMdlHandler.java | 3 +- org.simantics.sysdyn.ui/sysdyn.product | 3 +- org.simantics.sysdyn/META-INF/MANIFEST.MF | 1 + .../simantics/sysdyn/modelImport/MdlUtil.java | 16 +++++--- .../modelImport/mdl/SketchConnection.java | 2 - .../sysdyn/modelImport/mdl/Subscript.java | 6 ++- .../sysdyn/modelImport/model/Model.java | 40 +++++++++++++++++++ .../sysdyn/modelImport/model/Valve.java | 4 +- 8 files changed, 63 insertions(+), 12 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportMdlHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportMdlHandler.java index e1230b8a..a42b66b0 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportMdlHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportMdlHandler.java @@ -30,6 +30,7 @@ import org.simantics.sysdyn.modelImport.model.Model; import org.simantics.sysdyn.modelImport.model.WriteContext; import org.simantics.sysdyn.ui.Activator; import org.simantics.ui.SimanticsUI; +import org.simantics.utils.ui.ExceptionUtils; /** * Class for importing Vensim models (.mdl) to Simantics SysDyn using MdlParser @@ -71,7 +72,7 @@ public class ImportMdlHandler extends AbstractHandler { model = new MdlParser().parse(file); } catch (Exception e) { - e.printStackTrace(); + ExceptionUtils.logAndShowError(e); return null; } diff --git a/org.simantics.sysdyn.ui/sysdyn.product b/org.simantics.sysdyn.ui/sysdyn.product index faf45d98..6fbc128d 100644 --- a/org.simantics.sysdyn.ui/sysdyn.product +++ b/org.simantics.sysdyn.ui/sysdyn.product @@ -38,7 +38,7 @@ - org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6 + org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7 @@ -122,6 +122,7 @@ This Agreement is governed by the laws of the State of New York and the intellec + diff --git a/org.simantics.sysdyn/META-INF/MANIFEST.MF b/org.simantics.sysdyn/META-INF/MANIFEST.MF index 66b45852..218edabe 100644 --- a/org.simantics.sysdyn/META-INF/MANIFEST.MF +++ b/org.simantics.sysdyn/META-INF/MANIFEST.MF @@ -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, diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java index 980948d0..c1cf7fb3 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java @@ -37,9 +37,9 @@ public class MdlUtil { // by an open parenthesis or a special name) public static final String VARIABLE_EXPRESSION = "([A-Za-z](?![\\s\\w]*\\()\\w*(?:\\s+\\w+)*|"+SPECIAL_NAME+")"; - // matches a vensim subscript variable (basic name with a list of indices) + // matches a vensim subscript variable (variable with a list of indices) public static final String SUBSCRIPT = - "("+BASIC_NAME+")\\[("+BASIC_NAME+"\\!?(?:,"+BASIC_NAME+"\\!?)*)\\]"; + VARIABLE+"\\[("+BASIC_NAME+"\\!?(?:,"+BASIC_NAME+"\\!?)*)\\]"; // matches a vensim function (basic name followed by an open parenthesis) public static final String FUNCTION = "("+BASIC_NAME+")\\s*\\("; @@ -90,9 +90,13 @@ public class MdlUtil { // replace double quotes with single quotes in quoted variable // names as per modelica syntax (currently useless and might // actually break something) - result.append('\''); - result.append(variable.substring(1, variable.length() - 1)); - result.append('\''); +// result.append('\''); +// result.append(variable.substring(1, variable.length() - 1)); +// result.append('\''); + // try this: replace quoted variable names with normalized versions + String foobar = variable.replace('"', ' '); + foobar = foobar.replace('-', ' '); + result.append(variable); } else { // in all other cases, manipulate variable names slightly to @@ -137,6 +141,7 @@ public class MdlUtil { // are replaced with dot-operations (e.g. * with .*) inside // the parameters result.append("sum("); + // TODO: notice that this does not work correctly, should be replaced with a proper parser int closing = expression.indexOf(')', matcher.end()); String parameters = expression.substring(matcher.end(), closing); parameters = parameters.replaceAll("\\*", ".*"); @@ -145,6 +150,7 @@ public class MdlUtil { parameters = parameters.replaceAll("-", ".-"); result.append(parameters); offset = closing; + offset = matcher.end(); } else if (function.equalsIgnoreCase("if then else")) { result.append("IFTHENELSE("); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchConnection.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchConnection.java index e1fe384a..e4b292c1 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchConnection.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchConnection.java @@ -144,8 +144,6 @@ public class SketchConnection extends SketchObject { double x1 = (double)points[0]; double y1 = (double)points[1]; - //System.err.println("("+x0+","+y0+") -> ("+x1+","+y1+") -> ("+x2+","+y2+")"); - double dx0 = x1 - x0; double dy0 = y1 - y0; double dx1 = x1 - x2; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java index c6b4a940..dc3bdf2c 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java @@ -45,10 +45,12 @@ public class Subscript extends Declaration { String expression = MdlUtil.normalize(matcher.group(subscriptExpression)); Subscript subscript; - if (equivalent) + if (equivalent) { subscript = new Subscript(name, mdl, expression); - else + } + else { subscript = new Subscript(name, mdl, Arrays.asList(expression.split(","))); + } subscript.parseSuffix(matcher.group(subscriptSuffix)); return subscript; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model.java index 5bfd82ae..c9837bcf 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model.java @@ -125,6 +125,46 @@ public class Model implements IWriteableObject { connections.add(connection); } + public List getInFlows(Variable variable) { + List flows = new ArrayList(); + for (Connection connection : connections) { + if (connection instanceof Flow && connection.getHead().equals(variable)) { + flows.add((Flow)connection); + } + } + return flows; + } + + public List getOutFlows(Variable variable) { + List flows = new ArrayList(); + for (Connection connection : connections) { + if (connection instanceof Flow && connection.getTail().equals(variable)) { + flows.add((Flow)connection); + } + } + return flows; + } + + public List getInDependencies(Variable variable) { + List deps = new ArrayList(); + for (Connection connection : connections) { + if (connection instanceof Dependency && connection.getHead().equals(variable)) { + deps.add((Dependency)connection); + } + } + return deps; + } + + public List getOutDependencies(Variable variable) { + List deps = new ArrayList(); + for (Connection connection : connections) { + if (connection instanceof Dependency && connection.getTail().equals(variable)) { + deps.add((Dependency)connection); + } + } + return deps; + } + public void addEnumeration(Enumeration enumeration) { if (enumerations.get(enumeration.getName()) != null) { System.err.println("enumeration "+enumeration.getName()+" already defined"); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve.java index 011b3c15..1c9de120 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve.java @@ -22,7 +22,9 @@ public class Valve extends Variable { private TextPosition position; public Valve() { - + // TODO: should this really be done here? + this.orientation = Orientation.HORIZONTAL; + this.position = TextPosition.UNSET; } public Valve(Orientation orientation, TextPosition position) { -- 2.47.1