网上有很多关于如何加载 Drools DRL 规则集的示例。但是,我似乎找不到任何有关如何使用 JSR94 API 加载 Excel 格式的决策表的说明或示例。
有谁知道如何做到这一点?如果是的话,您能提供一个简单的代码示例吗?
下面是我正在使用的一段示例代码。我已经标记了我怀疑某些属性需要设置并作为第二个参数传递给 createRuleExectuionSet() 的区域(尽管这可能不是解决方案)。
package com.sample;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.rules.RuleRuntime;
import javax.rules.RuleServiceProvider;
import javax.rules.RuleServiceProviderManager;
import javax.rules.StatelessRuleSession;
import javax.rules.admin.LocalRuleExecutionSetProvider;
import javax.rules.admin.RuleAdministrator;
import javax.rules.admin.RuleExecutionSet;
import org.drools.jsr94.rules.RuleServiceProviderImpl;
/**
* This is a sample class to launch a decision table.
*/
public class DecisionTableTestJsr94 {
// URL to the Decision Table file (via the classpath)
private static final String DECISION_TABLE_PATH = "/rules/Sample.xls";
// An arbitrary URI to identify the rule set
private static final String BIND_URI = "uri://fake/bind/uri";
public DecisionTableTestJsr94() throws Exception{
// Initialize the needed services
RuleServiceProviderManager.registerRuleServiceProvider(RuleServiceProviderImpl.RULE_SERVICE_PROVIDER, RuleServiceProviderImpl.class);
RuleServiceProvider ruleServiceProvider = RuleServiceProviderManager.getRuleServiceProvider(RuleServiceProviderImpl.RULE_SERVICE_PROVIDER);
RuleAdministrator ruleAdmin = ruleServiceProvider.getRuleAdministrator();
LocalRuleExecutionSetProvider ruleExecutionSetProvider = ruleAdmin.getLocalRuleExecutionSetProvider(null);
// Read the decision table
InputStream rules = this.getClass().getResourceAsStream(DECISION_TABLE_PATH);
Map ruleProperties = new HashMap();
// ** (probably something needs to happen hear with a properties Map, but what? **
RuleExecutionSet ruleExecutionSet = ruleExecutionSetProvider.createRuleExecutionSet(rules, null);
// Add the rules
ruleAdmin.registerRuleExecutionSet(BIND_URI, ruleExecutionSet, null);
// Start the rule session
StatelessRuleSession ruleSession = null;
ruleSession = (StatelessRuleSession) ruleServiceProvider.getRuleRuntime().createRuleSession(BIND_URI, null, RuleRuntime.STATELESS_SESSION_TYPE);
// Create a domain object for the test
Message message = new Message();
message.setStatus(Message.HELLO);
System.out.println("Message is: '" + message.getMessage() + "'"); // should be null
// Run the object through the rules
List<Message> inputList = new ArrayList<Message>();
inputList.add(message);
ruleSession.executeRules(inputList);
// See if the rules modified the object
System.out.println("Message is: '" + message.getMessage() + "'"); // should have the appropriate message
}
public static final void main(String[] args) throws Exception {
new DecisionTableTestJsr94();
}
}
我认为 JSR-94 提供程序尚未提供决策表实现 - 您需要使用决策表 API 将 XLS 转换为 drl 格式,然后您可以将其传递给上面的代码。
因此,如果您使用 SpreadsheetCompiler(org.drools.decisiontables 包)可以为您做到这一点 - 不幸的是,这意味着您必须导入 drools 类(不是纯 JSR-94),这样可能会达不到目的。
无论如何,JSR-94 api 非常有用的情况很少——它没有作为 API 规范发展是有原因的。可以说,与使用 JSR-94 相比,您可以用更少的代码行来实现一些主要规则引擎的“存根”(我已经做到了!)。
它对我有用的一次是当我编写一个适用于 JRules 和 Drools 的测试工具时(在这种情况下它很有用,因为我只处理数据 - 而不是规则本身 - 在上面的代码中 - JSR - 94 不同规则引擎的“可插入性”是没有用的——如果你要切换到其他引擎,你的规则无论如何都必须重写)。
祝你好运 !
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)