Search in sources :

Example 11 with Accumulate

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

the class PhreakAccumulateNode method doNode.

public void doNode(AccumulateNode accNode, LeftTupleSink sink, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    BetaMemory bm = am.getBetaMemory();
    TupleSets<RightTuple> srcRightTuples = bm.getStagedRightTuples().takeAll();
    // order of left and right operations is to minimise wasted of innefficient joins.
    // We need to collect which leftTuple where updated, so that we can
    // add their result tuple to the real target tuples later
    TupleSets<LeftTuple> tempLeftTuples = new TupleSetsImpl<LeftTuple>();
    if (srcLeftTuples.getDeleteFirst() != null) {
        // use the real target here, as dealing direct with left tuples
        doLeftDeletes(accNode, am, wm, srcLeftTuples, trgLeftTuples, stagedLeftTuples);
    }
    if (srcRightTuples.getDeleteFirst() != null) {
        doRightDeletes(accNode, am, wm, srcRightTuples, tempLeftTuples);
    }
    if (srcRightTuples.getUpdateFirst() != null) {
        RuleNetworkEvaluator.doUpdatesReorderRightMemory(bm, srcRightTuples);
        doRightUpdates(accNode, am, wm, srcRightTuples, tempLeftTuples);
    }
    if (srcLeftTuples.getUpdateFirst() != null) {
        RuleNetworkEvaluator.doUpdatesReorderLeftMemory(bm, srcLeftTuples);
        doLeftUpdates(accNode, am, wm, srcLeftTuples, tempLeftTuples);
    }
    if (srcRightTuples.getInsertFirst() != null) {
        doRightInserts(accNode, am, wm, srcRightTuples, tempLeftTuples);
    }
    if (srcLeftTuples.getInsertFirst() != null) {
        doLeftInserts(accNode, am, wm, srcLeftTuples, tempLeftTuples);
    }
    Accumulate accumulate = accNode.getAccumulate();
    // LeftTuple retracts are already on the trgLeftTuples
    for (LeftTuple leftTuple = tempLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        evaluateResultConstraints(accNode, sink, accumulate, leftTuple, leftTuple.getPropagationContext(), wm, am, (AccumulateContext) leftTuple.getContextObject(), trgLeftTuples, stagedLeftTuples);
        leftTuple.clearStaged();
        leftTuple = next;
    }
    for (LeftTuple leftTuple = tempLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        evaluateResultConstraints(accNode, sink, accumulate, leftTuple, leftTuple.getPropagationContext(), wm, am, (AccumulateContext) leftTuple.getContextObject(), trgLeftTuples, stagedLeftTuples);
        leftTuple.clearStaged();
        leftTuple = next;
    }
    srcRightTuples.resetAll();
    srcLeftTuples.resetAll();
}
Also used : TupleSetsImpl(org.drools.core.common.TupleSetsImpl) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) Accumulate(org.drools.core.rule.Accumulate)

Example 12 with Accumulate

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

the class PhreakAccumulateNode method doRightDeletes.

public void doRightDeletes(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples) {
    TupleMemory rtm = am.getBetaMemory().getRightTupleMemory();
    Accumulate accumulate = accNode.getAccumulate();
    for (RightTuple rightTuple = srcRightTuples.getDeleteFirst(); rightTuple != null; ) {
        RightTuple next = rightTuple.getStagedNext();
        if (rightTuple.getMemory() != null) {
            // it may have been staged and never actually added
            rtm.remove(rightTuple);
            if (rightTuple.getFirstChild() != null) {
                LeftTuple match = rightTuple.getFirstChild();
                while (match != null) {
                    LeftTuple nextLeft = match.getRightParentNext();
                    LeftTuple leftTuple = match.getLeftParent();
                    final AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
                    removeMatch(accNode, accumulate, rightTuple, match, wm, am, accctx, true);
                    if (leftTuple.getStagedType() == LeftTuple.NONE) {
                        trgLeftTuples.addUpdate(leftTuple);
                    }
                    match = nextLeft;
                }
            }
        }
        rightTuple.clearStaged();
        rightTuple = next;
    }
}
Also used : RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) Accumulate(org.drools.core.rule.Accumulate)

