Drools Expert - mvel - LHS - basic

From Training Material
Jump to navigation Jump to search

TODO: Change to LHS

title
Drools Expert - mvel - RHS - basic
author
Bernard Szlachta (NobleProg Ltd)

Drools Expert - mvel - RHS - basic Training Materials

Overview ⌘

Facts used during course

Emp

+------+----------+-----------+------+----------+---------+--------+
| id   | name     | job       | mgr  | hiredate | sal     | deptno |
+------+----------+-----------+------+----------+---------+--------+
| 7839 | BUSH     | PRESIDENT | 0    | 1981     | 5000.00 | 10     |
| 7698 | BLAIR    | MANAGER   | 7839 | 1981     | 2850.00 | 30     |
| 7782 | MERKEL   | MANAGER   | 7839 | 1981     | 2450.00 | 10     |
| 7566 | PUTIN    | MANAGER   | 7839 | 1981     | 2975.00 | 20     |
| 7654 | CHIRACK  | SALESMAN  | 7698 | 1981     | 1250.00 | 30     |
| 7499 | BAROSSO  | SALESMAN  | 7698 | 1981     | 1600.00 | 30     |
| 7844 | GATES    | SALESMAN  | 7698 | 1981     | 1500.00 | 30     |
| 7900 | BUFFETT  | CLERK     | 7698 | 1981     | 950.00  | 30     |
| 7521 | WALTON   | SALESMAN  | 7698 | 1981     | 1250.00 | 30     |
| 7902 | TOOSK    | ANALYST   | 7566 | 1981     | 3000.00 | 20     |
| 7369 | THATCHER | CLERK     | 7902 | 1980     | 800.00  | 20     |
| 7788 | CARNEGIE | ANALYST   | 7566 | 1982     | 3000.00 | 20     |
| 7876 | FORD     | CLERK     | 7788 | 1983     | 1100.00 | 20     |
| 7934 | ELISON   | CLERK     | 7782 | 1982     | 1300.00 | 10     |
+------+----------+-----------+------+----------+---------+--------+

Dept

+--------+------------+----------+
| deptno | dname      | loc      |
+--------+------------+----------+
| 10     | ACCOUNTING | NEW YORK |
| 20     | RESEARCH   | LONDON   |
| 30     | SALES      | PARIS    |
| 40     | OPERATIONS | BERLIN   |
+--------+------------+----------+

Salgrade

+-------+-------+-------+
| grade | losal | hisal |
+-------+-------+-------+
| 1     | 700   | 1200  |
| 2     | 1201  | 1400  |
| 3     | 1401  | 2000  |
| 4     | 2001  | 3000  |
| 5     | 3001  | 9999  |
+-------+-------+-------+

Filtering

Filter by Fact Type 根据Fact Type过滤 。⌘

Find all facts of Emp type
搜索所有Emp type的facts


when
	e : Emp()
then    
    System.out.println(
       e.id + " " + 
       e.name + "\t" + 
       e.job + "   \t" +
       e.mgr  + "\t" +
       e.hiredate + " " + 
       e.sal + " " + 
       e.deptno );
end
7934 ELISON	CLERK   	7782	1982 1300 10
7876 FORD	CLERK   	7788	1983 1100 20
7788 CARNEGIE	ANALYST   	7566	1982 3000 20
7369 THATCHER	CLERK   	7902	1980 800 20
7902 TOOSK	ANALYST   	7566	1981 3000 20
7521 WALTON	SALESMAN   	7698	1981 1250 30
7900 BUFFETT	CLERK   	7698	1981 950 30
7844 GATES	SALESMAN   	7698	1981 1500 30
7499 BAROSSO	SALESMAN   	7698	1981 1600 30
7654 CHIRACK	SALESMAN   	7698	1981 1250 30
7566 PUTIN	MANAGER   	7839	1981 2975 20
7782 MERKEL	MANAGER   	7839	1981 2450 10
7698 BLAIR	MANAGER   	7839	1981 2850 30
7839 BUSH	PRESIDENT   	0	1981 5000 10

