Search in sources :

Example 16 with EvalCondition

use of org.drools.core.rule.EvalCondition in project drools by kiegroup.

the class AbstractASMEvalBuilder method buildEval.

private RuleConditionElement buildEval(RuleBuildContext context, EvalDescr evalDescr, AnalysisResult analysis, Declaration[] declarations) {
    String className = "eval" + context.getNextId();
    evalDescr.setClassMethodName(className);
    Arrays.sort(declarations, RuleTerminalNode.SortDeclarations.instance);
    EvalCondition eval = new EvalCondition(declarations);
    Map<String, Object> vars = createVariableContext(className, (String) evalDescr.getContent(), context, declarations, null, analysis.getBoundIdentifiers().getGlobals());
    generateMethodTemplate("evalMethod", context, vars);
    byte[] bytecode = createEvalBytecode(context, vars);
    registerInvokerBytecode(context, vars, bytecode, eval);
    return eval;
}
Also used : EvalCondition(org.drools.core.rule.EvalCondition)

Example 17 with EvalCondition

use of org.drools.core.rule.EvalCondition in project drools by kiegroup.

the class PhreakEvalNode method doLeftInserts.

public void doLeftInserts(EvalConditionNode evalNode, EvalMemory em, LeftTupleSink sink, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
    EvalCondition condition = evalNode.getCondition();
    for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        final boolean allowed = condition.isAllowed(leftTuple, wm, em.context);
        if (allowed) {
            boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(evalNode, leftTuple);
            trgLeftTuples.addInsert(sink.createLeftTuple(leftTuple, sink, leftTuple.getPropagationContext(), useLeftMemory));
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : LeftTuple(org.drools.core.reteoo.LeftTuple) EvalCondition(org.drools.core.rule.EvalCondition)

Example 18 with EvalCondition

use of org.drools.core.rule.EvalCondition 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 19 with EvalCondition

use of org.drools.core.rule.EvalCondition in project drools by kiegroup.

the class PhreakEvalNode method doLeftUpdates.

public void doLeftUpdates(EvalConditionNode evalNode, EvalMemory em, LeftTupleSink sink, ReteEvaluator reteEvaluator, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    EvalCondition condition = evalNode.getCondition();
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        boolean wasPropagated = leftTuple.getFirstChild() != null && leftTuple.getContextObject() != EVAL_LEFT_TUPLE_DELETED;
        boolean allowed = condition.isAllowed(leftTuple, reteEvaluator, em.context);
        if (allowed) {
            leftTuple.setContextObject(null);
            if (wasPropagated) {
                // update
                LeftTuple childLeftTuple = leftTuple.getFirstChild();
                childLeftTuple.setPropagationContext(leftTuple.getPropagationContext());
                normalizeStagedTuples(stagedLeftTuples, childLeftTuple);
                trgLeftTuples.addUpdate(childLeftTuple);
            } else {
                // assert
                trgLeftTuples.addInsert(sink.createLeftTuple(leftTuple, sink, leftTuple.getPropagationContext(), true));
            }
        } else {
            if (wasPropagated) {
                // retract
                leftTuple.setContextObject(EVAL_LEFT_TUPLE_DELETED);
                LeftTuple childLeftTuple = leftTuple.getFirstChild();
                childLeftTuple.setPropagationContext(leftTuple.getPropagationContext());
                RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
            }
        // else do nothing
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : LeftTuple(org.drools.core.reteoo.LeftTuple) EvalCondition(org.drools.core.rule.EvalCondition)

Example 20 with EvalCondition

use of org.drools.core.rule.EvalCondition in project drools by kiegroup.

the class MVELEvalBuilderTest method testSimpleExpression.

@Test
public void testSimpleExpression() {
    InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("pkg1");
    final RuleDescr ruleDescr = new RuleDescr("rule 1");
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
    final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
    DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
    final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
    final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
    final InternalReadAccessor extractor = store.getReader(Cheese.class, "price");
    final Pattern pattern = new Pattern(0, new ClassObjectType(int.class));
    final Declaration declaration = new Declaration("a", extractor, pattern);
    final Map map = new HashMap();
    map.put("a", declaration);
    declarationResolver.setDeclarations(map);
    context.setDeclarationResolver(declarationResolver);
    final EvalDescr evalDescr = new EvalDescr();
    evalDescr.setContent("a == 10");
    final MVELEvalBuilder builder = new MVELEvalBuilder();
    final EvalCondition eval = (EvalCondition) builder.build(context, evalDescr);
    ((MVELEvalExpression) eval.getEvalExpression()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
    org.drools.core.reteoo.MockLeftTupleSink sink = new MockLeftTupleSink(buildContext);
    MockTupleSource source = new MockTupleSource(1, buildContext);
    source.setObjectCount(1);
    sink.setLeftTupleSource(source);
    final Cheese cheddar = new Cheese("cheddar", 10);
    final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
    final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
    f0.removeLeftTuple(tuple);
    Object evalContext = eval.createContext();
    assertTrue(eval.isAllowed(tuple, ksession, evalContext));
    cheddar.setPrice(9);
    ksession.update(f0, cheddar);
    assertFalse(eval.isAllowed(tuple, ksession, evalContext));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) HashMap(java.util.HashMap) MockLeftTupleSink(org.drools.core.reteoo.MockLeftTupleSink) Cheese(org.drools.mvel.compiler.Cheese) EvalCondition(org.drools.core.rule.EvalCondition) MockTupleSource(org.drools.core.reteoo.MockTupleSource) EvalDescr(org.drools.drl.ast.descr.EvalDescr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) MVELEvalBuilder(org.drools.mvel.builder.MVELEvalBuilder) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Pattern(org.drools.core.rule.Pattern) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) MVELDialect(org.drools.mvel.builder.MVELDialect) MVELEvalExpression(org.drools.mvel.expr.MVELEvalExpression) MockLeftTupleSink(org.drools.core.reteoo.MockLeftTupleSink) BuildContext(org.drools.core.reteoo.builder.BuildContext) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) HashMap(java.util.HashMap) Map(java.util.Map) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

EvalCondition (org.drools.core.rule.EvalCondition)21 Test (org.junit.Test)9 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 Declaration (org.drools.core.rule.Declaration)6 Pattern (org.drools.core.rule.Pattern)5 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)4 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)4 LeftTuple (org.drools.core.reteoo.LeftTuple)4 EvalDescr (org.drools.compiler.lang.descr.EvalDescr)3 EvalExpression (org.drools.core.spi.EvalExpression)3 EvalDescr (org.drools.drl.ast.descr.EvalDescr)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Cheese (org.drools.compiler.Cheese)2 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)2 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)2 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)2 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)2 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)2