[rules-users] Drools rules not getting fired

arunodhaya80 arun at arunma.com
Mon Sep 3 05:51:44 EDT 2012


(Sorry about the repost. Was rejected earlier from the mailing list for lack
of subscription)

I am pretty new to Drools and am probably doing something really silly. The
problem is that for some reason my Drools rules are not getting fired.

If it helps, the rules fire when I shift back to a StatelessKnowledgeSession
and use the execute method to fire the rules. However, I need to use
AgendaFilter to filter rules and I understand that agenda filters aren't
supported on StatelessKnowledgeSession yet. As a first step, I just flipped
the session to a StatefulKnowledgeSession, inserted the facts and fired all
the rules.

Had to humbly say that I was breaking my head for the past couple of days on
this. Too bad, the documentation isn't that friendly for drools (I refer to
the official documentation and the user guide.

The Rule Validator code and the Spring factory bean with which I load the
Knowledge from the rule file is quoted below. I, personally, don't see any
issues with the FactoryBean. Any pointers would be of great help.

RuleValidator.java

StatefulKnowledgeSession ksession =
knowledgeBase.newStatefulKnowledgeSession();
ksession.setGlobal("validationReport", validationReport);
ksession.setGlobal("simpleCache", simpleCache);
ksession.setGlobal("ruleValidator", this);

ksession.insert(allClientDetails); //this is a list of ClientDetails objects
(facts)
ksession.fireAllRules();
ksession.dispose();

return validationReport;


KnowledgeBaseFactory.java

import java.io.IOException;
import java.util.Map;

import org.apache.log4j.Logger;
import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.Resource;
import org.drools.io.ResourceFactory;
import org.springframework.beans.factory.FactoryBean;


public class KnowledgeBaseFactory implements FactoryBean<KnowledgeBase> {

    private static Logger
logger=Logger.getLogger(KnowledgeBaseFactory.class);

    private KnowledgeBase knowledgeBase;

    public KnowledgeBaseFactory(Map<String,ResourceType> resourceMap) throws
IOException {
        logger.debug("Resource Map : "+resourceMap);
        final KnowledgeBuilder knowledgeBuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();
        for (Map.Entry<String, ResourceType>
eachRuleResource:resourceMap.entrySet()){
           
knowledgeBuilder.add(ResourceFactory.newClassPathResource(eachRuleResource.getKey()),eachRuleResource.getValue());

        }

        if (knowledgeBuilder.hasErrors()){
            logger.error(knowledgeBuilder.getErrors().toString());
            throw new
RuntimeException(knowledgeBuilder.getErrors().toString());
        }
        knowledgeBase = knowledgeBuilder.newKnowledgeBase();
       
knowledgeBase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());
    }

    @Override
    public KnowledgeBase getObject() throws Exception {
        return knowledgeBase;
    }

    @Override
    public Class<?> getObjectType() {
        return knowledgeBase.getClass();
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
} 



--
View this message in context: http://drools.46999.n3.nabble.com/Drools-rules-not-getting-fired-tp4019526.html
Sent from the Drools: User forum mailing list archive at Nabble.com.


More information about the rules-users mailing list