Drools rules not firing

1.2k views Asked by At

Drools rules in my project are not firing. I have checked official documentation and tutorials (not only official ones) and found nothing.

This is my rule:

package iwium.rules

import iwium.*;

rule "One"
    when
        $var : State();
    then
        $var.setName("fired!");
        System.out.println("fired!");
end

and this is how I complie rules and fire them:

public void run()
{
    createKnowledgeBase();

    //I do need to use it in infinite loop
    while (true)
    {
        kSession = kBase.newStatefulKnowledgeSession();

        State state = new State();
        kSession.insert(state);

        kSession.fireAllRules();

        //to check if rule fired
        for (Object o: kSession.getObjects()) {
            if(o instanceof State) {
                State rs = (State) o;
                if(rs.getName().length() > 1)
                    System.out.println("done");
            }
        }

        kSession.dispose();
    }
}

private void createKnowledgeBase()
{
    this.kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    this.kBuilder.add(ResourceFactory.newClassPathResource(RULES_FILE), ResourceType.DRL);

    if (kBuilder.hasErrors())
    {
        System.err.println(kBuilder.getErrors().toString());
    }

    kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addKnowledgePackages(kBuilder.getKnowledgePackages());
}

Output is empty.

I have also checked in debugger that rules are compiled properly.

Does anyone know what could be the problem here? Why this rule is not firing?

0

There are 0 answers