Example 13 with Accumulate

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

the class AccumulateBuilder method build.

/**
 * @inheritDoc
 */
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    final Accumulate accumulate = (Accumulate) rce;
    boolean existSubNetwort = false;
    context.pushRuleComponent(accumulate);
    final List<BetaNodeFieldConstraint> resultBetaConstraints = context.getBetaconstraints();
    final List<AlphaNodeFieldConstraint> resultAlphaConstraints = context.getAlphaConstraints();
    RuleConditionElement source = accumulate.getSource();
    if (source instanceof GroupElement) {
        GroupElement ge = (GroupElement) source;
        if (ge.isAnd() && ge.getChildren().size() == 1) {
            source = ge.getChildren().get(0);
        }
    }
    // get builder for the pattern
    final ReteooComponentBuilder builder = utils.getBuilderFor(source);
    // save tuple source and current pattern offset for later if needed
    LeftTupleSource tupleSource = context.getTupleSource();
    final int currentPatternIndex = context.getCurrentPatternOffset();
    // builds the source pattern
    builder.build(context, utils, source);
    // if object source is null, then we need to adapt tuple source into a subnetwork
    if (context.getObjectSource() == null) {
        // attach right input adapter node to convert tuple source into an object source
        RightInputAdapterNode riaNode = context.getComponentFactory().getNodeFactoryService().buildRightInputNode(context.getNextId(), 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;
    }
    NodeFactory nfactory = context.getComponentFactory().getNodeFactoryService();
    final BetaConstraints resultsBinder = utils.createBetaNodeConstraint(context, resultBetaConstraints, true);
    final BetaConstraints sourceBinder = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), false);
    AccumulateNode accNode = nfactory.buildAccumulateNode(context.getNextId(), context.getTupleSource(), context.getObjectSource(), resultAlphaConstraints.toArray(new AlphaNodeFieldConstraint[resultAlphaConstraints.size()]), sourceBinder, resultsBinder, accumulate, existSubNetwort, context);
    context.setTupleSource(utils.attachNode(context, accNode));
    // source pattern was bound, so nulling context
    context.setObjectSource(null);
    context.setCurrentPatternOffset(currentPatternIndex);
    context.popRuleComponent();
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) AccumulateNode(org.drools.core.reteoo.AccumulateNode) GroupElement(org.drools.core.rule.GroupElement) ArrayList(java.util.ArrayList) TupleStartEqualsConstraint(org.drools.core.common.TupleStartEqualsConstraint) RuleConditionElement(org.drools.core.rule.RuleConditionElement) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) TupleStartEqualsConstraint(org.drools.core.common.TupleStartEqualsConstraint) Accumulate(org.drools.core.rule.Accumulate) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Example 14 with Accumulate

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

the class MVELAccumulateBuilderTest method testSimpleExpression.

