Drools Planner Example using Grails

Posted By Alok Kumar Swain | 09-Jun-2014

The Drools Engine will identify the the rule and execute the rule as per the end user demand. I am giving here the another example of the DRL file 

     Person(age > 30 && < 40 || hair == "black")
     ---
     Person(pets["rover"].type == "dog")
     ---
     forall(Bus (color=="red"))
     ---
     $owner : Person( name == "mark" )  Pet( name == "rover" ) from $owner.pets
     ---
     $zipCode : ZipCode() Person( ) from $hbn.getNamedQuery("Find People")  
                                             .setParameters( [ "zipCode" : $zipCode ] )  

 

Follow the Example, here we can plan a rule

 

We have the controller class here

API Controller

class APIController {
    
    def apiService = new APIService()

    def index() { }
        
    def job(){
        Map<String,String> params = request.parameterMap
        JSONObject jsonObject = JSONUtil.getJSONObject(params)
        render apiService.job(jsonObject)
    }
        
    def event(){
        Map<String,String> params = request.parameterMap
        JSONObject jsonObject = JSONUtil.getJSONObject(params)
        render apiService.event(jsonObject)
    }
    
    def task(){
        Map<String,String> params = request.parameterMap
        JSONObject jsonObject = JSONUtil.getJSONObject(params)
        render apiService.task(jsonObject)
    }
    
    def fund(){
        Map<String,String> params = request.parameterMap
        JSONObject jsonObject = JSONUtil.getJSONObject(params)
        render apiService.fund(jsonObject)
    }
    
}

here is the another controller for the Drool Engine before the Drool controller You must be add the jar file of the drools APIs. So import all these

just like KnowledgebaseFactory

Knowledge session

Knowledge Builder

Resource Type

Knowledge Builder Errors etc

 

 

import volcare.brms.job.Job

class RuleEngineController {
    
        
    private KnowledgeBase kbase;

    def index() {
        testBasic();
        render "index"
    }
    
    public void setup() {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        FileInputStream  fis = new FileInputStream("/home/oodles/Documents/volcare-space/volcare-brms-poc/web-app/drl/jobrecruitment.drl");
        kbuilder.add(ResourceFactory.newInputStreamResource(fis), 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.");
        }
        kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    }
    
    
    public void testBasic() {
        setup();
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
        Job job = new Job(jobName:"Job1",jobDesc: "B")
        ksession.insert(job);

.....

our Rule Domain is like this  , There are I assigned the five differnt rule names I have given..

This is the Domain class

class Rule {

    public Long ruleId;
    
    public String ruleName;
    
    public String rulePriority;
    
    public String ruleDesc;
    
    public String ruleDialect;
    
    static constraints = {
    }


    @Override
    public String toString() {
        return "Rule [ruleName=" + ruleName + ", rulePriority=" + rulePriority
                + ", ruleDesc=" + ruleDesc + ", ruleDialect=" + ruleDialect
                + "]";
    }    
    
}

 Here take one another domain class of Job

 

class Job {
    
    public String jobName;
    
    public String jobDesc;

    static constraints = {
    }


    @Override
    public String toString() {
        return "Job [jobName=" + jobName + ", jobDesc=" + jobDesc + "]";
    }    
    
    
}

and the DRL file rule is like this and  it should be in side the web-app folder

 

package test.poc

import volcare.brms.job.Job

rule "Create Job"
    when
        m : Job(jobName=="Job1")
    then
        System.out.println( m );
            modify ( m ) { jobName = "Job2",
                   jobDesc = "jodDesc2" };
        System.out.println("After update");
        System.out.println( m );           
                   
end

rule "Update Job"
    when
        m : Job(jobName=="Job2")
    then
       System.out.println("In rule 2 ---- " + m );      
end


//rule "share Job"
//end

 

 

 

 

Request for Proposal

Recaptcha is required.

Sending message..