Search in sources :

Example 1 with ConditionalBranch

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

the class ConditionalBranchBuilder method build.

public void build(BuildContext context, BuildUtils utils, RuleConditionElement rce) {
    ConditionalBranch conditionalBranch = (ConditionalBranch) rce;
    ConditionalBranchEvaluator branchEvaluator = buildConditionalBranchEvaluator(context, conditionalBranch);
    context.pushRuleComponent(rce);
    ConditionalBranchNode node = CoreComponentFactory.get().getNodeFactoryService().buildConditionalBranchNode(context.getNextNodeId(), context.getTupleSource(), branchEvaluator, context);
    context.setTupleSource(utils.attachNode(context, node));
    context.popRuleComponent();
}
Also used : ConditionalBranchEvaluator(org.drools.core.reteoo.ConditionalBranchEvaluator) ConditionalBranch(org.drools.core.rule.ConditionalBranch) ConditionalBranchNode(org.drools.core.reteoo.ConditionalBranchNode)

Example 2 with ConditionalBranch

use of org.drools.core.rule.ConditionalBranch 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)

Example 3 with ConditionalBranch

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

the class ConditionalBranchBuilder method build.

public ConditionalBranch build(RuleBuildContext context, BaseDescr descr, Pattern prefixPattern) {
    ConditionalBranchDescr conditionalBranch = (ConditionalBranchDescr) descr;
    String consequenceName = conditionalBranch.getConsequence().getName();
    if (!context.getRuleDescr().getNamedConsequences().containsKey(consequenceName)) {
        DroolsError err = new RuleBuildError(context.getRule(), context.getParentDescr(), null, "Unknown consequence name: " + consequenceName);
        context.addError(err);
        return null;
    }
    RuleConditionBuilder evalBuilder = (RuleConditionBuilder) context.getDialect().getBuilder(EvalDescr.class);
    EvalCondition condition = (EvalCondition) evalBuilder.build(context, conditionalBranch.getCondition(), getLastPattern(context));
    NamedConsequenceBuilder namedConsequenceBuilder = (NamedConsequenceBuilder) context.getDialect().getBuilder(NamedConsequenceDescr.class);
    NamedConsequence consequence = namedConsequenceBuilder.build(context, conditionalBranch.getConsequence());
    ConditionalBranchDescr elseBranchDescr = conditionalBranch.getElseBranch();
    return new ConditionalBranch(condition, consequence, elseBranchDescr != null ? build(context, elseBranchDescr, prefixPattern) : null);
}
Also used : DroolsError(org.drools.drl.parser.DroolsError) NamedConsequenceDescr(org.drools.drl.ast.descr.NamedConsequenceDescr) ConditionalBranchDescr(org.drools.drl.ast.descr.ConditionalBranchDescr) RuleBuildError(org.drools.compiler.compiler.RuleBuildError) EvalDescr(org.drools.drl.ast.descr.EvalDescr) ConditionalBranch(org.drools.core.rule.ConditionalBranch) EvalCondition(org.drools.core.rule.EvalCondition) NamedConsequence(org.drools.core.rule.NamedConsequence)

Aggregations

ConditionalBranch (org.drools.core.rule.ConditionalBranch)3 EvalCondition (org.drools.core.rule.EvalCondition)2 NamedConsequence (org.drools.core.rule.NamedConsequence)2 RuleBuildError (org.drools.compiler.compiler.RuleBuildError)1 ConditionalBranchEvaluator (org.drools.core.reteoo.ConditionalBranchEvaluator)1 ConditionalBranchNode (org.drools.core.reteoo.ConditionalBranchNode)1 Declaration (org.drools.core.rule.Declaration)1 Pattern (org.drools.core.rule.Pattern)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 WindowDeclaration (org.drools.core.rule.WindowDeclaration)1 EvalExpression (org.drools.core.spi.EvalExpression)1 ConditionalBranchDescr (org.drools.drl.ast.descr.ConditionalBranchDescr)1 EvalDescr (org.drools.drl.ast.descr.EvalDescr)1 NamedConsequenceDescr (org.drools.drl.ast.descr.NamedConsequenceDescr)1 DroolsError (org.drools.drl.parser.DroolsError)1 AccumulatePattern (org.drools.model.AccumulatePattern)1 GroupByPattern (org.drools.model.GroupByPattern)1 QueryCallPattern (org.drools.model.patterns.QueryCallPattern)1 LambdaEvalExpression (org.drools.modelcompiler.constraints.LambdaEvalExpression)1 TypeDeclarationUtil.createTypeDeclaration (org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration)1