Drools DRL date-expires example - Java @ Desk

Sunday, December 28, 2014

Drools DRL date-expires example

Drools DRL date-expires example

date-expires attribute in a rule definition is required if the rule needs to be fired only if the date mentioned is after the current date. If the current date is greater than date-expires attribute, then rule will not be fired.

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 before the time mentioned in the date-expires 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-expires "36-UN-2014"
2) date-expires "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.DrlDateExpires.readKnowledgeBase(DrlDateExpires.java:32)
 at com.sample.DrlDateExpires.main(DrlDateExpires.java:19)


Complete Example Code

DrlDateExpires.drl - File contains 2 rules
1) Date Expires InValid - date-expires is BEFORE Current Date
2) Date Expires Valid - date-expires is AFTER Current Date

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

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




DrlDateExpires.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 DrlDateExpires {
 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("DrlDateExpires.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 BEFORE date-expires








1 comment: