Search code examples
droolskie

How to load all DRL files and DRL string content in single kie session


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!


Solution

  • 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;
    
    
      }