0

我有三个规则文件、三个类、change-set xml 和 spring xml(应用程序上下文)文件。我想在 Class1.java 中执行文件 commonRules.drl 和 RulesForClass1.drl。Class2.java 中的 commonRules.drl 和 RulesForClass2.drl 类似。使用我目前的方法,所有三个规则文件都在 Class1 和 Class2 中运行。非常感谢有关如何修改我的方法的任何帮助。下面我已经给出了所有的细节

规则文件:
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;
    }
}

类1.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();
            }
        }
    }
}

类2.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(弹簧文件)

<?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>  

更改集.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>
4

1 回答 1

1

听起来好像您希望这些隔离,因此只需创建两个变更集、两个知识代理和两个知识库。

当您只有一个知识库并且您希望触发特定的规则选择时,另一种选择是使用议程组。

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

于 2013-11-13T14:11:02.060 回答