0
votes

I would like to load all DRL files and generated DRL through rule template in single kie session through Kie Helper. With the below config, I am able to load individual DRL file and DRL content (String). But I would like to load all DRL files and drl content (String) at the same time.

        KieHelper kieHelper = new KieHelper();
        kieHelper.addContent(drl, ResourceType.DRL);
kieHelper.addResource(ResourceFactory.newClassPathResource("com/sample/Rules.drl"), ResourceType.DRL);
        Results results = kieHelper.verify();
        KieSession session = kieHelper.build().newKieSession();

If I use this, kieHelper.addResource(ResourceFactory.newClassPathResource("com/sample/*.drl"), ResourceType.DRL); I am getting File not found exception.

Please let me know how to achieve this. I do not want to create kie session through Kie class path container. Appreciate any help on this!

1
Why don't you call kieHelper.addResource(...) repeatedly for all your .drl files? - laune
Hi Laune, I agree this is one option but there are some DRL files generated at runtime. I would like to load those as well. I tried this kieHelper.addResource(ResourceFactory.newClassPathResource("*.drl"), ResourceType.DRL); But it is looking for actual file name. Is there a way to load *.drl using Kie Helper? - Suresh
Surely you know how to match file names from directory against a path name containing a wild card? - laune
Do you mean this? kieHelper.addResource(ResourceFactory.newClassPathResource("com/sample/*.drl"), ResourceType.DRL); This is not working. - Suresh
You'll need to write a few lines of Java code. - laune

1 Answers

1
votes

If anyone looking for solution, please use this. I used Spring's PathMatchingPatternResolver to load all DRL files.

 private static KieHelper getResourceFolderFiles (String folder, KieHelper kieHelper) throws IOException {  
    ClassLoader cl =  Thread.currentThread().getContextClassLoader().getClass().getClassLoader();
        ResourcePatternResolver resolver = new
       PathMatchingResourcePatternResolver(cl);     
Resource[] resources = resolver.getResources("classpath*:com/sample/rules/**/*.drl") ;  
for (Resource resource: resources){
    kieHelper.addResource(ResourceFactory.newFileResource(resource.getFile()),
   ResourceType.DRL);
        }   }   return kieHelper;


  }