From 1738b8ca2ee25235aa70f25de663ba213c6b88fa Mon Sep 17 00:00:00 2001 From: lehtonen Date: Mon, 28 May 2012 15:14:16 +0000 Subject: [PATCH] Merge until trunk@25061 to branches/simantics-1.6 on 2012-05-28. refs #3455 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/branches@25063 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../representation/utils/IndexUtils.java | 103 +++++++++--------- 1 file changed, 54 insertions(+), 49 deletions(-) diff --git a/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/IndexUtils.java b/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/IndexUtils.java index e2bfc7a0..053d146a 100644 --- a/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/IndexUtils.java +++ b/simantics-1.6/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/IndexUtils.java @@ -38,11 +38,11 @@ public class IndexUtils { } return result; } - - + + public static String rangeToIndexes(Variable variable, String range) { - if(variable == null) - return range; + if(variable == null) + return range; StringBuilder sb = new StringBuilder(); ArrayIndexes arrayIndexes = variable.getArrayIndexes(); if(arrayIndexes == null || range == null) @@ -66,61 +66,66 @@ public class IndexUtils { else sb.append(rangeToken); } - + } return sb.toString(); } - + private static String fixForRangeEnumerations(Variable variable, String equation) { - ExpressionParser parser = new ExpressionParser(new StringReader(equation)); - try { - parser.expr(); - for(ForRange forRange : parser.getForRanges()) { - if(forRange.start.equals(forRange.end)) { - Variable v = getVariable(variable.getParentConfiguration(), forRange.start.image); - if(v instanceof Enumeration) { - equation = equation.replaceAll("in[\\s]*" + forRange.start.image + "($|[^\\.])", "in " + forRange.start.image + ".elements$1"); - } - } - } - } catch (ParseException e) { + ExpressionParser parser = new ExpressionParser(new StringReader(equation)); + try { + parser.expr(); + for(ForRange forRange : parser.getForRanges()) { + if(forRange.start.equals(forRange.end)) { + Variable v = getVariable(variable.getParentConfiguration(), forRange.start.image); + if(v instanceof Enumeration) { + equation = equation.replaceAll("in[\\s]*" + forRange.start.image + "($|[^\\.])", "in " + forRange.start.image + ".elements$1"); + } + } + } + } catch (ParseException e) { e.printStackTrace(); } return equation; } - + public static String equationRangesToIndexes(Variable variable, String equation) { - if(equation == null || !equation.contains("[")) return equation; - - StringBuilder result = new StringBuilder(); - String delimiters = "+-*/(){}[],. "; - StringTokenizer st = new StringTokenizer(equation, delimiters, true); - String prevToken = st.nextToken(); - result.append(prevToken); - while (st.hasMoreTokens()) { - String nextToken = st.nextToken(); - if (nextToken.equals("[")) { - StringBuilder range = new StringBuilder(); - range.append("["); - String rangeToken = st.nextToken(); - while(!rangeToken.equals("]")) { - range.append(rangeToken); - rangeToken = st.nextToken(); - } - range.append("]"); - - Variable prevVar = getVariable(variable.getParentConfiguration(), prevToken.trim()); - result.append(rangeToIndexes(prevVar, range.toString())); - } else { - result.append(nextToken); - } - prevToken = nextToken; - } - - equation = fixForRangeEnumerations(variable, result.toString()); - return equation; + if(equation == null) return equation; + + if(equation.contains("[")) { + StringBuilder result = new StringBuilder(); + String delimiters = "+-*/(){}[],. "; + StringTokenizer st = new StringTokenizer(equation, delimiters, true); + String prevToken = st.nextToken(); + result.append(prevToken); + while (st.hasMoreTokens()) { + String nextToken = st.nextToken(); + if (nextToken.equals("[")) { + StringBuilder range = new StringBuilder(); + range.append("["); + String rangeToken = st.nextToken(); + while(!rangeToken.equals("]")) { + range.append(rangeToken); + rangeToken = st.nextToken(); + } + range.append("]"); + + Variable prevVar = getVariable(variable.getParentConfiguration(), prevToken.trim()); + result.append(rangeToIndexes(prevVar, range.toString())); + } else { + result.append(nextToken); + } + prevToken = nextToken; + } + equation = fixForRangeEnumerations(variable, result.toString()); + } else if(equation.contains("{")){ + // Cases where there are {something for i in Enum} without [] brackets + equation = fixForRangeEnumerations(variable, equation); + } + + return equation; } - + private static Variable getVariable(Configuration configuration, String name) { for(IElement element : configuration.getElements()) { if(element instanceof Variable) { -- 2.47.1