Uploaded image for project: 'OptaPlanner'
  1. OptaPlanner
  2. PLANNER-504

Expose SolverFactory in the KIE API so it can be loaded from a KieContainer

XMLWordPrintable

    • Icon: Feature Request Feature Request
    • Resolution: Done
    • Icon: Critical Critical
    • 6.4.0.Beta1
    • None
    • optaplanner-core
    • None
    • NEW
    • NEW

      Goal: Make a solver configuration XML file, a real member of a kjar. So it can be loaded ad hoc with kie-ci through maven.
      For example: in workbench we create a cloudBalancing.solver file and deploy it to a maven repo. Our client app can now run this solver easily without having that file cloudBalancing.solver in it's original classpath - instead it loads it based on a maven GAV.

      Presume we have these files:

      org/cb/cloudBalancing.solver (the solver configuration)

              <solver>
                <scoreDirectorFactory>
                  <ksessionName>cloudBalancingKsession</ksessionName><!-- Value matches in kmodule.xml -->
                </scoreDirectorFactory>
                ...
              </solver>
      

      META-INF/kmodule.xml

              <kmodule>
                <kbase ...>
                  <ksession name="cloudBalancingKsession"/><!-- the Drools rules -->
                </kbase>
              </kmodule>
      

      The code above already works. But how would an API usage look like?

      Proposal A) SolverFactory.createFromXmlResource(KieContainer, String)

      org/cb/App.java

              KieServices kieServices = KieServices.Factory.get();
              KieContainer kieContainer = kieServices.getKieClasspathContainer();
              SolverFactory<CloudBalance> solverFactory = SolverFactory.createFromXmlResource(
                      kieContainer, "org/cb/cloudBalancing.solver");
      

      Proposal B) KieContainer.getSolverFactory(String) which returns Object

      org/cb/App.java

              KieServices kieServices = KieServices.Factory.get();
              KieContainer kieContainer = kieServices.getKieClasspathContainer();
              SolverFactory<CloudBalance> solverFactory = (SolverFactory) kieContainer.getSolverFactory(
                      "org/cb/cloudBalancing.solver");
              // Note: the ugly cast to SolverFactory is unavoidable because we can't move SolverFactory out of optaplanner-core [1]
      

      Proposal C) A SolverFactory is a KieBase somehow and a Solver is a KieSession somehow.

      META-INF/kmodule.xml

              <kmodule>
                <kbase packages="org.cb.rules">
                  <ksession name="cloudBalancingKsession"/><!-- the Drools rules -->
                </kbase>
                <kbase packages="org.cb" file="cloudBalancing.solver">
                  <ksession name="cloudBalancingSolverFactory"/><!-- the Solver Configuration rules -->
                </kbase>
              </kmodule>
      

      org/cb/App.java

              KieServices kieServices = KieServices.Factory.get();
              KieContainer kieContainer = kieServices.getKieClasspathContainer();
              KieSession kSession = kieContainer.newKieSession("cloudBalancingSolverFactory");
              Solver solver = (Solver) ksession.convertToSolver();
              // Note: the ugly cast to SolverFactory is unavoidable because we can't move SolverFactory out of optaplanner-core [1]
      

      [1] Why can't we move SolverFactory from optaplanner-core to kie-api? Why can't the reflection trick fix that?

      • Because that interface has method signatures that expose Solution, Solver, ...
        which in turn expose Score, ProblemFactChange, ScoreDirectorFactory, ...
        which in turn expose SolverEventListener, ScoreDirector, ...
        which in turn expose ConstraintMatchTotal, ConstraintMatch, ...
      • Because SolverFactory and Solver are part of the public api, so they can't move to another java package.

              gdesmet@redhat.com Geoffrey De Smet (Inactive)
              gdesmet@redhat.com Geoffrey De Smet (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: