1 package com.calipso.reportgenerator.reportcalculator.expression; 2 3 import com.calipso.reportgenerator.reportcalculator.*; 4 5 import java.util.*; 6 7 15 16 public class ExpressionSimplifier extends ExpressionVisitor { 17 private Context context; 18 private static Map comparators; 19 20 21 public static Map getComparators(){ 22 if (comparators == null){ 23 comparators = new HashMap(); 24 fillComparators(comparators); 25 } 26 return comparators; 27 } 28 29 private static void fillComparators(Map comparators){ 30 comparators.put(String .class, new StringComparator()); 31 comparators.put(SharedFloat.class, new FloatComparator()); 32 comparators.put(Date.class, new DateComparator()); 34 comparators.put(SharedString.class, new SharedStringComparator()); 35 comparators.put(SharedDate.class, new SharedDateComparator()); 36 comparators.put(SharedInteger.class, new SharedIntegerComparator()); 37 } 38 39 44 public Object processAnd(AndExp expression) { 45 return ((Expression) visit(expression.getLeft())).newAnd((Expression) visit(expression.getRight())); 46 } 47 48 49 54 public Object processConstant(Expression expression) { 55 return expression; 56 } 57 58 63 public Object processEqualTo(EqualTo expression) { 64 Expression simpleLeft; 65 Expression simpleRight; 66 boolean value; 67 Expression result; 68 69 simpleLeft = (Expression) visit(expression.getLeft()); 70 simpleRight = (Expression) visit(expression.getRight()); 71 72 if (simpleLeft.isConstant() && simpleRight.isConstant()) { 73 value = compare(((ConstantExp) simpleLeft).value, ((ConstantExp) simpleRight).value) == 0; 75 result = ConstantExp.forValue(new Boolean (value)); 76 } 77 else { 78 result = simpleLeft.newEqualTo(simpleRight); 79 } 80 return result; 81 } 82 83 private int compare(Object value1, Object value2){ 84 Comparator comparator = getComparator(value1.getClass()); 85 return comparator.compare(value1, value2); 86 } 87 88 private Comparator getComparator(Class aClass) { 89 return (Comparator) getComparators().get(aClass); 90 } 91 92 93 98 public Object processGreaterOrEqualTo(GreaterOrEqualTo expression) { 99 Expression simpleLeft; 100 Expression simpleRight; 101 boolean value; 102 Expression result; 103 104 simpleLeft = (Expression) visit(expression.getLeft()); 105 simpleRight = (Expression) visit(expression.getRight()); 106 107 if (simpleLeft.isConstant() && simpleRight.isConstant()) { 108 int comparisson = compare(((ConstantExp) simpleLeft).value, ((ConstantExp) simpleRight).value); 109 value = comparisson >= 0; 111 result = ConstantExp.forValue(new Boolean (value)); 113 } 114 else { 115 result = simpleLeft.newGreaterOrEqualTo(simpleRight); 116 } 117 return result; 118 } 119 120 125 126 public Object processGreaterThan(GreaterThan expression) { 127 Expression simpleLeft; 128 Expression simpleRight; 129 boolean value; 130 Expression result; 131 132 simpleLeft = (Expression) visit(expression.getLeft()); 133 simpleRight = (Expression) visit(expression.getRight()); 134 135 if (simpleLeft.isConstant() && simpleRight.isConstant()) { 136 int comparisson = compare(((ConstantExp) simpleLeft).value, ((ConstantExp) simpleRight).value); 137 value = comparisson > 0; 139 140 result = ConstantExp.forValue(new Boolean (value)); 142 } 143 else { 144 result = simpleLeft.newGreaterThan(simpleRight); 145 } 146 return result; 147 } 148 149 150 155 public Object processLessOrEqualTo(LessOrEqualTo expression) { 156 Expression simpleLeft; 157 Expression simpleRight; 158 boolean value; 159 Expression result; 160 161 simpleLeft = (Expression) visit(expression.getLeft()); 162 simpleRight = (Expression) visit(expression.getRight()); 163 164 if (simpleLeft.isConstant() && simpleRight.isConstant()) { 165 int comparisson = compare(((ConstantExp) simpleLeft).value, ((ConstantExp) simpleRight).value); 167 value = comparisson <= 0; 168 result = ConstantExp.forValue(new Boolean (value)); 170 } 171 else { 172 result = simpleLeft.newLessOrEquealTo(simpleRight); 173 } 174 return result; 175 } 176 177 182 public Object processLessThan(LessThan expression) { 183 Expression simpleLeft; 184 Expression simpleRight; 185 boolean value; 186 Expression result; 187 188 simpleLeft = (Expression) visit(expression.getLeft()); 189 simpleRight = (Expression) visit(expression.getRight()); 190 191 if (simpleLeft.isConstant() && simpleRight.isConstant()) { 192 int comparisson = compare(((ConstantExp) simpleLeft).value, ((ConstantExp) simpleRight).value); 194 value = comparisson < 0; 195 result = ConstantExp.forValue(new Boolean (value)); 197 } 198 else { 199 result = simpleLeft.newLessThan(simpleRight); 200 } 201 return result; 202 } 203 204 205 210 public Object processNot(NotExp expression) { 211 return ((Expression) visit(expression.internalExp())).newNot(); 212 } 213 214 215 220 public Object processOr(OrExp expression) { 221 return ((Expression) visit(expression.getLeft())).newOr((Expression) visit(expression.getRight())); 222 } 223 224 225 231 public Object processVariable(VariableExp expression) { 232 Object value; 233 234 value = context.valueFor(expression.reference); 235 if (value == null) { 236 return expression; 237 } 238 return ConstantExp.forValue(value); 239 } 240 241 242 247 public Object processIncludes(IncludesExp expression) { 248 Expression simpleLeft; 249 Expression simpleRight; 250 boolean value; 251 Expression result; 252 String string; 253 String regExp; 254 255 simpleLeft = (Expression) visit(expression.getLeft()); 256 simpleRight = (Expression) visit(expression.getRight()); 257 258 if (simpleLeft.isConstant() && simpleRight.isConstant()) { 259 regExp = (String ) ((ConstantExp) simpleRight).value; 260 string = (String ) ((ConstantExp) simpleLeft).value; 261 value = string.indexOf(regExp) != -1; 262 result = ConstantExp.forValue(new Boolean (value)); 263 } 264 else { 265 result = simpleLeft.newIncludes(simpleRight); 266 } 267 return result; 268 } 269 270 271 276 public Object processBeginsWith(BeginsWithExp expression) { 277 Expression simpleLeft; 278 Expression simpleRight; 279 boolean value; 280 Expression result; 281 String string; 282 String prefix; 283 284 simpleLeft = (Expression) visit(expression.getLeft()); 285 simpleRight = (Expression) visit(expression.getRight()); 286 287 if (simpleLeft.isConstant() && simpleRight.isConstant()) { 288 prefix = (String ) ((ConstantExp) simpleRight).value; 289 string = (String ) ((ConstantExp) simpleLeft).value; 290 value = string.startsWith(prefix); 291 result = ConstantExp.forValue(new Boolean (value)); 292 } 293 else { 294 result = simpleLeft.newBeginsWith(simpleRight); 295 } 296 return result; 297 } 298 299 304 public Object processEndsWith(EndsWithExp expression) { 305 Expression simpleLeft; 306 Expression simpleRight; 307 boolean value; 308 Expression result; 309 String string; 310 String suffix; 311 312 simpleLeft = (Expression) visit(expression.getLeft()); 313 simpleRight = (Expression) visit(expression.getRight()); 314 315 if (simpleLeft.isConstant() && simpleRight.isConstant()) { 316 suffix = (String ) ((ConstantExp) simpleRight).value; 317 string = (String ) ((ConstantExp) simpleLeft).value; 318 value = string.endsWith(suffix); 319 result = ConstantExp.forValue(new Boolean (value)); 320 } 321 else { 322 result = simpleLeft.newEndsWith(simpleRight); 323 } 324 return result; 325 } 326 327 333 public Object simplifyIn(Expression expression, Context aContext) { 334 context = aContext; 335 return visit(expression); 336 } 337 338 343 public Object processIn(InExp inExp){ 344 Collection values = inExp.getValues(); 345 if(values == null || values.isEmpty()){ 346 return new TrueExp(); 347 } 348 Expression simple = (Expression) visit(inExp.getArgument()); 349 if(simple.isConstant()){ 350 return ConstantExp.forValue(new Boolean (inExp.getValues().contains(((ConstantExp)simple).getValue()))); 351 } 352 return inExp; 353 } 354 355 } | Popular Tags |