Drools Regensburg G1
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 )
endEX2
when
$e : Emp(sal >= 1000 , sal <= 2000)
then
System.out.println($e.name + " " + $e.job + " " + $e.sal)
endEX3
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 )
endEX4
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 )
endEX5
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 );
endEX6
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 )
endEX8
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 + " " )
endrule "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);
endAdvanced
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 )
endEX32
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 )
endEX33
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)
endEX34
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 )
endEX35
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 )
endEX36
rule "Exercise 36: Find departments without employees. "
when
d : Dept()
not (Emp(deptno == d.deptno))
then
System.out.println(d.dname)
endEX37
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)
endEX38
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 )
endEX39
rule "Exercise 39: Show all employees who have no subordinates. "
when
e : Emp()
not Emp(mgr == e.id)
then
System.out.println(e.name )
endEX41
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)
endEX44
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)
endrule "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)
endEX23
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")
endEX24
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))
endEX26
rule "Exercise Adv 26"
enabled true
when
accumulate (e :Emp(job == "MANAGER"),
$count : count(e.id)
)
then
System.out.println("Count: " + $count)
endEX25
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))
endEX27
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))
endEX43
rule "Exercise Adv 43"
enabled true
when
$e82 : Emp(hiredate==1982)
exists Emp(hiredate == 1983, job == $e82.job)
then
System.out.println($e82.job)
enddeclare 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));
endEX28
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))
endrule "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