2
votes

The drools decision table documentation (link) states that it is possible to select the first matching row in a decision table:

As each row is a rule, the same principles apply. As the rule engine processes the facts, any rules that match may fire. (Some people are confused by this. It is possible to clear the agenda when a rule fires and simulate a very simple decision table where only the first match effects an action.)

How is this done? I have set Sequential=true, but I'm not sure what else needs to be done.

2

2 Answers

2
votes

Drools provides a way to solve this problem (selecting only first matching row) by defining an activation groups in decision table rule set.

Your workaround, maxrules=1, might work only if you want to use drools decision table as lookup table. In this scenario since only one rule can be fired, using drools has no advantage over using a classical database lookup table, it just adds complexity.

If you have decided to use drools, or a rule engine in general, you should move your "rules" or "decisions" into the rule definitions to benefit most from its advantages (RETE engine, flexibility, seperation of rules from application code, etc.)

0
votes

A FireAllRulesCommand needs to be added as part of a batch execution with the max rules set to 1:

   StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();

    Transaction tx = new Transaction();
    tx.setSenderBin("12345");

    List<Command<?>> commands = new ArrayList<Command<?>>();     
    commands.add(CommandFactory.newInsert( tx ));
    commands.add(new FireAllRulesCommand(1));

    ExecutionResults results = ksession.execute(
        CommandFactory.newBatchExecution(commands));

-- EDIT: Please refer to the accepted answer for the preferred way of doing this.