From 987a8350a0a70c94e88dfbeb09927f054b0d7666 Mon Sep 17 00:00:00 2001 From: jkauttio Date: Thu, 5 Jun 2014 13:43:22 +0000 Subject: [PATCH] Fix variable name handling in vensim import. refs #2924 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29594 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../simantics/sysdyn/modelImport/MdlUtil.java | 28 +++++++++++++++---- .../sysdyn/modelImport/model/Model.java | 4 ++- 2 files changed, 25 insertions(+), 7 deletions(-) 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 43babc2f..2b80696b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java @@ -58,6 +58,9 @@ public class MdlUtil { // replace inline operations str = str.replaceAll(":AND:", " and "); str = str.replaceAll(":OR:", " or "); + + // NOTE: this might not be correct + str = str.replaceAll("=", "=="); return str; } @@ -72,23 +75,36 @@ public class MdlUtil { String variable = matcher.group(1); + // TODO: fix this after proper support for quoted variable names + // is implemented + if (variable.equalsIgnoreCase("time")) { + // replace all references to simulation time with references + // to the special purpose time variable result.append("time"); } else if (variable.startsWith("\"") && variable.endsWith("\"")) { + // 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('\''); } - else if (variable.contains("-")) { - result.append('\''); - result.append(variable); - result.append('\''); - } else { + // in all other cases, manipulate variable names slightly to + // make them conform to modelica syntax (the proper way to do + // this would be to simply quote problematic variable names + // but quoted variable names are currently broken in sysdyn so + // that is not an option) + + // capitalize each part of the variable name to prevent + // collisions with reserved keywords (e.g. "public") String[] parts = variable.split("\\s+"); for (int i = 0; i < parts.length; i++) { - if (i > 0) + // a part of a variable name can not start with a number so + // concatenate the parts if this is the case + if (i > 0 && !Character.isDigit(parts[i].charAt(0))) result.append(' '); result.append(parts[i].substring(0, 1).toUpperCase() + parts[i].substring(1)); } 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 7f1326a8..61e663ea 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 @@ -95,11 +95,11 @@ public class Model implements IWriteableObject { public void addSymbol(Variable variable) { if (variables.get(variable.getName()) != null) { System.err.println("variable "+variable.getName()+" already defined"); + return; } variables.put(variable.getName(), variable); - symbols.add(variable); } @@ -127,6 +127,7 @@ public class Model implements IWriteableObject { public void addEnumeration(Enumeration enumeration) { if (enumerations.get(enumeration.getName()) != null) { System.err.println("enumeration "+enumeration.getName()+" already defined"); + return; } enumerations.put(enumeration.getName(), enumeration); } @@ -142,6 +143,7 @@ public class Model implements IWriteableObject { public void addFunction(Function function) { if (functions.get(function.getName()) != null) { System.err.println("function "+function.getName()+" already defined"); + return; } functions.put(function.getName(), function); } -- 2.47.1