Filtering by field | 根据field过滤 。⌘

Filter employees earning more than 1500. | 过滤出收入超过1500的员工

when
  e : Emp(sal > 1500)
then    
    System.out.println(e.id + " " + e.name + "\t" + e.job  
    + "   \t" +  e.mgr  + "\t" + e.hiredate + " " + e.sal
     + " " + e.deptno );
end
7788 CARNEGIE	ANALYST   	7566	1982 3000 20
7902 TOOSK	ANALYST   	7566	1981 3000 20
7499 BAROSSO	SALESMAN   	7698	1981 1600 30
7566 PUTIN	MANAGER   	7839	1981 2975 20
7782 MERKEL	MANAGER   	7839	1981 2450 10
7698 BLAIR	MANAGER   	7839	1981 2850 30
7839 BUSH	PRESIDENT   	0	1981 5000 10

Operators 操作符 。⌘

There are different kinds of operators: 操作符有很多种类:

  • Arithmetic operators (+, -, *, /, %) 算数操作符
  • Relational operators (>, >=, ==, !=) 关系操作符
  • Logical operators 逻辑操作符
    • conjunction (and, &&, ",") 与
    • disjunction (or, ||) 或
    • negation (!, do not confuse with not) 取反 (!, 不要和 not 混淆)
  • Drools operators (in, matches, etc...) | Drools 操作符 (in, matches, 等等...)
$e : Emp(sal > 1500, deptno == 10 || deptno == 20) 
$e : Emp(sal > 1500 && deptno == 10 || deptno == 20) 

Relational Operators 关系操作符 。=⌘

Find managers | 搜索 managers

rule "Sample"
when
   e : Emp(job == "MANAGER")
7566 PUTIN	MANAGER   	7839	1981 2975 20
7782 MERKEL	MANAGER   	7839	1981 2450 10
7698 BLAIR	MANAGER   	7839	1981 2850 30

People hired in 1982 | 1982雇佣的人

when
   e : Emp(hiredate == 1982)
7934 ELISON	CLERK   	7782	1982 1300 10
7788 CARNEGIE	ANALYST   	7566	1982 3000 20

Select all except clerks. | 选择除了clerks之外的所有人.

when
  e : Emp(job != 'CLERK')
7788 CARNEGIE	ANALYST   	7566	1982 3000 20
7902 TOOSK	ANALYST   	7566	1981 3000 20
7521 WALTON	SALESMAN   	7698	1981 1250 30
7844 GATES	SALESMAN   	7698	1981 1500 30
7499 BAROSSO	SALESMAN   	7698	1981 1600 30
7654 CHIRACK	SALESMAN   	7698	1981 1250 30
7566 PUTIN	MANAGER   	7839	1981 2975 20
7782 MERKEL	MANAGER   	7839	1981 2450 10
7698 BLAIR	MANAGER   	7839	1981 2850 30
7839 BUSH	PRESIDENT   	0	1981 5000 10


To select people earning from 1000 to 2000 (inclusive) | 选择收入在1000和2000之间的人 (inclusive)

when
  e : Emp(sal >= 1000 , sal <= 2000)
7934 ELISON	CLERK   	7782	1982 1300 10
7876 FORD	CLERK   	7788	1983 1100 20
7521 WALTON	SALESMAN   	7698	1981 1250 30
7844 GATES	SALESMAN   	7698	1981 1500 30
7499 BAROSSO	SALESMAN   	7698	1981 1600 30
7654 CHIRACK	SALESMAN   	7698	1981 1250 30

All lines below are equivalent | 下面所有语句功能一样

e : Emp(sal >= 1000 , sal <= 2000)
e : Emp(sal >= 1000 && sal  <= 2000)
e : Emp(sal >= 1000 && <= 2000)


in operator 操作符 。⌘

