Optaplanner integration with Workbench Drools

I have an application (network based) based on Optaplanner and it reads in scoring constraints from a static .drl file in the classpath that needs to be resolved. However, I am now trying to allow the client to create / modify rules using the Drools Workbench product. I have had no luck finding documentation or examples related to integrating Workbench rules into my application. As far as I can tell, the output of the Workbench is a jar file.

  • How do I dynamically use this jar in Optaplanner to resolve the rules in the jar file?
  • Are there any examples that I have lost?

I read this blog post by Jeffrey 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 am looking for. Thank you for your help!

+3


source to share


2 answers


After a while, I was able to read in a jar file that contains rule files and fact objects and then use them in a plan solution. so, while serving the jar file, 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 various kbase definitions in the kModule.xml file to point to the correct packages that contain the correct rules files, so they have multiple kbases in kmodule. Hope this helps others get started in the right place.

+3


source


I think Jeffrey covered the instructions here in the manual .

I haven't tried this myself (yet), but I think you will have to add the jar (using the kmodule.xml file inside it) as a dependency. I believe you can add it directly or specify a maven dependency. After enabling the banner, you can get KieBase using



KieServices kieServices = KieServices.Factory.get();
KieContainer kContainer = kieServices.getKieClasspathContainer();
KieBase kBase1 = kContainer.getKieBase("KBase1");
solverFactory.getSolverConfig().getScoreDirectorFactoryConfig.setKieBase(kBase1 );

      

+1


source







All Articles