无法打开Drl文件,因为它不存在

时间:2013-05-27 13:09:35

标签: eclipse drools mvel

我开始用流口水。我收到类路径错误。不确定,我在这里缺少什么。任何提示?

主要类

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("sample.drl"),ResourceType.DRL);
KnowledgeBase kbase = kbuilder.newKnowledgeBase();
StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
Account account = new Account(200);
account.withdraw(100);
ksession.execute(account);

}

Sample.drl

//创建日期:2013年5月27日

package com.rules

//list any import classes here.
import com.model


//declare any global variables here

rule "accountBalanceAtLeast"

    when
        //conditions
        $account : Account( balance < 100)
    then
        //actions
        System.out.println("Warning, Running out of money.");

end

控制台错误

enter image description here

项目设置 enter image description here

3 个答案:

答案 0 :(得分:7)

我建议您将sample.drl移至src/main/resources,然后使用:

kbuilder.add(ResourceFactory.newClassPathResource("sample.drl"),ResourceType.DRL);

如果您决定将drl文件放在src/main/resources内的不同文件夹中,让我们说salesRulesvalidationRules,那么您应该使用:

kbuilder.add(ResourceFactory.newClassPathResource("salesRules/rules.drl"),ResourceType.DRL); kbuilder.add(ResourceFactory.newClassPathResource("validationRules/rules.drl"),ResourceType.DRL);

如果您仍想将您的drl文件包含在Java包中,请尝试以下操作:

kbuilder.add(ResourceFactory.newClassPathResource("com/rules/sample.drl"),ResourceType.DRL);

希望有所帮助

答案 1 :(得分:1)

只是寻求帮助:如果有人使用代码:

KieFileSystem kieFileSystem = kieServices.newKieFileSystem();    
Resource resource = kieResources.newClassPathResource("EvaluationDossier.drl");
String resourcepath = "src/main/resources/rules" + "/" + "RulesCompiled.drl";
kieFileSystem.write(resourcepath, resource);

并面临此例外:

Exception in thread « main » java.lang.RuntimeException: Unable to get LastModified for ClasspathResource
..........................
Caused by: java.io.FileNotFoundException: ‘EvaluationDossier.drl’ cannot be opened because it does not exist

经过几次搜索后,我通过在pom.xml中添加这些行来解决它

<build>
        <resources>
            <resource>
                <directory>src/main/resources/rules</directory>
            </resource>
            <resource>
                <directory>src/main/java</directory>
            </resource>
        </resources>
    </build>

答案 2 :(得分:0)

通常,您应该将引用的文件保留在源目录下。例如,&#39; src&#39;下的普通文件。并在&#39; test&#39;下测试文件。并且他们应该与使用它们的代码共享公共父目录。