]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Allow array definitions of type {a,b,...,n} inside Sysdyn variable name combo (refs...
authormiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 10 Apr 2014 09:29:25 +0000 (09:29 +0000)
committermiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 10 Apr 2014 09:29:25 +0000 (09:29 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29275 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ArrayVariableUtils.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/IndexUtils.java

index d6f024e1256152ab47ac954a3765ae0582232a49..73663c77e1022698c19d646c8b45aa5400662e2b 100644 (file)
@@ -1,3 +1,14 @@
+/*******************************************************************************\r
+ * Copyright (c) 2010, 2014 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
 package org.simantics.sysdyn.ui.utils;\r
 \r
 import java.util.ArrayList;\r
@@ -59,74 +70,91 @@ public class ArrayVariableUtils {
             result.put(elements.length > 0 ? elements.length - 1 : 0, error);\r
         }\r
 \r
-\r
         for(int i = 0; i < elements.length && i < enumerations.size(); i++) {\r
-            if(elements[i].trim().equals(":"))\r
-                continue;\r
-            if(elements[i].indexOf(":") != elements[i].lastIndexOf(":")) {\r
+               String trimmedElement = elements[i].trim();\r
+            if (trimmedElement.charAt(0) == '{' && trimmedElement.charAt(trimmedElement.length() - 1) == '}') {\r
+               // Something like "{a, b, c}"\r
+               String[] rangeComponents = trimmedElement.substring(1, trimmedElement.length() - 1).split(",");\r
+               // Single range component, equals to the enumeration at that index\r
+                if(rangeComponents.length == 1 && rangeComponents[0].trim().equals(enumerations.get(i).getName()))\r
+                       continue;\r
+                   // more than one range component, they all equal to individual indexes in the enumeration\r
+                result.putAll(areRangeComponentsValid(rangeComponents, enumerations, i));\r
+            } else if(trimmedElement.equals(":")) {\r
+                continue; // Just a ":"\r
+               } else if(elements[i].indexOf(":") != elements[i].lastIndexOf(":")) {\r
+                       // Something like "a : b : c"\r
                 error = new SyntaxError();\r
                 error.setMessage( "Too many ':' elements");\r
                 error.setType(ExpressionField.SYNTAX_ERROR);\r
                 result.put(i, error);\r
                 continue;\r
+            } else {\r
+               // Something like "a : c" OR "a"\r
+                   String[] rangeComponents = elements[i].split(":");\r
+                   if(rangeComponents.length > 2){\r
+                       error = new SyntaxError();\r
+                       error.setMessage( "Too many ':' elements");\r
+                       error.setType(ExpressionField.SYNTAX_ERROR);\r
+                       result.put(i, error);\r
+                       continue;\r
+                   }\r
+                   // Single range component, equals to the enumeration at that index\r
+                   if(rangeComponents.length == 1 && rangeComponents[0].trim().equals(enumerations.get(i).getName()))\r
+                       continue;\r
+                   // one or two range components, they all equal to individual indexes in the enumeration\r
+                   result.putAll(areRangeComponentsValid(rangeComponents, enumerations, i));\r
             }\r
+        }\r
+        if(result.isEmpty())\r
+            return null;\r
+        else\r
+            return result;\r
+    }\r
 \r
-            String[] rangeComponents = elements[i].split(":");\r
-            if(rangeComponents.length > 2){\r
-                error = new SyntaxError();\r
-                error.setMessage( "Too many ':' elements");\r
-                error.setType(ExpressionField.SYNTAX_ERROR);\r
-                result.put(i, error);\r
-                continue;\r
+    private static Map<Integer, SyntaxError> areRangeComponentsValid(String[] rangeComponents, ArrayList<Enumeration> enumerations, int i) {\r
+       SyntaxError error;\r
+       Map<Integer, SyntaxError> result = new HashMap<Integer, SyntaxError>();\r
+        for(String r : rangeComponents) {\r
+            r = r.trim();\r
+            boolean componentIsValid = false;\r
+            Enumeration enumeration = enumerations.get(i);\r
+            for(EnumerationIndex ei : enumeration.getEnumerationIndexes()) {\r
+                if(ei.getName().equals(r)) {\r
+                    componentIsValid = true;\r
+                    break;\r
+                }\r
             }\r
-            // Single range component, equals to the enumeration at that index\r
-            if(rangeComponents.length == 1 && rangeComponents[0].trim().equals(enumerations.get(i).getName()))\r
-                continue;\r
-            // one or two range components, they all equal to individual indexes in the enumeration\r
-            for(String r : rangeComponents) {\r
-                r = r.trim();\r
-                boolean componentIsValid = false;\r
-                Enumeration enumeration = enumerations.get(i);\r
-                for(EnumerationIndex ei : enumeration.getEnumerationIndexes()) {\r
-                    if(ei.getName().equals(r)) {\r
+            if(!componentIsValid  && r.length() > 0) {\r
+                // Check if the range is an integer that is between 0 and enumeration indexes size\r
+                try {\r
+                    int index = Integer.parseInt(r);\r
+                    int min = 1;\r
+                    int max = enumeration.getEnumerationIndexes().size();\r
+                    if(index >= min && index <= max || enumeration.isReplaceable()) {\r
                         componentIsValid = true;\r
-                        break;\r
-                    }\r
-                }\r
-                if(!componentIsValid  && r.length() > 0) {\r
-                    // Check if the range is an integer that is between 0 and enumeration indexes size\r
-                    try {\r
-                        int index = Integer.parseInt(r);\r
-                        int min = 1;\r
-                        int max = enumeration.getEnumerationIndexes().size();\r
-                        if(index >= min && index <= max || enumeration.isReplaceable()) {\r
-                            componentIsValid = true;\r
-                            error = new SyntaxError();\r
-                            error.setMessage("Using numbers as array indexes is not encouraged");\r
-                            error.setType(ExpressionField.SYNTAX_WARNING);\r
-                            result.put(i, error);\r
-                        } else {\r
-                            error = new SyntaxError();\r
-                            error.setMessage("Invalid array index " + index + ". Numbered index must be between " + min + " and " + max);\r
-                            error.setType(ExpressionField.SYNTAX_ERROR);\r
-                            result.put(i, error);\r
-                        }\r
-                    } catch (NumberFormatException e) {\r
                         error = new SyntaxError();\r
-                        error.setMessage("Invalid range");\r
+                        error.setMessage("Using numbers as array indexes is not encouraged");\r
+                        error.setType(ExpressionField.SYNTAX_WARNING);\r
+                        result.put(i, error);\r
+                    } else {\r
+                        error = new SyntaxError();\r
+                        error.setMessage("Invalid array index " + index + ". Numbered index must be between " + min + " and " + max);\r
                         error.setType(ExpressionField.SYNTAX_ERROR);\r
                         result.put(i, error);\r
                     }\r
+                } catch (NumberFormatException e) {\r
+                    error = new SyntaxError();\r
+                    error.setMessage("Invalid range");\r
+                    error.setType(ExpressionField.SYNTAX_ERROR);\r
+                    result.put(i, error);\r
                 }\r
             }\r
         }\r
-        if(result.isEmpty())\r
-            return null;\r
-        else\r
-            return result;\r
-    }\r
+               return result;\r
+       }\r
 \r
-    /**\r
+       /**\r
      * Checks if the given range can be applied to the given variable.\r
      * \r
      * @param graph ReadGraph\r
@@ -139,13 +167,30 @@ public class ArrayVariableUtils {
         if(variable == null)\r
             return true;\r
         String[] elements = range.split(",");\r
+        // Connect arrays, e.g. {a,b,c}\r
+        ArrayList<String> connectedElements = new ArrayList<String>();\r
+        for (int i = 0; i < elements.length; ++i) {\r
+               String lastElement = elements[i];\r
+               String fullElement = new String();\r
+               if (lastElement.length() > 0 && lastElement.charAt(0) == '{') {\r
+                       while (lastElement.charAt(lastElement.length() - 1) != '}' && i < elements.length - 1) {\r
+                               fullElement += lastElement + ",";\r
+                               ++i;\r
+                               lastElement = elements[i];\r
+                       }\r
+               }\r
+               fullElement += lastElement;\r
+               connectedElements.add(fullElement);\r
+        }\r
+        String fullElements[] = connectedElements.toArray(new String[connectedElements.size()]);\r
+        \r
         SysdynModel model = ModelUtils.getModel(graph, variable);\r
         if(model == null)\r
             return false;\r
         IElement e = model.getElement(variable);\r
         if(e != null && e instanceof Variable) {\r
             Variable v = (Variable) e;\r
-            if(isRangeValid(graph, v, elements) == null)\r
+            if(isRangeValid(graph, v, fullElements) == null)\r
                 return true;\r
             else\r
                 return false;\r
index cc79fda73c01d03c9b05b8236055a7c6a185f9fe..360ffe1b8da8b86381c3fdd52337e14c694a64dc 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************\r
- * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Copyright (c) 2007, 2011, 2014 Association for Decentralized Information Management in\r
  * Industry THTH ry.\r
  * All rights reserved. This program and the accompanying materials\r
  * are made available under the terms of the Eclipse Public License v1.0\r
@@ -45,15 +45,24 @@ public class IndexUtils {
                if(variable.getArrayIndexes() == null || range == null)\r
                        return "";\r
                ArrayList<Enumeration> enumerations = variable.getArrayIndexes();\r
-               StringTokenizer st = new StringTokenizer(range, "[]:,", true);\r
+               StringTokenizer st = new StringTokenizer(range, "{}[]:,", true);\r
                int index = 0;\r
+               boolean insideCurlyBrackets = false;\r
                while(st.hasMoreTokens()) {\r
                        String rangeToken = st.nextToken().trim();\r
                        if(rangeToken.matches("[\\[\\]:]")) {\r
                                sb.append(rangeToken);\r
+                       } else if(rangeToken.matches("[\\{]")) {\r
+                               sb.append(rangeToken);\r
+                               insideCurlyBrackets = true;\r
+                       } else if(rangeToken.matches("[\\}]")) {\r
+                               sb.append(rangeToken);\r
+                               insideCurlyBrackets = false;\r
                        } else if (rangeToken.equals(",")) {\r
                            sb.append(rangeToken);\r
-                           index++;\r
+                           if (!insideCurlyBrackets) {\r
+                               index++;\r
+                           }\r
                        } else if(index < enumerations.size()) {\r
                            if(rangeToken.equals(enumerations.get(index).getName())) {\r
                                sb.append(":");\r