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-7635] ansible-rulebook : Raise an error when a condition comp… #117

Merged
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 @@ -4,6 +4,8 @@

import org.drools.model.ConstraintOperator;

import static org.drools.ansible.rulebook.integration.api.domain.constraints.RulebookConstraintOperator.isCompatibleType;

public class NegationOperator implements ConstraintOperator {

private final ConstraintOperator toBeNegated;
Expand All @@ -14,6 +16,16 @@ public NegationOperator(ConstraintOperator toBeNegated) {

@Override
public <T, V> BiPredicate<T, V> asPredicate() {
if (toBeNegated instanceof RulebookConstraintOperator rulebookConstraintOperator) {
return (t, v) -> {
// if not compatible type, return false. Use the operator to log the type check error
if (!isCompatibleType(t, v)) {
rulebookConstraintOperator.logTypeCheck(t, v);
return false;
}
return !toBeNegated.asPredicate().test(t, v);
};
}
return (t, v) -> !toBeNegated.asPredicate().test(t, v);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public RulebookConstraintOperator(Index.ConstraintType type) {
this.type = type;
}

@Override
public void setConditionContext(RuleGenerationContext ruleContext, Map<?, ?> expression) {
this.conditionContext = new ConditionContext(ruleContext.getRuleSetName(), ruleContext.getRuleName(), expression.toString());
}
Expand Down Expand Up @@ -64,6 +65,7 @@ public RulebookConstraintOperator negate() {
return this;
}

@Override
public boolean canInverse() {
switch (this.type) {
case EQUAL:
Expand Down Expand Up @@ -107,23 +109,35 @@ public <T, V> BiPredicate<T, V> asPredicate() {
}

private <T, V> boolean predicateWithTypeCheck(T t, V v, BiPredicate<T, V> predicate) {
if (t == null
|| v == null
|| t instanceof Number && v instanceof Number
|| t.getClass() == v.getClass()) {
if (isCompatibleType(t, v)) {
return predicate.test(t, v);
} else {
if (!typeCheckLogged) {
LOG.error("Cannot compare values of different types: {} and {}. RuleSet: {}. RuleName: {}. Condition: {}",
convertJavaClassToPythonClass(t.getClass()),
convertJavaClassToPythonClass(v.getClass()),
conditionContext.getRuleSet(), conditionContext.getRuleName(), conditionContext.getConditionExpression());
typeCheckLogged = true; // Log only once per constraint
}
return false; // Different types are never equal
logTypeCheck(t, v);
return false; // Different types evaluation always return false even if the operator is NOT_EQUAL
}
}

/*
* Log a type check error once per constraint
*/
<T, V> void logTypeCheck(T t, V v) {
if (!typeCheckLogged) {
LOG.error("Cannot compare values of different types: {} and {}. RuleSet: {}. RuleName: {}. Condition: {}",
convertJavaClassToPythonClass(t.getClass()),
convertJavaClassToPythonClass(v.getClass()),
conditionContext.getRuleSet(), conditionContext.getRuleName(), conditionContext.getConditionExpression());
typeCheckLogged = true; // Log only once per constraint
}
}

public static boolean isCompatibleType(Object t, Object v) {
return t == null
|| v == null
|| t instanceof Number && v instanceof Number
|| t.getClass() == v.getClass();
}

@Override
public RulebookConstraintOperator inverse() {
switch (this.type) {
case GREATER_THAN:
Expand Down
Loading
Loading