Toggle navigation OptaPlanner logo
  • Home
  • Download
  • Learn
    • Documentation
    • Videos
    • Slides
    • Training
    • Use cases
    • Compatibility
    • Testimonials and case studies
  • Get help
  • Source
  • Team
  • Services
  • Star
  • @OptaPlanner
  • Fb
Fork me on GitHub
  • How much faster is Java 11?
  • Mechanic scheduling (part 1) - Can OptaPlanner ...

KIE Server OptaPlanner Task Assignment

Wed 13 March 2019

Avatar Musa Talluzi

Musa Talluzi


Twitter GitHub

Ex OptaPlanner developer

KIE Server is a standalone server component that can be used to instantiate and execute rules and processes. In this blog, you will learn how to build an OptaPlanner service that implements continuous and real-time planning to solve the Task Assignment problem and deploy it to the KIE Server.

Task Assignment KJAR

KJAR or "Knowledge JAR" is a standard JAR file that has some extra files included (at least a META-INF/kmodule.xml file). You will build Task Assignment Service as a KJAR.

Note
In order to build the KJAR, the project’s pom.xml needs to have a <packaging>kjar</packaging> entry and contains kie-maven-plugin.

Problem Description

Assign each task to a spot in an employee’s queue. Each task has a duration which is affected by the employee’s affinity level with the task’s customer.

Constraints:

  • [Hard] Skill: Each task requires one or more skill. The employee must possess all these skills.

  • [Soft 0] Critical tasks: Complete critical tasks first, sooner than major and minor tasks.

  • [Soft 1] Minimize makespan: Reduce the time to complete all tasks.

  • [Soft 2] Major tasks: Complete major tasks as soon as possible, sooner than minor tasks.

  • [Soft 3] Minor tasks: Complete minor tasks as soon as possible.

Domain Model

taskAssigningClassDiagram

Read more on how to model a planning problem.

Real-time Planning

As new tasks come in and others start being implemented, the optimal solution might change. OptaPlanner handles such scenarios using ProblemFactChange.

For example, if an employee starts working on a task, tell OptaPlanner to pin this task by sending a PinTaskProblemFactChange. Each ProblemFactChange type is implemented as a class that implements the interface ProblemFactChange<Solution_>. In the Task Assignment example, this class can be something like:

public class PinTaskProblemFactChange implements ProblemFactChange<TaskAssigningSolution> {
    ...

    @Override
    public void doChange(ScoreDirector<TaskAssigningSolution> scoreDirector) {
        ...
        scoreDirector.beforeProblemPropertyChanged(toBePinnedTask);
        toBePinnedTask.setPinned(true);
        scoreDirector.afterProblemPropertyChanged(toBePinnedTask);
        ...

        scoreDirector.triggerVariableListeners();
    }
}

The full class can be found on github.

Solver Configuration

The solver configuration file determines how the solving process works. For a KJAR deployed to a KIE Server and since we are using Drools for score calculation, use a ksessionName. This tells the KieContainer where to find the DRL file. Add an optataskKsession to META-INF/kmodule.xml file:

<kbase name="optataskKBase" packages="PATH_TO_SOLVER_RESOURCES">
    <ksession name="optataskKsession"/>
</kbase>

And to solver config file:

<scoreDirectorFactory>
    <ksessionName>optataskKsession</ksessionName>
</scoreDirectorFactory>

Notice that both solver configuration and DRL files need to be in the resources folder under the path PATH_TO_SOLVER_RESOURCES.

For real-time planning, set the solver in daemon mode in order to resume solving once a problem fact change is added. This is accomplished by adding the following to the solver config file:

<daemon>true</daemon>

Persistence

Since you will be sending and receiving data to/from KIE Server through REST API, you need to tell it how to marshall/unmarshall this data. Read how OptaPlanner marshals a score using Xstream.

OptaPlanner stores all planning entities as objects and references to these objects, which might result in a lot of redundant data received from the server. In our example, we are using @JsonIdentityInfo to avoid such redundancies.

Dependencies

The minimum required dependencies to build the OptaPlanner service are: optaplanner-core, optaplanner-persistence-xstream.

Note
Because you will be using several OptaPlanner modules, it’s recommended to import the optaplanner-bom in Maven’s dependencyManagement so that OptaPlanner version is specified only once.

Build and Deployment

Now that you have the KJAR ready, you can deploy it to the KIE Server and perform all planning operations. You can send HTTP requests to the server using REST API.

Deployment

POST /config

Through the above endpoint, you can execute various commands on the KIE Server.

For example, to create a container running your OptaPlanner service, the request’s body should be:

{
  commands: [
    {
      'create-container': {
        'container': {
          'container-id': CONTAINER_ID,
          'release-id': {
            'group-id': GROUP_ID,
            'artifact-id': ARTIFACT_ID,
            'version': VERSION,
          },
        },
      },
    },
  ],
}

