Search in sources :

Example 1 with CollectAccumulator

use of org.drools.core.base.accumulators.CollectAccumulator in project drools by kiegroup.

the class CollectBuilder method build.

/**
 * @inheritDoc
 */
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    boolean existSubNetwort = false;
    final Collect collect = (Collect) rce;
    context.pushRuleComponent(collect);
    final List<BetaNodeFieldConstraint> resultBetaConstraints = context.getBetaconstraints();
    final List<AlphaNodeFieldConstraint> resultAlphaConstraints = context.getAlphaConstraints();
    final Pattern sourcePattern = collect.getSourcePattern();
    // get builder for the pattern
    final ReteooComponentBuilder builder = utils.getBuilderFor(sourcePattern);
    // save tuple source and pattern offset for later if needed
    final LeftTupleSource tupleSource = context.getTupleSource();
    // builds the source pattern
    builder.build(context, utils, sourcePattern);
    // if object source is null, then we need to adapt tuple source into a subnetwork
    if (context.getObjectSource() == null) {
        RightInputAdapterNode riaNode = CoreComponentFactory.get().getNodeFactoryService().buildRightInputNode(context.getNextNodeId(), context.getTupleSource(), tupleSource, context);
        // attach right input adapter node to convert tuple source into an object source
        context.setObjectSource(utils.attachNode(context, riaNode));
        // restore tuple source from before the start of the sub network
        context.setTupleSource(tupleSource);
        // create a tuple start equals constraint and set it in the context
        final TupleStartEqualsConstraint constraint = TupleStartEqualsConstraint.getInstance();
        final List<BetaNodeFieldConstraint> betaConstraints = new ArrayList<BetaNodeFieldConstraint>();
        betaConstraints.add(constraint);
        context.setBetaconstraints(betaConstraints);
        existSubNetwort = true;
    }
    BetaConstraints binder = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), false);
    // indexing for the results should be always disabled
    BetaConstraints resultBinder = utils.createBetaNodeConstraint(context, resultBetaConstraints, true);
    // The node needs to declare the vars it returns
    Pattern resultPattern = collect.getResultPattern();
    Accumulate accumulate = new SingleAccumulate(sourcePattern, sourcePattern.getRequiredDeclarations(), new CollectAccumulator(collect, existSubNetwort));
    AccumulateNode accNode = CoreComponentFactory.get().getNodeFactoryService().buildAccumulateNode(context.getNextNodeId(), context.getTupleSource(), context.getObjectSource(), resultAlphaConstraints.toArray(new AlphaNodeFieldConstraint[resultAlphaConstraints.size()]), // source binder
    binder, resultBinder, accumulate, context);
    context.setTupleSource(utils.attachNode(context, accNode));
    // source pattern was bound, so nulling context
    context.setObjectSource(null);
    context.popRuleComponent();
}
Also used : Pattern(org.drools.core.rule.Pattern) BetaConstraints(org.drools.core.common.BetaConstraints) Collect(org.drools.core.rule.Collect) AccumulateNode(org.drools.core.reteoo.AccumulateNode) ArrayList(java.util.ArrayList) TupleStartEqualsConstraint(org.drools.core.common.TupleStartEqualsConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) SingleAccumulate(org.drools.core.rule.SingleAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) CollectAccumulator(org.drools.core.base.accumulators.CollectAccumulator) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Aggregations

ArrayList (java.util.ArrayList)1 CollectAccumulator (org.drools.core.base.accumulators.CollectAccumulator)1 BetaConstraints (org.drools.core.common.BetaConstraints)1 TupleStartEqualsConstraint (org.drools.core.common.TupleStartEqualsConstraint)1 AccumulateNode (org.drools.core.reteoo.AccumulateNode)1 LeftTupleSource (org.drools.core.reteoo.LeftTupleSource)1 RightInputAdapterNode (org.drools.core.reteoo.RightInputAdapterNode)1 Accumulate (org.drools.core.rule.Accumulate)1 Collect (org.drools.core.rule.Collect)1 Pattern (org.drools.core.rule.Pattern)1 SingleAccumulate (org.drools.core.rule.SingleAccumulate)1 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)1 BetaNodeFieldConstraint (org.drools.core.spi.BetaNodeFieldConstraint)1