Drools Stavanger

From Training Material
Jump to navigation Jump to search

Basic

EX1

rule "Exercise 1"
enabled false
when 
	$s : Salgrade()
then    
	System.out.println($s.grade + " " + $s.losal + " " + $s.hisal)
end

EX2

rule "Exercise 2"
when 
  e : Emp(sal > 1000, sal <2000)
then    
	System.out.println(
       e.id + " " + 
       e.name + "\t" + 
       e.job + "   \t" +
       e.mgr  + "\t" +
       e.hiredate + " " + 
       e.sal + " " + 
       e.deptno );
end

EX3

rule "Exercise 3"
enabled true
when 
	$e : Emp($e.job == "CLERK", $e.deptno  == 20)
then    
	System.out.println($e.id + " " + $e.name + " " + $e.job + " " + $e.sal)
end

EX4

rule "Exercise 4"
enabled true
when 
	$e : Emp($e.mgr != 0)
then    
	System.out.println($e.id + " " + $e.name + " " + $e.job + " " + $e.sal)
end

EX5

rule "Exercise 5"
enabled true
when 
	$e : Emp($e.job == "MANAGER")
then    
	System.out.println($e.id + " " + $e.name + " " + $e.job + " " + $e.sal*12)
end

EX6

rule "Exercise 6"
enabled true
when 
	$e : Emp(Name matches ".LA.*")
then    
	System.out.println($e.id + " " + $e.name + " " + $e.job + " " + $e.sal*12)
end

EX7

rule "Exercise 7"
when
 $e: Emp (Name matches ".*T.*N")
then
 System.out.println($e.name);
end

EX8

rule "Exercise 8"
when 
 $e : Emp((job == "MANAGER" && deptno != 10) || (job != "MANAGER" && deptno == 10)) 
then    
  System.out.println($e.id + " " + $e.name+ " " + $e.job+ " " + $e.deptno)
end

Advanced

EX31

rule "Exercise 31"
enabled false
when 
	$e : Emp()
	$d : Dept($e.deptno == deptno)
then    
	System.out.println($e.name + " " + $d.loc)
end

EX32

rule "Exercise 32"
enabled false
when 
	$e : Emp()
	$d : Dept($e.deptno == deptno)
then    
	System.out.println($e.name + " " + $d.dname+ " " + $d.deptno)
end

EX33

rule "Exercise 33"
enabled false
when 
	$e : Emp()
	$s : Salgrade($e.sal <= hisal, $e.sal >= losal, $e.sal > 2000)
then    
	System.out.println($e.name + " " + $e.sal+ " " + $s.grade)
end

EX34

rule "Exercise 34"
enabled false
when 
	$e : Emp()
	$d : Dept($e.deptno == deptno, loc == "LONDON")
then    
	System.out.println($e.name + " " + $d.loc)
end

EX35

rule "Exercise 35"
when 
 d: Dept (dn: deptno, l:loc != "LONDON")
 e: Emp (deptno == d.deptno, s: sal) 
 Salgrade( losal <= s, hisal >= s, sg: grade)
then    
  System.out.println(e.name + "\t"+ l + "\t" + sg)
end

EX36

rule "Exercise 36"
enabled false
when
 $de : Dept ()
 not ( Emp (deptno == $de.deptno) )
then
 System.out.println ( $de.dname )
end

EX37

rule "Exercise 37"
enabled false
when
 $e1 : Emp ( $ma : mgr)
 $e2 : Emp ( $ma == id )
then
 System.out.println ( $e1.name + "'s Boss is " + $e2.name)
end

EX38

rule "Exercise 38"
enabled false
when
 $e : Emp ( $n : name , mgr == 0 )
then
 System.out.println ( $n )
end
rule "Exercise 38"
when
   $e :Emp()
   not (Emp(id == $e.mgr))
then    
  System.out.println( $e.name)
end

EX39

rule "Exercise 39"
enabled false
when
 $e : Emp ()
 not ( Emp ( mgr == $e.id) )
then
 System.out.println ( $e.name )
end

EX41

