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

Unary tests #689

Merged
merged 4 commits into from
Jul 22, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ static boolean conformsTo(List<Type> list1, List<Type> list2) {
return true;
}

static boolean sameSemanticDomain(Type type1, Type type2) {
type1 = extractTypeFromConstraint(type1);
type2 = extractTypeFromConstraint(type2);
if (type1 == null) {
return type2 == null;
} else {
return type1.getClass().equals(type2.getClass());
}
}

// Check only types that share the same class
boolean equivalentTo(Type other);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ public Element<Type> visit(InExpression<Type> element, DMNContext parentContext)
value.accept(this, parentContext);

// Visit tests with value type injected in scope
DMNContext testContext = this.dmnTransformer.makeUnaryTestContext(value, parentContext);
DMNContext testContext = parentContext.isTestContext() ? parentContext : this.dmnTransformer.makeUnaryTestContext(value, parentContext);
element.getTests().forEach(t -> t.accept(this, testContext));

// Derive type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,31 @@ public Object visit(OperatorRange<Type> element, DMNContext context) {
} else {
// Evaluate as test
Object self = context.lookupBinding(INPUT_ENTRY_PLACE_HOLDER);
if (operator == null) {
if (Type.equivalentTo(inputExpressionType, endpointType)) {
return evaluateOperatorRange(element, "=", self, endpoint, context);
} else if (endpointType instanceof ListType && Type.equivalentTo(inputExpressionType, ((ListType) endpointType).getElementType())) {
List endpointValueList = (List)endpoint.accept(this, context);
if (Type.sameSemanticDomain(endpointType, inputExpressionType)) {
// input and endpoint are comparable
Object condition;
if (operator == null) {
condition = evaluateOperatorRange(element, "=", self, endpoint, context);
} else {
condition = evaluateOperatorRange(element, operator, self, endpoint, context);
}
return condition;
} else if (endpointType instanceof ListType) {
Type endpointElementType = ((ListType) endpointType).getElementType();
if (Type.sameSemanticDomain(endpointElementType, inputExpressionType)) {
// input and list elements are comparable
List endpointValueList = (List) endpoint.accept(this, context);
List results = new ArrayList();
for(Object endpointValue: endpointValueList) {
results.add(evaluateOperatorRange(element, "=", self, inputExpressionType, ((ListType) endpointType).getElementType(), endpointValue));
}
return this.lib.or(results);
}
throw new DMNRuntimeException(String.format("Cannot evaluate test '%s'", element));
} else {
return evaluateOperatorRange(element, operator, self, endpoint, context);
}

// Cannot compare
handleError(context, element, String.format("Cannot compare '%s', '%s'", inputExpressionType, endpointType));
return null;
}
} catch (Exception e) {
this.errorHandler.reportError(String.format("Cannot evaluate '%s'", element), e);
Expand Down Expand Up @@ -652,19 +662,29 @@ public Object visit(InExpression<Type> element, DMNContext context) {
Expression<Type> valueExp = element.getValue();
Object value = valueExp.accept(this, context);

DMNContext inParams = this.dmnTransformer.makeUnaryTestContext(valueExp, context);
inParams.bind(INPUT_ENTRY_PLACE_HOLDER, value);
DMNContext testContext = context.isTestContext() ? context : this.dmnTransformer.makeUnaryTestContext(valueExp, context);
testContext.bind(INPUT_ENTRY_PLACE_HOLDER, value);

List<Object> result = new ArrayList<>();
List<PositiveUnaryTest<Type>> positiveUnaryTests = element.getTests();
for (PositiveUnaryTest<Type> positiveUnaryTest : positiveUnaryTests) {
Object test = positiveUnaryTest.accept(this, inParams);
result.add(test);
}
if (result.size() == 1) {
return result.get(0);
} else {
return this.lib.booleanOr(result);
try {
List<Object> result = new ArrayList<>();
List<PositiveUnaryTest<Type>> positiveUnaryTests = element.getTests();
for (PositiveUnaryTest<Type> positiveUnaryTest : positiveUnaryTests) {
Object condition;
if (positiveUnaryTest instanceof ExpressionTest) {
condition = evaluateOperatorRange(element, "=", value, ((ExpressionTest<Type>) positiveUnaryTest).getExpression(), context);
} else {
condition = positiveUnaryTest.accept(this, testContext);
}
result.add(condition);
}
if (result.size() == 1) {
return result.get(0);
} else {
return this.lib.booleanOr(result);
}
} catch (Exception e) {
this.errorHandler.reportError(String.format("Cannot evaluate '%s'", element), e);
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,31 @@ public Triple visit(OperatorRange<Type> element, DMNContext context) {
return (Triple) element.getEndpointsRange().accept(this, context);
} else {
// Evaluate as test
String operator = element.getOperator();
Type inputExpressionType = context.getInputExpressionType();
Expression<Type> endpoint = element.getEndpoint();
Triple condition;
if (operator == null) {
condition = makeListTestCondition("=", inputExpressionToJava(context), endpoint, context);
} else {
condition = makeListTestCondition(operator, inputExpressionToJava(context), endpoint, context);
Type endpointType = endpoint.getType();
if (Type.sameSemanticDomain(endpointType, inputExpressionType)) {
// input and endpoint are comparable
Triple condition;
String operator = element.getOperator();
if (operator == null) {
condition = makeRangeCondition("=", (Expression) context.getInputExpression(), endpoint, context);
} else {
condition = makeRangeCondition(operator, (Expression) context.getInputExpression(), endpoint, context);
}
return condition;
} else if (endpointType instanceof ListType) {
Type endpointElementType = ((ListType) endpointType).getElementType();
if (Type.sameSemanticDomain(endpointElementType, inputExpressionType)) {
// input and list elements are comparable
Triple javaList = (Triple) endpoint.accept(this, context);
return this.triples.makeBuiltinFunctionInvocation("listContains", javaList, inputExpressionToJava(context));
}
}
return condition;

// Cannot compare
handleError(context, element, String.format("Cannot compare '%s', '%s'", inputExpressionType, endpointType));
return null;
}
}

Expand All @@ -143,8 +159,8 @@ public Triple visit(EndpointsRange<Type> element, DMNContext context) {
return this.triples.constructor(clsName, Arrays.asList(startIncluded, start, endIncluded, end));
} else {
// Evaluate as test
Triple leftCondition = makeListTestCondition(element.isOpenStart() ? ">" : ">=", inputExpressionToJava(context), startEndpoint, context);
Triple rightCondition = makeListTestCondition(element.isOpenEnd() ? "<" : "<=", inputExpressionToJava(context), endEndpoint, context);
Triple leftCondition = makeRangeCondition(element.isOpenStart() ? ">" : ">=", (Expression) context.getInputExpression(), startEndpoint, context);
Triple rightCondition = makeRangeCondition(element.isOpenEnd() ? "<" : "<=", (Expression) context.getInputExpression(), endEndpoint, context);
return this.triples.makeBuiltinFunctionInvocation("booleanAnd", leftCondition, rightCondition);
}
}
Expand Down Expand Up @@ -439,11 +455,16 @@ public Triple visit(InExpression<Type> element, DMNContext context) {
Expression<Type> valueExp = element.getValue();
List<PositiveUnaryTest<Type>> positiveUnaryTests = element.getTests();

DMNContext inContext = this.dmnTransformer.makeUnaryTestContext(valueExp, context);
DMNContext testContext = context.isTestContext() ? context : this.dmnTransformer.makeUnaryTestContext(valueExp, context);
List<Triple> result = new ArrayList<>();
for (PositiveUnaryTest<Type> positiveUnaryTest : positiveUnaryTests) {
Triple test = (Triple) positiveUnaryTest.accept(this, inContext);
result.add(test);
Triple condition;
if (positiveUnaryTest instanceof ExpressionTest) {
condition = makeRangeCondition("=", valueExp, ((ExpressionTest<Type>) positiveUnaryTest).getExpression(), context);
} else {
condition = (Triple) positiveUnaryTest.accept(this, testContext);
}
result.add(condition);
}
if (result.size() == 1) {
return result.get(0);
Expand Down Expand Up @@ -699,15 +720,15 @@ private Triple toBooleanOr(List<Triple> operands) {
}
}

private Triple makeListTestCondition(String feelOperator, Triple inputExpressionText, Expression<Type> rightOperand, DMNContext context) {
private Triple makeRangeCondition(String feelOperator, Expression<Type> leftOperand, Expression<Type> rightOperand, DMNContext context) {
Triple leftOpd = (Triple) leftOperand.accept(this, context);
Triple rightOpd = (Triple) rightOperand.accept(this, context);
Triple condition;
Expression<Type> inputExpression = (Expression) context.getInputExpression();
String javaOperator = listTestOperator(feelOperator, inputExpression, rightOperand);
String javaOperator = rangeOperator(feelOperator, leftOperand, rightOperand);
if (StringUtils.isEmpty(javaOperator)) {
condition = infixExpression(javaOperator, inputExpressionText, rightOpd);
condition = infixExpression(javaOperator, leftOpd, rightOpd);
} else {
condition = functionalExpression(javaOperator, inputExpressionText, rightOpd);
condition = functionalExpression(javaOperator, leftOpd, rightOpd);
}
return condition;
}
Expand Down Expand Up @@ -794,7 +815,7 @@ protected Triple makeCondition(String feelOperator, Triple leftOpd, Triple right
}
}

protected String listTestOperator(String feelOperatorName, Expression<Type> leftOperand, Expression<Type> rightOperand) {
protected String rangeOperator(String feelOperatorName, Expression<Type> leftOperand, Expression<Type> rightOperand) {
NativeOperator javaOperator = OperatorDecisionTable.javaOperator(feelOperatorName, rightOperand.getType(), rightOperand.getType());
if (javaOperator != null) {
return javaOperator.getName();
Expand Down
Loading
Loading