Search code examples
javadroolsrules

Dynamicaly pick the change from drool file at runtime | without maven


I am very very new to Drool, please forgive me if I have made any foolish mistake below. I am currently working on an application which is Ant based (migrating to maven is a high risk). I am trying to upgrade the drool version from 4.0.7 to 6.5.0. I want to dynamically get the updated version of the .drl file at runtime of java code so that I don't need to build the java files every time. Here is my below code.

DroolsPoc.java

package com.test;



import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.kie.api.KieServices;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;

public class DroolsPoc {

    private static AtomicBoolean isRulesChangeScanerStarted = new AtomicBoolean(
            false);

    public static void main(String[] args) throws InterruptedException {

        Map<String, Object> input = new HashMap<String, Object>();

        input.put("input", new Dto());

        KieServices ks = KieServices.Factory.get();

        KieFileSystem kfs = ks.newKieFileSystem();
        kfs.write(ResourceFactory.newFileResource(new File("C:/Users/workspace1/Drools_POC/src/poc.drl")));
        ks.newKieBuilder(kfs).buildAll();

        KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());

        KieSession kieSession = kieContainer.newKieSession();
        kieSession.insert(input);
        System.out.println("Fired all rules 1");
        kieSession.fireAllRules();

        System.out.println(((Dto) input.get("input")).getName());

        Thread.currentThread().sleep(20000);

        /*The updated rule should get printed by below code*/
        KieSession kieSession1 = kieContainer.newKieSession();
        kieSession1.insert(input);
        System.out.println("Fired all rules 2");
        kieSession1.fireAllRules();

        System.out.println(((Dto) input.get("input")).getName());

    }

}

Dto.java

package com.test;

public class Dto {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}

poc.drl

import java.util.Map;
import java.util.HashMap;
import com.test.Dto;


function Boolean checkIfNull(Map input){

  Dto dto = (Dto)input.get("input");

  if (null == dto.getName()){
      return true;
  }

      return false;

}


function void setName(Map input){

  Dto dto = (Dto)input.get("input");

  dto.setName("The moon is out");

}

rule "PopulateNameInDto"
dialect "mvel"
when
    map : HashMap() eval
    (
    checkIfNull(map)
    )
then
    setName(map);
end

The code is running fine, the only problem is, when the thread sleeps for 20 seconds, in DroolsPoc.java file at line Thread.currentThread().sleep(20000);, then I am updating the poc.drl file from "The moon is out" to "The sun is out". So I am expecting this change to reflect when the program again runs after sleep.

Any help would be appreciated, Many Thanks in advance.


Solution

  • It seems I had to recreate the service object and again import the file. worked but this way is not optimal.

    Thread.currentThread().sleep(20000);
    Map<String, Object> input1 = new HashMap<String, Object>();
    input1.put("input", new Dto());
    KieServices ks1 = KieServices.Factory.get();
    KieFileSystem kfs1 = ks1.newKieFileSystem();
    
    kfs1.write(ResourceFactory.newFileResource(new File(
            "C:/Users/workspace1/Drools_POC/src/poc.drl")));
    ks1.newKieBuilder(kfs1).buildAll();
    KieContainer kieContainer1 = ks.newKieContainer(ks.getRepository()
            .getDefaultReleaseId());
    
    KieSession kieSession1 = kieContainer1.newKieSession();
    kieSession1.insert(input1);
    System.out.println("Fired all rules 1");
    
    kieSession1.fireAllRules();
    System.out.println(((Dto) input1.get("input")).getName());