Drools exercises solutions
Jump to navigation
Jump to search
Basic
Ex1
rule "Exercise 1 Select all Salgrade facts"
when
$s : Salgrade()
then
System.out.println($s.grade + '\t' + $s.losal + '\t' + $s.hisal)
end
Ex2
//Select shown data about employees who earn between 1000 and 2000.
rule "Ex-2"
when
$e : Emp(sal > 1000, sal < 2000)
then
System.out.println($e.id + " " + $e.sal );
end
Ex3
// Select clerks working in department no 20.
rule "showclerkofdeptno20" enabled true
when
$e: Emp(job == "CLERK" && deptno == 20)
then
System.out.println($e.id + " " + $e.name + " " + $e.job + " " + $e.mgr + " " + $e.hiredate + " " + $e.sal + " " + $e.deptno)
end
Ex4
rule "Sample3" enabled false
when
$e:Emp(mgr!=0)
then
System.out.println($e.id + " " + $e.name + "\t" + $e.deptno+ "\t" + $e.sal )
end
Ex4 ver 2
rule "Ex4"
when
$e: Emp($mgr :mgr)
not Emp(id == $mgr)
//not (exists Emp(id == $mgr))
then
System.out.println($e.name)
end
EX5
rule "showmgrannualrem"
when
$e: Emp(job == "MANAGER")
then
System.out.println($e.id + " " + $e.name + " " + $e.job + " " + $e.mgr + " " + $e.hiredate + " " + $e.sal*12 + " " + $e.deptno)
end
EX5 ver 2
rule "showmgrannualrem"
when
$e : Emp( job == "MANAGER",$sal : sal )
then
System.out.println($e.name + " " + $sal * 12 )
end
EX6
// Find employees with second letter L and third A
rule "EX6" enabled false
when
$e: Emp(name matches '.LA.*')
then
System.out.println($e.name);
end
EX7
rule "Find employees whose name contains T and ends with N"
when
$e:Emp(name matches ".*T.*N")
then
System.out.println(" matches L.*$N")
System.out.println($e.id+" "+$e.name+" "+$e.job+" "+$e.sal)
end
Ex8
// Find employees who either work as managers or work in department no 10, but not both.
rule "Ex_08"
when
$e : Emp ((deptno == '10' || $e.job == 'MANAGER') && !(deptno == '10' && $e.job == 'MANAGER'))
//$e : Emp( (job == "MANAGER" && deptno != 10) || ( deptno == "10" && job not in ("MANAGER") ) )
then
System.out.println("Ex_08-" + $e.name + "job-" + $e.job +" salgrade="+ $e.deptno) ;
end
Ex8 version 2
rule "Find employees who either work as managers or work in department no 10, but not both" enabled true
when
$e: Emp((job == 'MANAGER' && deptno != 10) || (deptno == 10 && job != 'MANAGER'))
then
System.out.println($e.name + " " + $e.job + " " + $e.deptno);
end
Advanced
Ex23
rule "excersie23"
when
accumulate(e:Emp(hiredate==1981),$avg : average(e.sal),$minsal:min(e.sal),$maxsal:max(e.sal))
then
System.out.println( "Average salary " +$avg +"Min salary " +$minsal+"max salary " +$maxsal )
end
Ex24
// Find the difference between maximal and minimal salary
rule "EXE 24" enabled true
when
accumulate ($e :Emp(), $min : min($e.sal), $max : max($e.sal))
then
System.out.println("Difference between maximal and minimal salary: " + ($max - $min) + "\n");
end
Ex25
// Find the average salary for every post.
declare SalByJob1
job : String
//avgSal : Double
end
rule "EXE 25" enabled true
when
Emp($j: job) and
not SalByJob1($j == job)
accumulate ($e :Emp(job == $j), $avg : average($e.sal) )
then
System.out.println( $j + " " + $avg + "\n");
//insert(new SalByJob($j,$avg))
insert(new SalByJob1($j))
end
Ex26
// How many managers work for the company?
rule "EXE 26" enabled true
when
accumulate ($e :Emp(job == 'MANAGER'), $cnt : count($e.job))
then
System.out.println("How many managers work for the company: " + $cnt + "\n");
end
Ex27
declare SalByDept
deptno : Integer
end
rule "EX 27 Find the average annual salaries in departments" enabled false
when
Emp(d: deptno) and
not SalByDept(d == deptno)
accumulate (e :Emp(deptno == d), $avg : average(e.sal*12))
then
System.out.println("Salaries in dept " + d + " " + $avg)
insert(new SalByDept(d))
end
Ex28
declare EmpByDept
deptno : Integer
end
rule "EX 28 Find departments with more than 3 workers."
when
Emp(d: deptno) and
not EmpByDept(d == deptno)
accumulate (e :Emp(deptno == d), cn : count(e.id))
Number(intValue > 3) from cn
then
System.out.println("Employees in dept " + d + ": " + cn)
insert(new EmpByDept(d))
end
Exe31
rule "EmployeeLocation31" enabled false
when
d :Dept()
e :Emp(d.deptno == deptno)
then
System.out.println(e.name + "\t" + d.loc)
end
Exe32
rule "Ex_32"
when
e : Emp()
d : Dept( e.deptno == deptno);
then
System.out.println("----------------------");
System.out.println(
e.name + " \t" + d.dname + " \t" + e.deptno);
end
Exe33
rule "salarygrade33" enabled false
when
s :Salgrade()
e :Emp(e.sal>=2000,e.sal>=s.losal,e.sal<=s.hisal)
then
System.out.println(e.name + "\t" + e.sal+"\t"+s.grade)
end
Exe33 ver 2
rule "salarygrade33" enabled false
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
Exe34
rule "workingLondon34" enabled false
when
d :Dept(loc=='LONDON')
e :Emp(d.deptno == deptno)
then
System.out.println(e.name + "\t" + d.loc);
end
Exe35
rule "Ex-35"
when
$e : Emp()
$d : Dept(deptno == $e.deptno, loc != 'LONDON')
$s : Salgrade($e.sal > losal, $e.sal < hisal)
then
System.out.println($e.name+" "+$d.loc+" "+$s.grade );
end
Exe36
rule "36 Exercise"
enabled false
when
d:Dept()
not (Emp(deptno == d.deptno))
then
System.out.println( d.dname )
end
Exe37
rule "37 Exercise"
enabled false
when
d:Emp()
e:Emp(d.id==e.mgr)
then
System.out.println( e.name +"boss is "+ d.name);
end
Exe39
rule "39 Exercise"
when
d:Emp()
not (Emp(d.id==mgr))
then
System.out.println( d.name);
end
Exe41
rule "Ex-41"
when
$e1 : Emp()
$e2 : Emp(id == $e1.mgr, hiredate > $e1.hiredate)
then
System.out.println( $e1.name+" "+ $e2.name);
end
Exe43
declare FilledJobs
job : String
end
rule "Exercise43"
when
e :Emp(e.hiredate in(1982 ,1983 )) && not FilledJobs(e.job == job)
then
System.out.println(e.job)
insert(new FilledJobs(e.job))
end
Exe44
rule "Exercise44"
when
e :Emp(e.deptno ==10)
not (Emp (e.job == job && deptno ==20))
then
System.out.println(e.job)
end
Exe45
rule "Ex 45 Find employees earning more than the manager’s (job=='MANAGER') average." enabled true
when
Number($avg :longValue ) from accumulate (Emp(s: sal, job == "MANAGER"), average(s))
e : Emp(sal > $avg)
then
System.out.println(e.name + " " + e.sal)
end
Exe46
Solution 1
declare xJob
name : String
end
rule "Ex 46 Find Select employees earning the maximum salaries in their positions (jobs)." enabled true
//doesn't work if two people earn max values (TOOSK and CARNEGIE - both at analys positions)
when
Emp($j :job) and not(xJob(name == $j))
Number($max :longValue) from accumulate (Emp(s: sal,job == $j ), max(s))
e : Emp(job == $j,sal == $max)
then
System.out.println(e.name + "\t" + e.deptno + "\t" + e.sal)
insert(new xJob($j));
end
Solution 2
declare jobPost2
job: String
max: Double
end
rule "Exercise 46"
when
Emp(j:job)
and not jobPost2(j == job)
Number(max:doubleValue) from accumulate(Emp(s:sal, job==j), max(s))
then
insert(new jobPost2(j, max))
end
rule "Display"
when
jp: jobPost2(j: job, m: max)
e: Emp (e.sal == m, e.job == j)
then
System.out.println(e.name + " " + e.sal)
endExe51
rule "Ex 51 Find employees earning more than the average salary in their department."
when
Dept(dn :deptno)
Number($avg :longValue) from accumulate (Emp(s: sal,deptno == dn ), average(s))
e : Emp(deptno == dn,sal > $avg)
then
System.out.println(e.name + "\t" + e.deptno + "\t" + e.sal)
end
Exe55
rule "Ex 55" enabled true
when
$d: Dept(dn :deptno)
Number($avg :longValue ) from accumulate (Emp(s: sal,deptno == dn ), average(s))
then
insert (new DepsAvg($d, $avg))
//System.out.println(($maxAvg))
end
rule "Ex 55 second step" enabled true
when
Number($max :longValue ) from accumulate ($da :DepsAvg() , max($da.depAverage))
then
System.out.println($max)
end