Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DROOLS-1701 !=, not() unary tests #3

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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