use of org.drools.core.rule.PredicateConstraint in project drools by kiegroup.
the class JavaDialectTest method testEvalDetectionInBetaNode.
@Test
public void testEvalDetectionInBetaNode() {
// Tests evals are generated and executed with Java dialect
String drl = "";
drl += "package org.drools.compiler.test\n";
drl += "import org.drools.compiler.Person\n";
drl += "global java.util.List list\n";
drl += "rule test1\n";
drl += "when\n";
drl += " $s : String()\n";
drl += " $p1 : Person( eval( name \n != $s ), name == ( new String($s+\"xxx\") ) )\n";
drl += "then\n";
drl += "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drl)), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
assertFalse(kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
node = n;
break;
}
}
BetaNode betaanode = (BetaNode) node.getObjectSinkPropagator().getSinks()[0];
BetaNodeFieldConstraint[] constraint = betaanode.getConstraints();
PredicateConstraint c = (PredicateConstraint) constraint[0];
assertTrue(c.getPredicateExpression() instanceof PredicateExpression);
assertTrue(c.getPredicateExpression() instanceof CompiledInvoker);
}
use of org.drools.core.rule.PredicateConstraint in project drools by kiegroup.
the class JavaDialectTest method testEvalDetectionInAlphaNode.
@Test
public void testEvalDetectionInAlphaNode() {
// Tests evals are generated and executed with Java dialect
String drl = "";
drl += "package org.drools.mvel.compiler.test\n";
drl += "import " + Person.class.getCanonicalName() + "\n";
drl += "global java.util.List list\n";
drl += "rule test1\n";
drl += "when\n";
drl += " $p1 : Person( eval( name \n != null ), name == ( new String(\"xxx\") ) )\n";
drl += "then\n";
drl += "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
List<ObjectTypeNode> nodes = ((RuleBase) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
node = n;
break;
}
}
AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
PredicateConstraint c = (PredicateConstraint) alphanode.getConstraint();
assertTrue(c.getPredicateExpression() instanceof PredicateExpression);
assertTrue(c.getPredicateExpression() instanceof CompiledInvoker);
alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
KieSession ksession = kbase.newKieSession();
ksession.insert(new Person("xxx"));
int fired = ksession.fireAllRules();
assertEquals(1, fired);
}
use of org.drools.core.rule.PredicateConstraint in project drools by kiegroup.
the class JavaDialectBinaryEqualityTest method test1.
@Test
public void test1() {
KiePackage pkg1 = getKnowledgePackage1();
KiePackage pkg2 = getKnowledgePackage1();
KiePackage pkg3 = getKnowledgePackage2();
RuleImpl rule1 = ((InternalKnowledgePackage) pkg1).getRule("rule1");
RuleImpl rule2 = ((InternalKnowledgePackage) pkg2).getRule("rule1");
RuleImpl rule3 = ((InternalKnowledgePackage) pkg3).getRule("rule1");
// test return value
Pattern p1 = (Pattern) rule1.getLhs().getChildren().get(0);
Constraint rvc1 = p1.getConstraints().get(0);
Pattern p2 = (Pattern) rule2.getLhs().getChildren().get(0);
Constraint rvc2 = p2.getConstraints().get(0);
assertNotSame(rvc1, rvc2);
assertEquals(rvc1, rvc2);
Pattern p3 = (Pattern) rule3.getLhs().getChildren().get(0);
Constraint rvc3 = p3.getConstraints().get(0);
assertNotSame(rvc1, rvc3);
assertThat(rvc1, not(equalTo(rvc3)));
// test inline eval
PredicateConstraint pc1 = getPredicateConstraint(p1);
PredicateExpression pe1 = (PredicateExpression) pc1.getPredicateExpression();
PredicateConstraint pc2 = getPredicateConstraint(p2);
PredicateExpression pe2 = (PredicateExpression) pc2.getPredicateExpression();
assertNotSame(pe1, pe2);
assertEquals(pe1, pe2);
PredicateConstraint pc3 = getPredicateConstraint(p3);
PredicateExpression pe3 = (PredicateExpression) pc3.getPredicateExpression();
assertNotSame(pe1, pe3);
assertThat(pe1, not(equalTo(pe3)));
// test eval
EvalCondition ec1 = (EvalCondition) rule1.getLhs().getChildren().get(1);
EvalExpression ee1 = (EvalExpression) ec1.getEvalExpression();
EvalCondition ec2 = (EvalCondition) rule2.getLhs().getChildren().get(1);
EvalExpression ee2 = (EvalExpression) ec2.getEvalExpression();
assertNotSame(ee1, ee2);
assertEquals(ee1, ee2);
EvalCondition ec3 = (EvalCondition) rule3.getLhs().getChildren().get(1);
EvalExpression ee3 = (EvalExpression) ec3.getEvalExpression();
assertNotSame(ee1, ee3);
assertThat(ee1, not(equalTo(ee3)));
// test consequence
assertNotSame(rule1.getConsequence(), rule2.getConsequence());
assertEquals(rule1.getConsequence(), rule2.getConsequence());
assertNotSame(rule1.getConsequence(), rule3.getConsequence());
assertThat(rule1.getConsequence(), not(equalTo(rule3.getConsequence())));
// check LHS equals
assertNotSame(rule1.getLhs(), rule2.getLhs());
assertEquals(rule1.getLhs(), rule2.getLhs());
assertNotSame(rule1.getLhs(), rule3.getLhs());
assertThat(rule1.getLhs(), not(equalTo(rule3.getLhs())));
}
use of org.drools.core.rule.PredicateConstraint in project drools by kiegroup.
the class CompositeObjectSinkAdapterTest method testAddOneAlphaNotHashableRemoveOneAlpha.
@Test
public void testAddOneAlphaNotHashableRemoveOneAlpha() {
final AlphaNode al = createAlphaNode(new PredicateConstraint(null, null));
ad.addObjectSink(al);
ad.removeObjectSink(al);
sinksAreEmpty();
otherSinksAreEmpty();
}
use of org.drools.core.rule.PredicateConstraint in project drools by kiegroup.
the class KnowledgeBuilderTest method testPredicateMethodCompare.
@Test
public void testPredicateMethodCompare() {
final KnowledgeBuilderImpl builder1 = new KnowledgeBuilderImpl();
final PackageDescr packageDescr1 = new PackageDescr("package1");
createPredicateRule(packageDescr1, "eval(x==y)");
builder1.addPackage(packageDescr1);
if (builder1.hasErrors()) {
fail(builder1.getErrors().toString());
}
final Pattern pattern1 = (Pattern) ((RuleImpl) builder1.getPackage("package1").getRules().iterator().next()).getLhs().getChildren().get(0);
final PredicateConstraint predicate1 = (PredicateConstraint) pattern1.getConstraints().get(0);
final KnowledgeBuilderImpl builder2 = new KnowledgeBuilderImpl();
final PackageDescr packageDescr2 = new PackageDescr("package2");
createPredicateRule(packageDescr2, "eval(x==y)");
builder2.addPackage(packageDescr2);
if (builder2.hasErrors()) {
fail(builder2.getErrors().toString());
}
final Pattern pattern2 = (Pattern) ((RuleImpl) builder2.getPackage("package2").getRules().iterator().next()).getLhs().getChildren().get(0);
final PredicateConstraint predicate2 = (PredicateConstraint) pattern2.getConstraints().get(0);
final KnowledgeBuilderImpl builder3 = new KnowledgeBuilderImpl();
if (builder3.hasErrors()) {
fail(builder3.getErrors().toString());
}
final PackageDescr packageDescr3 = new PackageDescr("package3");
createPredicateRule(packageDescr3, "eval(x!=y)");
builder3.addPackage(packageDescr3);
final Pattern pattern3 = (Pattern) ((RuleImpl) builder3.getPackage("package3").getRules().iterator().next()).getLhs().getChildren().get(0);
final PredicateConstraint predicate3 = (PredicateConstraint) pattern3.getConstraints().get(0);
assertEquals(predicate1, predicate2);
assertFalse(predicate1.equals(predicate3));
assertFalse(predicate2.equals(predicate3));
}
Aggregations