The in operator checks if the value is in a set. | in 操作符检查值是否存在于某个集合内

Select people working in department no 10 or 20. | 选择在No.10 或者No.20部门工作的人

when
  e : Emp(deptno in (10,20))
7934 ELISON	CLERK   	7782	1982 1300 10
7876 FORD	CLERK   	7788	1983 1100 20
7788 CARNEGIE	ANALYST   	7566	1982 3000 20
7369 THATCHER	CLERK   	7902	1980 800 20
7902 TOOSK	ANALYST   	7566	1981 3000 20
7566 PUTIN	MANAGER   	7839	1981 2975 20
7782 MERKEL	MANAGER   	7839	1981 2450 10
7839 BUSH	PRESIDENT   	0	1981 5000 10

The above is equivalent to | 上面的语句也可以写成

   e : Emp(deptno == 10 || deptno == 20)
   e : (Emp(deptno == 10) or Emp(deptno == 20))
The above two lines are assessed by the Drools engine in completely different way.
The or keywords will split the rule in two separate rules.
PLEASE SEE RETE TREE FOR THOSE EXAMPLES!
Drools引擎对上两行的评估方式完全不同
关键字or 会将规则分割成两个规则
详情请看 RETE TREE 的举例说明!

Select managers or clerks. | 选择 managers 或者 clerks.

when
  e : Emp(job in ('CLERK','MANAGER'))
7934 ELISON	CLERK   	7782	1982 1300 10
7876 FORD	CLERK   	7788	1983 1100 20
7369 THATCHER	CLERK   	7902	1980 800 20 
7900 BUFFETT	CLERK   	7698	1981 950 30
7566 PUTIN	MANAGER   	7839	1981 2975 20
7782 MERKEL	MANAGER   	7839	1981 2450 10
7698 BLAIR	MANAGER   	7839	1981 2850 30

matches operator | matches 操作符 。⌘

The 'maches' operator checks whether the values match the pattern (java regular expression).
'maches' 操作符检查值是否与模式匹配 (java 正则表达式).

. matches exactly one character 
 . 匹配一个单一字符
.* matches any number of characters, even an empty string. 
.* 匹配任何字符, 包括空(没有)字符.

More information: http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

Select people with names starting with "B". | 选择名字以 "B" 开头的人.

when
    e: Emp(name matches "B.*")
7900 BUFFETT	CLERK   	7698	1981 950 30
7499 BAROSSO	SALESMAN   	7698	1981 1600 30
7698 BLAIR	MANAGER   	7839	1981 2850 30
7839 BUSH	PRESIDENT   	0	1981 5000 10

Select people with second letter ‘a’. | 选择名字的第二个字符是‘a’的人.

when
    e: Emp(name matches ".A.*")
7788 CARNEGIE	ANALYST   	7566	1982 3000 20
7521 WALTON	SALESMAN   	7698	1981 1250 30
7844 GATES	SALESMAN   	7698	1981 1500 30
7499 BAROSSO	SALESMAN   	7698	1981 1600 30

not matches operator | not matches 操作符 。⌘

 when
    e: Emp(name not matches "B.*")

Note that this syntax is incorrect | 这样写语法是不正确的:

 when
    e: Emp(name ! matches "B.*")
    e: ! Emp(name matches "B.*")

Compound conditions | 组合条件 。⌘

You may use logical operators to build compound conditions. | 你可以使用逻辑操作符书写组合条件

  • "and", "&&", "," (conjunction) (与)
  • "or", "||" (disjunction) (或)
  • "not", "!" (negation) (反)

Examples

Let’s find managers earning more than 2500. | 搜索收入高于2500的managers.

when
    e: Emp(job == "MANAGER" , sal > 2500) 
7566 PUTIN	MANAGER   	7839	1981 2975 20
7698 BLAIR	MANAGER   	7839	1981 2850 30

The line below gives the same result | 下面的语句运行结果一致