Notice that the GAV in the release-id object are your KJAR’s GAV.

Solver registration

PUT /containers/{CONTAINER_ID}/solvers/{SOLVER_ID}

With body:

{
  'solver-config-file': 'PATH_TO_SOLVER_CONFIG_FILE.xml'
}

This will build a new solver from the xml resource included in the KJAR.

Submit a problem

POST /containers/{CONTAINER_ID}/solvers/{SOLVER_ID}/state/solving

Once a solver is built it will be waiting for a problem to start solving. The body of this request contains the object annotated as @PlanningSolution, in the Task Assignment example it will be TaskAssigningSolution.

Query bestSolution

GET /containers/{CONTAINER_ID}/solvers/{SOLVER_ID}/bestsolution

This response body will contain best-solution object in addition to extra information about the solver status and score.

Submit a ProblemFactChange

POST /containers/{CONTAINER_ID}/solvers/{SOLVER_ID}/problemfactchanges

Submits a ProblemFactChange to update the problem the solver is solving. For example if you want to delete a task the body should be:

<problem-fact-change class="TaDeleteTaskProblemFactChange">
    <taskId>TO_BE_DELETED_TASK_ID</taskId>
</problem-fact-change>

Notice the use of class attribute, this is how you tell OptaPlanner service what type of ProblemFactChange you are submitting. Here TaDeleteTaskProblemFactChange is an @XStreamAlias for the DeleteTaskProblemFactChange.

Note
All the requests above have a base URL http://SERVER:PORT/CONTEXT/services/rest/server and require basic HTTP Authentication for the role kie-server.

Check all the available endpoints in the docs.

Conclusion

To integrate an OptaPlanner service with your application on KIE Server:

  1. Build the service as a KJAR.

  2. Send an HTTP request to the KIE Server to start a container that runs this service.

  3. Communicate with the service through the REST API the the KIE Server exposes.

Related material

KIE Server OptaPlanner Task Assignment demo

KIE Server and KIE container commands in OptaPlanner

OptaPlanner REST API


Comments Permalink
 tagged as use case task assignment execution server

Comments

Visit our forum to comment
  • How much faster is Java 11?
  • Mechanic scheduling (part 1) - Can OptaPlanner ...
Atom News feed
Don't want to miss a single blog post?
Follow us on
  • T
  • Fb
Blog archive
Latest release
  • 8.2.0.Final released
    Tue 9 February 2021
Upcoming events
  • Javaland
    Worldwide - Tue 16 March 2021
    • AI on Quarkus: I love it when an OptaPlan comes together by Geoffrey De Smet
  • SouJava MOTU
    Worldwide - Thu 15 April 2021
    • Planejamento de Recursos com OptaPlanner by Karina Varela, Otávio Santana
Add event / Archive
Latest blog posts
  • How much faster is Java 15?
    Tue 26 January 2021
     Michal Tomčo
  • Solve the facility location problem
    Fri 9 October 2020
     Jiří Locker
  • OptaPlanner Week 2020 recordings
    Mon 7 September 2020
     Geoffrey De Smet
  • Let’s OptaPlan your jBPM tasks (part 1) - Integrating the two worlds
    Fri 3 July 2020
     Walter Medvedeo
  • AI versus Covid-19: How Java helps nurses and doctors in this fight
    Fri 8 May 2020
     Christopher Chianelli
  • Workflow processes with AI scheduling
    Tue 5 May 2020
     Christopher Chianelli
  • Constraint Streams - Modern Java constraints without the Drools Rule Language
    Tue 7 April 2020
     Geoffrey De Smet
Blog archive
Latest videos
  • YT Maintenance scheduling
    Wed 24 February 2021
     Julian Cui
  • YT Vaccination appointment scheduling
    Wed 3 February 2021
     Geoffrey De Smet
  • YT Shadow variables
    Tue 19 January 2021
     Geoffrey De Smet
  • YT Domain modeling and design patterns
    Tue 17 November 2020
     Geoffrey De Smet
  • YT Quarkus insights: AI constraint solving
    Tue 20 October 2020
     Geoffrey De Smet
  • YT AI in kotlin
    Wed 23 September 2020
     Geoffrey De Smet
  • YT Planning agility: continuous planning, real-time planning and more
    Thu 3 September 2020
     Geoffrey De Smet
Video archive

KIE projects

  • Drools rule engine
  • OptaPlanner constraint solver
  • jBPM workflow engine

Community

  • Blog
  • Get Help
  • Team
  • Governance
  • Academic research

Code

  • Build from source
  • Submit a bug
  • License (Apache-2.0)
  • Release notes
  • Upgrade recipes
Sponsored by
Red Hat
More coder content at
Red Hat Developers
© Copyright 2006-2021, Red Hat, Inc. or third-party contributors - Privacy statement - Terms of use - Website info