07 October, 2020
Welcome to the Drools Tutorials, Drools is a business logic integration platform written in Java. Apart from the tutorials, we will also cover Interview Questions, Issues and How To’s of Docker.
Drools is a Business Rules Management System (BRMS) solution. It provides a core Business Rules Engine (BRE), a web authoring and rules management application (Drools Workbench) and an Eclipse IDE plugin for core development.
Drools is a Business Logic integration Platform (BLiP). It is written in Java. It is an open source project that is backed by JBoss and Red Hat, Inc. It extends and implements the Rete Pattern matching algorithm.
In layman’s terms, 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.
Drools is split into two main parts: Authoring and Runtime.
-Authoring: Authoring process involves the creation of Rules files (.DRL files).
-Runtime: It involves the creation of working memory and handling the activation.
The Authoring process involves the creation Rules files (.DRL) which contain the rules which are fed into a parser. The parser checks for correctly syntax of the rules and produces an intermediate structure that "describes" the rules. This is then passed to the Package Builder which produces Packagesand undertakes any code generation and compilation that is necessary for the creation of the Package.
Drools Runtime is required to instruct the editor to run the program with specific version of Drools jar. You can run your program/application with different Drools Runtime.
The Working memory is a key point of the Drools engine: it's here that Facts are inserted.
Facts are plain Java classes which rely on the Java Bean pattern( your Java beans from your application). Facts are asserted into the Working Memory where they may then be modified or retracted.
When facts are asserted into the working memory, it will result in one or more rules being concurrently true and scheduled for execution by the Agenda - we start with a fact, it propagates and we end in a conclusion. This method of execution for a Production Rule Systems is called Forward Chaining.
Interested in mastering Drools Training? Enroll now for FREE demo on Drools Training.
Drools is Rule Engine or a Production Rule System that uses the rule-based approach to implement and Expert System. Expert Systems are knowledge-based systems that use knowledge representation to process acquired knowledge into a knowledge base that can be used for reasoning.
The aim of the new project wizard is to set up an executable sample project to start using rules immediately. This will set up a basic structure, the classpath, sample rules and a test case to get you started. Supports selecting a Drools runtime for the Drools core dependencies.
Integrate with the rules repository (as part of Guvnor, also known as the Kie Workbench or business central) using Git tooling.
The heart of the Rules Engine where you specify conditions (if ‘a’ then ‘b’).
Facts are the data on which the rules will act upon. From Java perspective, Facts are the POJO (Plain Old Java Object).
A Knowledge Session in Drools is the core component to fire the rules. It is the knowledge session that holds all the rules and other resources. A Knowledge Session is created from the KnowledgeBase.
For the rules engine to work, Facts are inserted into the session and when a condition is met, the subsequent rule gets fired. A Session is of two types:
-Stateless Knowledge Session
-Stateful Knowledge Session
It’s a logical concept. The agenda is the logical place where activations are waiting to be fired.
Activations are the then part of the rule. Activations are placed in the agenda where the appropriate rule is fired.
The rules like “Low Credit Score Rejection” rule are contained in DRL files (files with the extension .drl) that comply with Drools native rule language syntax. To use the created rules in a Java application you must:
-Knowledge Builder: A knowledge builder is the class that knows how to load and process DRL files. The impact of parsing and building in memory objects from the DRL file happens at this point. An instance of a knowledge builder can be obtained from the KnowledgeBuilderFactory using the new KnowledgeBuilder() method
-Knowledge Package: A knowledge package is the result of processing a DRL file; a serializable object. The knowledge builder has a get Knowledge Packages() that returns a collection of knowledge packages.
-Knowledge Base: A knowledge base is where the knowledge packages are deployed to so that they can be used at runtime. The knowledge base is the object that will be used to create knowledge sessions. This knowledge base is a thread safe construct that serves as a factory of sessions.
-Knowledge Session: The knowledge session is the object used to interact with the rule engine. It is throught a session that you will make the rule engine aware of the facts (the data) that the rule engine will evaluate against the available rules. Inside the Rule Engine
Learn more about Drools Interview Questions in this blog post.
Inside the rule engine, after you have inserted your facts into a knowledge session and invoked the session fireAllRules() method:
-Rules are matched against the facts in the knowledge session using the pattern matcher (Rete algorithm)
-The unordered list of activations (the Rules that apply based on the facts) is ordered internally (by a conflict resolver) in the session’s agenda
-The first Rule in the agenda is the executed, possibly triggering steps 1 and 2 again
This loop is how a Rule Engine infers knowledge from existing facts using rules. You can see that a Rule Engine using pattern matching to reduce/transform the problem space to arrive at a set of facts that can be considered a solution to the problem at hand.
One of the big decisions you’ll face with implementing a rule-based system with Drools is how much Java to put in your rules. As with any other object-oriented application we want encapsulate complex behavior to make our rules more readable. There are also things that are much easily accomplished, tested and developed outside of the realm of the rules engine.
To deal with the interaction with Twitter I decided to use Twitter4J (http://yusuke.homeip.net/twitter4j), a Java library to interact with the Twitter API. Using Twitter4J I created a simple collection of static utility methods contained in the class TwitterUtils.java. Some of the available methods are:
-Double getTwitterInfluenceRatio(User user)
-Boolean hasSetProfileImage(User user)
-Double averageTweetsPerDay(User user)
-Boolean inactiveForTheLast(User user, Integer days)
-Integer followersInCommon(Twitter twitter, User target)
-Boolean isFollowing(Twitter twitter, String target)
Twitter4J provides the classes twitter4j.Twitter which represents the authenticated Twitter user and twitter4j.User which represents detailed information about a Twitter user.
Rule engines can provide a Java developer with an environment in which logic and data are clearly separated. In the simple example used in this article it is easy to see how the DRL file becomes you laboratory of centralized knowledge about the problem at hand. Once the plumbing code is in place you can truly concentrate on the “business logic” in atomic, discrete, manageable chunks.
In this article we’ve barely scratched the surface of the capabilities provided by Drools. Drools 5 is a complete offering that includes the rule engine (Drools Expert), a Business Rule Management System (Drools Guvnor), a process/workflow engine (Drools Flow) and an event processing/temporal reasoning engine (Drools Fusion).
For indepth understanding of Drools Tutorials click on
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .