stefano crona
stefano crona

Reputation: 21

Drools 5 load drl rule file from file system

I devloped a custom rule editor able to create drl file and save them in file system under a given directory. (e.g. c:\savedRules\rule.drl). The problem is that once the rule is saved I need to run it with drools engine. In my class I try to load rule in this way:

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); 
kbuilder.add(ResourceFactory.newClassPathResource("c:\savedRules\rule.drl"), ResourceType.DRL);

but its doesn't work. the exception is "rule.drl cannot be opened because it does not exist" but it actually exists....

What am I doing wrong? Is there another way to load rules directly from file system?

Upvotes: 2

Views: 8836

Answers (3)

Dhaval Shah
Dhaval Shah

Reputation: 9122

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

Just add this line and copy your drl file in resource folder of the project, when you will run it will automatically find the file from the project there is no need to give specific path for your file.

Try this way, may be you can get your required result.

Upvotes: 1

Midhun P
Midhun P

Reputation: 1

Try the below code, this will work.

kbuilder.add(ResourceFactory.newFileResource(drlFileName), ResourceType.DRL);

Upvotes: 0

Nikunj
Nikunj

Reputation: 3128

Try using,

FileInputStream  fis = new FileInputStream(drlFile);
kbuilder.add(ResourceFactory.newInputStreamResource(fis), ResourceType.DRL);

Thanks.

Upvotes: 5

Related Questions