1 package org.jacorb.idl; 2 3 22 23 24 import java.util.HashMap ; 25 import java.lang.Double ; 26 27 43 44 public class ConstExprEvaluator 45 { 46 protected static Operator[] operators = null; 47 private Node node = null; 48 private String expression = null; 49 private HashMap variables = new HashMap (); 50 51 55 56 public ConstExprEvaluator() 57 { 58 init(); 59 } 60 61 64 65 public ConstExprEvaluator(String s) 66 { 67 init(); 68 setExpression(s); 69 } 70 71 private void init() 72 { 73 if ( operators == null ) 74 initializeOperators(); 75 } 76 77 80 81 public void setExpression(String s) 82 { 83 expression = s; 84 } 85 86 89 90 public void reset() 91 { 92 node = null; 93 expression = null; 94 variables = new HashMap (); 95 } 96 97 98 101 102 public Double getValue() 103 { 104 if (expression == null) 105 return null; 106 107 try 108 { 109 node = new Node(expression); 110 return evaluate(node); 111 } 112 catch (Exception e) 113 { 114 e.printStackTrace(); 115 return null; 116 } 117 } 118 119 private static Double evaluate(Node n) 120 { 121 if ( n.hasOperator() && n.hasChild() ) 122 { 123 if ( n.getOperator().getType() == 1 ) 124 n.setValue ( evaluateExpression( n.getOperator(), evaluate( n.getLeft() ), null ) ); 125 else if ( n.getOperator().getType() == 2 ) 126 n.setValue( evaluateExpression( n.getOperator(), evaluate( n.getLeft() ), evaluate( n.getRight() ) ) ); 127 } 128 return n.getValue(); 129 } 130 131 132 private static Double evaluateExpression( Operator o, Double f1, Double f2) 133 { 134 String op = o.getOperator(); 135 Double res = null; 136 137 if ( "+".equals(op) ) 138 res = new Double ( f1.doubleValue() + f2.doubleValue() ); 139 else if ( "-".equals(op) ) 140 res = new Double ( f1.doubleValue() - f2.doubleValue() ); 141 else if ( "*".equals(op) ) 142 res = new Double ( f1.doubleValue() * f2.doubleValue() ); 143 else if ( "/".equals(op) ) 144 res = new Double ( f1.doubleValue() / f2.doubleValue() ); 145 else if ( "%".equals(op) ) 146 res = new Double ( f1.doubleValue() % f2.doubleValue() ); 147 else if ( "|".equals(op) ) 148 res = new Double (Double.longBitsToDouble( Double.doubleToLongBits( f1.doubleValue()) | Double.doubleToLongBits( f2.doubleValue() ) )); 149 else if ( "&".equals(op) ) 150 res = new Double ( Double.longBitsToDouble( Double.doubleToLongBits( f1.doubleValue() ) & Double.doubleToLongBits( f2.doubleValue() ) )); 151 else if ( "^".equals(op) ) 152 res = new Double ( Double.longBitsToDouble( Double.doubleToLongBits( f1.doubleValue() ) ^ Double.doubleToLongBits( f2.doubleValue() ) )); 153 else if ( "<<".equals(op) ) 154 res = new Double ( Double.longBitsToDouble( Double.doubleToLongBits( f1.doubleValue() ) << Double.doubleToLongBits( f2.doubleValue() ) )); 155 else if ( ">>".equals(op) ) 156 res = new Double ( Double.longBitsToDouble( Double.doubleToLongBits( f1.doubleValue() ) >> Double.doubleToLongBits( f2.doubleValue() ) )); 157 158 return res; 159 } 160 161 private void initializeOperators() 162 { 163 operators = new Operator[10]; 164 165 operators[0] = new Operator("|" , 2, 0); 167 168 operators[1] = new Operator("^" , 2, 2); 169 170 operators[2] = new Operator("&" , 2, 4); 171 172 operators[3] = new Operator(">>" , 2, 6); 173 operators[4] = new Operator("<<" , 2, 6); 174 operators[5] = new Operator("+" , 2, 8); 176 operators[6] = new Operator("-" , 2, 8); 177 178 operators[7] = new Operator("*" , 2, 10); 179 operators[8] = new Operator("/" , 2, 10); 180 operators[9] = new Operator("%" , 2, 10); 181 182 183 } 184 185 188 189 public Double getVariable(String s) 190 { 191 return( Double )variables.get(s); 192 } 193 194 private Double getDouble(String s) 195 { 196 if ( s == null ) 197 return null; 198 199 Double res = null; 200 try 201 { 202 res = new Double (Double.parseDouble(s)); 203 } 204 catch(Exception e) 205 { 206 return getVariable(s); 207 } 208 209 return res; 210 } 211 212 protected Operator[] getOperators() 213 { 214 return operators; 215 } 216 217 protected class Operator 218 { 219 private String op; 220 private int type; 221 private int priority; 222 223 public Operator(String o, int t, int p) 224 { 225 op = o; 226 type = t; 227 priority = p; 228 } 229 230 public String getOperator() 231 { 232 return op; 233 } 234 235 public void setOperator(String o) 236 { 237 op = o; 238 } 239 240 public int getType() 241 { 242 return type; 243 } 244 245 public int getPriority() 246 { 247 return priority; 248 } 249 } 250 251 protected class Node 252 { 253 public String nString = null; 254 public Operator nOperator = null; 255 public Node nLeft = null; 256 public Node nRight = null; 257 public Node nParent = null; 258 public int nLevel = 0; 259 public Double nValue = null; 260 261 public Node(String s) throws Exception 262 { 263 init(null, s, 0); 264 } 265 266 public Node(Node parent, String s, int level) throws Exception 267 { 268 init(parent, s, level); 269 } 270 271 private void init(Node parent, String s, int level) throws Exception 272 { 273 s = removeIllegalCharacters(s); 274 s = removeBrackets(s); 275 s = addZero(s); 276 if ( checkBrackets(s) != 0 ) 277 throw new Exception ("Wrong number of brackets in [" + s + "]"); 278 279 nParent = parent; 280 nString = s; 281 nValue = getDouble(s); 282 nLevel = level; 283 int sLength = s.length(); 284 int inBrackets = 0; 285 int startOperator = 0; 286 287 for (int i=0; i<sLength; i++) 288 { 289 if ( s.charAt(i) == '(' ) 290 inBrackets++; 291 else if ( s.charAt(i) == ')' ) 292 inBrackets--; 293 else 294 { 295 if ( inBrackets == 0 ) 297 { 298 Operator o = getOperator(nString,i); 299 if ( o != null ) 300 { 301 if ( nOperator == null || nOperator.getPriority() >= o.getPriority() ) 303 { 304 nOperator = o; 305 startOperator = i; 306 } 307 } 308 } 309 } 310 } 311 312 if ( nOperator != null ) 313 { 314 if ( startOperator==0 && nOperator.getType() == 1 ) 316 { 317 if ( checkBrackets( s.substring( nOperator.getOperator().length() ) ) == 0 ) 319 { 320 nLeft = 321 new Node( this, s.substring( nOperator.getOperator().length() ) , nLevel + 1); 322 nRight = null; 323 return; 324 } 325 else 326 throw new Exception ("Error during parsing... missing brackets in [" + s + "]"); 327 } 328 else if ( startOperator > 0 && nOperator.getType() == 2 ) 330 { 331 nOperator = nOperator; 332 nLeft = 333 new Node( this, s.substring(0, startOperator), nLevel + 1 ); 334 nRight = 335 new Node( this, s.substring(startOperator + nOperator.getOperator().length()), nLevel + 1); 336 } 337 } 338 } 339 340 private Operator getOperator(String s, int start) 341 { 342 Operator[] operators = getOperators(); 343 String temp = s.substring(start); 344 temp = getNextWord(temp); 345 for (int i=0; i<operators.length; i++) 346 { 347 if ( temp.startsWith(operators[i].getOperator()) ) 348 return operators[i]; 349 } 350 return null; 351 } 352 353 private String getNextWord(String s) 354 { 355 int sLength = s.length(); 356 for (int i=1; i<sLength; i++) 357 { 358 char c = s.charAt(i); 359 if ( (c > 'z' || c < 'a') && (c > '9' || c < '0') ) 360 return s.substring(0, i); 361 } 362 return s; 363 } 364 365 369 protected int checkBrackets(String s) 370 { 371 int sLength = s.length(); 372 int inBracket = 0; 373 374 for (int i=0; i<sLength; i++) 375 { 376 if ( s.charAt(i) == '(' && inBracket >= 0 ) 377 inBracket++; 378 else if ( s.charAt(i) == ')' ) 379 inBracket--; 380 } 381 382 return inBracket; 383 } 384 385 388 protected String addZero(String s) 389 { 390 if ( s.startsWith("+") || s.startsWith("-") ) 391 { 392 int sLength = s.length(); 393 for (int i=0; i<sLength; i++) 394 { 395 if ( getOperator(s, i) != null ) 396 return "0" + s; 397 } 398 } 399 400 return s; 401 } 402 403 404 protected boolean hasChild() 405 { 406 return ( nLeft != null || nRight != null ); 407 } 408 409 protected boolean hasOperator() 410 { 411 return ( nOperator != null ); 412 } 413 414 protected boolean hasLeft() 415 { 416 return ( nLeft != null ); 417 } 418 419 protected Node getLeft() 420 { 421 return nLeft; 422 } 423 424 protected boolean hasRight() 425 { 426 return ( nRight != null ); 427 } 428 429 protected Node getRight() 430 { 431 return nRight; 432 } 433 434 protected Operator getOperator() 435 { 436 return nOperator; 437 } 438 439 protected int getLevel() 440 { 441 return nLevel; 442 } 443 444 protected Double getValue() 445 { 446 return nValue; 447 } 448 449 protected void setValue(Double f) 450 { 451 nValue = f; 452 } 453 454 protected String getString() 455 { 456 return nString; 457 } 458 459 462 463 public String removeBrackets(String s) 464 { 465 String res = s; 466 if ( s.length() > 2 && res.startsWith("(") && res.endsWith(")") && checkBrackets(s.substring(1,s.length()-1)) == 0 ) 467 { 468 res = res.substring(1, res.length()-1 ); 469 } 470 if ( res != s ) 471 return removeBrackets(res); 472 else 473 return res; 474 } 475 476 479 480 public String removeIllegalCharacters(String s) 481 { 482 char[] illegalCharacters = { ' ' }; 483 String res = s; 484 485 for ( int j=0; j<illegalCharacters.length; j++) 486 { 487 int i = res.lastIndexOf(illegalCharacters[j], res.length()); 488 while ( i != -1 ) 489 { 490 String temp = res; 491 res = temp.substring(0,i); 492 res += temp.substring(i + 1); 493 i = res.lastIndexOf(illegalCharacters[j], s.length()); 494 } 495 } 496 return res; 497 } 498 499 } 500 } 501 | Popular Tags |