Package tcg.common.util

Examples of tcg.common.util.ExpressionParser


    // protect against "X" or "x"
    inExpression = inExpression.toLowerCase();

    // create the parser
    ecParser = new ExpressionParser(inExpression, type);

    // create the input variable
    ecParser.createVariable("x", type, initInputValue);

    // try to evaluate
View Full Code Here


    // parse the expression
    String parsedExpression = _parseExpression(inDpList, inExpression,
        params, varnames, vartypes, varvalues);

    // create the parser
    expressionParser = new ExpressionParser(parsedExpression, type);

    // create all the variables
    for (int i = 0; i < varnames.size(); i++)
    {
      expressionParser.createVariable(varnames.get(i), vartypes.get(i),
View Full Code Here

    // parse the expression
    String parsedExpression = _parseExpression(inDpList, inExpression,
        params, varnames, vartypes, varvalues);

    // create the parser
    rccParser = new ExpressionParser(parsedExpression, type);

    // create all the variables
    for (int i = 0; i < varnames.size(); i++)
    {
      rccParser.createVariable(varnames.get(i), vartypes.get(i),
View Full Code Here

    // parse the expression
    String parsedExpression = _parseExpression(inDpList, inExpression,
        params, varnames, vartypes, varvalues);

    // create the parser
    lccParser = new ExpressionParser(parsedExpression, type);

    // create all the variables
    for (int i = 0; i < varnames.size(); i++)
    {
      lccParser.createVariable(varnames.get(i), vartypes.get(i),
View Full Code Here

public class ExpressionParserTest extends TestCase {

  public void testReturnValue()
  {
    ExpressionParser parser = null;
    Boolean boolResult = null;
    Integer intResult = null;
    Double dblResult = null;
    String strResult = null;
   
    //boolean value

    //Note: assignment from integer/double to boolean is not supported
    //Note: Only the following constant is supported (case-sensitive): "true" and "false"
   
    //set-constant-value (string)
    parser = new ExpressionParser("true", Boolean.class);
    try
    {
      boolResult = (Boolean)parser.evaluate();
      Assert.assertNotNull(boolResult);
      Assert.assertEquals(true, boolResult.booleanValue());
    }
    catch(Exception ex)
    {
      Assert.fail(ex.toString());
    }
   
    //set-constant-value (string)
    parser = new ExpressionParser("false", Boolean.class);
    try
    {
      boolResult = (Boolean)parser.evaluate();
      Assert.assertNotNull(boolResult);
      Assert.assertEquals(false, boolResult.booleanValue());
    }
    catch(Exception ex)
    {
      Assert.fail(ex.toString());
    }
   
    //number/integer value

    //Note: no conversion from double is supported.
   
    //set-constant-value (positive value)
    parser = new ExpressionParser("15", Integer.class);
    try
    {
      intResult = (Integer)parser.evaluate();
      Assert.assertNotNull(intResult);
      Assert.assertEquals(15, intResult.intValue());
    }
    catch(Exception ex)
    {
      Assert.fail(ex.toString());
    }
   
    //set-constant-value (zero)
    parser = new ExpressionParser("0", Integer.class);
    try
    {
      intResult = (Integer)parser.evaluate();
      Assert.assertNotNull(intResult);
      Assert.assertEquals(0, intResult.intValue());
    }
    catch(Exception ex)
    {
      Assert.fail(ex.toString());
    }
   
    //set-constant-value (negative value)
    parser = new ExpressionParser("-15", Integer.class);
    try
    {
      intResult = (Integer)parser.evaluate();
      Assert.assertNotNull(intResult);
      Assert.assertEquals(-15, intResult.intValue());
    }
    catch(Exception ex)
    {
      Assert.fail(ex.toString());
    }
   
    //double value

    //Note: When doing double calculation, we must explicitly specify double value
    //      by using the decimal notation
   
    //set-constant-value (positive value)
    parser = new ExpressionParser("15.0", Double.class);
    try
    {
      dblResult = (Double)parser.evaluate();
      Assert.assertNotNull(dblResult);
      Assert.assertEquals(15.0, dblResult.doubleValue());
    }
    catch(Exception ex)
    {
      Assert.fail(ex.toString());
    }
   
    //set-constant-value (zero)
    parser = new ExpressionParser("0.0", Double.class);
    try
    {
      dblResult = (Double)parser.evaluate();
      Assert.assertNotNull(dblResult);
      Assert.assertEquals(0.0, dblResult.doubleValue());
    }
    catch(Exception ex)
    {
      Assert.fail(ex.toString());
    }
   
    //set-constant-value (negative value)
    parser = new ExpressionParser("-15.0", Double.class);
    try
    {
      dblResult = (Double)parser.evaluate();
      Assert.assertNotNull(dblResult);
      Assert.assertEquals(-15.0, dblResult.doubleValue());
    }
    catch(Exception ex)
    {
      Assert.fail(ex.toString());
    }
   
    //string value
   
    //Note: We must always escape the string argument with double quote
   
    //set-constant-value
    parser = new ExpressionParser("\"any-string\"", String.class);
    try
    {
      strResult = (String)parser.evaluate();
      Assert.assertNotNull(strResult);
      Assert.assertTrue(strResult.compareTo("any-string") == 0);
    }
    catch(Exception ex)
    {
View Full Code Here

      Assert.fail(ex.toString());
    }
  }

  public void testSetVariableString() {
    ExpressionParser parser = null;
    String strResult = null;
    boolean status = true;
   
    //string
    parser = new ExpressionParser("var1", String.class);
    parser.createVariable("var1", String.class, "");
    try
    {
      //initial state
      strResult = (String)parser.evaluate();
      Assert.assertNotNull(strResult);
      Assert.assertTrue(strResult.isEmpty());
      //set to value (using variable name)
      parser.set("var1", "some-string");
      strResult = (String)parser.evaluate();
      Assert.assertNotNull(strResult);
      Assert.assertTrue(strResult.compareTo("some-string") == 0);
      //get variable (using variable name)
      strResult = (String)parser.get("var1");
      Assert.assertNotNull(strResult);
      Assert.assertTrue(strResult.compareTo("some-string") == 0);
      //set to another value (using index)
      parser.set(0, "different string");
      strResult = (String)parser.evaluate();
      Assert.assertNotNull(strResult);
      Assert.assertTrue(strResult.compareTo("different string") == 0);
      //get variable (using variable name)
      strResult = (String)parser.get(0);
      Assert.assertNotNull(strResult);
      Assert.assertTrue(strResult.compareTo("different string") == 0);
      //set to empty string
      parser.set("var1", "");
      strResult = (String)parser.evaluate();
      Assert.assertNotNull(strResult);
      Assert.assertTrue(strResult.isEmpty());
      //set/get invalid variable name
      status = parser.set("var2", "string1");
      Assert.assertFalse(status);
      strResult = (String)parser.get("var2");
      Assert.assertNull(strResult);
      //set/get invalid variable index
      status = parser.set(1, "string2");
      Assert.assertFalse(status);
      strResult = (String)parser.get(1);
      Assert.assertNull(strResult);
    }
    catch(Exception ex)
    {
      Assert.fail(ex.toString());
View Full Code Here

      Assert.fail(ex.toString());
    }
  }

  public void testSetVariableBool() {
    ExpressionParser parser = null;
    Boolean boolResult = null;
    boolean status = true;
   
    //boolean
    parser = new ExpressionParser("var1", Boolean.class);
    parser.createVariable("var1", Boolean.class, Boolean.FALSE);
    try
    {
      //initial state
      boolResult = (Boolean)parser.evaluate();
      Assert.assertNotNull(boolResult);
      Assert.assertEquals(false, boolResult.booleanValue());
      //set to true (using variable name)
      parser.set("var1", Boolean.TRUE);
      boolResult = (Boolean)parser.evaluate();
      Assert.assertNotNull(boolResult);
      Assert.assertEquals(true, boolResult.booleanValue());
      //get variable (using variable name)
      boolResult = (Boolean)parser.get("var1");
      Assert.assertNotNull(boolResult);
      Assert.assertEquals(true, boolResult.booleanValue());
      //set to false (using index)
      parser.set(0, Boolean.FALSE);
      boolResult = (Boolean)parser.evaluate();
      Assert.assertNotNull(boolResult);
      Assert.assertEquals(false, boolResult.booleanValue());
      //get variable (using index)
      boolResult = (Boolean)parser.get(0);
      Assert.assertNotNull(boolResult);
      Assert.assertEquals(false, boolResult.booleanValue());
      //set/get invalid variable name
      status = parser.set("var2", Boolean.TRUE);
      Assert.assertFalse(status);
      boolResult = (Boolean)parser.get("var2");
      Assert.assertNull(boolResult);
      //set/get invalid variable index
      status = parser.set(1, Boolean.TRUE);
      Assert.assertFalse(status);
      boolResult = (Boolean)parser.get(1);
      Assert.assertNull(boolResult);
    }
    catch(Exception ex)
    {
      Assert.fail(ex.toString());
View Full Code Here

      Assert.fail(ex.toString());
    }   
  }

  public void testSetVariableInt() {
    ExpressionParser parser = null;
    Integer intResult = null;
    boolean status = true;
   
    //number
    parser = new ExpressionParser("var1", Integer.class);
    parser.createVariable("var1", Integer.class, new Integer(0));
    try
    {
      //initial state
      intResult = (Integer)parser.evaluate();
      Assert.assertNotNull(intResult);
      Assert.assertEquals(0, intResult.intValue());
      //set to positive value (using variable name)
      parser.set("var1", new Integer(15));
      intResult = (Integer)parser.evaluate();
      Assert.assertNotNull(intResult);
      Assert.assertEquals(15, intResult.intValue());
      //get variable (using variable name)
      intResult = (Integer)parser.get("var1");
      Assert.assertNotNull(intResult);
      Assert.assertEquals(15, intResult.intValue());
      //set to negative value (using index)
      parser.set(0, new Integer(-19));
      intResult = (Integer)parser.evaluate();
      Assert.assertNotNull(intResult);
      Assert.assertEquals(-19, intResult.intValue());
      //get variable (using index)
      intResult = (Integer)parser.get(0);
      Assert.assertNotNull(intResult);
      Assert.assertEquals(-19, intResult.intValue());
      //set to zero
      parser.set("var1", new Integer(0));
      intResult = (Integer)parser.evaluate();
      Assert.assertNotNull(intResult);
      Assert.assertEquals(0, intResult.intValue());
      //set/get invalid variable name
      status = parser.set("var2", new Integer(50));
      Assert.assertFalse(status);
      intResult = (Integer)parser.get("var2");
      Assert.assertNull(intResult);
      //set/get invalid variable index
      status = parser.set(1, new Integer(51));
      Assert.assertFalse(status);
      intResult = (Integer)parser.get(1);
      Assert.assertNull(intResult);
    }
    catch(Exception ex)
    {
      Assert.fail(ex.toString());
View Full Code Here

    }

  }

  public void testSetVariableDbl() {
    ExpressionParser parser = null;
    Double dblResult = null;
    boolean status = true;
   
    //double
    parser = new ExpressionParser("var1", Double.class);
    parser.createVariable("var1", Double.class, new Double(0));
    try
    {
      //initial state
      dblResult = (Double)parser.evaluate();
      Assert.assertNotNull(dblResult);
      Assert.assertEquals(0.0, dblResult.doubleValue());
      //set to positive value (using variable name)
      parser.set("var1", new Double(15.8));
      dblResult = (Double)parser.evaluate();
      Assert.assertNotNull(dblResult);
      Assert.assertEquals(15.8, dblResult.doubleValue());
      //get variable (using variable name)
      dblResult = (Double)parser.get("var1");
      Assert.assertNotNull(dblResult);
      Assert.assertEquals(15.8, dblResult.doubleValue());
      //set to negative value (using index)
      parser.set(0, new Double(-19.3));
      dblResult = (Double)parser.evaluate();
      Assert.assertNotNull(dblResult);
      Assert.assertEquals(-19.3, dblResult.doubleValue());
      //get variable (using index)
      dblResult = (Double)parser.get(0);
      Assert.assertNotNull(dblResult);
      Assert.assertEquals(-19.3, dblResult.doubleValue());
      //set to zero
      parser.set("var1", new Double(0));
      dblResult = (Double)parser.evaluate();
      Assert.assertNotNull(dblResult);
      Assert.assertEquals(0.0, dblResult.doubleValue());
      //set/get invalid variable name
      status = parser.set("var2", new Double(50.1));
      Assert.assertFalse(status);
      dblResult = (Double)parser.get("var2");
      Assert.assertNull(dblResult);
      //set/get invalid variable index
      status = parser.set(1, new Double(51.6));
      Assert.assertFalse(status);
      dblResult = (Double)parser.get(1);
      Assert.assertNull(dblResult);
    }
    catch(Exception ex)
    {
      Assert.fail(ex.toString());
View Full Code Here

  }

  public void testSetExpression()
  {
    ExpressionParser parser = null;
    Integer intResult = null;
    Boolean boolResult = null;
    boolean status = false;

    //initial state
    parser = new ExpressionParser();
    Assert.assertEquals(false, parser.isValid());
    Assert.assertEquals(false, parser.isInitialized());
    //default to integer value
    try
    {
      intResult = (Integer)parser.evaluate();
      Assert.assertNull(intResult);
    }
    catch(Exception ex)
    {
      Assert.fail(ex.toString());
    }

    //set the expression (use simple addition with variable)
    parser.setExpression("var1 + var2", Integer.class);
    parser.createVariable("var1", Integer.class, new Integer(0));
    parser.createVariable("var2", Integer.class, new Integer(0));
    try
    {
      //set the variable (using variable name)
      status = parser.set("var1", new Integer(1));
      Assert.assertTrue(status);
      status = parser.set("var2", new Integer(5));
      Assert.assertTrue(status);
      //set invalid variable name
      status = parser.set("var3", new Integer(3));
      Assert.assertFalse(status);
      //evaluate
      intResult = (Integer)parser.evaluate();
      Assert.assertNotNull(intResult);
      Assert.assertEquals(6, intResult.intValue());
      //set the variable (using index)
      status = parser.set(0, new Integer(2));
      Assert.assertTrue(status);
      status = parser.set(1, new Integer(7));
      Assert.assertTrue(status);
      //set invalid variable index
      status = parser.set(2, new Integer(3));
      Assert.assertFalse(status);
      //evaluate
      intResult = (Integer)parser.evaluate();
      Assert.assertNotNull(intResult);
      Assert.assertEquals(9, intResult.intValue());
    }
    catch(Exception ex)
    {
      Assert.fail(ex.toString());
    }
   
    //reset the expression. use simple boolean operation
    parser.setExpression("bool1 || bool2", Boolean.class);
    parser.createVariable("bool1", Boolean.class, Boolean.FALSE);
    parser.createVariable("bool2", Boolean.class, Boolean.FALSE);
    try
    {
      //set the variable (using variable name)
      status = parser.set("bool1", Boolean.FALSE);
      Assert.assertTrue(status);
      status = parser.set("bool2", Boolean.TRUE);
      Assert.assertTrue(status);
      //set invalid variable name
      status = parser.set("var1", Boolean.TRUE);
      Assert.assertFalse(status);
      status = parser.set("var2", Boolean.TRUE);
      Assert.assertFalse(status);
      //evaluate (false || true)
      boolResult = (Boolean)parser.evaluate();
      Assert.assertNotNull(boolResult);
      Assert.assertEquals(true, boolResult.booleanValue());
      //set the variable (using index)
      status = parser.set(0, Boolean.TRUE);
      Assert.assertTrue(status);
      status = parser.set(1, Boolean.FALSE);
      Assert.assertTrue(status);
      //set invalid variable name
      status = parser.set(3, Boolean.TRUE);
      Assert.assertFalse(status);
      //evaluate (false || true)
      boolResult = (Boolean)parser.evaluate();
      Assert.assertNotNull(boolResult);
      Assert.assertEquals(true, boolResult.booleanValue());
    }
    catch(Exception ex)
    {
View Full Code Here

TOP

Related Classes of tcg.common.util.ExpressionParser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.