Learn Drools: Part III (Filter Facts)
Interested in Drools but not sure how to sort your data? Learn about filters, how they compare to SQL, and what you can do with them.
Join the DZone community and get the full member experience.
Join For FreeIn the previous article, we saw how Drools creates a Cartesian product between Facts if there is a HAS-A relationship between Domain Object/Facts. In this article we will learn how to filter the facts. You can think it as of using Where clause in SQL to filter the resultset.
Let's understand it with the same problem we have used in the previous article, where we created the following Drools Rule.
rule "print cross product"
when
emp: Employee();
dept: Department();
then
System.out.println(dept.getName() + "::" + emp.getName()+ ": "+emp.getDept().getName());
end
We insert two Department Objects and two Employee Objects, so when the rule fires, it creates all possible combination of employee and department — four different combinations.
Civil::Shamik Mitra: IT
IT::Shamik Mitra: IT
Civil::Samir Mitra: Civil
IT::Samir Mitra: Civil
Obviously, this is not an optimal query. We can do better than that. Say we want to filter in such a way that will fetch only those combinations where the department name matches the Employees; department name.
In SQL, it's a piece of cake:
--Where '?' is a placeholder. At runtime, we pass the value as IT or Civil.
Select * from Employee emp Department dept where emp.deptId=dept.deptId and dept.name=’?’
Now, how can we do it in a Drools Rule? Turns out that it's also a piece of take. I'll just tweak the above rules to filter the combination.
rule "filter cross product"
when
$dept: Department();
$emp: Employee(dept == $dept);
then
System.out.println($dept.getName() + "::" + $emp.getName()+ ": "+$emp.getDept().getName());
end
Please pay close attention to the rule. Here, in When, we hold the incoming Department domain Object/Fact in the $dept
reference, then check if the Department matches the Employees department.
If so, we trigger 'then.' Otherwise, we do nothing.
So if two Department Objects come as Facts, rules fire on both and check with Employee's department association. This way, we only fetch valid combinations.
Output:
Civil::Samir Mitra: Civil
IT::Shamik Mitra: IT
Notice that it only fetches the valid combinations, not all the combinations.
Quiz Time
We want to give a laptop to those people who are in the IT department and who are designated as managers.
How you write the Drools rule?
Before seeing the answer take a paper and try to write the solution.
Solution
rule "give Laptop"
when
$dept: Department(name=="IT");
$emp: Employee(dept == $dept,manager==true);
then
$emp.setMessage("Give Laptop");
System.out.println($emp.getName()+ ": "+$emp.getDept().getName()+ ":"+$emp.getMessage());
end
Here, in the When part, we want to fetch only those Department Facts or Objects whose department name property equals ‘IT’. Then, we want to find those Employees Facts/Objects whose department values match IT and whose manager property is true, Note that we use “,” in Employee (dept == $dept,manager==true). Using “,” denotes an AND operation in Drools.
The equivalent SQL would be:
Select * from Employee emp Department dept where emp.deptId=dept.deptId and dept.name=’IT’ and emp.manager=’true’
Output:
Shamik Mitra: IT:Give Laptop
Full Solution
Employee.drl
package com.rules
import com.example.droolsExample.pojo.Employee
import com.example.droolsExample.pojo.Department
rule "print cross product"
when
emp: Employee();
dept: Department();
then
//System.out.println("Fire print cross product Rule");
System.out.println(dept.getName() + "::" + emp.getName()+ ": "+emp.getDept().getName());
end
rule "filter cross product"
when
$dept: Department();
$emp: Employee(dept == $dept);
then
System.out.println($dept.getName() + "::" + $emp.getName()+ ": "+$emp.getDept().getName());
end
rule "give Laptop"
when
$dept: Department(name=="IT");
$emp: Employee(dept == $dept,manager==true);
then
$emp.setMessage("Give Laptop");
System.out.println($emp.getName()+ ": "+$emp.getDept().getName()+ ":"+$emp.getMessage());
end
Employee.java
package com.example.droolsExample.pojo;
public class Employee {
String name;
boolean manager;
String message;
Department dept;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isManager() {
return manager;
}
public void setManager(boolean manager) {
this.manager = manager;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
}
Department.java
package com.example.droolsExample.pojo;
public class Department {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
DroolTest.java
package com.example.droolsExample;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.drools.compiler.compiler.DroolsParserException;
import org.drools.compiler.compiler.PackageBuilder;
import org.drools.core.RuleBase;
import org.drools.core.RuleBaseFactory;
import org.drools.core.WorkingMemory;
import com.example.droolsExample.pojo.Department;
import com.example.droolsExample.pojo.Employee;
public class DroolsTest {
public static void main(String[] args) throws DroolsParserException,
IOException {
DroolsTest droolsTest = new DroolsTest();
droolsTest.executeDroolsEmployee();
}
public void executeDroolsEmployee() throws DroolsParserException, IOException {
PackageBuilder packageBuilder = new PackageBuilder();
String ruleFile = "/com/rules/employee.drl";
InputStream resourceAsStream = getClass().getResourceAsStream(ruleFile);
Reader reader = new InputStreamReader(resourceAsStream);
packageBuilder.addPackageFromDrl(reader);
org.drools.core.rule.Package rulesPackage = packageBuilder.getPackage();
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage(rulesPackage);
WorkingMemory workingMemory = ruleBase.newStatefulSession();
Department dep = new Department();
dep.setName("Civil");
Department dep1 = new Department();
dep1.setName("IT");
Employee emp = new Employee();
emp.setName("Shamik Mitra");
emp.setManager(true);
emp.setDept(dep1);
Employee emp1 = new Employee();
emp1.setName("Samir Mitra");
emp1.setManager(true);
emp1.setDept(dep);
workingMemory.insert(dep);
workingMemory.insert(dep1);
workingMemory.insert(emp);
workingMemory.insert(emp1);
workingMemory.fireAllRules();
}
}
Output:
Cross product:
Civil::Shamik Mitra: IT
IT::Shamik Mitra: IT
Civil::Samir Mitra: Civil
IT::Samir Mitra: Civil
Filter Cross Product
Civil::Samir Mitra: Civil
IT::Shamik Mitra: IT
Give laptop
Shamik Mitra: IT:Give Laptop
In the next article, we will discuss the Drools Inference.
Published at DZone with permission of Shamik Mitra, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments