Drools DRL date-effective example - Java @ Desk

Wednesday, January 14, 2015

Drools DRL date-effective example


Drools DRL date-effective example

date-effective attribute in a rule definition is required if the rule needs to be fired only if the date mentioned is prior to current date.


The date format dd-mmm-yyyy is supported by default. If required, the alternate date format can also be used. In order to do so, you need to mention the required format as shown below:


System.setProperty("drools.dateformat", "dd-mmm-yyyy hh:mm");


While the rule is getting fired, if the current time is after the time mentioned in the date-effective attribute, the rule will get fired else the rule will be ignored.

What if the date provided has invalid date format. For example :
1) date-effective "36-UN-2014"
2) date-effective "36-JUN/2014"

In above case, following exception will be thrown
java.lang.IllegalArgumentException: Invalid date input format: [36-JUN/2014] it should follow: [dd-MMM-yyyy]
 at org.drools.core.util.DateUtils.parseDate(DateUtils.java:73)
 at org.drools.rule.builder.RuleBuilder.buildAttributes(RuleBuilder.java:161)
 at org.drools.rule.builder.RuleBuilder.build(RuleBuilder.java:95)
 at org.drools.compiler.PackageBuilder.addRule(PackageBuilder.java:2830)
 at org.drools.compiler.PackageBuilder.compileRules(PackageBuilder.java:970)
 at org.drools.compiler.PackageBuilder.compileAllRules(PackageBuilder.java:879)
 at org.drools.compiler.PackageBuilder.addPackage(PackageBuilder.java:871)
 at org.drools.compiler.PackageBuilder.addPackageFromDrl(PackageBuilder.java:466)
 at org.drools.compiler.PackageBuilder.addKnowledgeResource(PackageBuilder.java:694)
 at org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:51)
 at org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:40)
 at com.sample.DrlDateEffective.readKnowledgeBase(DrlDateEffective.java:32)
 at com.sample.DrlDateEffective.main(DrlDateEffective.java:19)


Complete Example Code

DrlDateEffective.drl - File contains 2 rules
1) Date Effective Valid - date-effective is AFTER Current Date
2) Date Effective InValid - date-effective is BEFORE Current Date



package com.sample
 
rule "Date Effective Valid"
date-effective "06-JUN-2014"
    when
 then
  System.out.println("Rule Fired because Current Date is AFTER date-effective");
end

rule "Date Effective InValid"
date-effective "06-JUN-2015"
    when
 then
 System.out.println("Rule Fired because Current Date is BEFORE date-effective");
end


DrlDateEffective.java
package com.sample;

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 DrlDateEffective {
 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");
            
            ksession.fireAllRules();
            logger.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

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

}


Output
Rule Fired because Current Date is AFTER date-effective








No comments:

Post a Comment