Search in sources :

Example 6 with InternalActivationGroup

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

the class DefaultAgenda method addItemToActivationGroup.

/**
 * If the item belongs to an activation group, add it
 *
 * @param item
 */
public void addItemToActivationGroup(final AgendaItem item) {
    if (item.isRuleAgendaItem()) {
        throw new UnsupportedOperationException("defensive programming, making sure this isn't called, before removing");
    }
    String group = item.getRule().getActivationGroup();
    if (group != null && group.length() > 0) {
        InternalActivationGroup actgroup = getActivationGroup(group);
        // Don't allow lazy activations to activate, from before it's last trigger point
        if (actgroup.getTriggeredForRecency() != 0 && actgroup.getTriggeredForRecency() >= item.getPropagationContext().getFactHandle().getRecency()) {
            return;
        }
        actgroup.addActivation(item);
    }
}
Also used : InternalActivationGroup(org.drools.core.spi.InternalActivationGroup)

Example 7 with InternalActivationGroup

use of org.drools.core.spi.InternalActivationGroup 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 8 with InternalActivationGroup

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

the class ActivationsManagerImpl method addItemToActivationGroup.

@Override
public void addItemToActivationGroup(AgendaItem activation) {
    if (activation.isRuleAgendaItem()) {
        throw new UnsupportedOperationException("defensive programming, making sure this isn't called, before removing");
    }
    String group = activation.getRule().getActivationGroup();
    if (!StringUtils.isEmpty(group)) {
        InternalActivationGroup actgroup = this.activationGroups.computeIfAbsent(group, k -> new ActivationGroupImpl(this, k));
        // Don't allow lazy activations to activate, from before it's last trigger point
        if (actgroup.getTriggeredForRecency() != 0 && actgroup.getTriggeredForRecency() >= activation.getPropagationContext().getFactHandle().getRecency()) {
            return;
        }
        actgroup.addActivation(activation);
    }
}
Also used : ActivationGroupImpl(org.drools.core.common.ActivationGroupImpl) InternalActivationGroup(org.drools.core.spi.InternalActivationGroup)

Example 9 with InternalActivationGroup

use of org.drools.core.spi.InternalActivationGroup 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

InternalActivationGroup (org.drools.core.spi.InternalActivationGroup)9 EventFactHandle (org.drools.core.common.EventFactHandle)2 RuleTerminalNodeLeftTuple (org.drools.core.reteoo.RuleTerminalNodeLeftTuple)2 Tuple (org.drools.core.spi.Tuple)2 ActivationGroupImpl (org.drools.core.common.ActivationGroupImpl)1 BeforeMatchFiredEvent (org.kie.api.event.rule.BeforeMatchFiredEvent)1