Drools Rules Writing - Drools

What is rules writing in Drools?

You can observe that there is a default rule that is written in the Hello World project (Sample.drl). You can find lot of keywords used which we will be explaining now.



  • Package: Here every Rule starts with a package name. The package supports as a namespace for Rules. Rule names within a package must be unique. Packages in Rules are similar to packages in Java.
  • Import statement: You need to import the facts to apply the rule on. For example, com.sample.DroolsTest.Message; in the above example.
  • Rule Definition: Rule definition includes the Rule Name, the condition, and the Consequence. Drools keywords are rule, when, then, and end. In the above example, the rule names are “Hello World” and “GoodBye”. The when part is the condition in both the rules and the then part is the consequence. In rule terminology, the when part is also called as LHS (left hand side) and the then part as the RHS (right hand side) of the rule.

Now let us walk through the terms used in the Java file used to load the Drools and execute the rules.

Knowledge Base

Knowledge Base is an interface that supports a collection of rules, processes, and internal types. It includes inside the package org.drools.KnowledgeBase. It is also known as knowledge definitions or knowledge. Knowledge definitions are grouped into knowledge packages as these can be added or removed. The main objective of Knowledge Base is to store and reuse them because their creation is expensive. Knowledge Base provides methods for creating knowledge sessions.

Knowledge Session

The knowledge session is retrieved from the knowledge base. This is called as the main interface for interacting with the Drools Engine. The knowledge session can be of two types:

  • Stateless Knowledge Session
  • Stateful Knowledge Session

Stateless Knowledge Session

Stateless Knowledge Session is known as a stateless session that creates the simplest use case, not utilizing inference. It is also called as the function, passing it some data and then receiving some results back. Common examples of a stateless session include:

  • Validation
    • DrooIs this person eligible for a mortgage?
  • Calculation
    • Compute a mortgage premium.
  • Routing and Filtering
    • Filter incoming messages, such as emails, into folders.
    • Send incoming messages to a destination

Stateful Knowledge Session

Stateful sessions are longer lived and allow iterative changes over time. Some common use cases for stateful sessions include:

  • Monitoring
    • Stock market monitoring and analysis for semi-automatic buying.
  • Diagnostics
    • Fault finding, medical diagnostics
  • Logistics
    • Parcel tracking and delivery provisioning

Knowledge Builder

The KnoledgeBuilder interface is designed for building a KnowledgePackage from knowledge definitions (rules, processes, types). It includes the inside the package org.drools.builder.KnowledgeBuilder. The knowledge definitions can be formed in various formats. If there are any problems with building, the KnowledgeBuilder will report errors through these two methods: hasErrorsand getError.

The following diagram explains the process


The above example discusses by taking a simple example of stateless knowledge session. As we have inserted the fact in the session, and then fireAllRules() method is called and you see the output.

In case of a stateful knowledge session, once the rules are fired, the stateful knowledge session object must call the method dispose() to release the session and avoid memory leaks.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Drools Topics