Drools Tutorial - Architecture
Overview
In this tutorial we will be looking at Drools Architecture, it's components.
Drools Tutorial :
- Drools Overview - Architecture
- Spring Boot + Drools Example
- Spring Boot + Drools Decision Table Example
- Drools Interview Questions and Answers
What is Drool?
Drools is a free and open source project licenced under the Apache License 2.0 and it is compatible with any JVM and available in Maven Central Repository also.
Drools is a BRMS (Business Rules Management System). It includes a Business Rules Engine (BRE), a web authoring and rules management framework (Drools Workbench), complete runtime support for Decision Model and Notation (DMN) models at Conformance level 3, and a core development Eclipse IDE plugin.
It is written in 100% pure Java, runs on any JVM and is available in the Maven Central repository too. The Drools assists you in separating and reasoning over logic and data in business processes. It has a forward and backward chaining inference-based drools rules engine that supports forward and backward chaining.
Drools are divided into two categories:
- Authoring- It refers the creation of Rules files (.DRL files).
- Runtime- It means the creation of working memory as well as the handling of activation.
In this Drools tutorial, you will learn the following key aspects:
- What is Drool?
- What is the Drool Rule Engine?
- Architecture of Drools
- What is Drool Rule?
- Why use Drools Rule Engine?
- What are Disadvantages of Rules Engine?
- What is Backward vs. Forward Chaining?
What is the Drool Rule Engine?
- Drools is a Rule Engine or a Production Rule System that implements an Expert System using a rule-based approach. Expert systems are information-based systems that process acquired knowledge into a knowledge base that can be used for reasoning.
- A Production Rule System is Turing complete, with a focus on knowledge representation to express propositional and first order logic in a concise, non-ambiguous and declarative manner.
- The brain of a Production Rules System is an Inference Engine that is able to expand to a large number of rules and facts.
- A Production Rule is a two-part structure that uses first-order logic for reasoning over knowledge representation.
- A Rule Engine allows you to specify "What to Do" rather than "How to Do It".
Architecture of Drools
The working of Drools architecture is as follows:
-
Production Memory
The Rules are stored in the Production Memory/Rule Base. -
Pattern Matching
The process of matching the new or existing facts against production rules is called pattern matching, which is performed by the Rule engine. -
Working Memory
The facts that the Inference Engine matches against are kept in the Working Memory. -
Agenda
A system with a large number of rules and facts which result in several rules being valid for the same fact assertion; these rules are said to be in conflict. The Agenda manages the execution order of these conflicting rules using a Conflict Resolution strategy.
What is Drool Rule?
A DRL Rule File (.drl)
DRL (Drools Rule Language) rules are business rules that you define directly in .drl text files. A DRL file can contain one or more rules that define at minimum the rule conditions (when) and actions (then). The DRL designer in Business Central provides syntax highlighting for Java, DRL, and XML.Syntax:
rule "name"
attributes
when
<conditions>
then
<actions>
end
Pattern Matching Method
Pattern Matching, which is done by the Inference Engine, is the process of matching new or existing facts against Production Rules.
Algorithms used for Pattern Matching are:
Rete Algorithm
Leaps Algorithm
Linear Algorithm
Treat Algorithm
However, the algorithm mostly used by Drools is the Rete Algorithm.
Rete Algorithm
The Drools Rete implementation is known as ReteOO, and it denotes that Drools has improved and optimised the Rete algorithm for object-oriented systems.It uses node sharing to minimise or remove particular types of redundancy. While performing joins between different fact types, it stores partial matches.
Take a look at our suggested posts:
Why to use Drools Rule Engine?
Below are the few Advantages of using Drools Rule Engine:
- Declarative Programming: A Rule Engine allows you to specify "What to Do" rather than "How to Do It".
- Logic and Data Separation: Your data is in your domain objects, the logic is in the rules.
- Speed and Scalability: The Rete algorithm,the Leaps algorithm, and their descendants such as Drools' ReteOO, provide very efficient ways of matching rule patterns to your domain object data.
- Centralization of Knowledge: You create a repository of knowledge (a knowledge base) which is executable by using rules.
- Tool Integration: Tools like Eclipse (and, in the future, Web-based user interfaces) make it possible to edit and manage rules while receiving instant input, validation, and content assistance. There are also auditing and debugging tools available.
- Explanation Facility: Rule systems effectively provide a "explanation facility" by logging the decisions made by the rule engine as well as the reasons for those decisions.
- Understandable Rules: You can set yourself up to write rules that are very similar to natural language by developing object models and, optionally, Domain Specific Languages that model your problem domain.
Disadvantages of Rules Engine
Here are some downsides of using a rules engine:
- Developers must put in a lot of work to learn this programming process.
- The rule engine is not a safe way to troubleshoot problems.
- One must understand how the rule engine works to consumes more memory.
- For a complex branching, a large number of rules exist.
- Rules can change over time and will take effect when the code is updated.
What is Backward vs. Forward Chaining?
There are two methods of execution for a rule system:
Forward Chaining
To begin, with forward chaining, we begin to analyze data and make our way towards a specific conclusion/decision.
For example, If you want to know who the top five students out of 100 were for a specific exam. Here we will take the data and then based on the analysis decide the results. This is forward chaining
Backward Chaining
We first take a decision and then check if the decision is true or not by backtracking through sequence of events or facts.
For example, If I want to know if a specific student has passed or not?
Then I'll take a decision that student has passed without first analyzing. Here, directly we decide the result and then back track to evaluate with data processing chain and as a result, determine whether or not
the decision is right.
When comparing forward chaining and backward chaining, the first one can be represented as "data-driven" (data as input), while the backward chaining can be described as "event(or goal)-driven" (goals as inputs).