Accumulate in drl drools:
Accumulate is a conditional element introduced in Drools version 4.0
Accumulate is used to iterate over the list of objects and allows to perform java operations to validate the data.
Consider an example,
HashMap object -> Employee
Key -> Employee Id
Value -> List that represents the number of leaves taken by that employee in an year
And the check need to be applied, if the leaves goes beyond 20 then deduct 2 days salary.
Structure:
Accumulate is a conditional element introduced in Drools version 4.0
Accumulate is used to iterate over the list of objects and allows to perform java operations to validate the data.
Consider an example,
HashMap object -> Employee
Key -> Employee Id
Value -> List
And the check need to be applied, if the leaves goes beyond 20 then deduct 2 days salary.
Structure:
ResultPattern( fieldconstraint* )
from accumulate ( SourcePattern( fieldconstraint* )
init( code )
action( code )
reverse( code )
result( code ) )
ResultPattern -> Expected result java class out of result block. For e.g. Integer() class in this case
SourcePattern -> List that needs to be iterated. For e.g. List in this example
init -> init block gets executed first. You can initialize any variable here. For e.g. Initailize variable int totalNoOfLeaves = 0;
action -> Perform java operations in this block. For e.g. Perform addition of leaves here
result -> Return the result required. For e.g. Return totalNoOfLeaves variable here
Employee.java
package com.pojo;
import java.util.List;
import java.util.Map;
public class Employee {
private Map<String, List<Integer>> employeeLeavesMap;
private boolean deductSalary;
public boolean isDeductSalary() {
return deductSalary;
}
public void setDeductSalary(boolean deductSalary) {
this.deductSalary = deductSalary;
}
public Map<String, List<Integer>> getEmployeeLeavesMap() {
return employeeLeavesMap;
}
public void setEmployeeLeavesMap(Map<String, List<Integer>> employeeLeavesMap) {
this.employeeLeavesMap = employeeLeavesMap;
}
}
EmployeeClient.java
package com.client;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderConfiguration;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.builder.conf.PropertySpecificOption;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import com.pojo.Customer;
import com.pojo.Employee;
import com.pojo.GroupMap;
import com.pojo.Person;
public class EmployeeClient {
public static void main(String args[]) throws Exception {
KnowledgeBase kbase = readKnowledgeBase();
Map<Integer, List<Person>> map = new HashMap<Integer, List<Person>>();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
Employee employee = new Employee();
Map<String, List<Integer>> employeeLeavesMap = new HashMap<String, List<Integer>>();
List<Integer> integers = new ArrayList<Integer>();
integers.add(2);
integers.add(3);
integers.add(1);
integers.add(5);
integers.add(3);
integers.add(8);
integers.add(1);
integers.add(0);
integers.add(0);
integers.add(0);
integers.add(1);
integers.add(3);
employeeLeavesMap.put("Kumar", integers);
employee.setEmployeeLeavesMap(employeeLeavesMap);
ksession.insert(employee);
ksession.fireAllRules();
ksession.dispose();
}
private static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("Employee.drl"), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error : errors) {
System.err.println(error);
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
}
Employee.drl
import com.pojo.*;
import java.util.*;
rule "Rule Name"
when
employee : Employee()
employeeLeavesMap : Map() from employee.employeeLeavesMap
$deductSalary : Number(intValue > 20) from accumulate
($t:List() from employeeLeavesMap.values , init(Integer totalNoOfLeaves = 0;),
action(
for(int i = 0 ; i < $t.size() ; i++) {
totalNoOfLeaves = totalNoOfLeaves + (Integer) $t.get(i);
}
System.out.println("Total No Of Leaves taken : "+totalNoOfLeaves);
),
result( new Integer(totalNoOfLeaves) ) )
then
employee.setDeductSalary(true);
System.out.println("Salary deducted for two days");
end
No comments:
Post a Comment