Control order of execution of 2 drools file in same package - drools

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.

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:
Tag your related rules with a ruleflow-group attribute.
Create a BPMN Flow (a Business Process)
Associate each of your ruleflow-groups with a Business Process Task
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

Related

Drools - multiple activation group

I have written a Drool rule as shown below:
rule "first and second"
activation-group "first"
activation-group "second"
when
// conditions
then
// actions
end
However, when compiling, I get this error:
Duplicate attribute definition: activation-group in rule first and second
I want to know if it is possible to include multiple activation-groups in a rule. If this is possible, how can you do it?
Thanks in advance.
It's not possible to use multiple activation groups in a single rule. If you feel the need to do this then you should restructure your rules to be a little smarter with your conditions so you can get it to a state that would work with single activation groups (per rule).

Drools all rules are getting loaded

I am using drools in my project and assume it has 100 rules. I have two process flow (typically it has start node->rule flow task->end node). One process flow's rule flow task is specified with rule flow-group which is assigned to 50 rules and another process flow's rule flow task is specified with flow flow groups which is assigned to rest of the 50 rules. The don't overlap.
Now I use kiesession and call start process of first process flow, I see that it loads all the 100 rules instead of only 50 and gives me compilation and runtime errors. So please help me in understanding why rules from different rule flow groups are getting executed in a process flow where those rules are no where related its rule flow-group ? I see all its when conditions are getting loaded.
The "unit of work" in Drools is the KieBase and not the rule-flow-group. All the rules in your KieBase will be present in your KieSessions and will be evaluated when required.
Hope it helps,

Need to fire same ruleflow-group multiple times from Single Flow

For reusable perspective, I have one Rule with Rule Flow group. I have use same rule flow group multiple times in flow. Now the situation is its should be fire same rule more then one time,but its only fire that rule single time.
I don't understand why its happens.
Can you give me an idea why its happening and whats the solution for same?
A Rule Task in a flow does not "execute" the rules in a rule-flow-group, it merely activates that group in the agenda so IF there are active rules, they will fire.
If between 2 executions of your Rule Task you modify the session in a way that new activations are created, consecutive executions of your task should fire new activations of those rules.
Hope it helps,

Rules in jBPM 6

I have created a process in jbpm 6. There is a class Person, with attributes name and age. In the process form, the name and age of the person is entered. The first node in the process is a human task to view the details. The second node is an XOR gateway with drools expression on its arcs like Person(age > 20) and Person (age < 20).
Now when I execute the process instance, the first human tasks works fine, but when it reaches the gateway, I can see this error -
"XOR split could not find at least one valid outgoing connection for
split Gateway".
Any idea whats wrong.
Gateways containing drools expressions only work with facts and not with process variables. If you want to make use of a drools expression in your gateways, you will need to insert the process variable (or the whole process instance) as a fact. You can do so by using a script node, an outgoing action in your human task.
From documentation:
Rule constraints do not have direct access to variables defined inside the process. It is however possible to refer to the current process instance inside a rule constraint, by adding the process instance to the Working Memory and matching for the process instance in your rule constraint. ....... Note that you are however responsible yourself to insert the process instance into the session and, possibly, to update it, for example, using Java code or an on-entry or on-exit or explicit action in your process.
Hope it helps,

Drools - Execute specific rule-flow group?

In my project i want Drools ( version 5.5.0.final) to execute specific rule flow group.
Is there a way to do this?
Use ruleflow. I tried it with Java AgendaFilters but the API is limited. Create a flow diagram that has your ruleflow-group node and start as process.