Search in sources :

Example 46 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class TupleIndexRBTree method iterator.

public Iterator<Tuple> iterator() {
    TupleList list = tree.first();
    Tuple firstTuple = list != null ? list.getFirst() : null;
    return new FastIterator.IteratorAdapter(fastIterator(), firstTuple);
}
Also used : Tuple(org.drools.core.spi.Tuple)

Example 47 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class ReactiveObjectUtil method notifyModification.

public static void notifyModification(Object object, Collection<Tuple> leftTuples, ModificationType type) {
    for (Tuple leftTuple : leftTuples) {
        if (!((ReactiveFromNodeLeftTuple) leftTuple).updateModificationState(object, type)) {
            continue;
        }
        PropagationContext propagationContext = leftTuple.getPropagationContext();
        ReactiveFromNode node = (ReactiveFromNode) leftTuple.getTupleSink();
        LeftTupleSinkNode sink = node.getSinkPropagator().getFirstLeftTupleSink();
        InternalWorkingMemory wm = getInternalWorkingMemory(propagationContext);
        wm.addPropagation(new ReactivePropagation(object, (ReactiveFromNodeLeftTuple) leftTuple, propagationContext, node, sink, type));
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) PropagationContext(org.drools.core.spi.PropagationContext) ReactiveFromNode(org.drools.core.reteoo.ReactiveFromNode) LeftTupleSinkNode(org.drools.core.reteoo.LeftTupleSinkNode) ReactiveFromNodeLeftTuple(org.drools.core.reteoo.ReactiveFromNodeLeftTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakFromNode.deleteChildLeftTuple(org.drools.core.phreak.PhreakFromNode.deleteChildLeftTuple) Tuple(org.drools.core.spi.Tuple) ReactiveFromNodeLeftTuple(org.drools.core.reteoo.ReactiveFromNodeLeftTuple)

Example 48 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class RuleExecutor method getNextTuple.

private Tuple getNextTuple() {
    if (tupleList.isEmpty()) {
        return null;
    }
    Tuple leftTuple;
    if (queue != null) {
        leftTuple = (Tuple) queue.dequeue();
        tupleList.remove(leftTuple);
    } else {
        leftTuple = tupleList.removeFirst();
        ((Activation) leftTuple).setQueued(false);
    }
    return leftTuple;
}
Also used : Activation(org.drools.core.spi.Activation) Tuple(org.drools.core.spi.Tuple) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple)

Example 49 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class RuleExecutor method fireActivation.

public void fireActivation(InternalWorkingMemory wm, InternalAgenda agenda, Activation activation) throws ConsequenceException {
    // We do this first as if a node modifies a fact that causes a recursion
    // on an empty pattern
    // we need to make sure it re-activates
    wm.startOperation();
    try {
        wm.getAgendaEventSupport().fireBeforeActivationFired(activation, wm);
        if (activation.getActivationGroupNode() != null) {
            // We know that this rule will cancel all other activations in the group
            // so lets remove the information now, before the consequence fires
            final InternalActivationGroup activationGroup = activation.getActivationGroupNode().getActivationGroup();
            activationGroup.removeActivation(activation);
            agenda.clearAndCancelActivationGroup(activationGroup);
        }
        activation.setQueued(false);
        try {
            innerFireActivation(wm, agenda, activation, activation.getConsequence());
        } finally {
            // if the tuple contains expired events
            for (Tuple tuple = activation.getTuple(); tuple != null; tuple = tuple.getParent()) {
                if (tuple.getFactHandle() != null && tuple.getFactHandle().isEvent()) {
                    // can be null for eval, not and exists that have no right input
                    EventFactHandle handle = (EventFactHandle) tuple.getFactHandle();
                    // decrease the activation count for the event
                    handle.decreaseActivationsCount();
                    // handles "expire" only in stream mode.
                    if (handle.expirePartition() && handle.isExpired()) {
                        if (handle.getActivationsCount() <= 0) {
                            // and if no more activations, retract the handle
                            handle.getEntryPoint().delete(handle);
                        }
                    }
                }
            }
        }
        wm.getAgendaEventSupport().fireAfterActivationFired(activation, wm);
    } finally {
        wm.endOperation();
    }
}
Also used : EventFactHandle(org.drools.core.common.EventFactHandle) InternalActivationGroup(org.drools.core.spi.InternalActivationGroup) Tuple(org.drools.core.spi.Tuple) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple)

