JBoss Rules Learning (1): What is a Rule

 I have been learning JBoss Rules for a few days. Because there is less Chinese information in this area, I have been reading the manual on the official website these days. This is a good tutorial. I have translated and sorted out some important things I read, hoping to provide some help to students who want to learn JBoss Rules.
       Before starting this tutorial, let me briefly introduce JBoss Rules:
       JBoss Rules was formerly an open source project from Codehaus called Drools. It was recently incorporated into JBoss, renamed JBoss Rules, and became the rule engine of the JBoss application server.
       Drools is an implementation of a rules engine based on Charles Forgy's RETE algorithm tailored for Java. RETE with OO interface makes business rules more natural expression.
  
       Since JBoss Rules is a business rules engine, we must first know what Rules are, that is, rules. How rules are represented in JBoss Rules

Rules

One rule is the encoding of business knowledge. A rule has attributes , a Left Hand Side ( LHS ) and a Right Hand Side ( RHS ). Drools allows the following attributes: salience , agenda-group , no-loop , auto-focus , duration , activation-group .

 

rule “ < name > ”    
     < attribute >   < value >     
    when        
         < LHS >     
    then        
         < RHS > 
end


A rule's LHS consists of one or more conditions ( Conditions ). When all conditions ( Conditions ) are met and true, RHS will be executed. The RHS is called the Consequence. LHS and RHS are similar to:

 

if  (  < LHS >  ) {
     < RHS > 
}

 

Rules can be associated with a namespace via the package keyword; other rule engines may call this a Rule Set. A package declares imports , global variables, functions and rules .

 

 

 

The regular matching of new data and modified data is called pattern matching (Pattern Matching). The matching engine is called the Inference Engine. The rules accessed are called ProductionMemory and the data matched by the inference engine is called WorkingMemory. Agenda manages the execution of matched rules. The pattern matching algorithms used by the inference engine are as follows: Linear , RETE , Treat , Leaps .

Drools 采用了 RETE 和 Leaps 的实现。 Drools 的 RETE 实现被称为 ReteOO ,表示 Drools 对 Rete 算法进行了加强和优化的实现。

一条规则的 LHS 由 Conditional Element 和域约束( Field Constraints )。下面的例子显示了对一个 Cheese Fact 使用了字面域约束( Literal Field Constraint )

rule  " Cheddar Cheese " 
    when
        Cheese( type  ==   " cheddar "  )
    then
        System.out.println(  " cheddar "  );
end

上面的这个例子类似于:

public   void  cheddarCheese(Cheese cheese) {
     if  ( cheese.getType().equals( " cheddar " ) {
        System.out.println(  " cheddar "  );
    }
}

<!--[if !vml]--> <!--[endif]-->

规则引擎实现了数据同逻辑的完全解耦。规则并不能被直接调用,因为它们不是方法或函数,规则的激发是对WorkingMemory 中数据变化的响应。结果( Consequence ,即 RHS )作为 LHS events 完全匹配的 Listener。

当 rules 被加入 Productioin Memory 后, rules 被规则引擎用 RETE 算法分解成一个图:

当 Facts 被 assert 进入 WorkingMemory 中后,规则引擎找到匹配的 ObjectTypeNode ,然后将此 Fact 传播到下一个节点。 ObjectTypeNode 拥有一块内存来保存所有匹配的 facts 。在我们的例子中,下一个节点是一个域约束( Field Constraint ), type = = “cheddar” 。如果某个 Cheese 对象的类型不是“ cheddar ”,这个 fact将不会被传播到网络的下一个节点。如果是“ cheddar ”类型,它将被记录到 AlphaNode 的内存中,并传播到网络的下一个节点。 AlphaNode 是古典 RETE 术语,它是一个单输入 / 单输出的节点。最后通过 AlphaNode 的fact 被传播到 Terminal Node 。 Terminal Node 是最终节点,到此我们说这条规则被完全匹配,并准备激发。

当一条规则被完全匹配,它并没有立刻被激发(在 RETE 中是这样,但在 Leaps 中它会立刻被激发)。这条规则和与其匹配的 facts 将激活被放入 Agenda ,由 Agenda 来负责安排激发 Activations (指的是 rule + the matched facts )。

 

下面的图很清楚的说明了 Drools 规则引擎的执行过程:

数据被 assert 进 WorkingMemory 后,和 RuleBase 中的 rule 进行匹配(确切的说应该是 rule 的 LHS ),如果匹配成功这条 rule 连同和它匹配的数据(此时就叫做 Activation )一起被放入 Agenda ,等待 Agenda 来负责安排激发 Activation (其实就是执行 rule 的 RHS ),上图中的菱形部分就是在 Agenda 中来执行的, Agenda 就会根据冲突解决策略来安排 Activation 的执行顺序。

 

package  com.sample

import  java.util.List
import  com.sample.Cheese

global List cheeses

function  void  exampleFunction(Cheese cheese) {
    System.out.println( cheese );
}

rule “A Cheesy Rule”
    when
        cheese : Cheese( type  ==   " stilton "  )
    then
        exampleFunction( cheese );
        cheeses.add( cheese );
end

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326644435&siteId=291194637