Skip to main content
15 min

What is Drools?

Drools is a Business Rules Management System (BRMS) solution that helps you capture business logic in a declarative way. It allows you to separate business logic from application code, making your systems more maintainable and adaptable to changing requirements. At its core, Drools is a forward-chaining rule engine that uses the rule-based approach to implement an expert system. The term “expert system” might sound intimidating, but in simple terms, Drools lets you write rules that describe what to do in specific situations - much like how human experts make decisions.

Why Use Drools?

Before diving into how to use Drools, let’s understand why you might want to use it:
  1. Separation of business logic from application code: Business rules can be managed separately by business analysts and domain experts.
  2. Declarative programming: Focus on “what” should happen rather than “how” it should happen.
  3. Centralized knowledge repository: All business rules are stored in a single location, making them easier to manage and update.
  4. Improved decision-making capabilities: Complex decision logic becomes more manageable and transparent.
  5. Scalability: Rules can be added or modified without requiring changes to the underlying application code.

Basic Components

Let’s understand some key components of the Drools rule engine:

Facts

Facts are the data that your rules operate on. They can be any Java objects that you insert into the rule engine’s working memory. For example, if you’re building a loan approval system, your facts might include Applicant, LoanApplication, and CreditCheck objects.

Rules

Rules are the business logic you define. Each rule has a condition part (the “when” clause) and an action part (the “then” clause). When the conditions match facts in working memory, the rule’s actions are executed. Rules are typically defined in .drl (Drools Rule Language) files like this:

Working Memory

This is where facts are stored during rule execution. When you insert, modify, or retract facts, the rule engine evaluates which rules’ conditions are satisfied.

KIE Session

A KIE session is your interaction point with the rule engine. You use it to:
  • Insert facts into working memory
  • Fire rules
  • Query the results

Setting Up Your First Drools Project

Let’s create a simple Drools project using Maven. This example assumes you have Maven and Java installed.

Step 1: Create a Maven Project

Create a new Maven project with the following structure:

Step 2: Configure Your pom.xml

Add the following to your pom.xml:

Step 3: Create the Fact Models

Create Java classes to represent your facts. Applicant.java:
LoanApplication.java:

Step 4: Create the KIE Module Descriptor

Create a file src/main/resources/META-INF/kmodule.xml:
This file declares a knowledge base named “rules” that will look for rule files in the “rules” package, and a KIE session named “ksession-rules” that will be created from this knowledge base.

Step 5: Create Your First Rule

Create a file src/main/resources/rules/loan-rules.drl:

Step 6: Create a Test Class

Create a Java class to test your rules:

Step 7: Run Your Project

Compile and run your project. You should see output similar to:

Understanding What Happened

Let’s break down what happened in our example:
  1. We created two Java classes (Applicant and LoanApplication) that represent our facts.
  2. We defined two rules in Drools Rule Language (DRL):
  3. One that approves loans for applicants with good credit scores (above 700)
  4. One that denies loans for applicants with bad credit scores (700 or below)
  5. In our test class:
  6. We loaded the KIE container, which manages the rules
  7. We created a new KIE session from the container
  8. We inserted facts (applicants and loan applications) into working memory
  9. We called fireAllRules() to execute rules that match our facts
  10. We checked the results to see which applications were approved or denied
This simple example demonstrates the basics of how Drools works:
  1. You define your data model (facts)
  2. You define rules based on those facts
  3. You insert facts into working memory
  4. You fire the rules to see what happens

Stateful vs. Stateless Sessions

In the example above, we used a stateful KIE session. There are two types of sessions you can use:

Stateful Sessions

Stateful sessions maintain state between rule invocations. Facts remain in working memory until explicitly removed. This is useful for scenarios where:
  • Facts are inserted, modified, or retracted over time
  • Rules need to be applied repeatedly as data changes
  • The state of the system needs to be tracked across multiple rule executions
Our loan approval example used a stateful session.

Stateless Sessions

Stateless sessions are instantiated and destroyed in a single call. They’re usually faster because they don’t need to maintain state. Use them when:
  • You need to apply rules once to a set of data
  • You don’t need to track changes over time
  • You want better performance for simple rule execution
Here’s how you would use a stateless session:

Next Steps

Now that you understand the basics of Drools, you might want to explore:
  1. More complex rule conditions: Learn about pattern binding, conditionals, and nested patterns
  2. Rule attributes: Discover how to use salience, no-loop, and other rule attributes to control rule execution
  3. Domain-specific languages (DSLs): Create a business-friendly language for writing rules
  4. Decision tables: Define rules in spreadsheet format
  5. Complex event processing: Handle events that occur over time

Common Mistakes and Troubleshooting

1. Rules Not Firing

If your rules aren’t firing, check:
  • Did you insert all the necessary facts into working memory?
  • Are your fact properties accessible (public or have getters)?
  • Do your fact values actually match the rule conditions?

2. Circular Rules

If your rules seem to execute in an infinite loop:
  • Use the no-loop attribute to prevent a rule from activating multiple times for the same set of facts
  • Make sure your modify statements aren’t triggering the same rule again

3. Class Not Found Exceptions

If you get class not found exceptions:
  • Make sure your package structure in Java matches what you’re importing in your DRL files
  • Check that your Maven dependencies are correctly configured

4. KModule Not Found

If you get errors related to your KIE module not being found:
  • Ensure your kmodule.xml is in the correct location (src/main/resources/META-INF/)
  • Verify that your package names in kmodule.xml match your actual package structure

Conclusion

This guide has introduced you to the basics of Drools. You’ve learned:
  • What Drools is and why it’s useful
  • How to set up a basic Drools project
  • How to define facts and rules
  • How to run and test your rules
  • The difference between stateful and stateless sessions
As you continue with Drools, remember that the power of the rule engine comes from being able to express complex business logic in a declarative way. This separation of business rules from application code will make your systems more flexible and easier to maintain as requirements change.