Drools – a business rules management system

By Anusha Jallipalli

Drools is a collection of tools that allow us to separate and reason over logic and data found within business processes. The two important keywords we need to notice are Logic and Data. Let us understand some terms used in the Drools rules engine , before we run a drool example:

Drools Rule Engine uses the rule-based approach to implement an Expert System.

Expert Systems are knowledge-based systems that use knowledge acquired through process followed converting it into a knowledge base that can be used for reasoning.

Rules are pieces of knowledge often expressed as, “When some conditions occur, then do some tasks.”

KnowledgeBase 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 as knowledge definitions or knowledge.

KnowledgeBuilder interface is responsible for building a KnowledgePackage from knowledge definitions (rules, processes, types). It is contained inside the package org.drools.builder. It  will report errors through these two methods: hasErrors and getError.

Knowledge Session in Drools is the core component that fires the rules. It is the knowledge session that holds all the rules and other resources.

Facts are inserted into the session and when a specified condition is met, the subsequent rule gets fired. Thus a  Knowledge Session is created from the KnowledgeBase.

Sessions are two types:

Stateless Knowledge Session – which  can be called like a function, passing into it some data and then receiving some results back.

Stateful Knowledge Session – which lives  longer and allows iterative changes over time

Here is an example of drools:

Step 1: We  create a pojo class as below:

Step 2: Add dependencies related to drools in pom.xml file

drools-core – this is the core engine, runtime component. This is the only runtime dependency if you have pre-compiling rules.
drools-complier – this contains the compiler/builder components to take rule source, and build executable rule bases. You don’t need this during runtime, if your rules are pre-compiled.

Step 3: Create a maven project with main class Employee.java

Step 4: This example is provided with a rule(Employee.drl): the Rule (“Simple Rule condition “) checks the value of age and salary field in class EmployeeBean, if the given condition gets matched it prints “rule executed successfully..!” otherwise it returns no output