Search in sources :

Example 66 with EventFactHandle

use of org.drools.core.common.EventFactHandle in project drools by kiegroup.

the class WindowNode method modifyRightTuple.

@Override
public void modifyRightTuple(RightTuple rightTuple, PropagationContext context, ReteEvaluator reteEvaluator) {
    EventFactHandle originalFactHandle = (EventFactHandle) rightTuple.getFactHandle();
    EventFactHandle cloneFactHandle = (EventFactHandle) rightTuple.getContextObject();
    // make sure all fields are updated
    originalFactHandle.quickCloneUpdate(cloneFactHandle);
    // behavior modify
    boolean isAllowed = true;
    for (AlphaNodeFieldConstraint constraint : constraints) {
        if (!constraint.isAllowed(cloneFactHandle, reteEvaluator)) {
            isAllowed = false;
            break;
        }
    }
    if (isAllowed) {
        ModifyPreviousTuples modifyPreviousTuples = new ModifyPreviousTuples(cloneFactHandle.detachLinkedTuples());
        this.sink.propagateModifyObject(cloneFactHandle, modifyPreviousTuples, context, reteEvaluator);
        modifyPreviousTuples.retractTuples(context, reteEvaluator);
    } else {
        ObjectTypeNode.doRetractObject(cloneFactHandle, context, reteEvaluator);
    }
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) EventFactHandle(org.drools.core.common.EventFactHandle)

Example 67 with EventFactHandle

use of org.drools.core.common.EventFactHandle in project drools by kiegroup.

the class SlidingLengthWindow method assertFact.

/**
 * @inheritDoc
 */
public boolean assertFact(final Object context, final InternalFactHandle handle, final PropagationContext pctx, final ReteEvaluator reteEvaluator) {
    SlidingLengthWindowContext window = (SlidingLengthWindowContext) context;
    window.pos = (window.pos + 1) % window.handles.length;
    if (window.handles[window.pos] != null) {
        final EventFactHandle previous = window.handles[window.pos];
        // retract previous
        final PropagationContext expiresPctx = createPropagationContextForFact(reteEvaluator, previous, PropagationContext.Type.EXPIRATION);
        ObjectTypeNode.doRetractObject(previous, expiresPctx, reteEvaluator);
    }
    window.handles[window.pos] = (EventFactHandle) handle;
    return true;
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) EventFactHandle(org.drools.core.common.EventFactHandle)

Example 68 with EventFactHandle

use of org.drools.core.common.EventFactHandle in project drools by kiegroup.

the class SlidingTimeWindow method retractFact.

@Override
public void retractFact(final Object context, final InternalFactHandle fact, final PropagationContext pctx, final ReteEvaluator reteEvaluator) {
    final SlidingTimeWindowContext queue = (SlidingTimeWindowContext) context;
    final EventFactHandle handle = (EventFactHandle) fact;
    // it may be a call back to expire the tuple that is already being expired
    if (!handle.equals(queue.getExpiringHandle())) {
        if (handle.equals(queue.peek())) {
            // it was the head of the queue
            queue.poll();
            // update next expiration time
            updateNextExpiration(queue.peek(), reteEvaluator, queue, nodeId);
        } else {
            queue.remove(handle);
        }
        if (queue.isEmpty() && queue.getJobHandle() != null) {
            reteEvaluator.getTimerService().removeJob(queue.getJobHandle());
        }
    }
}
Also used : EventFactHandle(org.drools.core.common.EventFactHandle)

Example 69 with EventFactHandle

use of org.drools.core.common.EventFactHandle in project drools by kiegroup.

the class EvaluatorWrapper method loadHandles.

public void loadHandles(InternalFactHandle[] handles, InternalFactHandle rightHandle) {
    InternalFactHandle localLeftHandle = null;
    InternalFactHandle localRightHandle = null;
    if (!selfLeft && handles != null) {
        localLeftHandle = getFactHandle(leftBinding, handles);
    }
    if (selfRight) {
        localRightHandle = rightHandle;
    } else if (handles != null) {
        localRightHandle = getFactHandle(rightBinding, handles);
    }
    // @FIXME else? what happens now (mdp) ? Maybe this can never happen?
    this.rightLiteral = localRightHandle == null;
    if (isTemporal()) {
        if (localLeftHandle == null) {
            localLeftHandle = rightHandle;
        }
        leftTimestamp = localLeftHandle instanceof EventFactHandle ? ((EventFactHandle) localLeftHandle).getStartTimestamp() : null;
        rightTimestamp = localRightHandle instanceof EventFactHandle ? ((EventFactHandle) localRightHandle).getStartTimestamp() : null;
    }
}
Also used : EventFactHandle(org.drools.core.common.EventFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 70 with EventFactHandle

use of org.drools.core.common.EventFactHandle in project drools by kiegroup.

the class RuleExecutor method fireActivation.

public void fireActivation(ReteEvaluator reteEvaluator, ActivationsManager activationsManager, 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
    reteEvaluator.startOperation();
    try {
        BeforeMatchFiredEvent beforeMatchFiredEvent = activationsManager.getAgendaEventSupport().fireBeforeActivationFired(activation, reteEvaluator);
        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);
            activationsManager.clearAndCancelActivationGroup(activationGroup);
        }
        activation.setQueued(false);
        try {
            innerFireActivation(reteEvaluator, activationsManager, activation, activation.getConsequence());
        } finally {
            // if the tuple contains expired events
            for (Tuple tuple = activation.getTuple().skipEmptyHandles(); tuple != null; tuple = tuple.getParent()) {
                if (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() && handle.getFirstRightTuple() == null && handle.getActivationsCount() <= 0) {
                        // and if no more activations, retract the handle
                        handle.getEntryPoint(reteEvaluator).delete(handle);
                    }
                }
            }
        }
        activationsManager.getAgendaEventSupport().fireAfterActivationFired(activation, reteEvaluator, beforeMatchFiredEvent);
    } finally {
        reteEvaluator.endOperation();
    }
}
Also used : BeforeMatchFiredEvent(org.kie.api.event.rule.BeforeMatchFiredEvent) EventFactHandle(org.drools.core.common.EventFactHandle) InternalActivationGroup(org.drools.core.spi.InternalActivationGroup) Tuple(org.drools.core.spi.Tuple) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple)

Aggregations

EventFactHandle (org.drools.core.common.EventFactHandle)86 Test (org.junit.Test)51 KieSession (org.kie.api.runtime.KieSession)23 KieBase (org.kie.api.KieBase)18 InternalFactHandle (org.drools.core.common.InternalFactHandle)16 DisconnectedWorkingMemoryEntryPoint (org.drools.core.common.DisconnectedWorkingMemoryEntryPoint)13 DisconnectedWorkingMemoryEntryPoint (org.drools.kiesession.entrypoints.DisconnectedWorkingMemoryEntryPoint)13 FactHandle (org.kie.api.runtime.rule.FactHandle)11 DuringEvaluatorDefinition (org.drools.core.base.evaluators.DuringEvaluatorDefinition)10 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)10 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)8 ArrayList (java.util.ArrayList)6 List (java.util.List)6 QueryElementFactHandle (org.drools.core.common.QueryElementFactHandle)6 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)5 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)5 PropagationContext (org.drools.core.spi.PropagationContext)5 StockTick (org.drools.testcoverage.common.model.StockTick)5 EntryPointId (org.drools.core.rule.EntryPointId)4 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)4