Drools DRL Collect List Example - Java @ Desk

Thursday, April 17, 2014

Drools DRL Collect List Example

Drools DRL Collect List Example

collect in Drools drl is a reserved keyword that is used to define a collection from objects. This is being used along with the from keyword in a DRL LHS when part.

Consider a customer that holds stocks of various companies. To define a type of a customer, the customer must hold set of stocks with particular quantity as explained below

1) If a Customer holds more than 5 Stocks where the quantity of each stocks > 10, then categorize Customer as Platinum Customer
2) If a Customer holds more than 3 Stocks where the quantity of each stocks > 10, then categorize Customer as Gold Customer

This can also be used to retrieve the list of all the Gold & Platinum customers

To solve the above problem, we will use a collect keywork which checks for each stock's quantity is > 10 or not and add them to the list as shown below

For Platinum Customer -
c : Customer()
items : List( size > 5 )
 from collect( Stocks( quantity > 10 ) from c.stocks )


For Gold Customer
c : Customer()
items : List( size > 3 && size < 5)
 from collect( Stocks( quantity > 10 ) from c.stocks )


Complete DRL File
package com.sample
 
import com.sample.*;
import java.util.ArrayList;
import java.util.List;


rule "Platinum Customer"
salience 100
lock-on-active true
    when
     c : Customer()
  items : List( size > 5 ) 
       from collect( Stocks( quantity > 10 ) from c.stocks )
 then
  c.setCustType("Platinum");
        System.out.println("Customer : " + c.getCustId() + " is a Platinum Customer");
        update(c);
end

rule "Gold Customer"
salience 50
lock-on-active true
    when
     c : Customer()
  items : List( size > 3 && size < 5) 
       from collect( Stocks( quantity > 10 ) from c.stocks )
 then
  c.setCustType("Gold");
        System.out.println("Customer : " + c.getCustId() + " is a Gold Customer");
        update(c);
end




package com.sample;

public class Stocks {

 private String name;

 private int quantity;

 private double buyPrice;

 public Stocks(String name, int quantity, double buyPrice) {
  super();
  this.name = name;
  this.quantity = quantity;
  this.buyPrice = buyPrice;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getQuantity() {
  return quantity;
 }

 public void setQuantity(int quantity) {
  this.quantity = quantity;
 }

 public double getBuyPrice() {
  return buyPrice;
 }

 public void setBuyPrice(double buyPrice) {
  this.buyPrice = buyPrice;
 }

}


package com.sample;

import java.util.ArrayList;
import java.util.List;

public class Customer {

 private String custId;

 private String custType;

 private List<Stocks> stocks;

 public String getCustId() {
  return custId;
 }

 public void setCustId(String custId) {
  this.custId = custId;
 }

 public String getCustType() {
  return custType;
 }

 public void setCustType(String custType) {
  this.custType = custType;
 }

 public List<Stocks> getStocks() {
  return stocks;
 }

 public void setStocks(List<Stocks> stocks) {
  this.stocks = stocks;
 }
}


package com.sample;

import java.util.ArrayList;
import java.util.List;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

public class DrlCollect {
 public static final void main(String[] args) {
  try {
   // load up the knowledge base
   KnowledgeBase kbase = readKnowledgeBase();
   StatefulKnowledgeSession ksession = kbase
     .newStatefulKnowledgeSession();
   KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory
     .newFileLogger(ksession, "test");
   // go !
   List<Stocks> stocks = new ArrayList<Stocks>();
   stocks.add(new Stocks("Apple", 10, 100));
   stocks.add(new Stocks("Google", 35, 28));
   stocks.add(new Stocks("Larsen", 100, 780));
   stocks.add(new Stocks("TCS", 180, 1100));
   stocks.add(new Stocks("Maruti", 160, 200));
   
   Customer customer = new Customer();
   customer.setStocks(stocks);
   customer.setCustId("C1500564");
   
   
   List<Stocks> stocks2 = new ArrayList<Stocks>();
   stocks2.add(new Stocks("Apple", 10, 100));
   stocks2.add(new Stocks("Google", 35, 28));
   stocks2.add(new Stocks("Larsen", 100, 780));
   stocks2.add(new Stocks("TCS", 180, 1100));
   stocks2.add(new Stocks("Maruti", 160, 200));
   stocks2.add(new Stocks("Bhel", 60, 80));
   stocks2.add(new Stocks("NTPC", 160, 40));
   
   Customer customer2 = new Customer();
   customer2.setStocks(stocks2);
   customer2.setCustId("C1506585");
   
   ksession.insert(customer);
   ksession.insert(customer2);
   ksession.fireAllRules();
   logger.close();
  } catch (Throwable t) {
   t.printStackTrace();
  }
 }

 private static KnowledgeBase readKnowledgeBase() throws Exception {
  KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
    .newKnowledgeBuilder();
  kbuilder.add(ResourceFactory.newClassPathResource("DrlCollect.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;
 }

}








1 comment:

  1. Hi, i have a question. Ä° wonder how can i use list's each value in a function
    For example :
    when
    $list : List()
    eval(Select each item on list and send to Function)
    then
    if(eval == true write List item which one is used by Function)

    ReplyDelete