Drools - Rules Writing



If you see the default rule that is written in the Hello World project (Sample.drl), there are a lot of keywords used which we will be explaining now.

Default Rule

Sample.drl

  • Package − Every Rule starts with a package name. The package acts as a namespace for Rules. Rule names within a package must be unique. Packages in Rules are similar to packages in Java.

  • Import statement − Whatever facts you want to apply the rule on, those facts needs to be imported. For example, com.sample.DroolsTest.Message; in the above example.

  • Rule Definition − It consists of 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 manages a collection of rules, processes, and internal types. It is contained inside the package org.drools.KnowledgeBase. In Drools, these are commonly referred to as knowledge definitions or knowledge. Knowledge definitions are grouped into knowledge packages. Knowledge definitions can be added or removed. The main purpose 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. It is 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 a stateless session that forms the simplest use case, not utilizing inference. A stateless session can be called like a function, passing it some data and then receiving some results back. Common examples of a stateless session include −

  • Validation

    • Is 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 responsible for building a KnowledgePackage from knowledge definitions (rules, processes, types). It is contained inside the package org.drools.builder.KnowledgeBuilder. The knowledge definitions can be in various formats. If there are any problems with building, the KnowledgeBuilder will report errors through these two methods: hasErrors and getError.

The following diagram explains the process

KnoledgeBuilder

In the above example, as we are taking a simple example of stateless knowledge session, 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.

Advertisements