-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Enhance ConstraintOperator capability #6104
Merged
tkobayas
merged 3 commits into
apache:main
from
tkobayas:DROOLS-7635-error-incompatible-type-02
Oct 1, 2024
Merged
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
254 changes: 254 additions & 0 deletions
254
...odegen/src/test/java/org/drools/model/codegen/execmodel/CustomConstraintOperatorTest.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,254 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.drools.model.codegen.execmodel; | ||
|
||
import java.util.function.BiPredicate; | ||
|
||
import org.drools.base.common.NetworkNode; | ||
import org.drools.base.prototype.PrototypeObjectType; | ||
import org.drools.base.rule.IndexableConstraint; | ||
import org.drools.core.reteoo.AlphaNode; | ||
import org.drools.core.reteoo.BetaNode; | ||
import org.drools.core.reteoo.EntryPointNode; | ||
import org.drools.core.reteoo.ObjectTypeNode; | ||
import org.drools.kiesession.rulebase.InternalKnowledgeBase; | ||
import org.drools.model.ConstraintOperator; | ||
import org.drools.model.Index; | ||
import org.drools.model.Model; | ||
import org.drools.model.Rule; | ||
import org.drools.model.codegen.execmodel.domain.Result; | ||
import org.drools.model.impl.ModelImpl; | ||
import org.drools.model.prototype.PrototypeVariable; | ||
import org.drools.modelcompiler.KieBaseBuilder; | ||
import org.drools.modelcompiler.constraints.LambdaConstraint; | ||
import org.junit.Test; | ||
import org.kie.api.KieBase; | ||
import org.kie.api.prototype.PrototypeFact; | ||
import org.kie.api.prototype.PrototypeFactInstance; | ||
import org.kie.api.runtime.KieSession; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.drools.model.DSL.on; | ||
import static org.drools.model.PatternDSL.rule; | ||
import static org.drools.model.prototype.PrototypeDSL.protoPattern; | ||
import static org.drools.model.prototype.PrototypeDSL.variable; | ||
import static org.drools.model.prototype.PrototypeExpression.fixedValue; | ||
import static org.drools.model.prototype.PrototypeExpression.prototypeField; | ||
import static org.kie.api.prototype.PrototypeBuilder.prototype; | ||
|
||
public class CustomConstraintOperatorTest { | ||
|
||
static class CustomConstraintOperator implements ConstraintOperator { | ||
|
||
public int counter = 0; | ||
|
||
@Override | ||
public <T, V> BiPredicate<T, V> asPredicate() { | ||
return (t, v) -> { | ||
counter++; | ||
return t.equals(v); | ||
}; | ||
} | ||
|
||
@Override | ||
public boolean hasIndex() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Index.ConstraintType getIndexType() { | ||
return Index.ConstraintType.EQUAL; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Index.ConstraintType.EQUAL.toString(); | ||
} | ||
} | ||
|
||
@Test | ||
public void alphaIndexIneffective() { | ||
CustomConstraintOperator customConstraintOperator = new CustomConstraintOperator(); | ||
|
||
PrototypeFact testPrototype = prototype("test").asFact(); | ||
PrototypeVariable testV = variable(testPrototype); | ||
|
||
Rule rule1 = rule("alpha1") | ||
.build( | ||
protoPattern(testV) | ||
.expr(prototypeField("fieldA"), customConstraintOperator, fixedValue(1)), | ||
on(testV).execute((drools, x) -> | ||
drools.insert(new Result("Found")) | ||
) | ||
); | ||
|
||
Model model = new ModelImpl().addRule(rule1); | ||
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model); | ||
KieSession ksession = kieBase.newKieSession(); | ||
|
||
PrototypeFactInstance testFact = testPrototype.newInstance(); | ||
testFact.put("fieldA", 1); | ||
|
||
ksession.insert(testFact); | ||
assertThat(ksession.fireAllRules()).isEqualTo(1); | ||
|
||
// Index is created, but actual alpha index hashing works only with more than 3 nodes | ||
Index index = getFirstAlphaNodeIndex((InternalKnowledgeBase) kieBase, testPrototype); | ||
assertThat(index.getIndexType()).isEqualTo(Index.IndexType.ALPHA); | ||
assertThat(index.getConstraintType()).isEqualTo(Index.ConstraintType.EQUAL); | ||
|
||
// alpha index hashing is not effective, so the predicated is called | ||
assertThat(customConstraintOperator.counter).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
public void alphaIndexEffective() { | ||
CustomConstraintOperator customConstraintOperator = new CustomConstraintOperator(); | ||
|
||
PrototypeFact testPrototype = prototype("test").asFact(); | ||
PrototypeVariable testV = variable(testPrototype); | ||
|
||
Rule rule1 = rule("alpha1") | ||
.build( | ||
protoPattern(testV) | ||
.expr(prototypeField("fieldA"), customConstraintOperator, fixedValue(1)), | ||
on(testV).execute((drools, x) -> | ||
drools.insert(new Result("Found")) | ||
) | ||
); | ||
Rule rule2 = rule("alpha2") | ||
.build( | ||
protoPattern(testV) | ||
.expr(prototypeField("fieldA"), customConstraintOperator, fixedValue(2)), | ||
on(testV).execute((drools, x) -> | ||
drools.insert(new Result("Found")) | ||
) | ||
); | ||
Rule rule3 = rule("alpha3") | ||
.build( | ||
protoPattern(testV) | ||
.expr(prototypeField("fieldA"), customConstraintOperator, fixedValue(3)), | ||
on(testV).execute((drools, x) -> | ||
drools.insert(new Result("Found")) | ||
) | ||
); | ||
|
||
Model model = new ModelImpl().addRule(rule1).addRule(rule2).addRule(rule3); | ||
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model); | ||
KieSession ksession = kieBase.newKieSession(); | ||
|
||
PrototypeFactInstance testFact = testPrototype.newInstance(); | ||
testFact.put("fieldA", 1); | ||
|
||
ksession.insert(testFact); | ||
assertThat(ksession.fireAllRules()).isEqualTo(1); | ||
|
||
Index index = getFirstAlphaNodeIndex((InternalKnowledgeBase) kieBase, testPrototype); | ||
assertThat(index.getIndexType()).isEqualTo(Index.IndexType.ALPHA); | ||
assertThat(index.getConstraintType()).isEqualTo(Index.ConstraintType.EQUAL); | ||
|
||
// alpha index hashing is effective, so the predicated is not called | ||
assertThat(customConstraintOperator.counter).isZero(); | ||
} | ||
|
||
private static Index getFirstAlphaNodeIndex(InternalKnowledgeBase kieBase, PrototypeFact testPrototype) { | ||
EntryPointNode epn = kieBase.getRete().getEntryPointNodes().values().iterator().next(); | ||
ObjectTypeNode otn = epn.getObjectTypeNodes().get(new PrototypeObjectType(testPrototype)); | ||
AlphaNode alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0]; | ||
IndexableConstraint constraint = (IndexableConstraint) alphaNode.getConstraint(); | ||
return ((LambdaConstraint) constraint).getEvaluator().getIndex(); | ||
} | ||
|
||
@Test | ||
public void betaIndex() { | ||
CustomConstraintOperator customConstraintOperator = new CustomConstraintOperator(); | ||
|
||
Result result = new Result(); | ||
|
||
PrototypeFact personFact = prototype("org.drools.Person").withField("name").withField("age").asFact(); | ||
|
||
PrototypeVariable markV = variable(personFact); | ||
PrototypeVariable ageMateV = variable(personFact); | ||
|
||
Rule rule = rule("beta") | ||
.build( | ||
protoPattern(markV) | ||
.expr("name", Index.ConstraintType.EQUAL, "Mark"), | ||
protoPattern(ageMateV) | ||
.expr("name", Index.ConstraintType.NOT_EQUAL, "Mark") | ||
.expr("age", customConstraintOperator, markV, "age"), | ||
on(ageMateV, markV).execute((p1, p2) -> result.setValue(p1.get("name") + " is the same age as " + p2.get("name"))) | ||
); | ||
|
||
Model model = new ModelImpl().addRule(rule); | ||
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model); | ||
|
||
KieSession ksession = kieBase.newKieSession(); | ||
|
||
PrototypeFactInstance mark = personFact.newInstance(); | ||
mark.put("name", "Mark"); | ||
mark.put("age", 37); | ||
|
||
PrototypeFactInstance john = personFact.newInstance(); | ||
john.put("name", "John"); | ||
john.put("age", 39); | ||
|
||
PrototypeFactInstance paul = personFact.newInstance(); | ||
paul.put("name", "Paul"); | ||
paul.put("age", 37); | ||
|
||
ksession.insert(mark); | ||
ksession.insert(john); | ||
ksession.insert(paul); | ||
|
||
ksession.fireAllRules(); | ||
assertThat(result.getValue()).isEqualTo("Paul is the same age as Mark"); | ||
|
||
Index index = getFirstBetaNodeIndex((InternalKnowledgeBase) kieBase, personFact); | ||
assertThat(index.getIndexType()).isEqualTo(Index.IndexType.BETA); | ||
assertThat(index.getConstraintType()).isEqualTo(Index.ConstraintType.EQUAL); | ||
|
||
// When beta index is used, the predicate in the custom operator is not actually called | ||
assertThat(customConstraintOperator.counter).isZero(); | ||
Comment on lines
+227
to
+228
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If beta index is effective, the predicate in the |
||
} | ||
|
||
private static Index getFirstBetaNodeIndex(InternalKnowledgeBase kieBase, PrototypeFact testPrototype) { | ||
EntryPointNode epn = kieBase.getRete().getEntryPointNodes().values().iterator().next(); | ||
ObjectTypeNode otn = epn.getObjectTypeNodes().get(new PrototypeObjectType(testPrototype)); | ||
NetworkNode[] sinks = otn.getObjectSinkPropagator().getSinks(); | ||
BetaNode betaNode = findBetaNode(sinks); | ||
|
||
IndexableConstraint constraint = (IndexableConstraint) betaNode.getConstraints()[0]; | ||
return ((LambdaConstraint) constraint).getEvaluator().getIndex(); | ||
} | ||
|
||
private static BetaNode findBetaNode(NetworkNode[] sinks) { | ||
for (NetworkNode sink : sinks) { | ||
if (sink instanceof BetaNode) { | ||
return (BetaNode) sink; | ||
} else { | ||
BetaNode betaNode = findBetaNode(sink.getSinks()); | ||
if (betaNode != null) { | ||
return betaNode; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} |
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.
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.
If alpha index is effective, the predicate in the
CustomConstraintOperator
is not executed.