when
    e: Emp(job == "MANAGER" && sal > 2500) 

Not that the "," and "&&" have different priority | 注意 "," 和 "&&" 优先级不同

Operators priority 操作符优先级 。⌘

(nested) property access    .	
List/Map access            [ ]	
constraint binding   :	
multiplicative       * / %	 
additive             + -	 
shift                << >> >>>	 
relational           < > <= >= instanceof	 
equality             == !=	
bit-wise non-short circuiting AND               &	 
bit-wise non-short circuiting exclusive OR	^	 
bit-wise non-short circuiting inclusive OR	|	 
logical AND	&&	 
logical OR	||	 
ternary	? :	 
Comma separated AND	,

Exercises 。⌘

Please switch go back to non-presentation mode to do exercises

Exercises

1. Exercise

Select all Salgrade 查询所有的工资等级的事实

5 3001 9999
4 2001 3000
3 1401 2000
2 1201 1400
1 700 1200

2. Exercise

Select shown data about employees who earn between 1000 and 2000. 查询工资从1000到2000员工的信息

7934 ELISON	CLERK   	7782	1982 1300 10
7876 FORD	CLERK   	7788	1983 1100 20
7521 WALTON	SALESMAN   	7698	1981 1250 30
7844 GATES	SALESMAN   	7698	1981 1500 30
7499 BAROSSO	SALESMAN   	7698	1981 1600 30
7654 CHIRACK	SALESMAN   	7698	1981 1250 30


3. Exercise

Select clerks working in department no 20. 查询在20号部门工作的出纳

7876 FORD	CLERK   	7788	1983 1100 20
7369 THATCHER	CLERK   	7902	1980 800 20

4. Exercise

Select employees who have a boss. 查询有老板的员工

7934 ELISON	CLERK   	7782	1982 1300 10
7876 FORD	CLERK   	7788	1983 1100 20
7788 CARNEGIE	ANALYST   	7566	1982 3000 20
7369 THATCHER	CLERK   	7902	1980 800 20
7902 TOOSK	ANALYST   	7566	1981 3000 20
7521 WALTON	SALESMAN   	7698	1981 1250 30
7900 BUFFETT	CLERK   	7698	1981 950 30
7844 GATES	SALESMAN   	7698	1981 1500 30
7499 BAROSSO	SALESMAN   	7698	1981 1600 30
7654 CHIRACK	SALESMAN   	7698	1981 1250 30
7566 PUTIN	MANAGER   	7839	1981 2975 20
7782 MERKEL	MANAGER   	7839	1981 2450 10
7698 BLAIR	MANAGER   	7839	1981 2850 30


5. Exercise

Select manager’s annual remuneration. 查询经理的年薪

7566 PUTIN	MANAGER   	7839	1981 35700 20
7782 MERKEL	MANAGER   	7839	1981 29400 10
7698 BLAIR	MANAGER   	7839	1981 34200 30

Use computation of annual salary in:

  • WHEN part
  • THEN part

What is the difference?

6. Exercise

Find employees with second letter “L” and third “A”. 查询名字中的字母第二个是L第三个是A的员工

+-------+
| name |
+-------+
| BLAIR |
+-------+

7. Exercise Find employees whose name ends with N and contains T. 查询名字以N结尾,并且名字里包含T的员工

+--------+
| name  |
+--------+
| PUTIN  |
| WALTON |
+--------+

8. Exercise

Find employees who either work as managers or work in department no 10, but not both. 查询要么是经理,要么在10号部门工作的员工,但不能同时既是经理,又在10号的部门工作

+--------+-----------+--------+
| name  | job       | deptno |
+--------+-----------+--------+
| BUSH   | PRESIDENT | 10     |
| BLAIR  | MANAGER   | 30     |
| PUTIN  | MANAGER   | 20     |
| ELISON | CLERK     | 10     |
+--------+-----------+--------+

Next Module

Drools Expert - mvel - LHS - advanced