@Test
public void testSimpleExpression() {
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
    pkgBuilder.addPackage(new PackageDescr("pkg1"));
    InternalKnowledgePackage pkg = pkgBuilder.getPackage("pkg1");
    final RuleDescr ruleDescr = new RuleDescr("rule 1");
    final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
    DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
    final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
    final AccumulateDescr accDescr = new AccumulateDescr();
    final PatternDescr inputPattern = new PatternDescr("org.drools.compiler.Cheese", "$cheese");
    accDescr.setInputPattern(inputPattern);
    accDescr.setInitCode("total = 0;");
    accDescr.setActionCode("total += $cheese.price;");
    accDescr.setReverseCode("total -= $cheese.price;");
    accDescr.setResultCode("new Integer(total)");
    final MVELAccumulateBuilder builder = new MVELAccumulateBuilder();
    final Accumulate acc = (Accumulate) builder.build(context, accDescr);
    ((MVELCompileable) acc.getAccumulators()[0]).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    MockLeftTupleSink sink = new MockLeftTupleSink();
    final Cheese cheddar1 = new Cheese("cheddar", 10);
    final Cheese cheddar2 = new Cheese("cheddar", 8);
    final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(new InitialFactImpl());
    final InternalFactHandle f1 = (InternalFactHandle) ksession.insert(cheddar1);
    final InternalFactHandle f2 = (InternalFactHandle) ksession.insert(cheddar2);
    final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
    Object wmContext = acc.createWorkingMemoryContext();
    Object accContext = acc.createContext();
    acc.init(wmContext, accContext, tuple, ksession);
    acc.accumulate(wmContext, accContext, tuple, f1, ksession);
    acc.accumulate(wmContext, accContext, tuple, f2, ksession);
    assertEquals(new Integer(18), acc.getResult(wmContext, accContext, tuple, ksession));
    acc.reverse(wmContext, accContext, tuple, f1, ksession);
    assertEquals(new Integer(8), acc.getResult(wmContext, accContext, tuple, ksession));
}
Also used : MVELCompileable(org.drools.core.base.mvel.MVELCompileable) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) MockLeftTupleSink(org.drools.compiler.reteoo.MockLeftTupleSink) Cheese(org.drools.compiler.Cheese) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) InitialFactImpl(org.drools.core.reteoo.InitialFactImpl) Accumulate(org.drools.core.rule.Accumulate) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 15 with Accumulate

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

the class JavaAccumulateBuilderTest method testBuildRuleBuildContextBaseDescr.

@Test
public void testBuildRuleBuildContextBaseDescr() {
    // $total : Integer() from accumulate( Cheese( $price : price ) init( int x = 0; ) action( x += $price ) result( new Integer( x ) ) )
    AccumulateDescr accumDescr = new AccumulateDescr();
    BindingDescr price = new BindingDescr("$price", "price");
    PatternDescr cheeseDescr = new PatternDescr("org.drools.compiler.Cheese");
    cheeseDescr.addConstraint(price);
    accumDescr.setInputPattern(cheeseDescr);
    accumDescr.setInitCode("int x = 0; int y = 0;");
    accumDescr.setActionCode("x += $price;");
    accumDescr.setResultCode("new Integer( x )");
    // org.drools.core.rule.Package pkg = new org.kie.rule.Package( "org.kie" );
    final KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
    pkgBuilder.addPackage(new PackageDescr("org.drools"));
    final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
    PackageRegistry pkgReg = pkgBuilder.getPackageRegistry("org.drools");
    InternalKnowledgePackage pkg = pkgReg.getPackage();
    DialectCompiletimeRegistry dialectRegistry = pkgReg.getDialectCompiletimeRegistry();
    Dialect dialect = dialectRegistry.getDialect("java");
    RuleDescr ruleDescr = new RuleDescr("test rule");
    RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, dialect);
    Accumulate accumulate = (Accumulate) builder.build(context, accumDescr);
    String generatedCode = (String) context.getMethods().get(0);
    assertTrue(generatedCode.contains("private int x;"));
    assertTrue(generatedCode.contains("private int y;"));
    assertTrue(generatedCode.contains("x = 0;y = 0;"));
// System.out.println( context.getInvokers() );
// System.out.println( context.getMethods() );
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) Accumulate(org.drools.core.rule.Accumulate) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Dialect(org.drools.compiler.compiler.Dialect) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

Accumulate (org.drools.core.rule.Accumulate)15 LeftTuple (org.drools.core.reteoo.LeftTuple)7 BetaConstraints (org.drools.core.common.BetaConstraints)6 BetaMemory (org.drools.core.reteoo.BetaMemory)6 RightTuple (org.drools.core.reteoo.RightTuple)6 TupleMemory (org.drools.core.reteoo.TupleMemory)6 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)5 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)4 ContextEntry (org.drools.core.rule.ContextEntry)4 RuleConditionElement (org.drools.core.rule.RuleConditionElement)4 SingleAccumulate (org.drools.core.rule.SingleAccumulate)4 ArrayList (java.util.ArrayList)3 Declaration (org.drools.core.rule.Declaration)3 MultiAccumulate (org.drools.core.rule.MultiAccumulate)3 FastIterator (org.drools.core.util.FastIterator)3 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)2 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)2 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)2