Drools — Agenda-Group vs Activation-Group and how to use them together

Paras Bansal
4 min readNov 12, 2021

Drools documentation has good examples on what Agenda-Group and Activation-Group are and this post is to demonstrate exactly that.

I’ll take example from drools documentation first to go over the concepts and then we’ll go over the demonstration

Saliencehttps://docs.jboss.org/drools/release/7.1.0.Final/drools-docs/html_single/#_salience

Salience decide the priority of a rule. Higher the salience, that rule will be executed first. Otherwise, the way rules are written in the rules file, that order will be followed.

Agenda-Grouphttps://docs.jboss.org/drools/release/7.1.0.Final/drools-docs/html_single/#_agendagroup

All the rules belong to “MAIN” agenda by default. So unless you set the agenda-group of a rule to a different value, they all belong to main. By default “MAIN” agenda-group is pushed to drools stack using “PRIORITY” or “SALIENCE” and then while execution they pop one at a time and execute.

Quote from documentation — “Agenda groups are known as “modules” in CLIPS terminology. While it best to design rules that do not need control flow, this is not always possible. Agenda groups provide a handy way to create a “flow” between grouped rules.”

So basically, you can make group of your rules and then create a flow by activating that group in order, you just have to use the command:

ksession.getAgenda().getAgendaGroup( "Group A" ).setFocus();

So the order you use the setFocus, that order your group of rules will be activated and executed.

Activation-Group https://docs.jboss.org/drools/release/7.1.0.Final/drools-docs/html_single/#_activationgroup

You can create another group of rules called activation-group, which ensures that if one rule (any first) is activated (satisfied the condition) in that group, all others are cancelled (not even evaluated)

Using these 3 constructs, a flow can be designed to ensure that only a set of rules are activated which satisfies the criteria. I have made a use case here, let’s discuss that first.

I have put some set of rules for a mortgage/lending problem. There can be hundred of different scenarios, but here I have listed 3 set of rules:

  1. Rules based on existing income

2. Rules based on credit history (or transunion score)

3. Rules based on existing loan applicant

We’ll create 1 REST endpoint with 3 services, which can be hit with required data and output can be expected

Below is our data model:

public class Customer {
private Double requested_loan_amount;
private Double income;
private Double transunion_score;
private Double existing_loan_amount;

private String result;
}

Below is our service processor:

public class RulesProcessingService {

@Autowired
RulesCompiler rulesCompiler;
public Customer applyRules(Customer req, String service) {

StatelessKieSession kSession = rulesCompiler.getKSession();
log.info("Executing rules ...");

AgendaGroupSetFocusCommand agendaGroup = new AgendaGroupSetFocusCommand(service);
List<Command> cmds = new ArrayList<>();
cmds.add(CommandFactory.newInsert(req));
cmds.add(agendaGroup);
cmds.add(CommandFactory.newFireAllRules());
cmds.add(CommandFactory.newGetObjects(new ClassObjectFilter(Customer.class), "output"));
ExecutionResults results = kSession.execute(CommandFactory.newBatchExecution(cmds));List<Customer> data_out = (List<Customer>) (Collection<?>) results.getValue("output");log.info("rules output = " + data_out);return data_out.get(0);
}
}

The flow here is, we expect the value of “Agenda-group” to appear in the URL which is then used to activate the “Agenda-group”

The RulesComplier takes are of compiling all the rules at the boot and creating the StatelessKieSession, reference:

If you see, the PRIORITY in the spreadsheet (same as SALIENCE) is same, i.e. rules will be evaluated in the order they are written. You can always use them the way you want.

Outputs:

Calling “ExistingLoanRules” Even though both PASS and REVIEW satisfies the criteria, 2nd rule is never evaluated because 1st one is success (activation group)

Also, because of Agenda-Group, none other groups are activated (except only existingLoanRules)

Another example:

No other “agenda-groups” got activated and moreover, only the 1st rule executed even though 2nd one also satisfies the criteria

I hope this helps understanding drools concepts. I am sure there is lot to explore, but this example can easily be packaged in a docker container and deployed as a microservice.

This is it for this post, please comment to leave your feedback.

--

--