Srik
Srik

Reputation: 2381

how to select the rule files to execute from the available rule files

I have three rule files, three classes, change-set xml and spring xml (application context) file. I want to execute files commonRules.drl and RulesForClass1.drl in Class1.java. Similarly commonRules.drl and RulesForClass2.drl in Class2.java. With my current approach all three rule files are run in both Class1 and Class2. Any help on how to modify my approach is greatly appreciated. Below I have given all the details

Rule Files:
1. commonRules.drl
2. RulesForClass1.drl
3. RulesForClass2.drl

DroolsRuleService.java:

public abstract class DroolsRuleService {
    private KnowledgeAgent knowledgeAgent; //injected through spring
    private String knowledgeLogFileName;
    }
    protected KnowledgeBase getKnowledgeBase() {
        return knowledgeAgent.getKnowledgeBase();
    }
    protected String getKnowledgeLogFileName() {
        return knowledgeLogFileName;
    }
    public void setKnowledgeAgent(KnowledgeAgent knowledgeAgent) {
        this.knowledgeAgent = knowledgeAgent;
    }
    public void setKnowledgeLogFileName(String knowledgeLogFileName) {
        this.knowledgeLogFileName = knowledgeLogFileName;
    }
}

Class1.java:

public class Class1 extends DroolsRuleService{
    @Override
    public Class1Response execute(Class1Request request) 
        throws RuleServiceException {
        Class1Response response = null;
        StatefulKnowledgeSession ksession = null;
        KnowledgeRuntimeLogger logger = null;
        try {
            ksession = getKnowledgeBase().newStatefulKnowledgeSession();
            //<code to process request goes here. Not provided intentionally>
            // Fire all rules
            ksession.fireAllRules();
            //<code to populate response goes here. Not provided intentionally>
            return response;
        } catch (Exception e) {
            // log the exception and wrap it to RuleServiceException
        } finally {
            // closes the resources
            if (ksession != null) {
                ksession.dispose();
            }
        }
    }
}

Class2.java:

public class Class2 extends DroolsRuleService{
    @Override
    public Class2Response execute(Class2Request request) 
        throws RuleServiceException {
        Class2Response response = null;
        StatefulKnowledgeSession ksession = null;
        KnowledgeRuntimeLogger logger = null;
        try {
            ksession = getKnowledgeBase().newStatefulKnowledgeSession();
            //<code to process request goes here. Not provided intentionally>
            // Fire all rules
            ksession.fireAllRules();
            //<code to populate response goes here. Not provided intentionally>
            return response;
        } catch (Exception e) {
            // log the exception and wrap it to RuleServiceException
        } finally {
            // closes the resources
            if (ksession != null) {
                ksession.dispose();
            }
        }
    }
}

applicationContext.xml (spring file)

<?xml version="1.0" encoding="UTF-8"?>  
    <beans xmlns="http://www.springframework.org/schema/beans"  
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:drools="http://drools.org/schema/drools-spring"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://drools.org/schema/drools-spring http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.0.0.xsd">

        <context:annotation-config/>

        <drools:kagent id="knowledgeAgent" kbase="knowledgeBase" new-instance="true" />

        <drools:kbase id="knowledgeBase">
            <drools:resources>
                <drools:resource type="CHANGE_SET" source="classpath:change-set.xml" />
            </drools:resources>
        </drools:kbase>
         <bean id="class1" class="<package>.Class1">
             <property name="knowledgeLogFileName" value="log/test" />
            <property name="knowledgeAgent" ref="knowledgeAgent" />
        </bean>
         <bean id="class2" class="<package>.Class2">
             <property name="knowledgeLogFileName" value="log/test" />
            <property name="knowledgeAgent" ref="knowledgeAgent" />
        </bean>

</beans>  

Change-set.xml

 <change-set xmlns='http://drools.org/drools-5.0/change-set'
             xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'
             xs:schemaLocation='http://drools.org/drools-5.0/change-set http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd' >
   <add>
        <resource source="classpath:rules/commonRules.drl" type="DRL" />
        <resource source="classpath:rules/RulesForClass1.drl" type="DRL"  />
        <resource source="classpath:rules/RulesForClass2.drl" type="DRL" />
   </add>
 </change-set>

Upvotes: 0

Views: 2965

Answers (1)

Steve
Steve

Reputation: 9480

It sounds as though you want these isolated, so just create two change sets and two knowledge agents and two knowledge bases.

The alternative when you have a single knowledge base and you wish to fire a specific selection of rules is to use agenda groups.

http://docs.jboss.org/drools/release/5.5.0.Final/drools-expert-docs/html_single/#d0e3150

Upvotes: 1

Related Questions