Search in sources :

Example 1 with PredicateConstraint

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);
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) PredicateExpression(org.drools.core.spi.PredicateExpression) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) CompiledInvoker(org.drools.core.spi.CompiledInvoker) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StringReader(java.io.StringReader) PredicateConstraint(org.drools.core.rule.PredicateConstraint) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 2 with PredicateConstraint

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);
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieSession(org.kie.api.runtime.KieSession) PredicateConstraint(org.drools.core.rule.PredicateConstraint) PredicateExpression(org.drools.core.spi.PredicateExpression) Person(org.drools.mvel.compiler.Person) AlphaNode(org.drools.core.reteoo.AlphaNode) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) CompiledInvoker(org.drools.core.spi.CompiledInvoker) Test(org.junit.Test)

Example 3 with PredicateConstraint

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())));
}
Also used : EvalExpression(org.drools.core.spi.EvalExpression) Pattern(org.drools.core.rule.Pattern) KiePackage(org.kie.api.definition.KiePackage) Constraint(org.drools.core.spi.Constraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PredicateConstraint(org.drools.core.rule.PredicateConstraint) PredicateExpression(org.drools.core.spi.PredicateExpression) EvalCondition(org.drools.core.rule.EvalCondition) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 4 with PredicateConstraint

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();
}
Also used : PredicateConstraint(org.drools.core.rule.PredicateConstraint) AlphaNode(org.drools.core.reteoo.AlphaNode) Test(org.junit.Test)

Example 5 with PredicateConstraint

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));
}
Also used : Pattern(org.drools.core.rule.Pattern) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PredicateConstraint(org.drools.core.rule.PredicateConstraint) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Aggregations

PredicateConstraint (org.drools.core.rule.PredicateConstraint)13 Test (org.junit.Test)12 PredicateExpression (org.drools.core.spi.PredicateExpression)7 Pattern (org.drools.core.rule.Pattern)5 AlphaNode (org.drools.core.reteoo.AlphaNode)4 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 CompiledInvoker (org.drools.core.spi.CompiledInvoker)4 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)3 BetaNodeFieldConstraint (org.drools.core.spi.BetaNodeFieldConstraint)3 Constraint (org.drools.core.spi.Constraint)3 StringReader (java.io.StringReader)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 BetaNode (org.drools.core.reteoo.BetaNode)2 Declaration (org.drools.core.rule.Declaration)2 EvalCondition (org.drools.core.rule.EvalCondition)2 EvalExpression (org.drools.core.spi.EvalExpression)2 Person (org.drools.mvel.compiler.Person)2 ObjectInput (java.io.ObjectInput)1