Drools Regensburg G1

From Training Material
Jump to navigation Jump to search

Basic

EX1

rule "Exercise 1: Select all Salgrade facts "
enabled false
when 
 $sg : Salgrade() 

then    
  System.out.println($sg.losal + " " + $sg.hisal  )

end

EX2

when 
 $e : Emp(sal >= 1000 , sal <= 2000) 
then    
  System.out.println($e.name + " " + $e.job + " " + $e.sal)
end

EX3

rule "Exercise 3: Select clerks working in department no 20."
when 
 $e : Emp(id: job == "CLERK" , deptno == 20) 

then    
  System.out.println($e.name + " " + $e.sal + " " + $e.deptno + " " + $e.job  )

end

EX4

 rule "Exercise4" enabled true
 when
     $e : Emp(mgr > 0 )
 then    
     System.out.println($e.id + " " + $e.name + " " + $e.job + "\t" + $e.mgr + " " + $e.hiredate + " " + $e.sal + " " + $e.deptno )
 end

EX5

rule "Select manager’s annual remuneration." enabled true
when 
	e : Emp(job == "MANAGER", $sal : sal)
then    
  	System.out.println(e.id + " " + e.name + "\t" + e.job  
    + "   \t" +  e.mgr  + "\t" + e.hiredate + " " + $sal*12
     + " " + e.deptno );
end

EX6

when 
 $e : Emp(name matches ".LA.*") 
then    
  System.out.println($e.name)
end

EX7

rule "Exercise 7"
enabled true
when 
 $e : Emp(name matches ".*T.*N") 

then    
  System.out.println($e.name + " " + $e.sal  )
end

EX8

rule "Exercise 8: Find employees who either work as managers or work in department no 10, but not both. "
when 
 $e : Emp((job == "MANAGER" && deptno != 10) || (job != "MANAGER" && deptno == 10)) 

then    
  System.out.println($e.name + " " + $e.sal + " " + $e.deptno + " " + $e.job + " " + $e.mgr + " " )

end
rule "Exercise 8"
when 
	$e : Emp(job == "MANAGER" || deptno == 10 , !(job == "MANAGER" && deptno == 10))

then    
	System.out.println($e.name + "\t" + $e.job + "\t" + $e.deptno);
end

Advanced

EX31

rule "Exercise 31: Select the name of the employee and the city (LOC column in DEPT table) in which they work. "
when 
 d : Dept()
 e : Emp(d.deptno == deptno) 

then    
  System.out.println(e.name + " " + d.dname + " " + d.loc  )

end

EX32

rule "Exercise 32: Select the names of the employees, and the name and number of their department. "
when 
 d : Dept()
 e : Emp(d.deptno == deptno) 

then    
  System.out.println(e.name + " " + d.dname + " " + d.deptno  )

end

EX33

rule "Exercise 33: Select the names of the employees, their salary and salary grade, but only those whose salary is more than 2000. "
when 
 e : Emp(sal > 2000) 
 s : Salgrade(e.sal >= losal, e.sal <= hisal)

then    
  System.out.println(e.name + " " + e.sal + " " + s.grade)

end

EX34

rule "Exercise 34: Select employees working in London. "
when 
 d : Dept(loc == "LONDON")
 e : Emp(d.deptno == deptno) 

then    
  System.out.println(e.name + " " + d.dname + " " + d.loc  )

end

EX35

rule "Exercise Adv 5"
enabled true
when
  $d : Dept(loc != "LONDON")
  $e : Emp($e.deptno == $d.deptno)
  $s : Salgrade($e.sal >= $s.losal, $e.sal <= $s.hisal) 
then    
  System.out.println($e.name + " " + $d.loc + " " + $s.grade )
end

EX36

rule "Exercise 36: Find departments without employees. "
when 
 d : Dept()
 not (Emp(deptno == d.deptno))

then    
  System.out.println(d.dname)

end

EX37

rule "37. Select an employee’s name and his/her boss’s name" enabled true
when 
	$emp : Emp($mgr : mgr)
	$boss : Emp(id == $mgr)
