Equinox
Equinox

Reputation: 46

Fire rules on drools workbench from java application

This seems to be a popular question around drools...

I've created some rules on drools workbench and a simple java application to communicate with it.

I'm able to connect to the workbench (authentication and so on), I can retrieve the names from the set of rules but my "fireAllRules()" instruction returns zero and has no impact on my object.

However, when I add the exact same rules locally (.drl file) it runs smoothly and the result is correct.

Any ideas?

Rule (both on workbench and local):

package org1.notif;
rule "validate"
    dialect "mvel"
    when
        c : Communication( status == "Pending" , type == "Dual" )
    then
        modify( c ) {
            setStatus( "Executed" )
        }
end

Java code (for communicating with Workbench):

public static final void main(String[] args) {
  try {
    KieServices ks = KieServices.Factory.get();
    KieResources resources = ks.getResources();
    String url = "http://192.168.9.20:8080/drools-wb/maven2/org1/notif/1.0/notif-1.0.jar";
    UrlResource urlResource = (UrlResource) resources.newUrlResource(url);
    urlResource.setUsername("admin");
    urlResource.setPassword("admin");
    urlResource.setBasicAuthentication("enabled");
    InputStream stream = urlResource.getInputStream();
    KieRepository repo = ks.getRepository();
    KieModule k = repo.addKieModule(resources.newInputStreamResource(stream));
    KieContainer kc = ks.newKieContainer(k.getReleaseId());
    KieBase kBase = kc.getKieBase();
    System.out.println(kBase.getKiePackage("org1.notif").getRules());

    KieSession kSession = kBase.newKieSession();

    Communication c = new Communication();
    c.setStatus("Pending");
    c.setType("Dual");

    kSession.insert(c);
    System.out.println(kSession.fireAllRules());
    System.out.println(c.getStatus());    
    kSession.dispose();
}
(...)

Upvotes: 0

Views: 532

Answers (1)

Equinox
Equinox

Reputation: 46

The problem is in the following statement System.out.println(kSession.fireAllRules());

In order for it to work, the "fireAllRules" instruction must not be inside the println method. Rookie mistake

Upvotes: 1

Related Questions