]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Automatic renaming Sysdyn variables with whitespace in equations. (refs #2930)
authormiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 1 Oct 2013 06:24:33 +0000 (06:24 +0000)
committermiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 1 Oct 2013 06:24:33 +0000 (06:24 +0000)
Fix to different tab sizes (refs #4448).

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

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/NameValidator.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/VariableNameValidator.java
org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj
org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/Token.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelParser/ModelicaParser.jj

index 25ce601e50d4a975c778142a2c4ce028a32abe4a..5612ca0f1e73b3d60e4e584acd96e435952dc197 100644 (file)
@@ -107,9 +107,11 @@ public abstract class NameValidator {
            SysdynResource sr = SysdynResource.getInstance(graph);\r
            SpreadsheetResource sheet = SpreadsheetResource.getInstance(graph);\r
        try {\r
+               if (resource == null)\r
+                       return false;\r
                // Function, FunctionLibrary, and SharedFunctionLibrary are not\r
                // allowed to have whitespace.\r
-                       if (graph.isInstanceOf(resource, sr.Variable)\r
+               if (graph.isInstanceOf(resource, sr.Variable)\r
 //                                     || graph.isInstanceOf(resource, sr.Module)\r
 //                                     || graph.isInstanceOf(resource, sr.ModuleType)\r
                                        || graph.isInstanceOf(resource, sr.Enumeration)\r
index 05b8382a31eb709d331b55b553b200a90fc899c5..b7f18312fe3454ef985359c76749eb35992ce594 100644 (file)
@@ -11,6 +11,9 @@
  *******************************************************************************/\r
 package org.simantics.sysdyn.ui.utils;\r
 \r
+import java.io.StringReader;\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
 import java.util.List;\r
 import java.util.StringTokenizer;\r
 \r
@@ -23,6 +26,9 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.request.Read;\r
 import org.simantics.layer0.Layer0;\r
 import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.expressionParser.ExpressionParser;\r
+import org.simantics.sysdyn.expressionParser.ParseException;\r
+import org.simantics.sysdyn.expressionParser.Token;\r
 import org.simantics.sysdyn.manager.SysdynModel;\r
 import org.simantics.sysdyn.representation.Configuration;\r
 import org.simantics.sysdyn.representation.Model;\r
@@ -80,20 +86,57 @@ public class VariableNameValidator extends NameValidator {
            }\r
        }\r
 \r
-       private String replaceAllWords(String original, String find, String replacement) {\r
-               if(!original.contains(find)) return original;\r
-               StringBuilder result = new StringBuilder(original.length());\r
-               String delimiters = "+-*/(){}[],.: \t\n\r\f";\r
-               StringTokenizer st = new StringTokenizer(original, delimiters, true);\r
-               while (st.hasMoreTokens()) {\r
-                       String w = st.nextToken();\r
-                       if (w.equals(find)) {\r
-                               result.append(replacement);\r
-                       } else {\r
-                               result.append(w);\r
-                       }\r
+       private static String replaceAllWords(String original, String find, String replacement) {\r
+               // Test if the new name (String find) is found in the original \r
+               // string in some format.\r
+               String pattern = ".*" + find.replace(" ", "\\s+") + ".*";\r
+               if(!original.matches(pattern)) return original;\r
+               if (find.equals(replacement)) return original;\r
+\r
+        ExpressionParser parser = new ExpressionParser(new StringReader(original));\r
+        try {\r
+                       parser.expr();\r
+               } catch (ParseException e) {\r
+                       // Best effort; if there are syntax errors, the replace may fail.\r
                }\r
-               return result.toString();\r
+        \r
+        // Collect all references\r
+        HashMap<String, List<Token>> allReferences = new HashMap<String, List<Token>>();\r
+        allReferences.putAll(parser.getReferences());\r
+        allReferences.putAll(parser.getFunctionCallReferences());\r
+        allReferences.putAll(parser.getEnumerationReferences());\r
+               \r
+        List<Token> replacedTokens = allReferences.get(find);\r
+        if (replacedTokens == null)\r
+               return original;\r
+        \r
+        // Sort the tokens so that they are in the reversed order based on\r
+        // their location in the expression.\r
+        Collections.sort(replacedTokens);\r
+        Collections.reverse(replacedTokens);\r
+        \r
+        // Go through the tokens in the reversed order\r
+        String result = new String(original);\r
+               for (Token token : replacedTokens) {\r
+               // Find the place where the last token points to in the original string\r
+               // First find where the correct line starts:\r
+               int startingPoint = 0;\r
+               for (int i = 0; i < token.beginLine - 1; ++i)\r
+                       startingPoint = result.indexOf('\n', startingPoint) + 1;\r
+               // Then where the replaced string starts: \r
+               startingPoint += token.beginColumn - 1;\r
+               \r
+               // Cut the string\r
+               String begin = result.substring(0, startingPoint);\r
+               String end = result.substring(startingPoint);\r
+               \r
+               // Replace the string\r
+               String regex = find.replaceAll(" ", "\\\\s+");\r
+               end = end.replaceFirst(regex, replacement);\r
+               result = begin + end;\r
+        }\r
+               \r
+               return result;\r
        }\r
 \r
 \r
@@ -201,6 +244,9 @@ public class VariableNameValidator extends NameValidator {
         * @throws DatabaseException\r
         */\r
        public boolean isValid(final Resource variable, final String name, final boolean hasRange) {\r
+               if (variable == null || name == null)\r
+                       return false;\r
+               \r
                boolean result = false;\r
                try {\r
                        result = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
index eab29f8c16ec297144068c1d4abc1fd0a143b0ce..da252cd6bbcf2785550fa1d00085e96b0c75edf2 100644 (file)
@@ -118,6 +118,7 @@ TOKEN:
 // { add_op term } -> ( add_op() term() )*\r
 \r
 void expr() : {\r
+       jj_input_stream.setTabSize(1);\r
 } {\r
        {\r
                firstToken = token;\r
index ed3a26a547caeb64543e828b6a48df9a0b0a982e..9cce5690902fbcd6c6440dba07845037414c5467 100644 (file)
@@ -15,7 +15,7 @@ package org.simantics.sysdyn.expressionParser;
  * Describes the input token stream.\r
  */\r
 \r
-public class Token implements java.io.Serializable {\r
+public class Token implements java.io.Serializable, Comparable<Token> {\r
 \r
   /**\r
    * The version identifier for this Serializable class.\r
@@ -136,5 +136,20 @@ public class Token implements java.io.Serializable {
     return newToken(ofKind, null);\r
   }\r
 \r
+       @Override\r
+       public int compareTo(Token o) {\r
+               if (this.beginLine < o.beginLine)\r
+                       return -1;\r
+               if (this.beginLine > o.beginLine)\r
+                       return 1;\r
+               else {\r
+                       if (this.beginColumn < o.beginColumn)\r
+                               return -1;\r
+                       if (this.beginColumn > o.beginColumn)\r
+                               return 1;\r
+                       return 0;\r
+               }\r
+       }\r
+\r
 }\r
 /* JavaCC - OriginalChecksum=ed7a6d865f7fbc2c64e008e34bd824b0 (do not edit this line) */\r
index 7bc48aa703ea9e174a133fc71e1e40a9b04a0779..2f9f31eba9ef76ceb225a4ab174046daaf8051fa 100644 (file)
@@ -91,6 +91,7 @@ TOKEN:
 // { add_op term } -> ( add_op() term() )*\r
 \r
 void parse() : {\r
+       jj_input_stream.setTabSize(1);\r
 } {\r
     stored_definition() <EOF>\r
 }\r