Drools DRL Date comparision syntax in LHS - Java @ Desk

Sunday, December 15, 2013

Drools DRL Date comparision syntax in LHS

Drools DRL Date comparision syntax in LHS

There are three ways to compare Date in LHS part of DRL rule as shown below :
a) Using '>' greater than or '>' less than operator
b) Using java.util.Date class methods .after() and .before()
c) Using java.util.Date class methods .compareTo()

Below is the implementation for three different types of rule for date comparision. It includes
1) Sample Pojo class with Date fields dateOne and dateTwo
2) Client file to create the Stateful Knowledge Session in which Pojo class object is created. Date fields are set to some default values.
dateOne is set to - Tue Jun 25 10:30:45 IST 2013
dateTwo is set to - Thu Jun 20 10:30:45 IST 2013
3) DRL file for 3 different rule implementations

 package com.sample;  
 import java.util.Date;  
 import java.util.List;  
 import java.util.Map;  
 public class Pojo {  
      private String name;  
      private String address;  
      private Date dateOne;  
      private Date dateTwo;  
      private Map<String, List<String>> cityPlaces;  
      public Map<String, List<String>> getCityPlaces() {  
           return cityPlaces;  
      }  
      public void setCityPlaces(Map<String, List<String>> cityPlaces) {  
           this.cityPlaces = cityPlaces;  
      }  
      public String getName() {  
           return name;  
      }  
      public void setName(String name) {  
           this.name = name;  
      }  
      public String getAddress() {  
           return address;  
      }  
      public void setAddress(String address) {  
           this.address = address;  
      }  
      public Date getDateOne() {  
           return dateOne;  
      }  
      public void setDateOne(Date dateOne) {  
           this.dateOne = dateOne;  
      }  
      public Date getDateTwo() {  
           return dateTwo;  
      }  
      public void setDateTwo(Date dateTwo) {  
           this.dateTwo = dateTwo;  
      }  
 }  

 package com.sample;  
 import java.util.Calendar;  
 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 DRLDateComparision {  
      public static final void main(String[] args) {  
     try {  
       // load up the knowledge base  
       KnowledgeBase kbase = readKnowledgeBase();  
       StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();  
       Pojo pojo = new Pojo();  
       Calendar calendar = Calendar.getInstance();  
       calendar.set(2013, 5, 25, 10, 30, 45);  
       pojo.setDateOne(calendar.getTime());  
       calendar.set(2013, 5, 20, 10, 30, 45);  
       pojo.setDateTwo(calendar.getTime());  
       ksession.insert(pojo);  
       ksession.fireAllRules();  
       ksession.dispose();  
     } catch (Throwable t) {  
       t.printStackTrace();  
     }  
   }  
   private static KnowledgeBase readKnowledgeBase() throws Exception {  
           KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();  
           kbuilder.add(ResourceFactory.newClassPathResource("DRLDateComparision.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;  
      }  
 }  

 package com.sample  
 import java.util.Date;  
 rule "Date Comparision using greater or less than operator"  
 salience 90  
   when  
           $pojo : Pojo(dateOne > dateTwo)  
   then  
        System.out.println(drools.getRule().getName());  
     System.out.println("Date One - " + $pojo.getDateOne());  
     System.out.println("is greater than");  
     System.out.println("Date Two - " + $pojo.getDateTwo());  
 end





 rule "Date Comparision using Date Function .after()/.before()"  
 salience 85  
   when  
           $pojo : Pojo(dateOne.after(dateTwo) || dateTwo.before(dateOne))  
   then  
        System.out.println("\n"+drools.getRule().getName());  
     System.out.println("Date One - " + $pojo.getDateOne());  
     System.out.println("is after");  
     System.out.println("Date Two - " + $pojo.getDateTwo());  
 end  
 rule "Date Comparision using Date Function compareTo"  
 salience 80  
   when  
           $pojo : Pojo(dateOne.compareTo(dateTwo) == 1)  
   then  
        System.out.println("\n"+drools.getRule().getName());  
     System.out.println("Date One - " + $pojo.getDateOne());  
     System.out.println("is after");  
     System.out.println("Date Two - " + $pojo.getDateTwo());  
 end  








No comments:

Post a Comment