From 09d9777d103fb5213bdca4f2fab861f3a01f710d Mon Sep 17 00:00:00 2001 From: miettinen Date: Thu, 26 Sep 2013 08:53:45 +0000 Subject: [PATCH] Sysdyn variable names with space characters allowed (refs #2930 #4426 #3944 #4265). Still lacks a few fundamental properties. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27888 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../chart/properties/RVIModifier.java | 2 +- .../ui/handlers/FindReplaceHandler.java | 2 +- .../widgets/arrays/EnumerationIndexNode.java | 2 +- .../expressions/CompletionProcessor.java | 2 +- .../ExpressionFieldConfiguration.java | 1 - .../sensitivity/VariableNameModifier.java | 2 +- .../sysdyn/ui/utils/NameValidator.java | 52 ++++++++++++-- org.simantics.sysdyn/ModelicaParser.html | 16 +---- .../expressionParser/ExpressionParser.jj | 72 +++++++++++++------ .../sysdyn/modelParser/ModelicaParser.jj | 1 + .../sysdyn/modelica/ModelicaWriter.java | 13 ++-- .../sysdyn/representation/Enumeration.java | 14 ++-- .../representation/EnumerationIndex.java | 11 +++ .../representation/IndependentVariable.java | 11 +-- .../sysdyn/representation/Input.java | 11 +-- .../sysdyn/representation/Module.java | 15 ++-- .../representation/ParameterOverride.java | 3 +- .../sysdyn/representation/Redeclaration.java | 8 +-- .../sysdyn/representation/Sheet.java | 4 +- .../sysdyn/representation/Stock.java | 5 +- .../sysdyn/representation/Variable.java | 12 ++++ .../expressions/ConstantExpression.java | 7 ++ .../expressions/DelayExpression.java | 43 ++++++----- .../expressions/Expression.java | 12 +++- .../expressions/NormalExpression.java | 13 +++- .../expressions/ParameterExpression.java | 11 ++- .../expressions/StockExpression.java | 18 +++-- .../expressions/WithLookupExpression.java | 11 ++- .../representation/utils/FormatUtils.java | 42 ++++++++++- .../representation/utils/IndexUtils.java | 18 +++-- .../utils/RepresentationUtils.java | 5 +- .../sysdyn/unitParser/UnitParser.jjt | 2 +- .../sysdyn/utils/DocumentationUtils.java | 4 +- 33 files changed, 322 insertions(+), 123 deletions(-) diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/RVIModifier.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/RVIModifier.java index 6a863802..3c87d5aa 100644 --- a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/RVIModifier.java +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/RVIModifier.java @@ -46,7 +46,7 @@ public class RVIModifier extends TextModifyListenerImpl { private char[] alphaNumericCharacters = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','å','ä','ö', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Å','Ä','Ö', - '1','2','3','4','5','6','7','8','9','0','.','_'}; + '1','2','3','4','5','6','7','8','9','0','.'}; /** * Create a new RVIModifier and attach a content proposal support to control diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/FindReplaceHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/FindReplaceHandler.java index 35c9dcc6..86a6a397 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/FindReplaceHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/FindReplaceHandler.java @@ -49,7 +49,7 @@ public class FindReplaceHandler extends AbstractHandler { // If focus is on model browser, default search in all models, allow also current diagram. FindReplaceDialog dialog = new FindReplaceDialog(shell, Scope.ALL_MODELS, true); dialog.open(); - } else if (d != null && d instanceof IDiagram) { + } else if (d != null && d instanceof IDiagram && workbenchPart instanceof DiagramEditor) { // If focus is on diagram editor, default search in current diagram, allow also all models. FindReplaceDialog dialog = new FindReplaceDialog(shell, Scope.CURRENT_DIAGRAM, true); dialog.open(); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationIndexNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationIndexNode.java index 45f1e1a1..ff7b7021 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationIndexNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationIndexNode.java @@ -47,7 +47,7 @@ public class EnumerationIndexNode extends AbstractNode implements IMo @Override public String isValid(String label) { - if(!new VariableNameValidator().isValidModelica(label)) + if(!new VariableNameValidator().isValidModelica(label, true)) return "Not valid"; return null; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/CompletionProcessor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/CompletionProcessor.java index 6db6f18f..b5bc30ea 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/CompletionProcessor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/CompletionProcessor.java @@ -57,7 +57,7 @@ public class CompletionProcessor implements IContentAssistProcessor { private final char[] allowedCharacters = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','å','ä','ö', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Å','Ä','Ö', - '1','2','3','4','5','6','7','8','9','0','.','_','(',')'}; + '1','2','3','4','5','6','7','8','9','0','.','(',')'}; private final String allowedConnectedCharactersRegExp = "[\\Q({[:;,<=>+-*/^\\E]"; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java index d2f646ba..205d41db 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java @@ -105,7 +105,6 @@ public class ExpressionFieldConfiguration extends SourceViewerConfiguration { final Token comment = new Token(new TextAttribute(resourceManager.createColor(new RGB(63, 127, 95)))); - WordRule reservedWord = new WordRule(new IWordDetector() { @Override public boolean isWordStart(char c) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/sensitivity/VariableNameModifier.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/sensitivity/VariableNameModifier.java index 1b04c098..eaac45db 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/sensitivity/VariableNameModifier.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/sensitivity/VariableNameModifier.java @@ -39,7 +39,7 @@ public class VariableNameModifier extends TextModifyListenerImpl { private char[] alphaNumericCharacters = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','å','ä','ö', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Å','Ä','Ö', - '1','2','3','4','5','6','7','8','9','0','.','_'}; + '1','2','3','4','5','6','7','8','9','0','.'}; public VariableNameModifier(Control control, WidgetSupport support, String variableNameRelationUri, String indexUri) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/NameValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/NameValidator.java index b349eb55..25ce601e 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/NameValidator.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/NameValidator.java @@ -9,6 +9,7 @@ import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ServiceException; import org.simantics.db.request.Read; import org.simantics.layer0.Layer0; import org.simantics.spreadsheet.resource.SpreadsheetResource; @@ -90,11 +91,41 @@ public abstract class NameValidator { if(nameIsTaken(graph, resource, name)) return false; // Decline names which are against Modelica naming rules - if(!isValidModelica(name)) return false; + boolean allowSpaces = doesResourceAllowSpacedName(graph, resource); + if(!isValidModelica(name, allowSpaces)) return false; return true; } /** + * Determine whether a Sysdyn Variable can have whitespace in its name (based on its type). + * @param graph + * @param resource Resource of the variable + * @return true iff spaces are allowed in the name + */ + protected boolean doesResourceAllowSpacedName(ReadGraph graph, + Resource resource) { + SysdynResource sr = SysdynResource.getInstance(graph); + SpreadsheetResource sheet = SpreadsheetResource.getInstance(graph); + try { + // Function, FunctionLibrary, and SharedFunctionLibrary are not + // allowed to have whitespace. + if (graph.isInstanceOf(resource, sr.Variable) +// || graph.isInstanceOf(resource, sr.Module) +// || graph.isInstanceOf(resource, sr.ModuleType) + || graph.isInstanceOf(resource, sr.Enumeration) + || graph.isInstanceOf(resource, sr.EnumerationIndex) + || graph.isInstanceOf(resource, sheet.Spreadsheet) + || graph.isInstanceOf(resource, sr.SysdynModel)) { + return true; + } + } catch (ServiceException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return false; + } + + /** * Checks that the syntax of the given name is valid and there * are no other variables that have the same name in the configuration * @@ -128,14 +159,23 @@ public abstract class NameValidator { * @param name name that is validated * @return true iff the syntax of the name is valid */ - public boolean isValidModelica(String name) { + public boolean isValidModelica(String name, boolean allowSpaces) { String lowerCase = name.toLowerCase(); - Pattern p = Pattern.compile("[a-zA-Z][a-zA-Z0-9]*+"); + String pattern = "[a-zA-Z][a-zA-Z0-9]*" + (allowSpaces ? "( [a-zA-Z][a-zA-Z0-9]*)*" : ""); + Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(lowerCase); - if (!m.matches() || ModelicaSourceViewerConfiguration.keywords.contains(name)) + if (!m.matches()) { return false; - else - return true; + } else { + String[] splitNames= name.split("\\s+"); + for (String splitName : splitNames) { + // Do not allow Modelica keywords to be a part of a whitespaced name + if (ModelicaSourceViewerConfiguration.keywords.contains(splitName)) { + return false; + } + } + } + return true; } /** diff --git a/org.simantics.sysdyn/ModelicaParser.html b/org.simantics.sysdyn/ModelicaParser.html index b33e433b..157b944c 100644 --- a/org.simantics.sysdyn/ModelicaParser.html +++ b/org.simantics.sysdyn/ModelicaParser.html @@ -284,7 +284,7 @@ component_clause ::= -( type_prefix ) type_specifier ( array_subscripts )? component_list +( type_prefix ) ( type_specifier ) ( array_subscripts )? ( component_list ) type_prefix @@ -326,17 +326,7 @@ modification ::= -class_modification ( "=" expression )? - - - -| -"=" expression - - - -| -":=" expression +( class_modification ( "=" expression )? | "=" expression | ":=" expression ) class_modification @@ -640,7 +630,7 @@ | -"(" output_expression_list ")" +"(" expression ")" diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj index 722c0911..7c79449e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj @@ -12,7 +12,7 @@ import java.util.List; import java.util.ArrayList; import java.util.HashMap; - +@SuppressWarnings({"unused", "serial"}) public class ExpressionParser { public class ForRange { public Token start; @@ -212,27 +212,55 @@ void primary() : { | "end" } +Token ident() : { + Token concatToken = null; + Token continuationToken = null; + String fullStr = null; +} { + + { concatToken = token; } + ( continuationToken = ident() + { + fullStr = new String(""); + // Replace whitespace sequence with a single space character. + fullStr += concatToken.image + " " + continuationToken.image; + + Token tempToken = new Token(concatToken.kind, fullStr); + tempToken.beginLine = concatToken.beginLine; + tempToken.beginColumn = concatToken.beginColumn; + tempToken.endLine = token.endLine; + tempToken.endColumn = token.endColumn; + concatToken = tempToken; + System.out.println(concatToken.image); + } + )? + { return concatToken; + } } void name() : { + Token identToken = null; } { - { if (functionCall == null) - functionCall = token.image; + identToken = ident() + { if (functionCall == null) + functionCall = identToken.image; else - functionCall += "." + token.image; + functionCall += "." + identToken.image; } ( "." name() )? } void component_reference(String prevToken) : { + Token identToken = null; } { - //IDENT [ array_subscripts ] [ "." component_reference ] - { - String name = token.image; + //IDENT [ array_subscripts ] [ "." component_reference ] + identToken = ident() + { + String name = identToken.image; // forIndex == true and prevToken != null => this is the second part of an enumeration in for-index if(forIndex == true) { if(prevToken != null) { if(enumerationReferences.get(prevToken) == null) { enumerationReferences.put(prevToken, new ArrayList()); } List list = enumerationReferences.get(prevToken); - list.add(token); + list.add(identToken); // forIndex == true and prevToken == null => this is the enumeration in for-index } else { @@ -240,7 +268,7 @@ void component_reference(String prevToken) : { enumerationReferences.put(name, new ArrayList()); } List list = enumerationReferences.get(name); - list.add(token); + list.add(identToken); } } else { if(prevToken != null) @@ -248,18 +276,18 @@ void component_reference(String prevToken) : { references.put(name, new ArrayList()); } List list = references.get(name); - list.add(token); + list.add(identToken); if(functionCall != null) { if(!functionCallReferences.containsKey(functionCall)) functionCallReferences.put(functionCall, new ArrayList()); List functionReferencelist = getFunctionCallReferences().get(functionCall); - functionReferencelist.add(token); } + functionReferencelist.add(identToken); } } } - ( array_subscripts() )? ( "." component_reference(name) )? + ( array_subscripts(identToken) )? ( "." component_reference(name) )? } void function_call_args() : { @@ -275,7 +303,7 @@ void function_arguments() : { } { //expression [ "," function_arguments | for for_indices ] //| named_arguments - LOOKAHEAD(2) named_argument() ( "," function_arguments() )? + LOOKAHEAD(named_argument()) named_argument() ( "," function_arguments() )? | expression() ( "," function_arguments() | "for" for_indices() )? } @@ -287,10 +315,12 @@ void for_indices() : { } void for_index() : { + Token identToken = null; } { //IDENT [ in expression ] - { - forIndices.put(token, currentRange); + identToken = ident() + { + forIndices.put(identToken, currentRange); forIndex = true; } ( "in" { ForRange forRange = new ForRange(); @@ -313,7 +343,7 @@ void named_arguments() : { void named_argument() : { } { - "=" expression() + ident() "=" expression() } void output_expression_list() : { @@ -327,10 +357,10 @@ void expression_list() : { expression() ( "," expression() )* } -void array_subscripts() : { if(ranges.get(token.image) == null) { - ranges.put(token.image, new ArrayList>()); +void array_subscripts(Token prevToken) : { if(ranges.get(prevToken.image) == null) { + ranges.put(prevToken.image, new ArrayList>()); } - List> rangesList = ranges.get(token.image); + List> rangesList = ranges.get(prevToken.image); currentRange = new ArrayList(); } { "[" subscript(currentRange) ( "," subscript(currentRange) )* "]" @@ -365,11 +395,13 @@ void subscript(List currentRange) : { } void rangeIndex(Token rangeToken, boolean first) : { + Token identToken = null; if(!first) rangeToken.image = rangeToken.image + ":"; } { { rangeToken.image = rangeToken.image + token.image; } - | { rangeToken.image = rangeToken.image + token.image; + | identToken = ident() + { rangeToken.image = rangeToken.image + identToken; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelParser/ModelicaParser.jj b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelParser/ModelicaParser.jj index fe483fe1..7bc48aa7 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelParser/ModelicaParser.jj +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelParser/ModelicaParser.jj @@ -8,6 +8,7 @@ package org.simantics.sysdyn.modelParser; import java.util.ArrayList; +@SuppressWarnings({"unused", "serial"}) public class ModelParser { private ArrayList inputs = new ArrayList(); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java index 3c293429..186c92a5 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java @@ -35,6 +35,7 @@ import org.simantics.sysdyn.representation.Variable; * ModelicaWriter writes Sysdyn model representations (objmap) into Modelica code. * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class ModelicaWriter { @@ -69,7 +70,7 @@ public class ModelicaWriter { HashSet sheetNames = new HashSet(); for(Sheet sheet : getSpreadSheets(configurations)) - sheetNames.add(sheet.getName()); + sheetNames.add(sheet.getModelicaName()); // Write all module configurations to the declarations part (first) for(Configuration conf : configurations) { @@ -286,7 +287,7 @@ public class ModelicaWriter { Module module = (Module)dependency.getHead(); Input reference = (Input)dependency.refersTo(); if(reference != null && reference.getName() != null && (reference.getVariability() == null || reference.getVariability().isEmpty())) { - b.append(" " + module.getName() + "." + reference.getName() + " = " + variable.getName() + ";\n"); + b.append(" " + module.getName() + "." + reference.getModelicaName() + " = " + variable.getModelicaName() + ";\n"); moduleInputs.get(module.getName()).remove(reference); } } @@ -295,7 +296,7 @@ public class ModelicaWriter { for(String moduleLabel : moduleInputs.keySet()) { for(Input input : moduleInputs.get(moduleLabel)) { if(input.getVariability() == null || input.getVariability().isEmpty()) - b.append(" " + moduleLabel + "." + input.getName() + " = " + input.getDefaultInputValue(moduleLabel) + ";\n"); + b.append(" " + moduleLabel + "." + input.getModelicaName() + " = " + input.getDefaultInputValue(moduleLabel) + ";\n"); } } @@ -313,7 +314,7 @@ public class ModelicaWriter { } - /** + /** * Define continuous input references * @param b String builder * @param inputReferences Input references @@ -323,7 +324,7 @@ public class ModelicaWriter { for(Input i : inputReferences.keySet()) { if(i.getVariability() == null || i.getVariability().isEmpty()) { // Define only continuous variables here - b.append(" " + i.getName() + " = " + inputReferences.get(i)); + b.append(" " + i.getModelicaName() + " = " + inputReferences.get(i)); } } } @@ -342,7 +343,7 @@ public class ModelicaWriter { String expression; // If reference exists, use reference name. Otherwise, use default value. if(reference != null && reference.getName() != null) - expression = module.getName() + "." + reference.getName() + ";\n"; + expression = module.getName() + "." + reference.getModelicaName() + ";\n"; else expression = input.getDefaultInputValue() + ";\n"; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java index 32a73ccd..c87889cb 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java @@ -43,12 +43,12 @@ public class Enumeration extends Variable { StringBuilder sb = new StringBuilder(); sb.append(getEnumerationClassDefinition()); sb.append(" "); - sb.append(this.name + "_class"); - sb.append(" " + this.name); + sb.append(getModelicaName() + "_class"); + sb.append(" " + getModelicaName()); sb.append(";\n"); - sb.append(" parameter Integer " + this.name + "_size = " + this.name + ".size;\n"); - sb.append(" parameter Integer " + this.name + "_elements[:] = " + this.name + ".elements;\n"); + sb.append(" parameter Integer " + getModelicaName() + "_size = " + getModelicaName() + ".size;\n"); + sb.append(" parameter Integer " + getModelicaName() + "_elements[:] = " + getModelicaName() + ".elements;\n"); return sb.toString(); } @@ -69,7 +69,7 @@ public class Enumeration extends Variable { StringBuilder indexesBuilder = new StringBuilder(); for(int i = 1; i <= indexes.size(); i++) { indexesBuilder.append(" constant Integer "); - indexesBuilder.append(indexes.get(i - 1).getName()); + indexesBuilder.append(indexes.get(i - 1).getModelicaName()); indexesBuilder.append(" = " + i + ";\n"); elementsBuilder.append(i); @@ -90,7 +90,7 @@ public class Enumeration extends Variable { StringBuilder sb = new StringBuilder(); sb.append(" class "); - sb.append(this.name); + sb.append(getModelicaName()); sb.append("_class\n"); sb.append(" extends Enumeration_class(size="); sb.append(indexes.size()); @@ -99,7 +99,7 @@ public class Enumeration extends Variable { sb.append(");\n"); sb.append(indexesBuilder); sb.append(" end "); - sb.append(this.name); + sb.append(getModelicaName()); sb.append("_class;\n"); return sb.toString(); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/EnumerationIndex.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/EnumerationIndex.java index 47ee9864..7e7e8364 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/EnumerationIndex.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/EnumerationIndex.java @@ -14,5 +14,16 @@ public class EnumerationIndex { public String getName() { return this.name; } + + /** + * Get Modelica compliant name + * @return The name of this variable with spaces (' ') replaced with + * underscore characters ('_') + */ + public String getModelicaName() { + if (this.name == null) + return null; + return this.name.replace(' ', '_'); + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java index c49eda85..bef05db0 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java @@ -27,6 +27,7 @@ import org.simantics.sysdyn.representation.utils.FormatUtils; * Representation of an independent variable * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public abstract class IndependentVariable extends Variable { @@ -48,13 +49,15 @@ public abstract class IndependentVariable extends Variable { sb.append(" "); sb.append(variability.getText().isEmpty() ? "" : variability.getText() + " "); sb.append(getType() + " "); - sb.append(getName()); + sb.append(getModelicaName()); sb.append(getRange()); // [= expression] if(variability == Variability.PARAMETER || variability == Variability.CONSTANT) { // parameters and constants are guaranteed to have only one expression - String equation = FormatUtils.formatExpressionForModelica(this, getExpressions().get(0).getExpression()); + String expression = getExpressions().get(0).getExpression(); + expression = FormatUtils.replaceWhitespace(expression); + String equation = FormatUtils.formatExpressionForModelica(this, expression); sb.append(" = " + equation); } @@ -74,7 +77,7 @@ public abstract class IndependentVariable extends Variable { return sb.toString(); } - /** + /** * Get the range of this variable, if it is an array variable. *

* Format: [EnumerationName.size (, Enumeration2Name.size)*] @@ -88,7 +91,7 @@ public abstract class IndependentVariable extends Variable { sb.append("["); Iterator iterator = enumerations.iterator(); while(iterator.hasNext()) { - sb.append(iterator.next().getName() + ".size"); + sb.append(iterator.next().getModelicaName() + ".size"); if(iterator.hasNext()) { sb.append(", "); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Input.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Input.java index 11bf8479..fe899208 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Input.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Input.java @@ -27,6 +27,7 @@ import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; * Representation of an input variable * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ @GraphType(SysdynResource.URIs.Input) @@ -62,7 +63,7 @@ public class Input extends Variable { Iterator i = getArrayIndexes().iterator(); while(i.hasNext()) { Enumeration e = i.next(); - sb.append((inModule != null && !inModule.isEmpty() ? inModule + "." : "") + e.getName() + ".size"); + sb.append((inModule != null && !inModule.isEmpty() ? inModule + "." : "") + e.getModelicaName() + ".size"); if(i.hasNext()) sb.append(", "); } @@ -110,7 +111,7 @@ public class Input extends Variable { sb.append("["); Iterator iterator = enumerations.iterator(); while(iterator.hasNext()) { - sb.append(iterator.next().getName() + ".size"); + sb.append(iterator.next().getModelicaName() + ".size"); if(iterator.hasNext()) { sb.append(", "); } @@ -120,7 +121,7 @@ public class Input extends Variable { } boolean continuous = variability == null || variability.isEmpty(); - return " " + (continuous ? "" : variability + " ") + getType() + " " + getName() + range + ";\n"; + return " " + (continuous ? "" : variability + " ") + getType() + " " + getModelicaName() + range + ";\n"; } @Override @@ -132,8 +133,8 @@ public class Input extends Variable { Module module = (Module)dependency.getTail(); Variable reference = (Variable)dependency.refersTo(); // If reference exists, use reference name. Otherwise, use default value. - if(reference != null && reference.getName() != null) - expression = "Reference:
" + module.getName() + "." + reference.getName() + ";\n"; + if(reference != null && reference.getModelicaName() != null) + expression = "Reference:
" + module.getName() + "." + reference.getModelicaName() + ";\n"; } return expression + super.getDocumentationDefinition(graph); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java index 70829bed..3938f200 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java @@ -29,6 +29,7 @@ import org.simantics.utils.datastructures.Pair; /** * Representation of a module instance * @author Teemu Lempinen + * @author Tuomas Miettinen * */ @GraphType(SysdynResource.URIs.Module) @@ -164,7 +165,7 @@ public class Module implements IElement { if(outputVariable instanceof Shadow) outputVariable = ((Shadow) outputVariable).getOriginal(); - String name = outputVariable.getName(); + String name = outputVariable.getModelicaName(); Module module = (Module)dependency.getHead(); @@ -192,16 +193,16 @@ public class Module implements IElement { else declaration = declaration.substring(0, declaration.length() - 2); // Remove ending ";\n" - name = outputVariable.getName() + "_reference"; - declaration = declaration.replace(outputVariable.getName(), name); - declaration += " = " + outputVariable.getName() + " /* Reference value to avoid name conflicts in module instantiation */ ;\n"; + name = outputVariable.getModelicaName() + "_reference"; + declaration = declaration.replace(outputVariable.getModelicaName(), name); + declaration += " = " + outputVariable.getModelicaName() + " /* Reference value to avoid name conflicts in module instantiation */ ;\n"; references.append(declaration); break; } } } - declarations.append(", " + reference.getName() + " = " + name); + declarations.append(", " + reference.getModelicaName() + " = " + name); } } } @@ -327,7 +328,7 @@ public class Module implements IElement { } first = false; - result.append(getName() + "." + ((Variable)d.refersTo()).getName() + " = " + ((Variable)d.getTail()).getName()); + result.append(getName() + "." + ((Variable)d.refersTo()).getModelicaName() + " = " + ((Variable)d.getTail()).getModelicaName()); result.append("
"); } result.append("

"); @@ -340,7 +341,7 @@ public class Module implements IElement { } first = false; - result.append(((Variable)d.getHead()).getName() + " = " + getName() + "." + ((Variable)d.refersTo()).getName()); + result.append(((Variable)d.getHead()).getModelicaName() + " = " + getName() + "." + ((Variable)d.refersTo()).getModelicaName()); result.append("
"); } result.append("

"); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/ParameterOverride.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/ParameterOverride.java index c9f1b2f9..713f7ee0 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/ParameterOverride.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/ParameterOverride.java @@ -20,6 +20,7 @@ import org.simantics.sysdyn.SysdynResource; /** * Representation of a parameter override * @author Teemu Lempinen + * @author Tuomas Miettinen * */ @GraphType(SysdynResource.URIs.Module_ParameterOverride) @@ -52,7 +53,7 @@ public class ParameterOverride extends Entity { * @return override equation */ public String getOverride() { - return variable.getName() + " = " + expression; + return variable.getModelicaName() + " = " + expression; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Redeclaration.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Redeclaration.java index 6f1e8b3c..0d2efc98 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Redeclaration.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Redeclaration.java @@ -29,13 +29,13 @@ public class Redeclaration { return ""; } StringBuilder sb = new StringBuilder(); - sb.append(replacedEnumeration.getName()); + sb.append(replacedEnumeration.getModelicaName()); sb.append(".size = "); - sb.append(replacingEnumeration.getName()); + sb.append(replacingEnumeration.getModelicaName()); sb.append("_size, "); - sb.append(replacedEnumeration.getName()); + sb.append(replacedEnumeration.getModelicaName()); sb.append(".elements = "); - sb.append(replacingEnumeration.getName()); + sb.append(replacingEnumeration.getModelicaName()); sb.append("_elements"); return sb.toString(); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java index 7037b585..7a87c0b5 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java @@ -100,7 +100,7 @@ public class Sheet extends org.simantics.sysdyn.representation.Variable { public String getStringRepresentation() { final StringBuilder clazz = new StringBuilder(); - clazz.append("class " + name + "_class\n"); + clazz.append("class " + getModelicaName() + "_class\n"); try { Simantics.getSession().syncRequest(new ReadRequest() { @@ -174,7 +174,7 @@ public class Sheet extends org.simantics.sysdyn.representation.Variable { e.printStackTrace(); } - clazz.append("end " + name + "_class;\n"); + clazz.append("end " + getModelicaName() + "_class;\n"); // clazz.append(" " + name + "_class " + name + ";\n"); return clazz.toString(); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Stock.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Stock.java index 8e259bbf..77b07ae5 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Stock.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Stock.java @@ -23,6 +23,7 @@ import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; /** * Representation of a Stock variable * @author Teemu Lempinen + * @author Tuomas Miettinen * */ @GraphType(SysdynResource.URIs.Stock) @@ -62,7 +63,7 @@ public class Stock extends IndependentVariable { // start parameter is not used, everything needs to be fixed=false if(getArrayIndexes() != null && !getArrayIndexes().isEmpty()) each = "each"; - return " " + getType() + " " + getName() + getRange() + "(" + each + " fixed=false);\n"; + return " " + getType() + " " + getModelicaName() + getRange() + "(" + each + " fixed=false);\n"; } else { // Start parameter is used. Parameter guarantees that there is only one expression. StockExpression e = (StockExpression)getExpressions().get(0); @@ -76,7 +77,7 @@ public class Stock extends IndependentVariable { return " " + getType() + " " + - getName() + + getModelicaName() + getRange() + "(" + (e.getStartValue() != null ? each : "") + diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java index 09a23c4f..997e61db 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java @@ -25,6 +25,7 @@ import org.simantics.sysdyn.representation.expressions.IExpression; /** * Abstract class for representing a variable in system dynamics models * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public abstract class Variable implements IElement { @@ -72,6 +73,17 @@ public abstract class Variable implements IElement { return this.name; } + /** + * Get Modelica compliant name + * @return The name of this variable with spaces (' ') replaced with + * underscore characters ('_') + */ + public String getModelicaName() { + if (this.name == null) + return null; + return this.name.replace(' ', '_'); + } + /** * * @return Parent configuration of this variable (or null if something is wrong) diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ConstantExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ConstantExpression.java index 2e4a5dad..3b4036fe 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ConstantExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ConstantExpression.java @@ -14,11 +14,13 @@ package org.simantics.sysdyn.representation.expressions; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.representation.utils.FormatUtils; /** * Represents a constant expression * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ @GraphType(SysdynResource.URIs.ConstantExpression) @@ -30,5 +32,10 @@ public class ConstantExpression extends Expression { @Override public String getExpression() { return equation; + } + + @Override + public String getModelicaExpression() { + return FormatUtils.replaceWhitespace(equation); } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java index 59121d64..6409704b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java @@ -45,6 +45,7 @@ import org.simantics.sysdyn.representation.utils.UnitUtils; * DL=delay time/3 * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ @GraphType(SysdynResource.URIs.DelayExpression) @@ -64,9 +65,10 @@ public class DelayExpression extends Expression { @Override public String getDeclarationAddition() { - - String delayTime = FormatUtils.formatExpressionForModelica(parent, this.delayTime); - String initialValue = FormatUtils.formatExpressionForModelica(parent, this.initialValue); + String delayTime = FormatUtils.replaceWhitespace(this.delayTime); + delayTime = FormatUtils.formatExpressionForModelica(parent, delayTime); + String initialValue = FormatUtils.replaceWhitespace(this.initialValue); + initialValue = FormatUtils.formatExpressionForModelica(parent, initialValue); Variability delayTimeVariability = Variability.getVariability((IndependentVariable)parent, delayTime); Variability initialVariability = Variability.CONTINUOUS; if(initialValue != null && initialValue.length() > 0) { @@ -83,7 +85,7 @@ public class DelayExpression extends Expression { String range = parent.getRange(); // Delay declaration - declaration.append(" " + parent.getName() + "_delayClass " + parent.getName() + "_delayClass_instance"); + declaration.append(" " + parent.getModelicaName() + "_delayClass " + parent.getModelicaName() + "_delayClass_instance"); // Change enumeration sizes from the delay class. Supports overridden enumerations in modules. if(range.length() > 0 || !initialVariability.equals(Variability.CONTINUOUS) || !delayTimeVariability.equals(Variability.CONTINUOUS)) { @@ -139,7 +141,7 @@ public class DelayExpression extends Expression { private String getDelayClass(String range, int n) { StringBuilder sb = new StringBuilder(); - sb.append("class " + parent.getName() + "_delayClass\n"); + sb.append("class " + parent.getModelicaName() + "_delayClass\n"); // Enumeration sizes as parameters. Sizes correspond the enumeration sizes of variable if(range.length() > 0) { @@ -149,6 +151,7 @@ public class DelayExpression extends Expression { } } + String delayTime = FormatUtils.replaceWhitespace(this.delayTime); String dt = FormatUtils.formatExpressionForModelica(parent,delayTime); Variability delayTimeVariability = Variability.getVariability(parent, dt); if(!delayTimeVariability.equals(Variability.CONTINUOUS)) { @@ -166,7 +169,8 @@ public class DelayExpression extends Expression { // Get initial value variability Variability initialValueVariability = Variability.CONTINUOUS; - String initEquation = FormatUtils.formatExpressionForModelica(parent, this.initialValue); + String initEquation = FormatUtils.replaceWhitespace(this.initialValue); + initEquation = FormatUtils.formatExpressionForModelica(parent, initEquation); if(initialValue != null && initialValue.length() > 0) { initialValueVariability = Variability.getVariability(parent, initEquation); } @@ -211,7 +215,7 @@ public class DelayExpression extends Expression { sb.append("\tdelay" + i + " = LV" + i + " " + (range.isEmpty() ? "/" : "./") + "DL;\n"); } - sb.append("end " + parent.getName() + "_delayClass;\n"); + sb.append("end " + parent.getModelicaName() + "_delayClass;\n"); return sb.toString(); } @@ -219,34 +223,37 @@ public class DelayExpression extends Expression { public String getEquation() { StringBuilder sb = new StringBuilder(); - String equation = FormatUtils.formatExpressionForModelica(parent, this.equation); - String delayTime = FormatUtils.formatExpressionForModelica(parent, this.delayTime); - String initialValue = FormatUtils.formatExpressionForModelica(parent, this.initialValue); + String equation = FormatUtils.replaceWhitespace(this.equation); + equation = FormatUtils.formatExpressionForModelica(parent, equation); + String delayTime = FormatUtils.replaceWhitespace(this.delayTime); + delayTime = FormatUtils.formatExpressionForModelica(parent, delayTime); + String initialValue = FormatUtils.replaceWhitespace(this.initialValue); + initialValue = FormatUtils.formatExpressionForModelica(parent, initialValue); // First "valve" in the delay - sb.append(" " + parent.getName() + "_delayClass_instance.delay0 = " + equation + ";\n"); + sb.append(" " + parent.getModelicaName() + "_delayClass_instance.delay0 = " + equation + ";\n"); // Delay time (if continuous) Variability delayTimeVariability = Variability.getVariability(parent, delayTime); if(delayTimeVariability.equals(Variability.CONTINUOUS)) { - sb.append(" " + parent.getName() + "_delayClass_instance.delayTime = " + delayTime + ";\n"); + sb.append(" " + parent.getModelicaName() + "_delayClass_instance.delayTime = " + delayTime + ";\n"); } // Initial value if(initialValue == null || initialValue.length() == 0) { // No initial value set, use delay0 (the first "valve") - sb.append(" " + parent.getName() + "_delayClass_instance.initialValue = " + parent.getName() + "_delayClass_instance.delay0;\n"); + sb.append(" " + parent.getModelicaName() + "_delayClass_instance.initialValue = " + parent.getModelicaName() + "_delayClass_instance.delay0;\n"); } else { // Continuous initial value Variability initialVariability = Variability.getVariability(parent, initialValue); if(initialVariability.equals(Variability.CONTINUOUS)) { - sb.append(" " + parent.getName() + "_delayClass_instance.initialValue = " + initialValue + ";\n"); + sb.append(" " + parent.getModelicaName() + "_delayClass_instance.initialValue = " + initialValue + ";\n"); } } // The value of the actual variable String range = IndexUtils.rangeToIndexes(parent, this.getArrayRange()); - sb.append(" " + parent.getName() + (range.equals("[:]") ? "" : range) + " = " + parent.getName() + "_delayClass_instance.delay" + order + ";\n"); + sb.append(" " + parent.getModelicaName() + (range.equals("[:]") ? "" : range) + " = " + parent.getModelicaName() + "_delayClass_instance.delay" + order + ";\n"); return sb.toString(); } @@ -255,7 +262,11 @@ public class DelayExpression extends Expression { return "This + is + not + a + parameter + at + any + time"; } - + @Override + public String getModelicaExpression() { + return getExpression(); + } + @Override public String validateUnits(ReadGraph graph, SysdynModel model) { if(parent.getUnit() == null) diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java index b262bc73..7463bfcc 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java @@ -25,6 +25,7 @@ import org.simantics.sysdyn.representation.utils.UnitUtils; * Abstract class for any expression * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public abstract class Expression implements IExpression { @@ -39,7 +40,7 @@ public abstract class Expression implements IExpression { public String getEquation() { return null; } - + @Override public String getInitialEquation() { return null; @@ -55,6 +56,15 @@ public abstract class Expression implements IExpression { return null; } + /** + * Get Modelica compliant expression + * @return The expression with spaces (' ') replaced with + * underscore characters ('_') + */ + public String getModelicaExpression() { + return null; + } + @Override public String getArrayRange() { if(range == null) diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java index a8d4d667..696854ea 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java @@ -21,6 +21,7 @@ import org.simantics.sysdyn.representation.utils.IndexUtils; * Representation of a normal (auxiliary) expression * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ @GraphType(SysdynResource.URIs.NormalExpression) @@ -33,12 +34,18 @@ public class NormalExpression extends Expression { public String getExpression() { return equation; } - + + @Override + public String getModelicaExpression() { + return FormatUtils.replaceWhitespace(getExpression()); + } + @Override public String getEquation() { - String equation = FormatUtils.formatExpressionForModelica(parent, this.equation); + String equation = FormatUtils.replaceWhitespace(this.equation); + equation = FormatUtils.formatExpressionForModelica(parent, equation); String range = IndexUtils.rangeToIndexes(parent, this.getArrayRange()); - return " " + parent.getName() + (range.equals("[:]") ? "" : range) + " = " + equation + ";\n"; + return " " + parent.getModelicaName() + (range.equals("[:]") ? "" : range) + " = " + equation + ";\n"; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java index 96ba6899..6723be47 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java @@ -14,12 +14,14 @@ package org.simantics.sysdyn.representation.expressions; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.representation.utils.FormatUtils; import org.simantics.sysdyn.representation.utils.IndexUtils; /** * Representation of a parameter expression * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ @GraphType(SysdynResource.URIs.ParameterExpression) @@ -33,6 +35,12 @@ public class ParameterExpression extends Expression { return equation + " /* Actual value read from init file */"; } + @Override + public String getModelicaExpression() { + String equation = FormatUtils.replaceWhitespace(this.equation); + return equation + " /* Actual value read from init file */"; + } + @Override public String getDeclarationAddition() { return ""; @@ -44,8 +52,9 @@ public class ParameterExpression extends Expression { */ @Override public String getEquation() { + String equation = FormatUtils.replaceWhitespace(this.equation); String range = IndexUtils.rangeToIndexes(parent, this.getArrayRange()); - return " " + parent.getName() + (range.equals("[:]") ? "" : range) + " = " + equation + ";\n"; + return " " + parent.getModelicaName() + (range.equals("[:]") ? "" : range) + " = " + equation + ";\n"; }; public Double getValue() { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java index 9bd2b3db..ba6916cc 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java @@ -36,6 +36,7 @@ import org.simantics.sysdyn.representation.utils.UnitUtils; * Class representing a stock expression in a variable * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ @GraphType(SysdynResource.URIs.StockExpression) @@ -48,8 +49,12 @@ public class StockExpression extends Expression { public String getExpression() { return initialEquation; } - + @Override + public String getModelicaExpression() { + return FormatUtils.replaceWhitespace(getExpression()); + } + @Override public String getEquation() { @@ -59,7 +64,7 @@ public class StockExpression extends Expression { // Stock equation is always der(Stock) StringBuilder b = new StringBuilder(); b.append(" der(") - .append(parent.getName() + range) + .append(parent.getModelicaName() + range) .append(") ="); // Stock equation is formed automatically using incoming and outgoing flows (actually the nearest valves in those flows) @@ -73,7 +78,7 @@ public class StockExpression extends Expression { } else { b.append(" zeros("); for(int i = 0; i < enumerations.size(); i++) { - b.append(enumerations.get(i).getName() + ".size"); + b.append(enumerations.get(i).getModelicaName() + ".size"); if(i != enumerations.size() - 1) b.append(", "); } @@ -83,9 +88,9 @@ public class StockExpression extends Expression { } else { // incoming valves add and outgoing valves reduce the stock for(Valve valve : outgoing) - b.append("\n - ").append(valve.getName() + range); + b.append("\n - ").append(valve.getModelicaName() + range); for(Valve valve : incoming) - b.append("\n + ").append(valve.getName() + range); + b.append("\n + ").append(valve.getModelicaName() + range); } b.append(";\n"); return b.toString(); @@ -143,6 +148,7 @@ public class StockExpression extends Expression { @Override public String getInitialEquation() { + String initialEquation = FormatUtils.replaceWhitespace(this.initialEquation); // if start value is used, no initial equation is returned if(useStartValue()) return null; @@ -151,7 +157,7 @@ public class StockExpression extends Expression { String range = IndexUtils.rangeToIndexes(parent, this.getArrayRange()); if(range == null) range = ""; - return " " + parent.getName() + range + " = " + equation + ";\n"; + return " " + parent.getModelicaName() + range + " = " + equation + ";\n"; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java index def0b556..27c09439 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java @@ -45,6 +45,7 @@ import org.simantics.sysdyn.representation.utils.UnitUtils; * Representation of a withlookup expression * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ @GraphType(SysdynResource.URIs.WithLookupExpression) @@ -57,13 +58,19 @@ public class WithLookupExpression extends Expression { @Override public String getEquation() { - String equation = FormatUtils.formatExpressionForModelica(parent, this.equation); + String equation = FormatUtils.replaceWhitespace(this.equation); + equation = FormatUtils.formatExpressionForModelica(parent, equation); String range = IndexUtils.rangeToIndexes(parent, this.getArrayRange()); return - " " + parent.getName() + (range.equals("[:]") ? "" : range) + " = interpolate(" + equation + ", " + SheetFormatUtils.reformatSheetReferences(parent, lookupTable) + ");\n"; + " " + parent.getModelicaName() + (range.equals("[:]") ? "" : range) + " = interpolate(" + equation + ", " + SheetFormatUtils.reformatSheetReferences(parent, lookupTable) + ");\n"; } + @Override + public String getModelicaExpression() { + return "interpolate(" + FormatUtils.replaceWhitespace(equation) + ", " + lookupTable + ")"; + } + @Override public String getExpression() { return "interpolate(" + equation + ", " + lookupTable + ")"; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/FormatUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/FormatUtils.java index bb2d136a..19fadc7b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/FormatUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/FormatUtils.java @@ -11,8 +11,11 @@ *******************************************************************************/ package org.simantics.sysdyn.representation.utils; +import java.io.StringReader; import java.util.ArrayList; +import org.simantics.sysdyn.expressionParser.ExpressionParser; +import org.simantics.sysdyn.expressionParser.ParseException; import org.simantics.sysdyn.representation.Enumeration; import org.simantics.sysdyn.representation.IndependentVariable; import org.simantics.sysdyn.representation.Variability; @@ -79,7 +82,7 @@ public class FormatUtils { reference.getArrayIndexes() != null && !reference.getArrayIndexes().isEmpty(); - String refName = reference.getName(); + String refName = reference.getModelicaName(); if(reference.getType().equals("Boolean")) { condition= "if initial() or " + refName + " or not " + refName + " then ("; @@ -110,10 +113,45 @@ public class FormatUtils { if(!first) sb.append(", "); first = false; - sb.append(e.getName() + ".size"); + sb.append(e.getModelicaName() + ".size"); } sb.append(")"); return sb.toString(); } } + + /** + * Replaces each whitespace sequence with a single underscore character ('_'). + * @param app String of which whitespace are replaced + * @return a new String with whitespace replaced + */ + public static String replaceWhitespace(String app) { + String equation = app; + //String equation = FormatUtils.formatExpressionForModelica(variable, app, false); + ExpressionParser parser = new ExpressionParser(new StringReader(equation)); + String modifiedEquation = new String(equation); + + try { + parser.expr(); + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + // Collect all references + ArrayList allReferences = new ArrayList(); + allReferences.addAll(parser.getReferences().keySet()); + allReferences.addAll(parser.getFunctionCallReferences().keySet()); + allReferences.addAll(parser.getEnumerationReferences().keySet()); + + // Replace whitespace sequences with underscore characters + for (String reference : allReferences) { + String regex = reference.replaceAll(" ", "\\\\s+"); + String replacement = reference.replaceAll(" ", "_"); + modifiedEquation = modifiedEquation.replaceAll(regex, replacement); + } + + return modifiedEquation; + } + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/IndexUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/IndexUtils.java index bc9a21ab..cc79fda7 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/IndexUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/IndexUtils.java @@ -18,6 +18,7 @@ import java.util.StringTokenizer; import org.simantics.sysdyn.expressionParser.ExpressionParser; import org.simantics.sysdyn.expressionParser.ExpressionParser.ForRange; import org.simantics.sysdyn.expressionParser.ParseException; +import org.simantics.sysdyn.expressionParser.Token; import org.simantics.sysdyn.representation.Enumeration; import org.simantics.sysdyn.representation.EnumerationIndex; import org.simantics.sysdyn.representation.Variable; @@ -75,11 +76,18 @@ public class IndexUtils { try { parser.expr(); for(ForRange forRange : parser.getForRanges()) { - if(forRange.start.equals(forRange.end)) { - Variable v = RepresentationUtils.getVariable(variable.getParentConfiguration(), forRange.start.image); - if(v instanceof Enumeration) { - equation = equation.replaceAll("in[\\s]*" + forRange.start.image + "($|[^\\.])", "in " + forRange.start.image + ".elements$1"); - } + // Concat the tokens that form the range, this is + // necessary for names that have whitespace. + String rangeString = new String(forRange.start.image); + Token temp = forRange.start; + while (!temp.equals(forRange.end)) { + temp = temp.next; + rangeString += " " + temp.image; + } + Variable v = RepresentationUtils.getVariable(variable.getParentConfiguration(), rangeString); + if(v instanceof Enumeration) { + equation = equation.replaceAll("in[\\s]*" + rangeString + "($|[^\\.])", + "in " + rangeString.replaceAll(" ", "_") + ".elements$1"); } } } catch (ParseException e) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/RepresentationUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/RepresentationUtils.java index 80faa484..09a9a202 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/RepresentationUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/RepresentationUtils.java @@ -163,7 +163,10 @@ public class RepresentationUtils { public static Variable getVariable(Configuration configuration, String name) { Configuration conf = configuration; - String[] components = name.split("\\."); + // This function seems to be used with both whitespaced and + // underscored versions of the variable name. + String whitespacedName = name.replace('_', ' '); + String[] components = whitespacedName.split("\\."); for(String component : components) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jjt b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jjt index 9e8521e1..c6e3c4fa 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jjt +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jjt @@ -44,7 +44,7 @@ TOKEN: | "*" | "/" | ".*" | "./" | "^" | ".^" | "=" | ":=" -| +| | { matchedToken.image = matchedToken.image.substring(1,matchedToken.image.length()-1); } | diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java index 2e5a5c64..b6d0190c 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java @@ -242,7 +242,7 @@ public class DocumentationUtils { Variable v = (Variable) element; StringBuilder sb = new StringBuilder(); - sb.append(v.getName()); + sb.append(v.getModelicaName()); if(v.getArrayIndexes() != null && v.getArrayIndexes().size() > 0) { @@ -252,7 +252,7 @@ public class DocumentationUtils { if(!first) sb.append(", "); first = false; - sb.append(e.getName()); + sb.append(e.getModelicaName()); } sb.append("]"); -- 2.47.1