Skip to content

Commit

Permalink
[DROOLS-7635] ansible-rulebook : Raise an error when a condition comp…
Browse files Browse the repository at this point in the history
…ares incompatible types
  • Loading branch information
tkobayas committed Sep 27, 2024
1 parent 011e17a commit f970acc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@

public interface ConstraintOperator {
<T, V> BiPredicate<T, V> asPredicate();

default boolean containsConstraintType() {
return false;
}

default Index.ConstraintType getConstraintType() {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,22 @@ public PrototypePatternDef expr(PrototypeExpression left, ConstraintOperator ope
reactOnFields.addAll(left.getImpactedFields());
reactOnFields.addAll(right.getImpactedFields());

// If operator is not Index.ConstraintType, it may contain Index.ConstraintType internally for indexing purposes
ConstraintOperator operatorAsConstraintType = operator;
if (operator.containsConstraintType()) {
operatorAsConstraintType = operator.getConstraintType();
}

expr(createExprId(left, operator, right),
asPredicate1(leftExtractor, operator, right.asFunction(prototype)),
createAlphaIndex(left, operator, right, prototype, leftExtractor),
createAlphaIndex(left, operatorAsConstraintType, right, prototype, leftExtractor),
reactOn( reactOnFields.toArray(new String[reactOnFields.size()])) );

return this;
}

private static AlphaIndex createAlphaIndex(PrototypeExpression left, ConstraintOperator operator, PrototypeExpression right, Prototype prototype, Function1<PrototypeFactInstance, Object> leftExtractor) {
if (left.getIndexingKey().isPresent() && right instanceof PrototypeExpression.FixedValue && operator instanceof Index.ConstraintType constraintType) {
private static AlphaIndex createAlphaIndex(PrototypeExpression left, ConstraintOperator operatorAsConstraintType, PrototypeExpression right, Prototype prototype, Function1<PrototypeFactInstance, Object> leftExtractor) {
if (left.getIndexingKey().isPresent() && right instanceof PrototypeExpression.FixedValue && operatorAsConstraintType instanceof Index.ConstraintType constraintType) {
String fieldName = left.getIndexingKey().get();
Prototype.Field field = prototype.getField(fieldName);
Object value = ((PrototypeExpression.FixedValue) right).getValue();
Expand Down

0 comments on commit f970acc

Please sign in to comment.