From: miettinen Date: Tue, 1 Oct 2013 06:24:33 +0000 (+0000) Subject: Automatic renaming Sysdyn variables with whitespace in equations. (refs #2930) X-Git-Tag: 1.8.1~240 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=763447b56b32c63cb5706b4a393a0d4edcf721c1;p=simantics%2Fsysdyn.git Automatic renaming Sysdyn variables with whitespace in equations. (refs #2930) Fix to different tab sizes (refs #4448). git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27902 ac1ea38d-2e2b-0410-8846-a27921b304fc --- 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 25ce601e..5612ca0f 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 @@ -107,9 +107,11 @@ public abstract class NameValidator { SysdynResource sr = SysdynResource.getInstance(graph); SpreadsheetResource sheet = SpreadsheetResource.getInstance(graph); try { + if (resource == null) + return false; // Function, FunctionLibrary, and SharedFunctionLibrary are not // allowed to have whitespace. - if (graph.isInstanceOf(resource, sr.Variable) + if (graph.isInstanceOf(resource, sr.Variable) // || graph.isInstanceOf(resource, sr.Module) // || graph.isInstanceOf(resource, sr.ModuleType) || graph.isInstanceOf(resource, sr.Enumeration) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/VariableNameValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/VariableNameValidator.java index 05b8382a..b7f18312 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/VariableNameValidator.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/VariableNameValidator.java @@ -11,6 +11,9 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.utils; +import java.io.StringReader; +import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.StringTokenizer; @@ -23,6 +26,9 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.request.Read; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.expressionParser.ExpressionParser; +import org.simantics.sysdyn.expressionParser.ParseException; +import org.simantics.sysdyn.expressionParser.Token; import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.representation.Configuration; import org.simantics.sysdyn.representation.Model; @@ -80,20 +86,57 @@ public class VariableNameValidator extends NameValidator { } } - private String replaceAllWords(String original, String find, String replacement) { - if(!original.contains(find)) return original; - StringBuilder result = new StringBuilder(original.length()); - String delimiters = "+-*/(){}[],.: \t\n\r\f"; - StringTokenizer st = new StringTokenizer(original, delimiters, true); - while (st.hasMoreTokens()) { - String w = st.nextToken(); - if (w.equals(find)) { - result.append(replacement); - } else { - result.append(w); - } + private static String replaceAllWords(String original, String find, String replacement) { + // Test if the new name (String find) is found in the original + // string in some format. + String pattern = ".*" + find.replace(" ", "\\s+") + ".*"; + if(!original.matches(pattern)) return original; + if (find.equals(replacement)) return original; + + ExpressionParser parser = new ExpressionParser(new StringReader(original)); + try { + parser.expr(); + } catch (ParseException e) { + // Best effort; if there are syntax errors, the replace may fail. } - return result.toString(); + + // Collect all references + HashMap> allReferences = new HashMap>(); + allReferences.putAll(parser.getReferences()); + allReferences.putAll(parser.getFunctionCallReferences()); + allReferences.putAll(parser.getEnumerationReferences()); + + List replacedTokens = allReferences.get(find); + if (replacedTokens == null) + return original; + + // Sort the tokens so that they are in the reversed order based on + // their location in the expression. + Collections.sort(replacedTokens); + Collections.reverse(replacedTokens); + + // Go through the tokens in the reversed order + String result = new String(original); + for (Token token : replacedTokens) { + // Find the place where the last token points to in the original string + // First find where the correct line starts: + int startingPoint = 0; + for (int i = 0; i < token.beginLine - 1; ++i) + startingPoint = result.indexOf('\n', startingPoint) + 1; + // Then where the replaced string starts: + startingPoint += token.beginColumn - 1; + + // Cut the string + String begin = result.substring(0, startingPoint); + String end = result.substring(startingPoint); + + // Replace the string + String regex = find.replaceAll(" ", "\\\\s+"); + end = end.replaceFirst(regex, replacement); + result = begin + end; + } + + return result; } @@ -201,6 +244,9 @@ public class VariableNameValidator extends NameValidator { * @throws DatabaseException */ public boolean isValid(final Resource variable, final String name, final boolean hasRange) { + if (variable == null || name == null) + return false; + boolean result = false; try { result = SimanticsUI.getSession().syncRequest(new Read() { 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 eab29f8c..da252cd6 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj @@ -118,6 +118,7 @@ TOKEN: // { add_op term } -> ( add_op() term() )* void expr() : { + jj_input_stream.setTabSize(1); } { { firstToken = token; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/Token.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/Token.java index ed3a26a5..9cce5690 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/Token.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/Token.java @@ -15,7 +15,7 @@ package org.simantics.sysdyn.expressionParser; * Describes the input token stream. */ -public class Token implements java.io.Serializable { +public class Token implements java.io.Serializable, Comparable { /** * The version identifier for this Serializable class. @@ -136,5 +136,20 @@ public class Token implements java.io.Serializable { return newToken(ofKind, null); } + @Override + public int compareTo(Token o) { + if (this.beginLine < o.beginLine) + return -1; + if (this.beginLine > o.beginLine) + return 1; + else { + if (this.beginColumn < o.beginColumn) + return -1; + if (this.beginColumn > o.beginColumn) + return 1; + return 0; + } + } + } /* JavaCC - OriginalChecksum=ed7a6d865f7fbc2c64e008e34bd824b0 (do not edit this line) */ 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 7bc48aa7..2f9f31eb 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelParser/ModelicaParser.jj +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelParser/ModelicaParser.jj @@ -91,6 +91,7 @@ TOKEN: // { add_op term } -> ( add_op() term() )* void parse() : { + jj_input_stream.setTabSize(1); } { stored_definition() }