Search Mastertheboss

Our eBooks

What is JBoss Drools?

In this tutorial we will learn what is Drools and some basics of its architecture and API.

Drools is the rules engine of the KIE (Knowledge Is Everything) umbrella project. Drools engine stores, processes, and evaluates data to execute the business rules or decision models that you define.

The main concept to understand about Drools is that its decision engine matches incoming data, or facts, to the conditions of rules and determine whether and how to execute the rules.

Where Drools can be downloaded

The current version of Drools is the 7.36 and can be downloaded from here: https://www.drools.org/download/download.html.
The Enterprise-ready supported by Red Hat version of Drools is called Red Hat Decision Manager and can be downloaded from here: https://developers.redhat.com/products/red-hat-decision-manager/download

Drools main components

Drools engine includes the following components:

  • Rules: Business rules or DMN decisions defined by you. All rules contain the conditions that trigger the rule execution and the actions to be taken accordingly.
  • Facts: This is the data used by the Rules and thus are used by the decision engine to match the rule conditions and execute actions.
  • Production memory: This is where rules are stored in Drools.
  • Working memory: This is where facts are stored in Drools.
  • Agenda: This is where activated rules are registered and sorted (if applicable) in order to be executed.

When an user or an automated process inserts/updates Facts, that information is inserted into the working memory of the Drools. Drools' decision engine then matches those facts to the conditions of the rules that are stored in the production memory to determine which Rule are eligible for execution. This process is also known as pattern matching. Finally, if rule conditions are met, Drools engine activates and registers rules in the agenda, where the decision engine then sorts prioritized or conflicting rules in preparation for execution.

Here is an example of Rule defined to determine the age limit of an user:

rule "CanDrive"
  salience 15
  agenda-group "applicationGroup"
  when
    $application : LoanApplication()
    Applicant( age < 18 )
  then
    $application.setApproved( false );
    $application.setExplanation( "Sorry you cannot drive" );
end

The following diagram illustrates these basic components of the decision engine:

what is jboss drools what is drools

Drools API

These are the main API available to manage Drools engine from your code:

  • KieBase: This is the repository of all the application’s knowledge definitions. It contains rules, processes, functions, type models. The KieBase itself does not contain runtime data, instead sessions are created from the KieBase in which data can be inserted and process instances started.
  • KieModule: This is the container of all the resources necessary to define a set of KieBases, It is defined in a file named kmodule.xml which deckres the KieBases names and configurations together with all the .KieSession that can be created and all the other files necessary to build the KieBases themselves.
  • KieContainer: This is the container for all the KieBases of a given KieModule.
  • KieFileSystem: This is the in.memory file system used to define the resources composing a KieModule.
  • KieBuilder: This is the tool to build the resources contained in a KieModule.

Now let’s see how to create these objects.

KieContainer

This is the way to programmatically create a KieContainer in Drools 7:

KieServices kieServices = KieServices.Factory.get();
KieContainer kContainer = kieServices.getKieClasspathContainer();

KieBase

Once we have created the KieContainer, we create the KieBase. This is how to retrieve the default KieBase:

KieBase kBase1 = kContainer.getKieBase();  

On the other hand, to retrieve an user-defined KieBase named "KBase1":

KieBase kBase1 = kContainer.getKieBase("KBase1");

Stateless Session

A stateless KIE session is a session that does not use inference to make iterative changes to facts over time. In a stateless KIE session, data from a previous invocation of the KIE session (the previous session state) is discarded between session invocations. Here is how to create a new Stateless Session:

KieServices kieServices = KieServices.Factory.get();
KieContainer kContainer = kieServices.getKieClasspathContainer();
StatelessKieSession kSession = kContainer.newStatelessKieSession();

Stateful Session

A stateful KIE session is a session that uses inference to make iterative changes to facts over time. In a stateful KIE session, data from a previous invocation of the KIE session is retained between session invocations.

KieServices kieServices = KieServices.Factory.get();
KieContainer kContainer = kieServices.getKieClasspathContainer();
KieSession ksession = kContainer.newKieSession();

KieContainer

Since a Kie project is also a Maven project the groupId, artifactId and version declared in the pom.xml file are used to generate a ReleaseId that uniquely identifies your project in your application. Therefore you can create a KieContainer from the project by passing thr ReleaseId to the KieServices.

KieServices kieServices = KieServices.Factory.get();
ReleaseId releaseId = kieServices.newReleaseId( "com.sample", "myapplication", "1.0" );
KieContainer kieContainer = kieServices.newKieContainer( releaseId );
© 2020 mastertheboss.com. All Rights Reserved.

Please publish modules in offcanvas position.