Drools Overview and Fundamentals

Table of contents

​edit

 1. What is Drools?

 2. Drools usage scenarios

 3. Drool architecture content

3.1 Overall Architecture

 3.2 Description of composition content

 3.2.1 Rules

 3.2.2 Production memory

 3.2.3 Facts

 3.2.4 Working memory

 3.2.5 Pattern matches

 3.2.6 Agenda

 4. Why use a rule engine?

 4.1 Declarative programming

 4.2 Logic and data separation

 4.3 Centralized management of rules

 4.4 High readability

 5. Comparison of different rule engines

 6. Principle analysis of Drools rule engine

 6.1 Introduction to rete algorithm

 6.2 Principle

 6.2.1 Forward-Chaining

 6.2.2 Backward-Chaining


 1. What is Drools?

Developed from the inference engine, the rule engine is a component embedded in the application, which separates business decisions from application code and writes business decisions using predefined semantic modules. Accept data input, interpret business rules, and make business decisions based on business rules.

Drools features an open source business rules engine that is easy to access enterprise policy, easy to tune, and easy to manage, and is industry standard for speed and efficiency. It allows a business analyst or reviewer to easily view business rules to verify that the coded rules enforce the desired business rules. Its predecessor was Drools, an open source project of Codehaus, which was later incorporated into JBoss, renamed JBoss Rules, and became the rules engine of the JBoss application server.

 2. Drools usage scenarios

business field example
financial decision Loan issuance, credit information system
inventory management just-in-time supply chain
Fare calculation Air, spread, train and other bus transportation
Production Procurement System Product raw material procurement management
Risk control system Calculation of risk control rules
Promotion platform system Full discount, discount, increase price purchase

 3. Drool architecture content

3.1 Overall Architecture

 3.2 Description of composition content

 3.2.1 Rules

Business rules defined by ourselves, such as rule files written by ourselves. All rules must contain at least a condition that triggers the rule and an action prescribed by the rule.

 3.2.2 Production memory

Where rules are stored in the Drools engine.

 3.2.3 Facts

Data entered or changed into the Drools engine, which matches rule conditions to execute applicable rules. If the value of the Fact object is modified in the rule, the data of the real JavaBean will also change. For example: when we call ksession.insert (object), then the inserted object can be understood as a Facts object.

 3.2.4 Working memory

Where facts are stored in the Drools engine. The drools rule engine will obtain data from Working Memory and perform pattern matching with the rules defined in the rule file
, so the application we develop only needs to insert our data into Working Memory.

 3.2.5 Pattern matches

Matcher, pattern match all the rules in the Rule Base with the Fact objects in the Working memory, and the matching rules will be activated and put into the Agenda.

 3.2.6 Agenda

Agenda, used to store the rules that are activated after pattern matching by the matcher.

 4. Why use a rule engine?

 4.1 Declarative programming

A rules engine allows you to say "how to do it", not "how to do it". Based on this idea, the use of rules makes it easy to express solutions to difficult problems, and rules are easier to read than code.

 4.2 Logic and data separation

The data is in the domain objects and the logic is in the rules, as future changes the logic can be more easily maintained by organizing the logic all in one or more very different rule files instead of distributing the logic across many domain objects or in the controller.

 4.3 Centralized management of rules

By using rules, executable rule bases can be created and these rules can be written and managed centrally. Such as storing in a directory or database.

 4.4 High readability

By creating an object model and (optionally) a domain-specific language that models the problem domain, you can set yourself up to write rules that closely approximate natural language. They express their logic in their own language, which may be understandable to non-technical field experts, and all programs are checked, while technical knowledge is hidden in regular code.

 5. Comparison of different rule engines

In addition to Drools, there are some other rule engines, including:

1)CLIPS

2)Jess

3)OpenL Tablets

4)JBoss Rules/Drools Fusion

5)IBM Operational Decision Manager (ODM)

6)Azul Zulu Rule Engine

Feature Drools CLIPS Jess OpenL Tablets JBoss Rules/Drools Fusion IBM ODM Azul Zulu Rule Engine
open source/commercial Business open source Business Business Business Business Business
language support Java CLIPS Jess Java Java Java Java
visual interface support not support not support not support support support support
regular expression language DSL/FLR CLIPS Jess Excel DSL/FLR ODM PegaRules PRPC
fact model POJO native type POJO POJO POJO XOM POJO
performance medium higher higher medium medium high medium

As can be seen from the above table, there are some differences between Drools and other rule engines, such as language support, visual interface, regular expression language, fact model, etc. Therefore, when choosing a rule engine, you need to consider your own business requirements and technical capabilities, as well as engine performance, ease of use, scalability and other factors. Additionally, factors such as budget and open source/commercial licensing models need to be considered.

 6. Principle analysis of Drools rule engine

 6.1 Introduction to rete algorithm

The Rete algorithm was originally an algorithm described in a paper published by Dr. Charles L. Forgy of Carnegie Mellon University in 1974, which provides an efficient implementation of an expert system. Since the Rete algorithm was proposed, it has been used in some large-scale rule systems, such as ILog, Jess, JBoss Rules, etc. are all rule engines based on the RETE algorithm.
Rete translates to "net" in Latin, which means network. The Rete matching algorithm is an efficient method for comparing a large number of pattern collections and large object collections. It finds all objects and rules that match each pattern through the network screening method.
Its core idea is to dynamically construct a matching tree according to the content of the separated matching items, so as to achieve the effect of significantly reducing the amount of calculation. The Rete algorithm can be divided into two parts: rule compilation and rule execution. When the Rete algorithm asserts facts, it contains three stages: matching, selection and execution, called match-select-act cycle.

 6.2 Principle

In the field of AI, the production system is a very important theory. Production reasoning is divided into forward reasoning and reverse reasoning. The general form of the rules is: IF condition THEN operation. The rete algorithm is an efficient pattern matching algorithm to realize forward reasoning in the production system. By forming a rete network for pattern matching, the time redundancy and structural similarity characteristics of the rule-based system are used to improve the system pattern matching efficiency.

 6.2.1 Forward-Chaining

Forward reasoning, also called deductive method, is driven by facts, starting from an initial fact, and continuously draws conclusions from the application rules. First,
select a rule in the candidate queue as the enabling rule for inference, and record its conclusion as evidence for the next step of inference. This process is repeated until no more rules are available or the required solution is obtained.

 6.2.2 Backward-Chaining

Reverse reasoning is also called induction, which is driven by the goal. First, a certain hypothesis is put forward, and then the evidence supporting the hypothesis is found. If the required
evidence means that the original hypothesis is correct. If it cannot be found anyway If there is no evidence required, it means that the original hypothesis is not established, and a new hypothesis needs to be made at this time.

If you think it is helpful to you, welcome to like + bookmark + follow!

Guess you like

Origin blog.csdn.net/qq_25409421/article/details/131557025