阅读drools休息java中的规则

时间:2015-10-29 09:07:06

标签: java rest drools rules

我正在尝试使用Jersey读取.drl在我的REST with Java(JAX-RS)中的规则。

我将.drl文件放在WEB-INF / rules文件夹中,并试图用这行代码读取它们

private RuleBase trainingRules;
private RuleBase kuperMaleTrainingResultsRules;
private RuleBase kuperFemaleTrainingResultsRules;
private RuleBase basicTrainingResultsRules;

private final String trainingRulesPath = "/WEB-INF/rules/ZatraziTrening.drl";
private final String kuperMaleTrainingResultsPath = "/WEB-INF/rules/KuperMuski.drl";
private final String kuperFemaleTrainingResultsPath = "/WEB-INF/rules/KuperZenski.drl";
private final String trainingResultsRulesPath = "/WEB-INF/rules/RezultatiTreninga.drl";

private static ControllerDrools INSTANCE;

private ControllerDrools() {
    try {
        this.trainingRules = loadRules(trainingRulesPath);
        this.kuperMaleTrainingResultsRules = loadRules(kuperMaleTrainingResultsPath);
        this.kuperFemaleTrainingResultsRules = loadRules(kuperFemaleTrainingResultsPath);
        this.basicTrainingResultsRules = loadRules(trainingResultsRulesPath);
    } catch (DroolsParserException | IOException e) {
        // TODO: dodati ispis poruke u logger
        e.printStackTrace();
    }
}

private RuleBase loadRules(String rulesPath) throws DroolsParserException, IOException {
    Reader source = new InputStreamReader(ControllerDrools.class.getResourceAsStream(rulesPath));

    PackageBuilder builder = new PackageBuilder();

    builder.addPackageFromDrl(source);

    Package pkg = builder.getPackage();

    // Kreiranje baze znanja i dodavanje paketa u nju
    RuleBase ruleBase = RuleBaseFactory.newRuleBase();
    ruleBase.addPackage(pkg);
    return ruleBase;
}

public static ControllerDrools getInstance() {
    if (INSTANCE == null) {
        INSTANCE = new ControllerDrools();
    }
    return INSTANCE;
}

/**
 * Get's the training from drools
 * @param user
 * @return training to do
 */
public Training getTrening(User user) {

    user.setNumberOfTrainingInCategory(user.getNumberOfTrainingInCategory() + 1);

    Training training = new Training();
    training.setUser(user);

    try {
        WorkingMemory workingMemory = trainingRules.newStatefulSession();

        workingMemory.insert(user);
        workingMemory.insert(training);
        workingMemory.insert(user.getCategory());

        workingMemory.fireAllRules();

    } catch (Exception e) {
        // TODO: logger
        e.printStackTrace();
    }

    return training;

}

但是我得到了

java.lang.NullPointerException
                java.io.Reader.<init>(Unknown Source)
                java.io.InputStreamReader.<init>(Unknown Source)
                loadRules(ControllerDrools.java:48)

是这一行

Reader source = new InputStreamReader(ControllerDrools.class.getResourceAsStream(rulesPath));

任何团体都知道如何阅读这条规则?把它们放在哪里?

非常感谢您阅读本文,这对我来说意义重大。

1 个答案:

答案 0 :(得分:0)

使用ControllerDrools.class.getResourceAsStream(rulesPath),您的路径应该是来自ControllerDrools类包的 relative

要使路径绝对,您应该添加一个前导斜杠&#34; /rules/ZatraziTrening.drl"

您还可以使用以下语法来获得绝对路径:

this.getClass().getClassLoader().getResourceAsStream(rulesPath)

如果您使用maven,我建议您直接将drl文件放入&#34; src / main / resources&#34;