2
votes

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?

3
you need to escape those backslashes kbuilder.add(ResourceFactory.newClassPathResource("c:\\savedRules\\rule.drl"), ResourceType.DRL);Augusto
either escape the backslashes (as Augusto pointed) or use File.separator to contact directories and files. Also, check if your file is really written in the disk before adding it to the knowledgebase.Lucas de Oliveira

3 Answers

5
votes

Try using,

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

Thanks.

1
votes
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.

0
votes

Try the below code, this will work.

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