+/*******************************************************************************\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
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
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