rule "Exercise 41"
enabled false
when
 $e1 : Emp ()
 $e2 : Emp ( $e1.mgr == id , $e1.hiredate < hiredate )
then
 System.out.println ( $e1.name + " was hired in " + $e1.hiredate + ", her boss " + $e2.name + " who was hired in " + $e2.hiredate )
end

EX44

rule "Exercise 44"

when
 $e : Emp ( deptno == 10 )
 not ( Emp ( deptno == 20 , job == $e.job) )
then
 System.out.println ( "Position in Department 10 and not in Department 20 is " + $e.job )
end

EX23

rule "Exercise 23"
//enabled false
when 
  accumulate (e :Emp(hiredate == 1981), 
  	$avg : average(e.sal),
  	$min : min(e.sal),
  	$max : max(e.sal)
  	) 
 
then    
	 System.out.println(
   "Average salary: " + $avg + "\n" +
  "Min salary: " + $min + "\n" +
  "Max salary: " + $max + "\n"
  )
end

EX24

rule "Exercise 24 Adv"
when 
 accumulate (e: Emp(), min : min(e.sal), max : max(e.sal)) 
then    
  System.out.println("Difference: " + (max - min))
end

EX26

rule "Exercise 26"
when 
 accumulate ( e: Emp(job == "MANAGER"), c: count() )
then    
  System.out.println("There are " + c + " Managers")
end

EX25

EX27

declare SalByDept
   dept : int
end

rule "Exercise 27"
when
   Emp(dn : deptno) and
   not SalByDept(dept == dn)
   accumulate (e :Emp(deptno ==dn), 	$avg : average(e.sal*12)  ) 
then    
   System.out.println(   "Average salary in department " + dn + " is: " + $avg + "" )
   insert(new SalByDept(dn))
end

EX43

EX28

EX45

EX46

EX51

EX55

inserLogical

rule

import com.nobleprog.FactModel.*
dialect "mvel" 

declare AccessToSecretFile
    emp : Emp
end

rule "Grant access to employees of Dept. 10"
when 
    e : Emp( deptno == 10 )
then    
    insertLogical( new AccessToSecretFile(e) )
end

jUnit

Emp $blair = new Emp(7698, "BLAIR", "MANAGER", 7839, 1981, 2850, 10);
FactHandle $blairFH = ksession.insert($blair);

KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger((KnowledgeRuntimeEventManager) ksession, "/tmp/logicalinsert_exercise");
		
ksession.fireAllRules();
$blair.setDeptno(30);
ksession.update($blairFH, $blair);
ksession.fireAllRules();


DSL

Exercise 1

dslr

package rules1
import com.nobleprog.FactModel.*;
import java.lang.Math
dialect "mvel" 

expander  Exercise01.dsl

rule "Rule 1"
 
when
 There are employees earning more than 2000
then
 Show name and salary

end

dsl

[condition][]There are employees earning more than {salary}=e:Emp(sal > {salary})
[*][]Show name and salary=System.out.println("Name: " + e.name + " Salary: " + e.sal)

Exercise 2

dslr

rule "Rule 2"
when
 There is a department which
	- is located in LONDON
	- department number is greater than 5
then
 	Show department name
end

dsl

[condition][]There is a department which=d: Dept()
[condition][]- is located in {location}=loc == "{location}"
[condition][]- department number is greater than {nr}=deptno > {nr}
[consequence][]Show department name=System.out.println(d.dname)

Exercise 3

dslr

rule "Rule 3"
when
// Make sure that the rule will work for both cases
 There are departments where:
//  There is a department where:
	there are managers working in 
	//also there is an anlyst working in it
then
 Show department name
 
end

dsl

[Condition][]There are departments where:= d: Dept()
[Condition][]There is a department where:= d: Dept()
[Condition][]there are {job}s working in = Emp (job == "{job!uc}", deptno==d.deptno)
[Condition][]also there is an {job} working in it = Emp (job == "{job!uc}", deptno==d.deptno)
[Consequence][]Show department name= System.out.println(d.dname)