Example 50 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class ConsequenceGenerator method generate.

public static void generate(final ConsequenceStub stub, KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) {
    RuleTerminalNode rtn = (RuleTerminalNode) knowledgeHelper.getMatch().getTuple().getTupleSink();
    final Declaration[] declarations = rtn.getRequiredDeclarations();
    final Tuple tuple = knowledgeHelper.getTuple();
    // Sort declarations based on their offset, so it can ascend the tuple's parents stack only once
    final List<DeclarationMatcher> declarationMatchers = matchDeclarationsToTuple(declarations);
    final ClassGenerator generator = createInvokerClassGenerator(stub, workingMemory).setInterfaces(Consequence.class, CompiledInvoker.class);
    generator.addMethod(ACC_PUBLIC, "getName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            push(stub.getGeneratedInvokerClassName());
            mv.visitInsn(ARETURN);
        }
    }).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(null, KnowledgeHelper.class, WorkingMemory.class), new String[] { "java/lang/Exception" }, new GeneratorHelper.EvaluateMethod() {

        public void body(MethodVisitor mv) {
            // Tuple tuple = knowledgeHelper.getTuple();
            mv.visitVarInsn(ALOAD, 1);
            invokeInterface(KnowledgeHelper.class, "getTuple", Tuple.class);
            cast(LeftTuple.class);
            // LeftTuple
            mv.visitVarInsn(ASTORE, 3);
            // Declaration[] declarations = ((RuleTerminalNode)knowledgeHelper.getMatch().getTuple().getTupleSink()).getDeclarations();
            mv.visitVarInsn(ALOAD, 1);
            invokeInterface(KnowledgeHelper.class, "getMatch", Activation.class);
            invokeInterface(Activation.class, "getTuple", Tuple.class);
            invokeInterface(Tuple.class, "getTupleSink", Sink.class);
            cast(RuleTerminalNode.class);
            invokeVirtual(RuleTerminalNode.class, "getRequiredDeclarations", Declaration[].class);
            mv.visitVarInsn(ASTORE, 4);
            Tuple currentTuple = tuple;
            // astore start position for objects to store in loop
            objAstorePos = 6;
            int[] paramsPos = new int[declarations.length];
            // declarationMatchers is already sorted by offset with tip declarations now first
            for (DeclarationMatcher matcher : declarationMatchers) {
                // original index refers to the array position with RuleTerminalNode.getDeclarations()
                int i = matcher.getOriginalIndex();
                int handlePos = objAstorePos;
                int objPos = ++objAstorePos;
                paramsPos[i] = handlePos;
                currentTuple = traverseTuplesUntilDeclaration(currentTuple, matcher.getRootDistance(), 3);
                // handle = tuple.getFactHandle()
                mv.visitVarInsn(ALOAD, 3);
                invokeInterface(Tuple.class, "getOriginalFactHandle", InternalFactHandle.class);
                mv.visitVarInsn(ASTORE, handlePos);
                String declarationType = declarations[i].getTypeName();
                if (stub.getNotPatterns()[i]) {
                    // notPattern indexes field declarations
                    // declarations[i].getValue((InternalWorkingMemory)workingMemory, fact[i].getObject());
                    // org.kie.rule.Declaration[]
                    mv.visitVarInsn(ALOAD, 4);
                    // i
                    push(i);
                    // declarations[i]
                    mv.visitInsn(AALOAD);
                    // WorkingMemory
                    mv.visitVarInsn(ALOAD, 2);
                    cast(InternalWorkingMemory.class);
                    // handle[i]
                    mv.visitVarInsn(ALOAD, handlePos);
                    invokeInterface(InternalFactHandle.class, "getObject", Object.class);
                    storeObjectFromDeclaration(declarations[i], declarationType);
                    // The facthandle should be set to that of the field, if it's an object, otherwise this will return null
                    // fact[i] = (InternalFactHandle)workingMemory.getFactHandle(obj);
                    mv.visitVarInsn(ALOAD, 2);
                    loadAsObject(objPos);
                    invokeInterface(WorkingMemory.class, "getFactHandle", FactHandle.class, Object.class);
                    cast(InternalFactHandle.class);
                    mv.visitVarInsn(ASTORE, handlePos);
                } else {
                    // handle[i]
                    mv.visitVarInsn(ALOAD, handlePos);
                    invokeInterface(InternalFactHandle.class, "getObject", Object.class);
                    mv.visitTypeInsn(CHECKCAST, internalName(declarationType));
                    // obj[i]
                    objAstorePos += store(objPos, declarationType);
                }
            }
            // @{ruleClassName}.@{methodName}(KnowledgeHelper, @foreach{declr : declarations} Object, FactHandle @end)
            StringBuilder consequenceMethodDescr = new StringBuilder("(L" + KnowledgeHelper.class.getName().replace('.', '/') + ";");
            // KnowledgeHelper
            mv.visitVarInsn(ALOAD, 1);
            for (int i = 0; i < declarations.length; i++) {
                // obj[i]
                load(paramsPos[i] + 1);
                // handle[i]
                mv.visitVarInsn(ALOAD, paramsPos[i]);
                consequenceMethodDescr.append(typeDescr(declarations[i].getTypeName())).append("L" + FactHandle.class.getName().replace('.', '/') + ";");
            }
            // @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
            parseGlobals(stub.getGlobals(), stub.getGlobalTypes(), 2, consequenceMethodDescr);
            consequenceMethodDescr.append(")V");
            mv.visitMethodInsn(INVOKESTATIC, stub.getInternalRuleClassName(), stub.getMethodName(), consequenceMethodDescr.toString());
            mv.visitInsn(RETURN);
        }
    });
    stub.setConsequence(generator.<Consequence>newInstance());
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) WorkingMemory(org.drools.core.WorkingMemory) Activation(org.drools.core.spi.Activation) DeclarationMatcher(org.drools.core.rule.builder.dialect.asm.GeneratorHelper.DeclarationMatcher) LeftTuple(org.drools.core.reteoo.LeftTuple) MethodVisitor(org.mvel2.asm.MethodVisitor) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) GeneratorHelper.createInvokerClassGenerator(org.drools.core.rule.builder.dialect.asm.GeneratorHelper.createInvokerClassGenerator) Sink(org.drools.core.reteoo.Sink) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) GeneratorHelper.matchDeclarationsToTuple(org.drools.core.rule.builder.dialect.asm.GeneratorHelper.matchDeclarationsToTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) Tuple(org.drools.core.spi.Tuple) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

Aggregations

Tuple (org.drools.core.spi.Tuple)54 InternalFactHandle (org.drools.core.common.InternalFactHandle)17 LeftTuple (org.drools.core.reteoo.LeftTuple)16 RightTuple (org.drools.core.reteoo.RightTuple)14 FastIterator (org.drools.core.util.FastIterator)14 Declaration (org.drools.core.rule.Declaration)9 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 WorkingMemory (org.drools.core.WorkingMemory)7 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)7 Cheese (org.drools.core.test.model.Cheese)7 Test (org.junit.Test)7 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)6 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)6 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)5 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)5 BetaMemory (org.drools.core.reteoo.BetaMemory)5 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)5 TupleMemory (org.drools.core.reteoo.TupleMemory)5 MethodVisitor (org.mvel2.asm.MethodVisitor)5 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)4