Search in sources :

Example 1 with PropagationEntry

use of org.drools.core.phreak.PropagationEntry in project drools by kiegroup.

the class DefaultAgenda method fireLoop.

private int fireLoop(AgendaFilter agendaFilter, int fireLimit, RestHandler restHandler, boolean isInternalFire) {
    int fireCount = 0;
    try {
        PropagationEntry head = propagationList.takeAll();
        int returnedFireCount;
        // -1 or > 0 will return false. No reason for user to give 0, just handled for completeness.
        boolean limitReached = fireLimit == 0;
        // 
        while (isFiring()) {
            if (head != null) {
                // it is possible that there are no action propagations, but there are rules to fire.
                propagationList.flush(head);
                head = null;
            }
            // which changes the isFiring state. So a second isFiring guard is needed
            if (!isFiring()) {
                break;
            }
            evaluateEagerList();
            InternalAgendaGroup group = getAgendaGroupsManager().getNextFocus();
            if (group != null && !limitReached) {
                // only fire rules while the limit has not reached.
                // if halt is called, then isFiring will be false.
                // The while loop may continue to loop, to keep flushing the action propagation queue
                returnedFireCount = ruleEvaluator.evaluateAndFire(agendaFilter, fireCount, fireLimit, group);
                fireCount += returnedFireCount;
                limitReached = (fireLimit > 0 && fireCount >= fireLimit);
                head = propagationList.takeAll();
            } else {
                // no rules fired this iteration, so we know this is 0
                returnedFireCount = 0;
                // set the group to null in case the fire limit has been reached
                group = null;
            }
            if (returnedFireCount == 0 && head == null && (group == null || (group.isEmpty() && !group.isAutoDeactivate())) && !flushExpirations()) {
                // if true, the engine is now considered potentially at rest
                head = restHandler.handleRest(this, isInternalFire);
                if (!isInternalFire && head == null) {
                    break;
                }
            }
        }
        agendaGroupsManager.deactivateMainGroupWhenEmpty();
    } finally {
        // if it safely returns, then the engine should already be inactive
        if (isInternalFire) {
            executionStateMachine.immediateHalt(propagationList);
        }
    }
    return fireCount;
}
Also used : PropagationEntry(org.drools.core.phreak.PropagationEntry) InternalAgendaGroup(org.drools.core.common.InternalAgendaGroup) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint)

Example 2 with PropagationEntry

use of org.drools.core.phreak.PropagationEntry in project drools by kiegroup.

the class DefaultAgenda method fireLoop.

private int fireLoop(AgendaFilter agendaFilter, int fireLimit, RestHandler restHandler, boolean isInternalFire) {
    int fireCount = 0;
    try {
        PropagationEntry head = propagationList.takeAll();
        int returnedFireCount;
        // -1 or > 0 will return false. No reason for user to give 0, just handled for completeness.
        boolean limitReached = fireLimit == 0;
        // 
        while (isFiring()) {
            if (head != null) {
                // it is possible that there are no action propagations, but there are rules to fire.
                propagationList.flush(head);
                head = null;
            }
            // which changes the isFiring state. So a second isFiring guard is needed
            if (!isFiring()) {
                break;
            }
            evaluateEagerList();
            InternalAgendaGroup group = getNextFocus();
            if (group != null && !limitReached) {
                // only fire rules while the limit has not reached.
                // if halt is called, then isFiring will be false.
                // The while loop may continue to loop, to keep flushing the action propagation queue
                returnedFireCount = ruleEvaluator.evaluateAndFire(agendaFilter, fireCount, fireLimit, group);
                fireCount += returnedFireCount;
                limitReached = (fireLimit > 0 && fireCount >= fireLimit);
                head = propagationList.takeAll();
            } else {
                // no rules fired this iteration, so we know this is 0
                returnedFireCount = 0;
                // set the group to null in case the fire limit has been reached
                group = null;
            }
            if (returnedFireCount == 0 && head == null && (group == null || (group.isEmpty() && !group.isAutoDeactivate())) && !flushExpirations()) {
                // if true, the engine is now considered potentially at rest
                head = restHandler.handleRest(this, isInternalFire);
                if (!isInternalFire && head == null) {
                    break;
                }
            }
        }
        if (this.focusStack.size() == 1 && this.mainAgendaGroup.isEmpty()) {
            // the root MAIN agenda group is empty, reset active to false, so it can receive more activations.
            this.mainAgendaGroup.setActive(false);
        }
    } finally {
        // if it safely returns, then the engine should already be inactive
        if (isInternalFire) {
            executionStateMachine.immediateHalt(propagationList);
        }
    }
    return fireCount;
}
Also used : PropagationEntry(org.drools.core.phreak.PropagationEntry) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint)

