Drools Orange 13092017

From Training Material
Jump to navigation Jump to search

Basic

EX1

rule "Exercise 1"
enabled false
when 
 $s : Salgrade()

then	    
  System.out.println($s.toString())

end

EX2

rule "Exercise 2"
enabled false
when 
 $e : Emp ( sal > 1000 , sal<2000 )

then	    
  System.out.println($e.toString())

end

EX3

rule "Exercise 3"
enabled false
when 
 $e : Emp ( deptno==20 , job=="CLERK")

then	    
  System.out.println($e.toString())

end

EX4

rule "Exercise 4"
enabled false
when 
 $e : Emp ( mgr != 0)

then	    
  System.out.println($e.toString())

end

EX5

rule "Exercise 5"
enabled false
when 
 $e : Emp ( job == "MANAGER" , $s: (sal * 12))

then	    
  //$e.setSal($e.getSal*12)
  System.out.println($e.toString() + " Roczne wynagrodzenie: " + $s)

end

EX6

rule "Exercise 6"
enabled false
when 
 $e : Emp ( name matches ".LA.*")

then	    
  System.out.println($e.toString())

end

EX7

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

then	    
  System.out.println($e.toString())

end

EX8

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

then	    
  System.out.println($e.toString())

end
rule "Exercise 8"
enabled true
when 
	$e : Emp((job=="MANAGER" || deptno==10) not in ((job=="MANAGER" && deptno==10)))
then 
	  
  	System.out.println($e.toString())
end

Advanced

EX31

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

EX32

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

EX33

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

EX34

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

EX35

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

EX36

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

EX37

rule "Exercise 37"
when
   $e :Emp()
   $m :Emp(id == $e.mgr)
then    
  System.out.println( $e.name + " boss is " + $m.name )
end

EX38

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

EX39

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

EX41

rule "Exercise 41"
when
   $m :Emp()
   $e :Emp($m.id == mgr, hiredate < $m.hiredate)
then    
  System.out.println( $e.name + " was hired in " + $e.hiredate + ", their boss " + $m.name + " was hired in " + $m.hiredate )
end

EX44

rule "Exercise 44"
when
   $e :Emp(deptno == 10)
  not (Emp(deptno == 20, job == $e.job))
then    
  System.out.println( $e.job + " " + $e.deptno)
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"
enabled false
when
  accumulate (e: Emp(), 
  	$min : min(e.sal),
  	$max : max(e.sal)
  	) 
then    
  System.out.println("Salary range: " + ($max - $min))

end

EX26

rule "Exercise 26"
//enabled false
when
  accumulate ($e: Emp($e.job == "MANAGER"), 
  	$count : count() )
then    
  System.out.println("MANAGER: " + $count)

end

EX25

 declare SalByJob
   job : String
end
 
 rule "Exercise 25"
when
   Emp(j: job) and
   not SalByJob(j == job)
   accumulate (e :Emp(job == j), 	$avg : average(e.sal)   	) 
then    
   System.out.println(   "Average salary in position " + j + " is: " + $avg + "\n" )
   insert(new SalByJob(j))
end

EX27

declare Dep
   $depno : int
end

rule "Exercise 24"
when
   Emp($d: deptno) and
   not Dep($depno == $d)
   accumulate (e :Emp( deptno== $d), 	$avg : average(e.sal)   	) 
then    
   System.out.println($d + " " + ($avg*12))
   insert(new Dep($d))
end

EX43

rule "43+"
when
	e1 : Emp(hiredate == 1982)
	e2 : Emp(hiredate == 1983)
	exists (Emp(e1.job == job, e2.job == job))
then
   System.out.println( e1.job)
end
rule "Exercise 43"
enabled true
when
   $d:Emp(hiredate==1982)
   $dd:Emp($d.job==$dd.job,hiredate==1983)
then    
   System.out.println($dd.job)
   
end

EX28

rule "28 Find departments with more than 3 workers. "
when
  x: Dept(d: deptno) 
  accumulate (e :Emp(deptno == d), 
  	$count : count()
   	) 
  Number(doubleValue > 3) from $count
then    
   System.out.println(x.deptno + "  " + $count
)
end
declare Depts
   dept : Integer
end

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

EX45

rule "Exercise 45 Find employees earning more than the manager’s (job=='MANAGER') average." enabled false
when
  Number($avgNumber : longValue) from accumulate (e:Emp(job=='MANAGER'), average(e.sal))
  $e:Emp(sal > $avgNumber)
then
  System.out.println($e.name + ":" + $e.sal)
end
declare SalByJob
   $job : String
   $d: double
end

rule "Exercise 45"
enabled true
when
   Emp($j: job, job=="MANAGER") //and
   not SalByJob($j == $job)
   accumulate (e :Emp(job == $j), 	$avg : average(e.sal)   	) 
then    
   //System.out.println($j + " " + $avg)
   insert(new SalByJob($j,$avg))
end

rule "Exercise 45a"
enabled true
when 
	$dd: SalByJob()
	$e: Emp(sal>$dd.$d) 
then 
 System.out.println($e)
end

EX46

rule "Exercise 46 Select employees earning the maximum salaries in their positions (jobs)." enabled false
when
  $e:Emp()
  Number($max : longValue) from accumulate (e:Emp(job==$e.job), max(e.sal))
  Number(doubleValue == $e.sal) from $max
then
  System.out.println($e.name + " " +$e.job + ":" + $e.sal)
end

DSL

EX1

[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)
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


EX2

[condition][]There is a department which:=d: Dept()
[condition][]- is located in {city}=loc == "{city}"
[consequence][]Show department name=System.out.println(d.dname)
[condition][]- department number is greater than {cnt}=deptno > {cnt}
package rules2
import com.nobleprog.FactModel.*;
import java.lang.Math
dialect "mvel" 

expander  Exercise02.dsl

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