Getting Started with Drools 7: A Hello World Rule Engine Example

This tutorial walks through setting up Drools 7, configuring Maven dependencies, kmodule.xml, and writing a simple Hello World rule test that demonstrates product discount calculation using the new KIE API.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Getting Started with Drools 7: A Hello World Rule Engine Example

In the previous chapter we used Drools 5.x, where the KnowledgeBase class was central; Drools 7 marks that class as @Deprecated. This section shows how to use Drools 7, and all subsequent examples assume this version.

Project Structure

The Drools project follows the standard Maven layout. Key files include pom.xml for dependencies, src/main/resources for rule files, and a new kmodule.xml configuration.

Maven Dependency

Add the Drools core and compiler dependencies to pom.xml as shown in the screenshot.

Domain Model

The fact object remains a Product class, representing items with a type (e.g., DIAMOND, GOLD) and a discount field.

Rule File

The rule definitions are stored in Rules.drl. The content is unchanged from the Drools 5 example.

kmodule.xml Configuration

Drools 7 introduces kmodule.xml. Its main elements are:

One or more kbase entries, each linking to one or more DRL packages.

Each kbase must have a unique name attribute.

The packages attribute lists the resource paths of DRL files; multiple packages are comma‑separated. By default, all resources under src/main/resources are scanned.

The default attribute marks a KieBase as the default, allowing it to be retrieved without specifying a name (only one default per module).

Each kbase can contain one or more ksession elements, each requiring a unique name.

Unit Test Example

The example is implemented as a JUnit test. The test creates a Product fact, sets its type to GOLD, inserts it into a KieSession, fires all rules, and prints the resulting discount.

The test output shows that one rule was fired and the product received the expected discount.

Key Takeaways

This Hello World example demonstrates the complete workflow for Drools 7: Maven setup, kmodule configuration, rule definition, and execution via a unit test. It also highlights that Drools 7 uses a completely new API compared to Drools 5, requiring developers to adapt their code accordingly.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavarule enginemavenDroolsKIE API
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.