Search in sources :

Example 1 with ConditionalBranchDescr

use of org.drools.drl.ast.descr.ConditionalBranchDescr 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)

Example 2 with ConditionalBranchDescr

use of org.drools.drl.ast.descr.ConditionalBranchDescr in project drools by kiegroup.

the class KiePMMLDescrLhsFactoryTest method declareIfBreak.

@Test
public void declareIfBreak() {
    String ifBreakField = "TEMPERATURE";
    String ifBreakOperator = "<";
    Object ifBreakValue = 24;
    KiePMMLDescrLhsFactory.factory(lhsBuilder).declareIfBreak(ifBreakField, ifBreakOperator, ifBreakValue);
    assertNotNull(lhsBuilder.getDescr());
    final List<BaseDescr> descrs = lhsBuilder.getDescr().getDescrs();
    assertNotNull(descrs);
    assertEquals(2, descrs.size());
    assertTrue(descrs.get(0) instanceof PatternDescr);
    assertTrue(descrs.get(1) instanceof ConditionalBranchDescr);
    PatternDescr patternDescr = (PatternDescr) descrs.get(0);
    assertEquals(ifBreakField, patternDescr.getObjectType());
    assertEquals(INPUT_FIELD, patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    ConditionalBranchDescr conditionalBranchDescr = (ConditionalBranchDescr) descrs.get(1);
    String expectedCondition = String.format(INPUT_FIELD_CONDITIONAL, ifBreakOperator, ifBreakValue);
    assertEquals(expectedCondition, conditionalBranchDescr.getCondition().getContent());
    assertTrue(conditionalBranchDescr.getConsequence().isBreaking());
    assertEquals(BREAK_LABEL, conditionalBranchDescr.getConsequence().getText());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) ConditionalBranchDescr(org.drools.drl.ast.descr.ConditionalBranchDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) Test(org.junit.Test)

Aggregations

ConditionalBranchDescr (org.drools.drl.ast.descr.ConditionalBranchDescr)2 RuleBuildError (org.drools.compiler.compiler.RuleBuildError)1 ConditionalBranch (org.drools.core.rule.ConditionalBranch)1 EvalCondition (org.drools.core.rule.EvalCondition)1 NamedConsequence (org.drools.core.rule.NamedConsequence)1 AndDescr (org.drools.drl.ast.descr.AndDescr)1 BaseDescr (org.drools.drl.ast.descr.BaseDescr)1 EvalDescr (org.drools.drl.ast.descr.EvalDescr)1 NamedConsequenceDescr (org.drools.drl.ast.descr.NamedConsequenceDescr)1 PatternDescr (org.drools.drl.ast.descr.PatternDescr)1 DroolsError (org.drools.drl.parser.DroolsError)1 Test (org.junit.Test)1