Search in sources :

Example 1 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, InternalWorkingMemory wm, 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, wm, 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 2 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 = new KnowledgePackageImpl("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();
    MockLeftTupleSink sink = new MockLeftTupleSink();
    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.compiler.reteoo.MockLeftTupleSink) Cheese(org.drools.compiler.Cheese) EvalCondition(org.drools.core.rule.EvalCondition) EvalDescr(org.drools.compiler.lang.descr.EvalDescr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Pattern(org.drools.core.rule.Pattern) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) MVELEvalExpression(org.drools.core.base.mvel.MVELEvalExpression) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) HashMap(java.util.HashMap) Map(java.util.Map) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 3 with EvalCondition

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

the class KnowledgeBuilderTest method testEvalMethodCompare.

@Test
public void testEvalMethodCompare() {
    final KnowledgeBuilderImpl builder1 = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr1 = new PackageDescr("package1");
    createEvalRule(packageDescr1, "1==1");
    builder1.addPackage(packageDescr1);
    final EvalCondition eval1 = (EvalCondition) ((RuleImpl) builder1.getPackage("package1").getRules().iterator().next()).getLhs().getChildren().get(0);
    final KnowledgeBuilderImpl builder2 = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr2 = new PackageDescr("package2");
    createEvalRule(packageDescr2, "1==1");
    builder2.addPackage(packageDescr2);
    final EvalCondition eval2 = (EvalCondition) ((RuleImpl) builder2.getPackage("package2").getRules().iterator().next()).getLhs().getChildren().get(0);
    final KnowledgeBuilderImpl builder3 = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr3 = new PackageDescr("package3");
    createEvalRule(packageDescr3, "1==3");
    builder3.addPackage(packageDescr3);
    final EvalCondition eval3 = (EvalCondition) ((RuleImpl) builder3.getPackage("package3").getRules().iterator().next()).getLhs().getChildren().get(0);
    assertEquals(eval1, eval2);
    assertFalse(eval1.equals(eval3));
    assertFalse(eval2.equals(eval3));
}
Also used : PackageDescr(org.drools.compiler.lang.descr.PackageDescr) EvalCondition(org.drools.core.rule.EvalCondition) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 4 with EvalCondition

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

the class EvalBuilder method build.

/**
 * @inheritDoc
 */
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    final EvalCondition eval = (EvalCondition) rce;
    context.pushRuleComponent(rce);
    context.setTupleSource(utils.attachNode(context, CoreComponentFactory.get().getNodeFactoryService().buildEvalNode(context.getNextNodeId(), context.getTupleSource(), eval, context)));
    context.popRuleComponent();
}
Also used : EvalCondition(org.drools.core.rule.EvalCondition)

Example 5 with EvalCondition

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

the class KiePackagesBuilder method buildConditionalConsequence.

private ConditionalBranch buildConditionalConsequence(RuleContext ctx, ConditionalNamedConsequenceImpl consequence) {
    // This makes the assumption the variable is for a pattern (not field) binding, and this binding must already exist.
    // It's used for the implicit bindings
    // The main issue with all of this, is it doesn't allow access to other previous variables. And it should work with
    // with pattern bindings and field bindings. What if getVariables() returns an array.length > 1?
    EvalCondition evalCondition;
    if (consequence.getExpr() != null) {
        Pattern pattern = ctx.getPattern(consequence.getExpr().getVariables()[0]);
        if (pattern.getDeclaration() != null) {
            // pattern has a root binding, so use that.
            EvalExpression eval = new LambdaEvalExpression(pattern, consequence.getExpr());
            evalCondition = new EvalCondition(eval, new Declaration[] { pattern.getDeclaration() });
        } else {
            // Pattern does not have root binding, so use field bindings.
            Declaration[] declarations = pattern.getDeclarations().values().toArray(new Declaration[pattern.getDeclarations().size()]);
            EvalExpression eval = new LambdaEvalExpression(declarations, consequence.getExpr());
            evalCondition = new EvalCondition(eval, declarations);
        }
    } else {
        evalCondition = new EvalCondition(LambdaEvalExpression.EMPTY, null);
    }
    return new ConditionalBranch(evalCondition, new NamedConsequence(consequence.getThenConsequence().getName(), consequence.getThenConsequence().isBreaking()), consequence.getElseBranch() != null ? buildConditionalConsequence(ctx, consequence.getElseBranch()) : null);
}
Also used : LambdaEvalExpression(org.drools.modelcompiler.constraints.LambdaEvalExpression) EvalExpression(org.drools.core.spi.EvalExpression) QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) GroupByPattern(org.drools.model.GroupByPattern) ConditionalBranch(org.drools.core.rule.ConditionalBranch) LambdaEvalExpression(org.drools.modelcompiler.constraints.LambdaEvalExpression) Declaration(org.drools.core.rule.Declaration) WindowDeclaration(org.drools.core.rule.WindowDeclaration) TypeDeclarationUtil.createTypeDeclaration(org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) EvalCondition(org.drools.core.rule.EvalCondition) NamedConsequence(org.drools.core.rule.NamedConsequence)

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