Skip to main content

On This Page

Find List of Matched Rules in Drools

2 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

Find List of Matched Rules in Drools

Drools, a business rules management system, provides multiple approaches to track which rules fired during a session, crucial for debugging and auditing. The core framework, KIE (Knowledge Is Everything), allows for dynamic rule loading and execution, making rule tracking essential for modern applications.

Why This Matters

Ideal rule engine models assume perfect accuracy and predictability, but in reality, unexpected rule firings can lead to incorrect decisions or system errors. Tracking fired rules is vital for diagnosing issues and ensuring the system behaves as intended; failures in rule execution can have significant financial or operational costs.

Key Insights

  • KIE dependency version 10.0.1: Required for Maven-based KIE modules and dynamic rule loading.
  • AgendaEventListener: Provides a non-intrusive way to observe rule execution from outside the rule base.
  • RuleContext: Offers finer control over tracking, allowing rule authors to specify precisely when a rule’s execution is recorded.

Working Example

public class Person {
private String name;
private int age;
private boolean eligibleToVote;
private boolean priorityVoter;
public Person(String name, int age) {
this.name = name;
this.age = age;
this.eligibleToVote = false;
this.priorityVoter = false;
}
// standard getters and setters
}
public class RuleUtils {
public static void track(RuleContext ctx, RuleTracker tracker) {
String ruleName = ctx.getRule().getName();
tracker.add(ruleName);
}
}
public class RuleTracker {
private final List<String> firedRules = new ArrayList<>();
public void add(String ruleName) {
firedRules.add(ruleName);
}
public List<String> getFiredRules() {
return firedRules;
}
}
rule "Check Voting Eligibility"
when
$person : Person(age >= 18)
$tracker : RuleTracker()
then
track(drools, $tracker);
$person.setEligibleToVote(true);
update($person);
end

Practical Applications

  • Fraud Detection System: A financial institution uses Drools to detect fraudulent transactions. Tracking fired rules helps identify patterns and refine the ruleset for improved accuracy.
  • Pitfall: Relying solely on rule execution without tracking can lead to “silent failures” where incorrect decisions are made without any indication of a problem.

References:

Continue reading

Next article

Google Cloud and Palo Alto Networks Announce Nearly $10 Billion Security Partnership

Related Content