网上有很多关于如何加载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 不同规则引擎的“可插入性”是没有用的——如果你要切换到其他引擎,你的规则无论如何都必须重写)。
祝你好运 !
| 归档时间: | 
 | 
| 查看次数: | 7951 次 | 
| 最近记录: |