DDMExpressionImpl implements DDMExpression<T> { @Override public T evaluate() throws DDMExpressionException { Map<String, DDMExpressionFunctionFactory> ddmExpressionFunctions = _ddmExpressionFunctionTracker.getDDMExpressionFunctionFactories( _ddmExpressionFunctionNames); try { Set<String> undefinedFunctionNames = new HashSet<>( _ddmExpressionFunctionNames); undefinedFunctionNames.removeAll(ddmExpressionFunctions.keySet()); if (!undefinedFunctionNames.isEmpty()) { throw new DDMExpressionException.FunctionNotDefined( undefinedFunctionNames); } DDMExpressionEvaluatorVisitor ddmExpressionEvaluatorVisitor = new DDMExpressionEvaluatorVisitor( ddmExpressionFunctions, _variables, _ddmExpressionActionHandler, _ddmExpressionFieldAccessor, _ddmExpressionObserver, _ddmExpressionParameterAccessor); return (T)_expressionContext.accept(ddmExpressionEvaluatorVisitor); } catch (DDMExpressionException ddmExpressionException) { throw ddmExpressionException; } catch (Exception exception) { throw new DDMExpressionException(exception); } } protected  DDMExpressionImpl(String expressionString); @Override T evaluate(); @Override Expression getModel(); @Override void setVariable(String name, Object value);  }
@Test public void testAddition() throws Exception { DDMExpressionImpl<BigDecimal> ddmExpressionImpl = createDDMExpression( "1 + 3 + 6"); Assert.assertEquals(new BigDecimal("10"), ddmExpressionImpl.evaluate()); }
@Test public void testAndExpression1() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "3 > 1 && 1 < 2"); Assert.assertTrue(ddmExpressionImpl.evaluate()); }
@Test public void testAndExpression2() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "4 > 2 && 1 < 0"); Assert.assertFalse(ddmExpressionImpl.evaluate()); }
@Test public void testAndExpression3() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "3 >= 4 and 2 <= 4"); Assert.assertFalse(ddmExpressionImpl.evaluate()); }
@Test public void testDivision1() throws Exception { BigDecimal bigDecimal = new BigDecimal(2); DDMExpressionImpl<BigDecimal> ddmExpressionImpl = createDDMExpression( "6 / 3"); Assert.assertEquals( bigDecimal.setScale(2), ddmExpressionImpl.evaluate()); }
@Test public void testDivision2() throws Exception { BigDecimal bigDecimal = new BigDecimal(7.5); DDMExpressionImpl<BigDecimal> ddmExpressionImpl = createDDMExpression( "15 / 2"); Assert.assertEquals( bigDecimal.setScale(2), ddmExpressionImpl.evaluate()); }
@Test public void testDivision3() throws Exception { BigDecimal bigDecimal = new BigDecimal(1.11); DDMExpressionImpl<BigDecimal> ddmExpressionImpl = createDDMExpression( "10 / 9"); Assert.assertEquals( bigDecimal.setScale(2, RoundingMode.FLOOR), ddmExpressionImpl.evaluate()); }
@Test public void testEquals1() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "3 == '3'"); Assert.assertFalse(ddmExpressionImpl.evaluate()); }
@Test public void testGreaterThan1() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "3 > 2.0"); Assert.assertTrue(ddmExpressionImpl.evaluate()); }
@Test public void testGreaterThan2() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "4 > 5"); Assert.assertFalse(ddmExpressionImpl.evaluate()); }
@Test public void testGreaterThanOrEquals1() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "-2 >= -3"); Assert.assertTrue(ddmExpressionImpl.evaluate()); }
@Test public void testGreaterThanOrEquals2() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "1 >= 2"); Assert.assertFalse(ddmExpressionImpl.evaluate()); }
@Test(expected = DDMExpressionException.InvalidSyntax.class) public void testInvalidSyntax1() throws Exception { DDMExpressionImpl<BigDecimal> ddmExpressionImpl = createDDMExpression( "1 ++ 2"); ddmExpressionImpl.evaluate(); }
@Test(expected = DDMExpressionException.InvalidSyntax.class) public void testInvalidSyntax2() throws Exception { DDMExpressionImpl<BigDecimal> ddmExpressionImpl = createDDMExpression( "(1 * 2"); ddmExpressionImpl.evaluate(); }
@Test public void testLessThan1() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "0 < 4"); Assert.assertTrue(ddmExpressionImpl.evaluate()); }
@Test public void testLessThan2() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "0 < -1.5"); Assert.assertFalse(ddmExpressionImpl.evaluate()); }
@Test public void testLessThanOrEquals1() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "1.6 <= 1.7"); Assert.assertTrue(ddmExpressionImpl.evaluate()); }
@Test public void testLessThanOrEquals2() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "1.9 <= 1.89"); Assert.assertFalse(ddmExpressionImpl.evaluate()); }
@Test public void testLogicalConstant() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "TRUE || false"); Assert.assertTrue(ddmExpressionImpl.evaluate()); }
@Test public void testMultiplication1() throws Exception { DDMExpressionImpl<BigDecimal> ddmExpressionImpl = createDDMExpression( "2.45 * 2"); BigDecimal bigDecimal = ddmExpressionImpl.evaluate(); Assert.assertEquals(0, bigDecimal.compareTo(new BigDecimal("4.9"))); }
@Test public void testMultiplication2() throws Exception { DDMExpressionImpl<BigDecimal> ddmExpressionImpl = createDDMExpression( "-2 * -3.55"); BigDecimal bigDecimal = ddmExpressionImpl.evaluate(); Assert.assertEquals(0, bigDecimal.compareTo(new BigDecimal("7.10"))); }
@Test public void testNot() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "not(-1 != 1.0)"); Assert.assertFalse(ddmExpressionImpl.evaluate()); }
@Test public void testNotEquals() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "1.6 != 1.66"); Assert.assertTrue(ddmExpressionImpl.evaluate()); }
@Test public void testNotEquals2() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "2 != 2.0"); Assert.assertTrue(ddmExpressionImpl.evaluate()); }
@Test public void testOr1() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "2 >= 1 || 1 < 0"); Assert.assertTrue(ddmExpressionImpl.evaluate()); }
@Test public void testOr2() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "4 == 3 or -1 >= -2"); Assert.assertTrue(ddmExpressionImpl.evaluate()); }
@Test public void testOr3() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "2 < 2 or 0 > 1"); Assert.assertFalse(ddmExpressionImpl.evaluate()); }
@Test public void testParenthesis() throws Exception { BigDecimal bigDecimal = new BigDecimal(4); DDMExpressionImpl<BigDecimal> ddmExpressionImpl = createDDMExpression( "(8 + 2) / 2.5"); Assert.assertEquals( bigDecimal.setScale(2), ddmExpressionImpl.evaluate()); }
@Test public void testPrecedence() throws Exception { DDMExpressionImpl<BigDecimal> ddmExpressionImpl = createDDMExpression( "4 - 2 * 6"); BigDecimal expected = new BigDecimal("-8"); BigDecimal bigDecimal = ddmExpressionImpl.evaluate(); Assert.assertEquals(0, bigDecimal.compareTo(expected)); }
@Test public void testSubtraction1() throws Exception { DDMExpressionImpl<BigDecimal> ddmExpressionImpl = createDDMExpression( "-2 -3.55"); BigDecimal bigDecimal = ddmExpressionImpl.evaluate(); Assert.assertEquals(0, bigDecimal.compareTo(new BigDecimal("-5.55"))); }
@Test public void testSubtraction2() throws Exception { DDMExpressionImpl<BigDecimal> ddmExpressionImpl = createDDMExpression( "4 - 2 - 1"); BigDecimal bigDecimal = ddmExpressionImpl.evaluate(); Assert.assertEquals(0, bigDecimal.compareTo(new BigDecimal("1"))); }
@Test(expected = DDMExpressionException.class) public void testUnavailableLogicalVariable() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "a > 5"); ddmExpressionImpl.evaluate(); }
@Test(expected = DDMExpressionException.class) public void testUnavailableNumericVariable() throws Exception { DDMExpressionImpl<Boolean> ddmExpressionImpl = createDDMExpression( "b + 1"); ddmExpressionImpl.evaluate(); }
@Test(expected = DDMExpressionException.FunctionNotDefined.class) public void testUndefinedFunction() throws Exception { DDMExpressionImpl<BigDecimal> ddmExpressionImpl = createDDMExpression( "sum(1,b)"); ddmExpressionImpl.evaluate(); }