我有三个规则文件,三个类,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;
}
}
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文件)
<?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>
更改-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>
答案 0 :(得分:1)
听起来好像你希望这些是孤立的,所以只需创建两个变更集和两个知识代理和两个知识库。
当您拥有单一知识库并希望触发特定选择的规则时,另一种方法是使用议程组。
http://docs.jboss.org/drools/release/5.5.0.Final/drools-expert-docs/html_single/#d0e3150