Wojo
Wojo

Reputation: 131

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.

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!

Upvotes: 3

Views: 521

Answers (2)

Wojo
Wojo

Reputation: 131

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.

Upvotes: 3

Eugene Shvartsman
Eugene Shvartsman

Reputation: 205

I think Geoffrey outlined the instructions here in the manual.

I haven't tried it myself(yet) but I think you would have to add the jar(with the kmodule.xml inside of it) as a dependency. I believe you can do add it directly or specify a maven dependency. Once the jar is included you can retrieve the KieBase using

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

Upvotes: 1

Related Questions