Search code examples
javadrools

drools: access objects in drools excel decision table


The scenario is as follows:

I have a java pojo class Name having 2 variables type and name with appropriate getters and setters. I am using decision table drools and I want:

Condition: when type is set to "1"

Action: call setName setter of the name class and set the appropriate parameter from excel

Now I want to access this set value of name in java.

This is the thing i have done:

Name.java-

public class Name{
    

    String name,type;

    public String getName() {
        return name;
    }

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

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

MainClass.java- That has drools implementation!!

public class MainClass {
    
    public static StatefulKnowledgeSession session;
    public static KnowledgeBase kbase;
    public static Name name;
    public static void main(String args[])

{   
    init();
    initialiseNameObject();
    fireAllRules();
}

public static void init()
{
    System.out.println("inside init");
    KnowledgeBuilder kbuilder=KnowledgeBuilderFactory.newKnowledgeBuilder();
    DecisionTableConfiguration dconf=KnowledgeBuilderFactory.newDecisionTableConfiguration();
    
    dconf.setInputType(DecisionTableInputType.XLS);
    dconf.setWorksheetName("rulesW");
    
    kbuilder.add(ResourceFactory.newClassPathResource("/drools.xls",MainClass.class), ResourceType.DTABLE, dconf);
    
    KnowledgeBuilderErrors errors=kbuilder.getErrors();
    
    if(!errors.isEmpty())
    {
        System.out.println("Errors in package");
        Iterator i=errors.iterator();
        while(i.hasNext())
        {
            System.out.println("The error: "+i.next());
        }
    }
    
    kbase=KnowledgeBaseFactory.newKnowledgeBase();
    
    try
    {
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
        session=kbase.newStatefulKnowledgeSession();
        
        name=new Name();
    }
    catch(Exception e)
    
    {
        System.out.println("The exception "+e);
    }
    
}

public static void initialiseNameObject()
{
    

    System.out.println("insideNameObject");
    name.setType("1");
    
    
}

public static void fireAllRules()
{
    System.out.println("inside fire all");
    
    session.fireAllRules();
    
    System.out.println(name.getName());
    
}

The issue is I am not able to get the name .. always getting null for that. The name should be updated in the object and i should get ABCD as output. I guess it has to do something regarding global instances!!


Solution

  • Your code just do not make sense to me. One of the major problems is, after you have created the StatefulKnowledgeSession, you have not insert any object in the session. Without anything in the session, I doubt anything Drool can do.

    Try to do something like :

    name.setType(1);
    session.insert(name);    // Important! Drools need the objects to work on 
                             // inserted to session
    session.fireAllRules();
    

    Then in your decision table, change the action to namea.setName($param)

    I believe it should work after such changes