Sunday, July 29, 2012


Once you become familiar with the topics discussed in my previous two posts:
You can start playing more and more with the Rule Engine. One of the things that you will notice during your first steps is the behavior mismatch between a Rule Engine and  Process Engine. This is the main topic of this short post.


Every time that we start a new Process Instance inside any Business Process Engine we expect that the engine will execute all the activities defined inside the process definition. We understand that the process will run until it reaches a Wait State, meaning that an external Asynchronous interaction is required by the process to continue. All the Synchronous activities will be automatically executed by the engine as soon as possible.
In the previous post we've analyzed a process which includes some rules to Rank a Car and Define the Price of that Car.
Synchronous Process
For that example, we never doubt about the Synchronous nature of the Rules Evaluation and Execution. Once again, unless we introduce an Asynchronous External Interaction into our process like a Human Interaction or an Asynchronous call to an external system  our process will run each activity as soon as it finishes the previous one.
Now if we jump to the Rule Engine Arena, we have a different expected behavior.
Rule Engine: Evaluation and Firing Cycle
Rule engines work using a two phase execution cycle:
  • Evaluation / Activation: When we insert new data (Facts) into the Rule Engine, the information is evaluated and all the rules that evaluates their conditions to true will be activated. All this activations are placed inside the Agenda.
  • Firing: Using a Conflict Resolution Strategy, the Rule Engine will pick one Activation from the agenda and it will execute the Consequence for that rule, which can cause  new activations to be placed inside the agenda or the cancellation of previous activations. After finishing executing the selected activation, the engine will pick another activation to execute. This loop will continue until there are no more Activations in the Agenda.
This recursive nature of the rule execution cycle, and the fact that we need to enter into the firing phase explicitly by calling the fireAllRules() method, force us to think about how the process and rules combination will work at runtime.

Stateful vs Stateless

As I've promised in my previous post, from now on we will work with Stateful Sessions, meaning that our processes and our rules will be hosted in a session that will be responsible for keeping the status, allowing us to have a richer context to work.
Stateless Sessions can be considered a simplified version of Stateful Sessions, were the execution cycle just run once, and the context cannot be reused to add more information later on. When we start using Stateful Sessions, long running Processes and Rules can coexist and influence each other behaviors.
The Process Instance itself can become a Fact inside the Rule Engine and we can start doing inferences about it. At the same time we can create Rules to evaluate a set of Process Instances which are running in the same session, opening the door for a whole set of patterns. But, before jumping into the patterns we need to understand the execution behavior in this Stateful Environment.
Now if we have a Process which requires to evaluate some Rules as part of an activity we will find out that we explicitly need to call the fireAllRules() method to execute the consequences of the activated rules. We need to know that our Rules living in the same session where the process is being executed will be evaluated as soon as we insert information into the Rule Engine, activations will be created and placed inside the Agenda, and they will be there until we call the fireAllRules() method.
From our previous example we can see clearly how different contexts were used and how with stateless session we can obtain a synchronous execution.
Stateful and Stateless Contexts
Now if we want to do all the evaluations inside the same Stateful Context we need to be careful with the execution behavior. Take a look at the following figure which shows the Process Instance and a set of facts coexisting in the same Stateful Session, where all the evaluations will be made.
Stateful Context
Now we have facts that we will be evaluated as soon as we insert them into the Session, so it will be our responsibility to define where the current activations will be fired. If we want to simulate the same behavior from our previous example, we will need to call the fireAllRules() method after each activity which is related with rules evaluations.
From the application perspective this is not easy to handle, because if we have several processes being executed we will not know when it is necessary to call the fireAllRules() method for sure.
For this reason, the following section explains a technique which help us to put the Rule Engine a Reactive Mode.

The Reactive Mode

There are two ways to put the Rule Engine in what we call "The Reactive Mode".
  • Fire Until Halt
  • Agenda & Process Event Listeners
Using these two alternatives we will be forcing the Engine to fire the activations as soon as they are created, without the need of calling explicitly the fireAllRules() method.

Fire Until Halt

The Fire Until Halt alternative requires another thread to be created, which will be in charge of monitoring the activations and firing them as soon as they are created. In order to put the Engine in a reactive mode using the fireUntilHalt() method, we use the following code snippet:
[code language="java"]

