Search code examples
droolsoptaplannerkie-workbench

Optaplanner Integration with Drools Workbench


I have an application (web-based) based on Optaplanner and it reads in scoring constraints from a static .drl file in the classpath to solve against. However, I am now trying to allow the customer to create/change the rules via the Drools Workbench product. I am having no luck finding any documentation or examples related to integrating rules created using Workbench into my app. As far as I can tell, the output of Workbench is a jar file.

  • How do I dynamically use that jar in Optaplanner to solve against the rules in the jar file?
  • Is there any examples out there I am missing?

I read this blog post by Geoffrey De Smet (http://www.optaplanner.org/blog/2014/04/17/PutTheUserInControlOfTheScoreConstraints.html) where he suggested he would demonstrate in a future post, but hasn't yet. This is exactly what I'm looking for. Thanks for your help!


Solution

  • After a while, I was able to read in a jar file that contains rules files and fact objects, then use those in solving for a plan. so, with the jar file being served, here is the code that worked for me.

    String url = "http://<enter url to service endpoint serving jar file here>";
    
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    
    UrlResource urlResource = (UrlResource) ks.getResources().newUrlResource(url);
    InputStream is = urlResource.getInputStream();
    Resource rs = ks.getResources().newInputStreamResource(is);
    KieModule kModule = kr.addKieModule(rs);
    
    KieContainer kContainer = ks.newKieContainer(kModule.getReleaseId());
    KieBase kbase = kContainer.getKieBase("<kbase name here>");
    
    // solver factory injected
    solverFactory.getSolverConfig().getScoreDirectoryFactoryConfig().setKieBase(kbase);
    Solver solver = solverFactory.buildSolver();
    

    I am using different kbase definitions in the kModule.xml file to point to the correct packages that contain the right rules files, so have multiple kbases in the kmodule. Hope this might help others start in the right place.