Search in sources :

Example 1 with PredicateExpression

use of org.drools.core.spi.PredicateExpression 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 PredicateExpression

use of org.drools.core.spi.PredicateExpression 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 PredicateExpression

use of org.drools.core.spi.PredicateExpression 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 PredicateExpression

use of org.drools.core.spi.PredicateExpression in project drools by kiegroup.

the class FieldConstraintTest method testPredicateConstraint.

/**
 * <pre>
 *
 *                (Cheese (price ?price1 )
 *                (Cheese (price ?price2&amp;:(= ?price2 (* 2 ?price1) )
 *
 * </pre>
 */
@Test
public void testPredicateConstraint() {
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final InternalReadAccessor priceExtractor = store.getReader(Cheese.class, "price");
    Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
    // Bind the extractor to a decleration
    // Declarations know the pattern they derive their value form
    final Declaration price1Declaration = new Declaration("price1", priceExtractor, pattern);
    pattern = new Pattern(1, new ClassObjectType(Cheese.class));
    // Bind the extractor to a decleration
    // Declarations know the pattern they derive their value form
    final Declaration price2Declaration = new Declaration("price2", priceExtractor, pattern);
    final PredicateExpression evaluator = new PredicateExpression() {

        private static final long serialVersionUID = 510l;

        public boolean evaluate(InternalFactHandle handle, Tuple tuple, Declaration[] previousDeclarations, Declaration[] localDeclarations, WorkingMemory workingMemory, Object context) {
            int price1 = previousDeclarations[0].getIntValue((InternalWorkingMemory) workingMemory, tuple.getObject(previousDeclarations[0]));
            int price2 = localDeclarations[0].getIntValue((InternalWorkingMemory) workingMemory, handle.getObject());
            return (price2 == (price1 * 2));
        }

        public Object createContext() {
            return null;
        }

        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }

        public void writeExternal(ObjectOutput out) throws IOException {
        }
    };
    final PredicateConstraint constraint1 = new PredicateConstraint(evaluator, new Declaration[] { price1Declaration }, new Declaration[] { price2Declaration });
    final Cheese cheddar0 = new Cheese("cheddar", 5);
    final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar0);
    LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
    final Cheese cheddar1 = new Cheese("cheddar", 10);
    final InternalFactHandle f1 = (InternalFactHandle) ksession.insert(cheddar1);
    tuple = new LeftTupleImpl(tuple, new RightTupleImpl(f1, null), null, true);
    final PredicateContextEntry context = (PredicateContextEntry) constraint1.createContextEntry();
    context.updateFromTuple(ksession, tuple);
    assertTrue(constraint1.isAllowedCachedLeft(context, f1));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Cheese(org.drools.core.test.model.Cheese) PredicateExpression(org.drools.core.spi.PredicateExpression) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) PredicateContextEntry(org.drools.core.rule.PredicateConstraint.PredicateContextEntry) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) ObjectInput(java.io.ObjectInput) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 5 with PredicateExpression

use of org.drools.core.spi.PredicateExpression 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.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 += "   $p1 : Person( eval( name \n != null ), name == ( new String(\"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;
        }
    }
    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();
    if (constraint instanceof MvelConstraint) {
        FieldValue fieldVal = ((MvelConstraint) constraint).getField();
        assertEquals("xxx", fieldVal.getValue());
    }
}
Also used : KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) PredicateExpression(org.drools.core.spi.PredicateExpression) AlphaNode(org.drools.core.reteoo.AlphaNode) CompiledInvoker(org.drools.core.spi.CompiledInvoker) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) StringReader(java.io.StringReader) PredicateConstraint(org.drools.core.rule.PredicateConstraint) FieldValue(org.drools.core.spi.FieldValue) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

PredicateExpression (org.drools.core.spi.PredicateExpression)8 Test (org.junit.Test)8 PredicateConstraint (org.drools.core.rule.PredicateConstraint)7 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 CompiledInvoker (org.drools.core.spi.CompiledInvoker)4 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 Pattern (org.drools.core.rule.Pattern)3 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)3 BetaNodeFieldConstraint (org.drools.core.spi.BetaNodeFieldConstraint)3 ObjectInput (java.io.ObjectInput)2 ObjectOutput (java.io.ObjectOutput)2 StringReader (java.io.StringReader)2 ClassObjectType (org.drools.core.base.ClassObjectType)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 AlphaNode (org.drools.core.reteoo.AlphaNode)2 BetaNode (org.drools.core.reteoo.BetaNode)2 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)2 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)2