eclipsedroolsmvel

Drl file cannot be opened because it does not exist


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


Solution

  • 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