Skip to content

Commit

Permalink
DROOLS-1701 !=, not() unary tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kurobako authored and tarilabs committed Dec 11, 2017
1 parent 7c15633 commit 45d3f11
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.kie.dmn.feel.codegen.feel11;

import org.kie.dmn.feel.lang.ast.InfixOpNode;
import org.kie.dmn.feel.runtime.UnaryTest;
import org.kie.dmn.feel.util.EvalHelper;

/**
Expand Down Expand Up @@ -136,4 +137,25 @@ public static Boolean gt(Object left, Object right) {
public static Boolean eq(Object left, Object right) {
return EvalHelper.isEqual(left, right, null);
}

/**
* FEEL spec Table 39
*/
public static Boolean ne(Object left, Object right) {
return not(EvalHelper.isEqual(left, right, null));
}

public static Boolean not(Object arg, UnaryTest test) {
return not(test.apply(null, arg));
}

private static Boolean not(Object arg) {
if (Boolean.TRUE.equals(arg)) {
return Boolean.FALSE;
}
if (Boolean.FALSE.equals(arg)) {
return Boolean.TRUE;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,20 @@ private DirectCompilerResult createUnaryTestExpression(ParserRuleContext ctx, Di
}
break;
case NE:
throw new UnsupportedOperationException("TODO"); // TODO
case NOT:
throw new UnsupportedOperationException("TODO"); // TODO
{
MethodCallExpr expression = new MethodCallExpr(null, "ne");
expression.addArgument(new NameExpr("left"));
expression.addArgument(endpoint.getExpression());
lambdaBody = new ExpressionStmt(expression);
}
break;
case NOT: {
MethodCallExpr expression = new MethodCallExpr(null, "not");
expression.addArgument(new NameExpr("left"));
expression.addArgument(endpoint.getExpression());
lambdaBody = new ExpressionStmt(expression);
}
break;
default:
throw new UnsupportedOperationException("Unable to determine operator of unary test");
}
Expand Down Expand Up @@ -896,8 +907,9 @@ public DirectCompilerResult visitCompilation_unit(FEEL_1_1Parser.Compilation_uni
return visit( ctx.expression() );
}

// @Override
// public DirectCompilerResult visitNegatedUnaryTests(FEEL_1_1Parser.NegatedUnaryTestsContext ctx) {
// throw new UnsupportedOperationException("TODO"); // TODO
// }
@Override
public DirectCompilerResult visitNegatedUnaryTests(FEEL_1_1Parser.NegatedUnaryTestsContext ctx) {
FEEL_1_1Parser.SimpleUnaryTestsContext child = (FEEL_1_1Parser.SimpleUnaryTestsContext) ctx.getChild(2);
return createUnaryTestExpression(ctx, child.accept(this), UnaryOperator.NOT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void test_positiveUnaryTestIneq() {
assertThat(parseCompileEvaluate(">=1, >2, <3, <=4", 3), is(Arrays.asList(true, true, false, true)));
assertThat(parseCompileEvaluate(">=1, >2, <3, <=4", 4), is(Arrays.asList(true, true, false, true)));
assertThat(parseCompileEvaluate(">=1, >2, <3, <=4", 5), is(Arrays.asList(true, true, false, false)));
assertThat(parseCompileEvaluate("!=1, !=42", 1), is(Arrays.asList(false, true)));
}

@Test
Expand All @@ -75,6 +76,11 @@ public void test_positiveUnaryTestIneq_forEQ() {
assertThat(parseCompileEvaluate("<47, 47", 1), is(Arrays.asList(true, false)));
}

@Test
public void test_not() {
assertThat(parseCompileEvaluate("not(=47), not(<1), not(!=1)", 1), is(Arrays.asList(true, true, true)));
}

@Test
public void test_simpleUnaryTest_forRANGE() {
assertThat(parseCompileEvaluate("[1..2]", 1), is(Arrays.asList(true)));
Expand Down

0 comments on commit 45d3f11

Please sign in to comment.