new Thread(new Runnable() {

  public void run() {



} ).start();

[/code]The only downside of using the Fire Until Halt approach is that we need to create another thread – this is not always possible. We will see that when we use the persistence layer for our business process, using this alternative is not recommended. For testing purposes relying on other thread to fire our rules can add extra complexity and possible race conditions. That’s why the following method, which uses listeners, is usually recommended.
Check an example of Fire Until Halt here:

Agenda & Process Event Listeners

Using the Agenda and Process Event Listeners mode allows us to get the internal Engine events and execute a set of actions as soon as the events are triggered. In order to set up these listeners, we need to add the following code snippet right after the session creation, so we don’t miss any event:
[code language="java"]


new DefaultAgendaEventListener() {

   public void activationCreated(
                          ActivationCreatedEvent event) {

        ((StatefulKnowledgeSession) event.getKnowledgeRuntime())

    new DefaultProcessEventListener(){

    public void afterProcessStarted(
                             ProcessStartedEvent event) {

        ((StatefulKnowledgeSession) event.getKnowledgeRuntime())


Notice that we are attaching a DefaultAgendaEventListener and a DefaultProcessEventListener that define several events where we can hook up behavior. In this example, we are overriding the behavior of the activationCreated(…) and afterProcessStarted(…) methods, because we need to fire all the rules as soon as an activation is created or a process has started. Look at the other methods inside the DefaultAgendaEventListener and DefaultProcessEventListener to see the other events that can be used as hook points. This approach using listeners gives us a more precise and single threaded approach to work.
Check an example using Agenda and Process Event Listeners here:
Notice that this example works in the same way that the previous one, it give us the same results, but in this one we don't need to sleep to wait another thread to finish the execution of the fire all rules cycle. Sleeping is not an option for real applications, and because of the recursive nature of the cycle we will never know for sure how much time the other thread will require to finish.


Knowing about how to put the Rule Engine in Reactive Mode will be a common practice to work with processes and rules. It is important to know that both approaches – Fire Until Halt and Agenda and Process Event Listeners – give the same results in the previous tests, but they work in different ways. We need to understand these differences in order to choose wisely.
I strongly recommend you to check the examples provided in the links, to get familiar with this behavior. Future posts will work in Reactive Mode, and understanding how everything works in the back will help you to understand more advanced examples. Enable the logs in the example and analyze the output to understand the Process and Rules execution.
If you have questions about the examples, post a comment :)
If you have suggestions about how to improve the examples, post a comment :)



  1. This is a great explanation for the Business Process Engine. I guess, having some units in business management isn't enough for me to clarify some concepts in my head but I'm glad on how you put this into light. I'm preparing for an executive recruitment melbourne and this might be an edge for me. Thanks!

    1. Hi Jennifer,
      Good that you like it, if you have questions about the posts, please ask them. I'm trying to create more examples to demonstrate these concepts.

  2. This comment has been removed by a blog administrator.

  3. This comment has been removed by a blog administrator.

  4. This comment has been removed by a blog administrator.

  5. This comment has been removed by a blog administrator.

  6. Hi Marc,
    is this approach still correct with jBPM6 ? (Agenda & Process Event Listeners)

    I noticed no evaluation / activation of rules in a rule-task behind another rule-task.
    I insert a new fact or update a process variable between the rule-tasks. (jBPM 6.1.0-SNAPSHOT)
    In jBPM5 there was a re evaluation / activation.

    1. In jBPM6 we can use TriggerRulesEventListener for fireAllRules like the strategy in this blog entry and RuleAwareProcessEventLister for update Facts after variable changed in the process to evaluate / activate again.

      Also in jBPM5 I inserted a fact in beforeNodeTriggered (RuleNode) and retract / delete it in afterRuleFlowGroupDeactivated.
      This Agenda Event cannot be used any longer to delete the fact. The result is no activation again. Maybe now the event is before activation phase in jBPM6.

      Now i am using the Data Input Assignment on the RuleNode task in the Prcoess Modeler.

      Maybe this info helps somebody.

  7. Swiftly this amazing site may well definitely turn out to be well-known between almost all writing a blog men and women, for your careful content and even assessments. kmspico

  8. IEEE Cloud computing DOamin is a general term for anything that involves delivering hosted services over the Internet. cloud computing projects The cloud projects for cse is a metaphor for a global network of remote servers which operates as a single ecosystem, commonly associated with the Internet. IEEE FInal Year Networking Projects for CSE Domains Networking Projects cloud computing is the delivery of computing projects services—including servers, storage, databases, networking projects, software, analytics, and intelligence

    JavaScript Training in Chennai

    JavaScript Training in Chennai