Search code examples
droolsrules

Control order of execution of 2 drools file in same package


I have 2 drools file in the same package com.sample.order.rules orders.drl and order-summary.drl. I want to process all the orders in orders.drl and then from the results of order.drl processing, create an order summary using order-summary.drl. I currently control the order of execution by setting all the saliences in order-summary.drl to -1. Is it possible to solve this problem any other way? The problem is the number of rules is increasing and adding salience to every rule in order-summary.drl just doesn't feel right.


Solution

  • It is possible to control the execution order of groups of rules with the attribute ruleflow-group, see examples from the docs. In general, the approach is:

    1. Tag your related rules with a ruleflow-group attribute.
    2. Create a BPMN Flow (a Business Process)
    3. Associate each of your ruleflow-groups with a Business Process Task
    4. Trigger your rules under a jBPM Process

    This allows you to (optionally) only trigger your Summary steps when some conditions in the previous Orders step have been satisfied. I would suggest looking at the Mortgage_Process example provided by the Drools Business Central Showcase Docker image. To set up the KIE Execution Server API to test your rules and processes iteratively, see the docker-compose set up here.

    Step 1

    rule "order rule 1"
      ruleflow-group "orders"
    when
      Order ( value > 10 )
    then
      insert ( new Shipment () );
    end
    
    rule "summary rule 1"
      ruleflow-group "summary"
    when
      Shipment ()
    then
      // do stuff
    end
    

    Step 2 enter image description here