]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Merge until trunk@25061 to branches/simantics-1.6 on 2012-05-28. refs #3455
authorlehtonen <lehtonen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 28 May 2012 15:14:16 +0000 (15:14 +0000)
committerlehtonen <lehtonen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 28 May 2012 15:14:16 +0000 (15:14 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/branches@25063 ac1ea38d-2e2b-0410-8846-a27921b304fc

simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/IndexUtils.java

index e2bfc7a0405d47cb6f04cab47f102ec6557c9e79..053d146a5a6627f22966c9cc523970fb9b4c7ebf 100644 (file)
@@ -38,11 +38,11 @@ public class IndexUtils {
                }\r
                return result;\r
        }\r
-       \r
-       \r
+\r
+\r
        public static String rangeToIndexes(Variable variable, String range) {\r
-           if(variable == null)\r
-               return range;\r
+               if(variable == null)\r
+                       return range;\r
                StringBuilder sb = new StringBuilder();\r
                ArrayIndexes arrayIndexes = variable.getArrayIndexes();\r
                if(arrayIndexes == null || range == null)\r
@@ -66,61 +66,66 @@ public class IndexUtils {
                                else\r
                                        sb.append(rangeToken);\r
                        }\r
-                       \r
+\r
                }\r
                return sb.toString();\r
        }\r
-       \r
+\r
        private static String fixForRangeEnumerations(Variable variable, String equation) {\r
-       ExpressionParser parser = new ExpressionParser(new StringReader(equation));\r
-       try {\r
-               parser.expr();\r
-               for(ForRange forRange : parser.getForRanges()) {\r
-                       if(forRange.start.equals(forRange.end)) {\r
-                               Variable v = getVariable(variable.getParentConfiguration(), forRange.start.image);\r
-                               if(v instanceof Enumeration) {\r
-                                       equation = equation.replaceAll("in[\\s]*" + forRange.start.image + "($|[^\\.])", "in " + forRange.start.image + ".elements$1");\r
-                               }\r
-                       }\r
-               }\r
-       } catch (ParseException e) {\r
+               ExpressionParser parser = new ExpressionParser(new StringReader(equation));\r
+               try {\r
+                       parser.expr();\r
+                       for(ForRange forRange : parser.getForRanges()) {\r
+                               if(forRange.start.equals(forRange.end)) {\r
+                                       Variable v = getVariable(variable.getParentConfiguration(), forRange.start.image);\r
+                                       if(v instanceof Enumeration) {\r
+                                               equation = equation.replaceAll("in[\\s]*" + forRange.start.image + "($|[^\\.])", "in " + forRange.start.image + ".elements$1");\r
+                                       }\r
+                               }\r
+                       }\r
+               } catch (ParseException e) {\r
                        e.printStackTrace();\r
                }\r
                return equation;\r
        }\r
-       \r
+\r
        public static String equationRangesToIndexes(Variable variable, String equation) {\r
-               if(equation == null || !equation.contains("[")) return equation;\r
-               \r
-           StringBuilder result = new StringBuilder();\r
-           String delimiters = "+-*/(){}[],. ";\r
-           StringTokenizer st = new StringTokenizer(equation, delimiters, true);\r
-           String prevToken = st.nextToken();\r
-           result.append(prevToken);\r
-           while (st.hasMoreTokens()) {\r
-               String nextToken = st.nextToken();\r
-               if (nextToken.equals("[")) {\r
-                       StringBuilder range = new StringBuilder();\r
-                       range.append("[");\r
-                       String rangeToken = st.nextToken();\r
-                       while(!rangeToken.equals("]")) {\r
-                               range.append(rangeToken);\r
-                               rangeToken = st.nextToken();\r
-                       }\r
-                       range.append("]");\r
-                       \r
-                       Variable prevVar = getVariable(variable.getParentConfiguration(), prevToken.trim());\r
-                   result.append(rangeToIndexes(prevVar, range.toString()));\r
-               } else {\r
-                       result.append(nextToken);\r
-               }\r
-               prevToken = nextToken;\r
-           }\r
-           \r
-           equation = fixForRangeEnumerations(variable, result.toString());\r
-           return equation;\r
+               if(equation == null) return equation;\r
+\r
+               if(equation.contains("[")) {\r
+                       StringBuilder result = new StringBuilder();\r
+                       String delimiters = "+-*/(){}[],. ";\r
+                       StringTokenizer st = new StringTokenizer(equation, delimiters, true);\r
+                       String prevToken = st.nextToken();\r
+                       result.append(prevToken);\r
+                       while (st.hasMoreTokens()) {\r
+                               String nextToken = st.nextToken();\r
+                               if (nextToken.equals("[")) {\r
+                                       StringBuilder range = new StringBuilder();\r
+                                       range.append("[");\r
+                                       String rangeToken = st.nextToken();\r
+                                       while(!rangeToken.equals("]")) {\r
+                                               range.append(rangeToken);\r
+                                               rangeToken = st.nextToken();\r
+                                       }\r
+                                       range.append("]");\r
+\r
+                                       Variable prevVar = getVariable(variable.getParentConfiguration(), prevToken.trim());\r
+                                       result.append(rangeToIndexes(prevVar, range.toString()));\r
+                               } else {\r
+                                       result.append(nextToken);\r
+                               }\r
+                               prevToken = nextToken;\r
+                       }\r
+                       equation = fixForRangeEnumerations(variable, result.toString());\r
+               } else if(equation.contains("{")){\r
+                       // Cases where there are {something for i in Enum} without [] brackets\r
+                       equation = fixForRangeEnumerations(variable, equation);\r
+               }\r
+\r
+               return equation;\r
        }\r
-       \r
+\r
        private static Variable getVariable(Configuration configuration, String name) {\r
                for(IElement element : configuration.getElements()) {\r
                        if(element instanceof Variable) {\r