Search in sources :

Example 1 with CompiledInvoker

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

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

the class KnowledgeBuilderTest method testEval.

@Test
public void testEval() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
    lhs.addDescr(pattern);
    BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
    pattern.addConstraint(fieldBindingDescr);
    fieldBindingDescr = new BindingDescr("y", "price");
    pattern.addConstraint(fieldBindingDescr);
    packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
    final EvalDescr evalDescr = new EvalDescr("( ( Integer )map.get( new Integer(x) ) ).intValue() == y");
    lhs.addDescr(evalDescr);
    ruleDescr.setConsequence("update(stilton);");
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
    final RuleImpl rule = pkg.getRule("rule-1");
    final EvalCondition eval = (EvalCondition) rule.getLhs().getChildren().get(1);
    final CompiledInvoker invoker = (CompiledInvoker) eval.getEvalExpression();
    final List list = invoker.getMethodBytecode();
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) Cheese(org.drools.compiler.Cheese) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) EvalCondition(org.drools.core.rule.EvalCondition) CompiledInvoker(org.drools.core.spi.CompiledInvoker) EvalDescr(org.drools.compiler.lang.descr.EvalDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) List(java.util.List) LinkedList(org.drools.core.util.LinkedList) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 3 with CompiledInvoker

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

the class MultiAccumulate method writeExternal.

public void writeExternal(ObjectOutput out) throws IOException {
    super.writeExternal(out);
    out.writeInt(accumulators.length);
    for (Accumulator acc : accumulators) {
        if (acc instanceof CompiledInvoker) {
            out.writeObject(null);
        } else {
            out.writeObject(acc);
        }
    }
}
Also used : Accumulator(org.drools.core.spi.Accumulator) CompiledInvoker(org.drools.core.spi.CompiledInvoker)

Example 4 with CompiledInvoker

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

CompiledInvoker (org.drools.core.spi.CompiledInvoker)4 Test (org.junit.Test)3 StringReader (java.io.StringReader)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)2 PredicateConstraint (org.drools.core.rule.PredicateConstraint)2 PredicateExpression (org.drools.core.spi.PredicateExpression)2 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)2 KnowledgeBuilderErrors (org.kie.internal.builder.KnowledgeBuilderErrors)2 List (java.util.List)1 Cheese (org.drools.compiler.Cheese)1 AndDescr (org.drools.compiler.lang.descr.AndDescr)1 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)1 EvalDescr (org.drools.compiler.lang.descr.EvalDescr)1 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)1 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)1 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)1 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1