001: /*
002: * Copyright 2006 JBoss Inc
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.drools.reteoo.builder;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.drools.common.BetaConstraints;
024: import org.drools.common.TupleStartEqualsConstraint;
025: import org.drools.reteoo.AccumulateNode;
026: import org.drools.reteoo.ObjectSource;
027: import org.drools.reteoo.RightInputAdapterNode;
028: import org.drools.reteoo.TupleSource;
029: import org.drools.rule.Accumulate;
030: import org.drools.rule.Pattern;
031: import org.drools.rule.Declaration;
032: import org.drools.rule.LiteralConstraint;
033: import org.drools.rule.RuleConditionElement;
034: import org.drools.spi.AlphaNodeFieldConstraint;
035:
036: /**
037: * @author etirelli
038: *
039: */
040: public class AccumulateBuilder implements ReteooComponentBuilder {
041:
042: /**
043: * @inheritDoc
044: */
045: public void build(final BuildContext context,
046: final BuildUtils utils, final RuleConditionElement rce) {
047: final Accumulate accumulate = (Accumulate) rce;
048: boolean existSubNetwort = false;
049:
050: final List resultBetaConstraints = context.getBetaconstraints();
051: final List resultAlphaConstraints = context
052: .getAlphaConstraints();
053:
054: final Pattern sourcePattern = accumulate.getSourcePattern();
055:
056: // get builder for the pattern
057: final ReteooComponentBuilder builder = utils
058: .getBuilderFor(sourcePattern);
059:
060: // save tuple source and current pattern offset for later if needed
061: final TupleSource tupleSource = context.getTupleSource();
062: final int currentPatternIndex = context
063: .getCurrentPatternOffset();
064:
065: // builds the source pattern
066: builder.build(context, utils, sourcePattern);
067:
068: // if object source is null, then we need to adapt tuple source into a subnetwork
069: if (context.getObjectSource() == null) {
070:
071: // attach right input adapter node to convert tuple source into an object source
072: context.setObjectSource((ObjectSource) utils.attachNode(
073: context, new RightInputAdapterNode(context
074: .getNextId(), context.getTupleSource())));
075:
076: // restore tuple source from before the start of the sub network
077: context.setTupleSource(tupleSource);
078:
079: // create a tuple start equals constraint and set it in the context
080: final TupleStartEqualsConstraint constraint = TupleStartEqualsConstraint
081: .getInstance();
082: final List betaConstraints = new ArrayList();
083: betaConstraints.add(constraint);
084: context.setBetaconstraints(betaConstraints);
085: existSubNetwort = true;
086: }
087:
088: final BetaConstraints resultsBinder = utils
089: .createBetaNodeConstraint(context,
090: resultBetaConstraints, false);
091: final BetaConstraints sourceBinder = utils
092: .createBetaNodeConstraint(context, context
093: .getBetaconstraints(), false);
094:
095: context
096: .setTupleSource((TupleSource) utils
097: .attachNode(
098: context,
099: new AccumulateNode(
100: context.getNextId(),
101: context.getTupleSource(),
102: context.getObjectSource(),
103: (AlphaNodeFieldConstraint[]) resultAlphaConstraints
104: .toArray(new AlphaNodeFieldConstraint[resultAlphaConstraints
105: .size()]),
106: sourceBinder, resultsBinder,
107: accumulate, existSubNetwort)));
108: // source pattern was bound, so nulling context
109: context.setObjectSource(null);
110: context.setCurrentPatternOffset(currentPatternIndex);
111: }
112:
113: /**
114: * @inheritDoc
115: */
116: public boolean requiresLeftActivation(final BuildUtils utils,
117: final RuleConditionElement rce) {
118: return true;
119: }
120:
121: }
|