2

I'm starting out with drools. I'm getting a class path error. Not sure, what I'm missing here. Any hints?

Main Class

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

//created on: May 27, 2013

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

Console Error

enter image description here

Project Setup enter image description here

AppSensei
  • 8,270
  • 22
  • 71
  • 99

3 Answers3

9

I would recommend you to move sample.drl to src/main/resources and then use:

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

If you decide to put your drl files in different folders inside src/main/resources, let's say salesRules and validationRules, then you should use:

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

If you still want to have your drl files inside your java packages, then try this:

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

Hope it helps

Esteban Aliverti
  • 6,259
  • 2
  • 19
  • 31
2

Just for help :If any one use code:

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

and facing this exception:

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

After a few search i resolve it by adding these line in the pom.xml

<build>
        <resources>
            <resource>
                <directory>src/main/resources/rules</directory>
            </resource>
            <resource>
                <directory>src/main/java</directory>
            </resource>
        </resources>
    </build>
siwar
  • 53
  • 7
0

In general you should keep your referenced files under source directories. For example normal files under 'src' and test files under 'test'. And they should share the common parent directory with the code that uses them.