-
Notifications
You must be signed in to change notification settings - Fork 9
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
[DO-NOT-MERGE][DROOLS-7635] ansible-rulebook : Raise an error when a condition comp… #112
Closed
tkobayas
wants to merge
1
commit into
kiegroup:main
from
tkobayas:DROOLS-7635-error-incompatible-type
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
...rools/ansible/rulebook/integration/api/domain/constraints/RulebookConstraintOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package org.drools.ansible.rulebook.integration.api.domain.constraints; | ||
|
||
import java.util.Map; | ||
import java.util.function.BiPredicate; | ||
|
||
import org.drools.ansible.rulebook.integration.api.domain.RuleGenerationContext; | ||
import org.drools.model.ConstraintOperator; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static org.drools.model.util.OperatorUtils.areEqual; | ||
import static org.drools.model.util.OperatorUtils.compare; | ||
|
||
public class RulebookConstraintOperator implements ConstraintOperator { | ||
|
||
enum RulebookConstraintOperatorType { | ||
EQUAL, | ||
NOT_EQUAL, | ||
GREATER_THAN, | ||
GREATER_OR_EQUAL, | ||
LESS_THAN, | ||
LESS_OR_EQUAL, | ||
UNKNOWN; | ||
} | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(RulebookConstraintOperator.class); | ||
|
||
private RulebookConstraintOperatorType type; | ||
private ConditionContext conditionContext; | ||
private boolean typeCheckLogged = false; | ||
|
||
public RulebookConstraintOperator(RulebookConstraintOperatorType type) { | ||
this.type = type; | ||
} | ||
|
||
public void setConditionContext(RuleGenerationContext ruleContext, Map<?, ?> expression) { | ||
this.conditionContext = new ConditionContext(ruleContext.getRuleSetName(), ruleContext.getRuleName(), expression.toString()); | ||
} | ||
|
||
public RulebookConstraintOperator negate() { | ||
switch (this.type) { | ||
case EQUAL: | ||
this.type = RulebookConstraintOperatorType.NOT_EQUAL; | ||
return this; | ||
case NOT_EQUAL: | ||
this.type = RulebookConstraintOperatorType.EQUAL; | ||
return this; | ||
case GREATER_THAN: | ||
this.type = RulebookConstraintOperatorType.LESS_OR_EQUAL; | ||
return this; | ||
case GREATER_OR_EQUAL: | ||
this.type = RulebookConstraintOperatorType.LESS_THAN; | ||
return this; | ||
case LESS_OR_EQUAL: | ||
this.type = RulebookConstraintOperatorType.GREATER_THAN; | ||
return this; | ||
case LESS_THAN: | ||
this.type = RulebookConstraintOperatorType.GREATER_OR_EQUAL; | ||
return this; | ||
} | ||
this.type = RulebookConstraintOperatorType.UNKNOWN; | ||
return this; | ||
} | ||
|
||
public boolean canInverse() { | ||
switch (this.type) { | ||
case EQUAL: | ||
case NOT_EQUAL: | ||
case GREATER_THAN: | ||
case GREATER_OR_EQUAL: | ||
case LESS_THAN: | ||
case LESS_OR_EQUAL: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public <T, V> BiPredicate<T, V> asPredicate() { | ||
final BiPredicate<T, V> predicate; | ||
switch (this.type) { | ||
case EQUAL: | ||
predicate = (t, v) -> areEqual(t, v); | ||
break; | ||
case NOT_EQUAL: | ||
predicate = (t, v) -> !areEqual(t, v); | ||
break; | ||
case GREATER_THAN: | ||
predicate = (t,v) -> t != null && compare(t, v) > 0; | ||
break; | ||
case GREATER_OR_EQUAL: | ||
predicate = (t,v) -> t != null && compare(t, v) >= 0; | ||
break; | ||
case LESS_THAN: | ||
predicate = (t,v) -> t != null && compare(t, v) < 0; | ||
break; | ||
case LESS_OR_EQUAL: | ||
predicate = (t,v) -> t != null && compare(t, v) <= 0; | ||
break; | ||
default: | ||
throw new UnsupportedOperationException("Cannot convert " + this + " into a predicate"); | ||
} | ||
return (t, v) -> predicateWithTypeCheck(t, v, predicate); | ||
} | ||
|
||
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()) { | ||
return predicate.test(t, v); | ||
} else { | ||
if (!typeCheckLogged) { | ||
// TODO: rewrite the message to be python friendly | ||
LOG.error("Cannot compare values of different types: {} and {}. RuleSet: {}. RuleName: {}. Condition: {}", | ||
t.getClass(), v.getClass(), conditionContext.getRuleSet(), conditionContext.getRuleName(), conditionContext.getConditionExpression()); | ||
typeCheckLogged = true; // Log only once per constraint | ||
} | ||
return false; // Different types are never equal | ||
} | ||
} | ||
|
||
public RulebookConstraintOperator inverse() { | ||
switch (this.type) { | ||
case GREATER_THAN: | ||
this.type = RulebookConstraintOperatorType.LESS_THAN; | ||
return this; | ||
case GREATER_OR_EQUAL: | ||
this.type = RulebookConstraintOperatorType.LESS_OR_EQUAL; | ||
return this; | ||
case LESS_THAN: | ||
this.type = RulebookConstraintOperatorType.GREATER_THAN; | ||
return this; | ||
case LESS_OR_EQUAL: | ||
this.type = RulebookConstraintOperatorType.GREATER_OR_EQUAL; | ||
return this; | ||
default: | ||
return this; | ||
} | ||
} | ||
|
||
public boolean isComparison() { | ||
return isAscending() || isDescending(); | ||
} | ||
|
||
public boolean isAscending() { | ||
return this.type == RulebookConstraintOperatorType.GREATER_THAN || this.type == RulebookConstraintOperatorType.GREATER_OR_EQUAL; | ||
} | ||
|
||
public boolean isDescending() { | ||
return this.type == RulebookConstraintOperatorType.LESS_THAN || this.type == RulebookConstraintOperatorType.LESS_OR_EQUAL; | ||
} | ||
|
||
private class ConditionContext { | ||
|
||
private String ruleSet; | ||
private String ruleName; | ||
private String conditionExpression; | ||
|
||
public ConditionContext(String ruleSet, String ruleName, String conditionExpression) { | ||
this.ruleSet = ruleSet; | ||
this.ruleName = ruleName; | ||
this.conditionExpression = conditionExpression; | ||
} | ||
|
||
public String getRuleSet() { | ||
return ruleSet; | ||
} | ||
|
||
public String getRuleName() { | ||
return ruleName; | ||
} | ||
|
||
public String getConditionExpression() { | ||
return conditionExpression; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I introduced
RulebookConstraintOperator
instead ofIndex.ConstraintType
.RulebookConstraintOperator
is not enum/singleton, because each instance holdsCondtionContext
which is used for error logging.Problem: This doesn't create alpha index, because
PrototypeDSL
requiresIndex.ConstraintType
, but we cannot extendsIndex.ConstraintType
because it's enum.https://github.com/apache/incubator-kie-drools/blob/main/drools-model/drools-model-prototype/src/main/java/org/drools/model/prototype/PrototypeDSL.java#L139
Indeed,
AlphaTest
fails.Other concerns:
CondtionContext
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a quite hard problem and I believe that this is something that we cannot fix in a reasonable way (and without losing features that for sure we don't want to lose, like indexing and node sharing) without also changing the Drools code base. In essence I believe that this will require also some changes in Drools, that's practically unavoidable, so let's do it.
Regarding the way on how to proceed to fix this, the main problem now is that for those indexed constraint the existing enumeration implies a tight coupling in Drools between the kind of the constraint (this is an equal, so we have some sort of index based on that fact that it is an equal) and the comparison function using to evaluate that constraint. As a first thing we need in some way to be able to avoid this coupling (at least in the prototypes part) and have a way to say "this is an equal, so it performs as an equal for indexing and node sharing matters, but the I want to evaluate it with this custom predicate that I'm providing".
Please let me know if my suggestion is clear or you have any remarks or see any other showstopper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the suggestion. Thinking...
CompositeObjectSinkAdapter.propagateAssertObject
.