Standav is now a Brillio company. Read More

Drools – a business rules management system

Drools – a business rules management system

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:

package com.bimarian.main;


public class EmployeeBean implements Serializable {
	private static final long serialVersionUID = -4539053707066601845L;
	private int age;
	private double salary;
	//getter and setter methods

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

package com.bimarian.main;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.runtime.StatefulKnowledgeSession;

public class Employee {
	public static void main(String args[]) {

		Employee employee = new Employee();

	private void droolsTest() {
		try {
            KnowledgeBase kbase = readKnowledgeBase();
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            EmployeeBean employee = new EmployeeBean();
            ksession.fireAllRules(); //  no rules are executed until you call "fireAllRules()"
        } catch (Throwable t) {

	private KnowledgeBase readKnowledgeBase() throws Exception {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("Employee.drl"), ResourceType.DRL);
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (KnowledgeBuilderError error: errors) {
            throw new IllegalArgumentException("Could not parse knowledge.");
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        return kbase;

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

package com.bimarian.main

import com.bimarian.main.EmployeeBean;

rule "Simple Rule condition"
	EmployeeBean( age > 20 && salary > 1500 )
	System.out.println("rule executed successfully..!");

Leave a Reply