---
title: "Drools and Jbpm 学习"
author: "bruce008"
date: "2012-07-09T10:54:57Z"
tags: []
categories: ["企业架构"]
slug: "drools-and-jbpm"
draft: false
lastmod: "2012-07-09T10:54:56Z"
id: 1582248
url: "https://bruce008.iteye.com/blog/1582248"
---

进入新公司新项目， 新公司更变态很多网站都没有办法用，youdao note 就是其一。 唯一的好处就是现在上网不需要翻那堵墙， 在看某些技术博客的时候爽些。 闲话少说，目前项目需要用到Drools and JBPM. 所以从现在开始学习Drools and jBPM.

 

   我们在项目里用到的 Drools and jBPM 其实可以分为两部分： Drools Expert 它是一个rule engine， 我们根据Drools 的drl 语法来定义N 多的rules, 然后 这个rule engine 来 驱动这些rules。

 

另外一块就是jBPM 其实它可以看成Workflow ， 在项目里面每一个use case 可以看成是我rkflow 的 一个step。最主要的是工作流引擎（在项目中用作Execution Controller ) 来决定下一个step。

 

现在还没有自己的机器，也就没有开发环境，只有纸上谈兵了，到官方网站瞅瞅Drools Expert。从最基本的dlr 语法看起吧， 一个drl 文件由下面部分组成：

 

```
package package-name 

 imports 

 globals 

 functions 

 queries 

 rules 
```

其中每一部分都是optional 的。顺序的话除了package  必须要在第一位， 其它部分都不重要的。

 

1， Rule 。 每一个rule 的组成部分又是：

**rule** *`"name"`*  
    *`attributes`*  
    **when**  
        *`LHS`*  
    **then**  
        *`RHS`*  
**end**

其中LHS 又叫条件， RHS 又叫Action 可以由具体的方言组成的。比如我们的dialect 是Java， 这Action 部分我们就可以写Java语法的语句。

 

2， Drools 5 的关键字 分为 ：soft  和 hard  关键字。

 

hard  关键字是保留的， 不能用它们来命名我们自己的domain object 。hard  关键字只有3个：

   true

   false

   null

 

soft 关键字 是在上下文中被识别出来的。 建议是避免混淆不要用它们来命名自己的domain object。 soft 关键字 就比较多聊：

- `lock-on-active`
- `date-effective`
- `date-expires`
- `no-loop`
- `auto-focus`
- `activation-group`
- `agenda-group`
- `ruleflow-group`
- `entry-point`
- `duration`
- `package`
- `import`
- `dialect`
- `salience`
- `enabled`
- `attributes`
- `rule`
- `extend`
- when
- then
- `template`
- `query`
- `declare`
- `function`
- `global`
- `eval`
- `not`
- `in`
- `or`
- `and`
- `exists`
- `forall`
- accumulate
- collect
- from
- `action`
- `reverse`
- `result`
- `end`
- over
- `init`

3， Drools 的 注释分为 single line （由 # 或 // 开始）和 multi line  (  /*    \*/ ）

 

4， Drools 的 Error message  的 格式 比较严格：

    由左至右分为 5 部分：

**1st Block:** This area identifies the error code.

**2nd Block:** Line and column information.

**3rd Block:** Some text describing the problem.

**4th Block:** This is the first context. Usually indicates the rule, function, template or query where the error occurred. This block is not mandatory.

**5th Block:** Identifies the pattern where the error occurred. This block is not mandatory.

 

 

5， Drools 的 function 定义：

  

```
  function String hello(String name) {
    return "Hello "+name+"!";
   }
```

 

  function  返回类型   function name  参数 {

    body

  }

 

 定义好了function, 我们可以在其它地方来 import  function . 比如：

   import function my.package.Foo.hello

 

6， 定义新类型

  6.1   Drools  天生支持Java 类型， 我们也可以在Drools 自己来定义新的类型。比如：

   

```
import java.util.Date

declare Person
    name : String
    dateOfBirth : Date
    address : Address
end
```

 

 

  一旦定义 我们就可以在rule 中来使用它们：

 

```
rule "Using a declared Type"
when 
    $p : Person( name == "Bob" )
then
    // Insert Mark, who is Bob's mate.
    Person mark = new Person();
    mark.setName("Mark");
    insert( mark );
end


```

 

 

7， rule 的 属性： salience 

    这个rule 最重要的属性是用来表示rule 的优先级， 值越大优先级越高。

 

8， Timers and Calendars

    Drools 现在支持基于cron 的 timer

     

```
timer ( int: <initial delay> <repeat interval>? )
timer ( int: 30s )
timer ( int: 30s 5m )

timer ( cron: <cron expression> )
timer ( cron:* 0/15 * * * ? )
```

 

9，  Pattern

 Pattern  是最重要的 condition　元素，它用来匹配工作内存中的所有ｆａｃｔ。

 

　　　$pattern Binding :  pattern type ( contraints )

 

```
rule ...
when
    $p : Person(age < 18)
then
    System.out.println( "Person " + $p );
end
```

简单的就这么多， 剩下的就是 rule 里面的条件的一些 复杂的用法了。 可以参见 [http://docs.jboss.org/drools/release/5.2.0.Final/drools-expert-docs/html/ch05.html#d0e3719](http://docs.jboss.org/drools/release/5.2.0.Final/drools-expert-docs/html/ch05.html#d0e3719)