Accumulate function in drl drools 2013

View previous topic View next topic Go down

Accumulate function in drl drools 2013

Post by Admin on Sun Sep 15, 2013 4:03 am

2013 2013 2013 <div dir="ltr" style="text-align: left;" trbidi="on">Accumulate in drl drools:<br>Accumulate is a conditional element introduced in Drools version 4.0<br>Accumulate is used to iterate over the list of objects and allows to perform java operations to validate the data.<br><br>Consider an example,<br>HashMap object -> Employee<br>Key -> Employee Id<br>Value -> List<Integer> that represents the number of leaves taken by that employee in an year<br><br>And the check need to be applied, if the leaves goes beyond 20 then deduct 2 days salary.<br><br> Structure:<br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"><br />ResultPattern( fieldconstraint* )<br />from accumulate ( SourcePattern( fieldconstraint* )<br /> init( code )<br /> action( code )<br /> reverse( code )<br /> result( code ) )<br /><br />ResultPattern -> Expected result java class out of result block. For e.g. Integer() class in this case<br />SourcePattern -> List that needs to be iterated. For e.g. List<Integer> in this example<br />init -> init block gets executed first. You can initialize any variable here. For e.g. Initailize variable int totalNoOfLeaves = 0;<br />action -> Perform java operations in this block. For e.g. Perform addition of leaves here<br />result -> Return the result required. For e.g. Return totalNoOfLeaves variable here<br /> </code></pre>Employee.java <pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">package com.pojo; <br /> import java.util.List; <br /> import java.util.Map; <br /> public class Employee { <br /> private Map&lt;String, List&lt;Integer&gt;&gt; employeeLeavesMap; <br /> private boolean deductSalary; <br /> public boolean isDeductSalary() { <br /> return deductSalary; <br /> } <br /> public void setDeductSalary(boolean deductSalary) { <br /> this.deductSalary = deductSalary; <br /> } <br /> public Map&lt;String, List&lt;Integer&gt;&gt; getEmployeeLeavesMap() { <br /> return employeeLeavesMap; <br /> } <br /> public void setEmployeeLeavesMap(Map&lt;String, List&lt;Integer&gt;&gt; employeeLeavesMap) { <br /> this.employeeLeavesMap = employeeLeavesMap; <br /> } <br /> }<br /></code></pre>EmployeeClient.java <pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">package com.client; <br /> import java.util.ArrayList; <br /> import java.util.HashMap; <br /> import java.util.Iterator; <br /> import java.util.List; <br /> import java.util.Map; <br /> import org.drools.KnowledgeBase; <br /> import org.drools.KnowledgeBaseFactory; <br /> import org.drools.builder.KnowledgeBuilder; <br /> import org.drools.builder.KnowledgeBuilderConfiguration; <br /> import org.drools.builder.KnowledgeBuilderError; <br /> import org.drools.builder.KnowledgeBuilderErrors; <br /> import org.drools.builder.KnowledgeBuilderFactory; <br /> import org.drools.builder.ResourceType; <br /> import org.drools.builder.conf.PropertySpecificOption; <br /> import org.drools.io.ResourceFactory; <br /> import org.drools.runtime.StatefulKnowledgeSession; <br /> import com.pojo.Customer; <br /> import com.pojo.Employee; <br /> import com.pojo.GroupMap; <br /> import com.pojo.Person; <br /> public class EmployeeClient { <br /> public static void main(String args[]) throws Exception { <br /> KnowledgeBase kbase = readKnowledgeBase(); <br /> Map&lt;Integer, List&lt;Person&gt;&gt; map = new HashMap&lt;Integer, List&lt;Person&gt;&gt;(); <br /> StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); <br /> Employee employee = new Employee(); <br /> Map&lt;String, List&lt;Integer&gt;&gt; employeeLeavesMap = new HashMap&lt;String, List&lt;Integer&gt;&gt;(); <br /> List&lt;Integer&gt; integers = new ArrayList&lt;Integer&gt;(); <br /> integers.add(2); <br /> integers.add(3); <br /> integers.add(1); <br /> integers.add(5); <br /> integers.add(3); <br /> integers.add(Cool; <br /> integers.add(1); <br /> integers.add(0); <br /> integers.add(0); <br /> integers.add(0); <br /> integers.add(1); <br /> integers.add(3); <br /> employeeLeavesMap.put("Kumar", integers); <br /> employee.setEmployeeLeavesMap(employeeLeavesMap); <br /> ksession.insert(employee); <br /> ksession.fireAllRules(); <br /> ksession.dispose(); <br /> } <br /> private static KnowledgeBase readKnowledgeBase() throws Exception { <br /> KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); <br /> kbuilder.add(ResourceFactory.newClassPathResource("Employee.drl"), ResourceType.DRL); <br /> KnowledgeBuilderErrors errors = kbuilder.getErrors(); <br /> if (errors.size() &gt; 0) { <br /> for (KnowledgeBuilderError error : errors) { <br /> System.err.println(error); <br /> } <br /> throw new IllegalArgumentException("Could not parse knowledge."); <br /> } <br /> KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); <br /> kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); <br /> return kbase; <br /> } <br /> } <br /></code></pre>Employee.drl <pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">import com.pojo.*;<br />import java.util.*;<br /><br />rule "Rule Name"<br />when<br /> employee : Employee()<br /> employeeLeavesMap : Map() from employee.employeeLeavesMap<br /> $deductSalary : Number(intValue > 20) from accumulate<br /> ($t:List() from employeeLeavesMap.values , init(Integer totalNoOfLeaves = 0;),<br /> action( <br /> for(int i = 0 ; i < $t.size() ; i++) {<br /> totalNoOfLeaves = totalNoOfLeaves + (Integer) $t.get(i);<br /> }<br /> System.out.println("Total No Of Leaves taken : "+totalNoOfLeaves);<br /> ),<br /> result( new Integer(totalNoOfLeaves) ) )<br /> <br />then<br /> employee.setDeductSalary(true);<br /> System.out.println("Salary deducted for two days");<br />end<br /><br /></code></pre></div><br> 2013 2013 2013 <br><div dir="ltr" style="text-align: left;" trbidi="on">Accumulate in drl drools:<br>Accumulate is a conditional element introduced in Drools version 4.0<br>Accumulate is used to iterate over the list of objects and allows to perform java operations to validate the data.<br><br>Consider an example,<br>HashMap object -> Employee<br>Key -> Employee Id<br>Value -> List<Integer> that represents the number of leaves taken by that employee in an year<br><br>And the check need to be applied, if the leaves goes beyond 20 then deduct 2 days salary.<br><br> Structure:<br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"><br />ResultPattern( fieldconstraint* )<br />from accumulate ( SourcePattern( fieldconstraint* )<br /> init( code )<br /> action( code )<br /> reverse( code )<br /> result( code ) )<br /><br />ResultPattern -> Expected result java class out of result block. For e.g. Integer() class in this case<br />SourcePattern -> List that needs to be iterated. For e.g. List<Integer> in this example<br />init -> init block gets executed first. You can initialize any variable here. For e.g. Initailize variable int totalNoOfLeaves = 0;<br />action -> Perform java operations in this block. For e.g. Perform addition of leaves here<br />result -> Return the result required. For e.g. Return totalNoOfLeaves variable here<br /> </code></pre>Employee.java <pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">package com.pojo; <br /> import java.util.List; <br /> import java.util.Map; <br /> public class Employee { <br /> private Map&lt;String, List&lt;Integer&gt;&gt; employeeLeavesMap; <br /> private boolean deductSalary; <br /> public boolean isDeductSalary() { <br /> return deductSalary; <br /> } <br /> public void setDeductSalary(boolean deductSalary) { <br /> this.deductSalary = deductSalary; <br /> } <br /> public Map&lt;String, List&lt;Integer&gt;&gt; getEmployeeLeavesMap() { <br /> return employeeLeavesMap; <br /> } <br /> public void setEmployeeLeavesMap(Map&lt;String, List&lt;Integer&gt;&gt; employeeLeavesMap) { <br /> this.employeeLeavesMap = employeeLeavesMap; <br /> } <br /> }<br /></code></pre>EmployeeClient.java <pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">package com.client; <br /> import java.util.ArrayList; <br /> import java.util.HashMap; <br /> import java.util.Iterator; <br /> import java.util.List; <br /> import java.util.Map; <br /> import org.drools.KnowledgeBase; <br /> import org.drools.KnowledgeBaseFactory; <br /> import org.drools.builder.KnowledgeBuilder; <br /> import org.drools.builder.KnowledgeBuilderConfiguration; <br /> import org.drools.builder.KnowledgeBuilderError; <br /> import org.drools.builder.KnowledgeBuilderErrors; <br /> import org.drools.builder.KnowledgeBuilderFactory; <br /> import org.drools.builder.ResourceType; <br /> import org.drools.builder.conf.PropertySpecificOption; <br /> import org.drools.io.ResourceFactory; <br /> import org.drools.runtime.StatefulKnowledgeSession; <br /> import com.pojo.Customer; <br /> import com.pojo.Employee; <br /> import com.pojo.GroupMap; <br /> import com.pojo.Person; <br /> public class EmployeeClient { <br /> public static void main(String args[]) throws Exception { <br /> KnowledgeBase kbase = readKnowledgeBase(); <br /> Map&lt;Integer, List&lt;Person&gt;&gt; map = new HashMap&lt;Integer, List&lt;Person&gt;&gt;(); <br /> StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); <br /> Employee employee = new Employee(); <br /> Map&lt;String, List&lt;Integer&gt;&gt; employeeLeavesMap = new HashMap&lt;String, List&lt;Integer&gt;&gt;(); <br /> List&lt;Integer&gt; integers = new ArrayList&lt;Integer&gt;(); <br /> integers.add(2); <br /> integers.add(3); <br /> integers.add(1); <br /> integers.add(5); <br /> integers.add(3); <br /> integers.add(Cool; <br /> integers.add(1); <br /> integers.add(0); <br /> integers.add(0); <br /> integers.add(0); <br /> integers.add(1); <br /> integers.add(3); <br /> employeeLeavesMap.put("Kumar", integers); <br /> employee.setEmployeeLeavesMap(employeeLeavesMap); <br /> ksession.insert(employee); <br /> ksession.fireAllRules(); <br /> ksession.dispose(); <br /> } <br /> private static KnowledgeBase readKnowledgeBase() throws Exception { <br /> KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); <br /> kbuilder.add(ResourceFactory.newClassPathResource("Employee.drl"), ResourceType.DRL); <br /> KnowledgeBuilderErrors errors = kbuilder.getErrors(); <br /> if (errors.size() &gt; 0) { <br /> for (KnowledgeBuilderError error : errors) { <br /> System.err.println(error); <br /> } <br /> throw new IllegalArgumentException("Could not parse knowledge."); <br /> } <br /> KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); <br /> kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); <br /> return kbase; <br /> } <br /> } <br /></code></pre>Employee.drl <pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">import com.pojo.*;<br />import java.util.*;<br /><br />rule "Rule Name"<br />when<br /> employee : Employee()<br /> employeeLeavesMap : Map() from employee.employeeLeavesMap<br /> $deductSalary : Number(intValue > 20) from accumulate<br /> ($t:List() from employeeLeavesMap.values , init(Integer totalNoOfLeaves = 0;),<br /> action( <br /> for(int i = 0 ; i < $t.size() ; i++) {<br /> totalNoOfLeaves = totalNoOfLeaves + (Integer) $t.get(i);<br /> }<br /> System.out.println("Total No Of Leaves taken : "+totalNoOfLeaves);<br /> ),<br /> result( new Integer(totalNoOfLeaves) ) )<br /> <br />then<br /> employee.setDeductSalary(true);<br /> System.out.println("Salary deducted for two days");<br />end<br /><br /></code></pre></div><br>2013 2013 2013 <br> <a href="http://www.matrixar.com/" title="Matrix ">المصفوفة : أجمل الخلفيات والصور</a>

Admin
Admin

Posts : 64122
Join date : 2013-02-22

View user profile http://prowebsites1.forumaroc.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum