Drools DRL forall example - Java @ Desk

Thursday, April 17, 2014

Drools DRL forall example

Drools DRL forall example

forall is a conditional element in a DRL file that gets used in the LHS part of the rule.

forall as the name suggest applies the condition to all the facts in the working memory.

Basic example for forall is:
1) Find all the customers where the holding pattern is like : quantity of each stock > 100 units, price of each stock > 10$. Categorize such customers as Platinum
2) Find the companies where each board of director earns a monthly salary > 50000$ and years of experience < 9 Years. Categorize such company in a Fortune 500 company

Complete Code:
Refer Customer and Stocks Java Pojo classes in this post : Drools DRL Collect List Example

Client Code :

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 DroolsDrlForAll {
 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("Tata Motors", 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("Tata Motors", 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");
   
   
   List<Stocks> stocks3 = new ArrayList<Stocks>();
   stocks3.add(new Stocks("Apple", 150, 100));
   stocks3.add(new Stocks("Google", 350, 28));
   stocks3.add(new Stocks("Larsen", 120, 780));
   stocks3.add(new Stocks("TCS", 180, 1100));
   stocks3.add(new Stocks("Tata Motors", 160, 200));
   
   Customer customer3 = new Customer();
   customer3.setStocks(stocks3);
   customer3.setCustId("C1506825");
   
   ksession.insert(customer);
   ksession.insert(customer2);
   ksession.insert(customer3);
   ksession.fireAllRules();
   logger.close();
  } catch (Throwable t) {
   t.printStackTrace();
  }
 }

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

}




Below is the sample rule for 1st requirement

rule "Platinum Customer"
salience 100
lock-on-active true
    when
     c : Customer()
  forall( $stock : Stocks( quantity > 100, buyPrice > 10 ) from c.stocks)
 then
  c.setCustType("Platinum");
        System.out.println("Customer : " + c.getCustId() + " is a Platinum Customer");
        update(c);
end
Below is the output

Customer : C1506825 is a Platinum Customer





1 comment:

  1. This is really Good job. Thank you for sharing it with us! Keep posting
    it is best article for software learners

    ReplyDelete