Search in sources :

Example 1 with Forall

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

the class ForallBuilder method build.

public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    final ForallDescr forallDescr = (ForallDescr) descr;
    final PatternBuilder patternBuilder = (PatternBuilder) context.getDialect().getBuilder(PatternDescr.class);
    final Pattern basePattern = (Pattern) patternBuilder.build(context, forallDescr.getBasePattern());
    if (basePattern == null) {
        return null;
    }
    final Forall forall = new Forall(basePattern);
    // adding the newly created forall CE to the build stack
    // this is necessary in case of local declaration usage
    context.getDeclarationResolver().pushOnBuildStack(forall);
    for (BaseDescr baseDescr : forallDescr.getRemainingPatterns()) {
        final Pattern anotherPattern = (Pattern) patternBuilder.build(context, (PatternDescr) baseDescr);
        forall.addRemainingPattern(anotherPattern);
    }
    if (forallDescr.getDescrs().size() == 1) {
        // An optimization for unlinking, where we allow unlinking if the resulting 'not' node has no constraints
        // we need to record this here, due to getRemainingPatterns injecting "this == " + BASE_IDENTIFIER $__forallBaseIdentifier
        // which we wish to ignore
        PatternDescr p = (PatternDescr) forallDescr.getDescrs().get(0);
        if (p.getConstraint().getDescrs().isEmpty()) {
            forall.setEmptyBetaConstraints(true);
        }
    }
    // poping the forall
    context.getDeclarationResolver().popBuildStack();
    return forall;
}
Also used : Pattern(org.drools.core.rule.Pattern) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) Forall(org.drools.core.rule.Forall) ForallDescr(org.drools.compiler.lang.descr.ForallDescr)

Example 2 with Forall

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

the class ForallBuilder method build.

/**
 * @inheritDoc
 */
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    final Forall forall = (Forall) rce;
    context.pushRuleComponent(forall);
    // forall can be translated into
    // not( basePattern and not( <remaining_patterns>+ ) )
    // so we just do that:
    final GroupElement and = GroupElementFactory.newAndInstance();
    and.addChild(forall.getBasePattern());
    final GroupElement not2 = GroupElementFactory.newNotInstance();
    if (forall.getRemainingPatterns().size() == 1) {
        if (forall.isEmptyBetaConstraints()) {
            // The reason why this is here is because forall can inject a
            // "this == " + BASE_IDENTIFIER $__forallBaseIdentifier
            // Which we don't want to actually count in the case of forall node linking
            context.setEmptyForAllBetaConstraints();
        }
        not2.addChild(forall.getRemainingPatterns().get(0));
        and.addChild(not2);
    } else if (forall.getRemainingPatterns().size() > 1) {
        final GroupElement and2 = GroupElementFactory.newAndInstance();
        for (Pattern pattern : forall.getRemainingPatterns()) {
            and2.addChild(pattern);
        }
        not2.addChild(and2);
        and.addChild(not2);
    }
    final GroupElement not = GroupElementFactory.newNotInstance();
    not.addChild(and);
    // get builder for the CEs
    final ReteooComponentBuilder builder = utils.getBuilderFor(not);
    // builds the CEs
    builder.build(context, utils, not);
    context.popRuleComponent();
}
Also used : Pattern(org.drools.core.rule.Pattern) GroupElement(org.drools.core.rule.GroupElement) Forall(org.drools.core.rule.Forall)

Aggregations

Forall (org.drools.core.rule.Forall)2 Pattern (org.drools.core.rule.Pattern)2 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)1 ForallDescr (org.drools.compiler.lang.descr.ForallDescr)1 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)1 GroupElement (org.drools.core.rule.GroupElement)1