Example 3 with PropagationEntry

use of org.drools.core.phreak.PropagationEntry in project drools by kiegroup.

the class ProtobufOutputMarshaller method writeActionQueue.

public static void writeActionQueue(MarshallerWriteContext context, ProtobufMessages.RuleData.Builder _session) throws IOException {
    Iterator<? extends PropagationEntry> i = context.wm.getActionsIterator();
    if (!i.hasNext()) {
        return;
    }
    ProtobufMessages.ActionQueue.Builder _queue = ProtobufMessages.ActionQueue.newBuilder();
    while (i.hasNext()) {
        PropagationEntry entry = i.next();
        if (entry instanceof WorkingMemoryAction) {
            _queue.addAction(((WorkingMemoryAction) entry).serialize(context));
        }
    }
    _session.setActionQueue(_queue.build());
}
Also used : PropagationEntry(org.drools.core.phreak.PropagationEntry) WorkingMemoryAction(org.drools.core.common.WorkingMemoryAction)

Example 4 with PropagationEntry

use of org.drools.core.phreak.PropagationEntry in project drools by kiegroup.

the class ActivationsManagerImpl method fireLoop.

private int fireLoop(AgendaFilter agendaFilter, int fireLimit, RestHandler restHandler) {
    firing = true;
    int fireCount = 0;
    PropagationEntry head = propagationList.takeAll();
    int returnedFireCount;
    // -1 or > 0 will return false. No reason for user to give 0, just handled for completeness.
    boolean limitReached = fireLimit == 0;
    while (isFiring()) {
        if (head != null) {
            // it is possible that there are no action propagations, but there are rules to fire.
            propagationList.flush(head);
            head = null;
        }
        evaluateEagerList();
        InternalAgendaGroup group = getAgendaGroupsManager().getNextFocus();
        if (group != null && !limitReached) {
            // only fire rules while the limit has not reached.
            // if halt is called, then isFiring will be false.
            // The while loop may continue to loop, to keep flushing the action propagation queue
            returnedFireCount = ruleEvaluator.evaluateAndFire(agendaFilter, fireCount, fireLimit, group);
            fireCount += returnedFireCount;
            limitReached = (fireLimit > 0 && fireCount >= fireLimit);
            head = propagationList.takeAll();
        } else {
            // no rules fired this iteration, so we know this is 0
            returnedFireCount = 0;
            // set the group to null in case the fire limit has been reached
            group = null;
        }
        if (returnedFireCount == 0 && head == null && (group == null || (group.isEmpty() && !group.isAutoDeactivate())) && !flushExpirations()) {
            // if true, the engine is now considered potentially at rest
            head = restHandler.handleRest(this);
        }
    }
    agendaGroupsManager.deactivateMainGroupWhenEmpty();
    return fireCount;
}
Also used : PropagationEntry(org.drools.core.phreak.PropagationEntry) InternalAgendaGroup(org.drools.core.common.InternalAgendaGroup) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint)

Example 5 with PropagationEntry

use of org.drools.core.phreak.PropagationEntry in project drools by kiegroup.

the class ProtobufOutputMarshaller method writeActionQueue.

public static void writeActionQueue(MarshallerWriteContext context, ProtobufMessages.RuleData.Builder _session) throws IOException {
    Iterator<? extends PropagationEntry> i = context.getWorkingMemory().getActionsIterator();
    if (!i.hasNext()) {
        return;
    }
    ProtobufMessages.ActionQueue.Builder _queue = ProtobufMessages.ActionQueue.newBuilder();
    while (i.hasNext()) {
        PropagationEntry entry = i.next();
        if (entry instanceof ProtobufWorkingMemoryAction) {
            _queue.addAction(((ProtobufWorkingMemoryAction) entry).serialize(context));
        }
    }
    _session.setActionQueue(_queue.build());
}
Also used : PropagationEntry(org.drools.core.phreak.PropagationEntry)

Aggregations

PropagationEntry (org.drools.core.phreak.PropagationEntry)5 InternalAgendaGroup (org.drools.core.common.InternalAgendaGroup)2 InternalWorkingMemoryEntryPoint (org.drools.core.common.InternalWorkingMemoryEntryPoint)2 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)1 WorkingMemoryAction (org.drools.core.common.WorkingMemoryAction)1