鍍金池/ 教程/ Java/ Drools教程
Drools規(guī)則語法
Drools調(diào)試
Drools常用術(shù)語
創(chuàng)建Drools程序(入門)
Drools教程
Drools規(guī)則編寫
Drools Eclipse插件
Drools運(yùn)行時(shí)
Drools簡(jiǎn)單項(xiàng)目

Drools教程

任何Java企業(yè)級(jí)應(yīng)用可以分成三個(gè)部分 −

  • UI – 用戶界面(前端)
  • 服務(wù)層連接到數(shù)據(jù)庫
  • 業(yè)務(wù)層

我們有一些框架,用來處理UI和業(yè)務(wù)層在一起,例如,Spring和Struts。然而,我們沒有一個(gè)標(biāo)準(zhǔn)的方式來處理業(yè)務(wù)邏輯,直到Drools的出現(xiàn)。

Drools是什么?

Drools是一個(gè)業(yè)務(wù)邏輯集成平臺(tái)(BLip)。它是用Java編寫。它是由JBoss和紅帽公司擴(kuò)展支持,并實(shí)現(xiàn)Rete模式匹配算法的一個(gè)開源項(xiàng)目。

通俗地說,Drools是一種工具,使我們能夠分離內(nèi)部業(yè)務(wù)流程,找到邏輯和數(shù)據(jù)的集合。我們需要注意的兩個(gè)重要關(guān)鍵詞是邏輯和數(shù)據(jù)。

Drools的被分成兩個(gè)主要部分:編寫和運(yùn)行系統(tǒng)。

  • 制作: 制作過程涉及創(chuàng)建規(guī)則文件(.DRL文件)。
  • 運(yùn)行時(shí): 它涉及到創(chuàng)建工作存儲(chǔ)器和處理活化。

什么是規(guī)則引擎?

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.

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 can scale to a large number of rules and facts. The Inference Engine matches facts and data against Production Rules – also called Productions or just Rules – to infer conclusions which result in actions.

A Production Rule is a two-part structure that uses first-order logic for reasoning over knowledge representation. A business rule engine is a software system that executes one or more business rules in a runtime production environment.

A Rule Engine allows you to define “What to Do” and not “How to do it.”

What is a Rule?

Rules are pieces of knowledge often expressed as, "When some conditions occur, then do some tasks."

When
    <Condition is true>
Then
    <Take desired Action>

The most important part of a Rule is its when part. If the when part is satisfied, the thenpart is triggered.

rule  <rule_name>
      <attribute> <value>
      
      when
         <conditions>
      
      then
         <actions>
end

Pattern Matching

The process of matching the new or existing facts against Production Rules is called Pattern Matching, which is performed by the Inference Engine. There are a number of algorithms used for Pattern Matching including:

  • Linear
  • Rete
  • Treat
  • Leaps

Drools Implements and extends the Rete Algorithm. The Drools Rete implementation is called ReteOO, signifying that Drools has an enhanced and optimized implementation of the Rete algorithm for object-oriented systems.

Advantages of a Rule Engine

Declarative Programming

Rules make it easy to express solutions to difficult problems and get the solutions verified as well. Unlike codes, Rules are written in less complex language; Business Analysts can easily read and verify a set of rules.

Logic and Data Separation

The data resides in the Domain Objects and the business logic resides in the Rules. Depending upon the kind of project, this kind of separation can be very advantageous.

Speed and Scalability

The Rete OO algorithm on which Drools is written is already a proven algorithm. With the help of Drools, your application becomes very scalable. If there are frequent change requests, one can add new rules without having to modify the existing rules.

Centralization of Knowledge

By using Rules, you create a repository of knowledge (a knowledge base) which is executable. It is a single point of truth for business policy. Ideally, Rules are so readable that they can also serve as documentation.

Tool Integration

Tools such as Eclipse provide ways to edit and manage rules and get immediate feedback, validation, and content assistance. Auditing and debugging tools are also available.