MVEL basic

From Training Material
Jump to navigation Jump to search

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  |
+-------+-------+-------+



Comments ⌘

Use double forward slash to comment a whole line

// This is commented line

Use C langue comment notation (/* and */) to comment some text inside a line or a couple of lines.

So please comment your answer to exercises, for example

/**
 * Exercise 4
 */
when 
	e : Emp()
then
   ....

Filtering

Filter by Fact Type ⌘

Find all facts of Emp type

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 ⌘

Filter employees earning more than 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 (+, -, *, /, %, MOD, DIV)
  • Relational operators (>, >=, ==, !=)
  • Logical operators
    • conjunction (and, &&, ",")
    • disjunction (or, ||)
    • negation (!, do not confuse with not)
  • Drools operators (in, matches, etc...)

Relational operators ⌘

Find 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

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

Select all except 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)

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.

Select people working in department no 10 or 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!

Select managers or 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 ⌘

The 'maches' operator checks whether the values match the pattern (java regular expression).

.* matches any number of characters, even an empty string.
. matches exactly one character

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

Select people with names starting with "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’.

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 ⌘

 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.

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

1. Exercise

Select all Salgrade facts

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.

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.

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

6. Exercise

Find employees with second letter “L” and third “A”.

 BLAIR

7. Exercise Find employees whose name ends with N and contains T.

 PUTIN  
 WALTON 

8. Exercise

Find employees who either work as managers or work in department no 10, but not both.

 BUSH    PRESIDENT  10     
 BLAIR   MANAGER    30     
 PUTIN   MANAGER    20     
 ELISON  CLERK      10