then    
  	System.out.println($boss.name + " is boss of " + $emp.name)
end

EX38

rule "Exercise 38: Select employees who doesn't have a boss. "
when 
 e : Emp() 
 not Emp(id == e.mgr) 

then    
  System.out.println(e.name )

end

EX39

rule "Exercise 39: Show all employees who have no subordinates. "
when 
 e : Emp() 
 not Emp(mgr == e.id) 

then    
  System.out.println(e.name )

end

EX41

rule "Exercise 41: Select employees hired earlier than their bosses. "
when 
 e : Emp() 
 m: Emp(id == e.mgr, e.hiredate < m.hiredate)
 

then    
  System.out.println(e.name + " was hired " + e.hiredate + " boss " + m.name + " was hired " + m.hiredate)

end

EX44

rule "Exercise Adv 44"
enabled true
when
  $d10 : Dept(deptno == 10)
  $d20 : Dept(deptno == 20)
  $emp10 : Emp(deptno == $d10.deptno)
  not Emp(deptno == $d20.deptno, job == $emp10.job)
then    
  System.out.println($emp10.job)
end
rule "44. Find jobs which are in department 10 but not in department 20." enabled true
when 
	$e : Emp(deptno == 10, $job : job)
	not Emp(deptno == 20, $job == job)
then    
  	System.out.println($job)
end

EX23

rule "Exercise 23: Find the minimal, maximal and average salaries of people employed in 1981. "
when 
accumulate (e :Emp(e.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: Find the difference between maximal and minimal salary. "
when 
accumulate (e :Emp(), 
  	$min : min(e.sal),
  	$max : max(e.sal)
  	) 

then    
  System.out.println("Range: " + ($max - $min))
  
end

EX26

rule "Exercise Adv 26"
enabled true
when
  accumulate (e :Emp(job == "MANAGER"),
  	$count : count(e.id)
  	) 
then    
  System.out.println("Count: " + $count)
end

EX25

declare SalByJob
   job : String
end

rule "25. Find the average salary for every post." enabled true
when
   	Emp($job: job) and
   	not SalByJob($job == job)
   	accumulate (
   		e :Emp(job == $job),
   		$avg : average(e.sal)
	) 
then    
   System.out.println("Position: " + $job + " average salary: " + $avg)
   insert(new SalByJob($job))
end

EX27

declare SalByDept
   deptno : Integer
end

rule "Exercise 27: Find the average annual salaries in departments. "
when 

   Emp(d: deptno) and
   not SalByDept(d == deptno)
   accumulate (e :Emp(deptno == d), 	$avg : average(e.sal*12)   	) 
   
then    
   System.out.println(   "Deptono: " + d + " avg sal: " + $avg )
   insert(new SalByDept(d))  
end

EX43

rule "Exercise Adv 43"
enabled true
when
  $e82 : Emp(hiredate==1982)
  exists Emp(hiredate == 1983, job == $e82.job)
then
  System.out.println($e82.job)
end
declare xJob
   name : String
end


rule "Exercise 43" enabled true
when
  $e1:Emp($job:job && hiredate == 1982)
  $e2:Emp(job == $job && hiredate == 1983)
  not(xJob(name == $job))
then
  System.out.println($job)
  insert(new xJob($job));
end

EX28

declare SalByDept
   deptno : Integer
end

rule "Exercise 28: Find departments with more than 3 workers. "
when 
  Emp(d: deptno) and  not SalByDept(d == deptno)
  accumulate (e :Emp(deptno == d), 
  	$count : count()   	
  ) 
  Number(intValue > 3) from $count
then    
   System.out.println(d + " " + $count)
    insert(new SalByDept(d))
end
rule "Exercise 28 Find departments with more than 3 workers." enabled true
when
  $d : Dept()
  accumulate(e:Emp(deptno == $d.deptno), $wc : count(e))
  Number(doubleValue > 3) from $wc
then
  System.out.println($d.deptno + ":" + $wc)
end

EX45

EX46

EX51

EX55