Package org.optaplanner.core.impl.solver

## Class DefaultSolverManager<Solution_,​ProblemId_>

- java.lang.Object
- - org.optaplanner.core.impl.solver.DefaultSolverManager<Solution_,​ProblemId_>

- Type Parameters:
    `Solution_` - the solution type, the class with the `PlanningSolution` annotation
    `ProblemId_` - the ID type of a submitted problem, such as `Long` or `UUID`.
  All Implemented Interfaces:
    `AutoCloseable`, `SolverManager<Solution_,​ProblemId_>`

      public final class DefaultSolverManager<Solution_,​ProblemId_>
      extends Object
      implements SolverManager<Solution_,​ProblemId_>

- - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `protected org.slf4j.Logger` | `logger` |

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `DefaultSolverManager​(SolverFactory<Solution_> solverFactory, SolverManagerConfig solverManagerConfig)` |

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `void` | `close()` | Terminates all solvers, cancels all solver jobs that haven't (re)started yet and discards all queued `ProblemFactChange`s.
    `protected ConcurrentMap<Object,​DefaultSolverJob<Solution_,​ProblemId_>>` | `getProblemIdToSolverJobMap()` |
    `SolverFactory<Solution_>` | `getSolverFactory()` |
    `SolverStatus` | `getSolverStatus​(ProblemId_ problemId)` | Returns if the `Solver` is scheduled to solve, actively solving or not.
    `SolverJob<Solution_,​ProblemId_>` | `solve​(ProblemId_ problemId, Function<? super ProblemId_,​? extends Solution_> problemFinder, Consumer<? super Solution_> finalBestSolutionConsumer, BiConsumer<? super ProblemId_,​? super Throwable> exceptionHandler)` | As defined by `SolverManager.solve(Object, Function, Consumer)`.
    `protected SolverJob<Solution_,​ProblemId_>` | `solve​(ProblemId_ problemId, Function<? super ProblemId_,​? extends Solution_> problemFinder, Consumer<? super Solution_> bestSolutionConsumer, Consumer<? super Solution_> finalBestSolutionConsumer, BiConsumer<? super ProblemId_,​? super Throwable> exceptionHandler)` |
    `SolverJob<Solution_,​ProblemId_>` | `solveAndListen​(ProblemId_ problemId, Function<? super ProblemId_,​? extends Solution_> problemFinder, Consumer<? super Solution_> bestSolutionConsumer, Consumer<? super Solution_> finalBestSolutionConsumer, BiConsumer<? super ProblemId_,​? super Throwable> exceptionHandler)` | As defined by `SolverManager.solveAndListen(Object, Function, Consumer)`.
    `void` | `terminateEarly​(ProblemId_ problemId)` | Terminates the solver or cancels the solver job if it hasn't (re)started yet.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface org.optaplanner.core.api.solver.SolverManager

      `solve, solve, solve, solve, solveAndListen, solveAndListen`

- - ### Field Detail

    - #### logger

          protected final transient org.slf4j.Logger logger

  - ### Constructor Detail

    - #### DefaultSolverManager

          public DefaultSolverManager​(SolverFactory<Solution_> solverFactory,
                                      SolverManagerConfig solverManagerConfig)

  - ### Method Detail

    - #### getSolverFactory

          public SolverFactory<Solution_> getSolverFactory()

    - #### getProblemIdToSolverJobMap

          protected ConcurrentMap<Object,​DefaultSolverJob<Solution_,​ProblemId_>> getProblemIdToSolverJobMap()

    - #### solve

          public SolverJob<Solution_,​ProblemId_> solve​(ProblemId_ problemId,
                                                             Function<? super ProblemId_,​? extends Solution_> problemFinder,
                                                             Consumer<? super Solution_> finalBestSolutionConsumer,
                                                             BiConsumer<? super ProblemId_,​? super Throwable> exceptionHandler)

      Description copied from interface: `SolverManager`
      As defined by `SolverManager.solve(Object, Function, Consumer)`.
      Specified by:
        `solve` in interface `SolverManager<Solution_,​ProblemId_>`
      Parameters:
        `problemId` - never null, a ID for each planning problem. This must be unique. Use this problemId to `terminate` the solver early, `to get the status` or if the problem changes while solving.
        `problemFinder` - never null, function that returns a `PlanningSolution`, usually with uninitialized planning variables
        `finalBestSolutionConsumer` - sometimes null, called only once, at the end, on a consumer thread
        `exceptionHandler` - sometimes null, called if an exception or error occurs. If null it defaults to logging the exception as an error.
      Returns:
        never null

    - #### solveAndListen

          public SolverJob<Solution_,​ProblemId_> solveAndListen​(ProblemId_ problemId,
                                                                      Function<? super ProblemId_,​? extends Solution_> problemFinder,
                                                                      Consumer<? super Solution_> bestSolutionConsumer,
                                                                      Consumer<? super Solution_> finalBestSolutionConsumer,
                                                                      BiConsumer<? super ProblemId_,​? super Throwable> exceptionHandler)

      Description copied from interface: `SolverManager`
      As defined by `SolverManager.solveAndListen(Object, Function, Consumer)`.

      The final best solution is delivered twice: first to the `bestSolutionConsumer` when it is found and then again to the `finalBestSolutionConsumer` when the solver terminates. Do not store the solution twice. This allows for use cases that only process the `Score` first (during best solution changed events) and then store the solution upon termination.

      Specified by:
        `solveAndListen` in interface `SolverManager<Solution_,​ProblemId_>`
      Parameters:
        `problemId` - never null, an ID for each planning problem. This must be unique. Use this problemId to `terminate` the solver early, `to get the status` or if the problem changes while solving.
        `problemFinder` - never null, function that returns a `PlanningSolution`, usually with uninitialized planning variables
        `bestSolutionConsumer` - never null, called multiple times, on a consumer thread
        `finalBestSolutionConsumer` - sometimes null, called only once, at the end, on a consumer thread. That final best solution is already consumed by the bestSolutionConsumer earlier.
        `exceptionHandler` - sometimes null, called if an exception or error occurs. If null it defaults to logging the exception as an error.
      Returns:
        never null

    - #### solve

          protected SolverJob<Solution_,​ProblemId_> solve​(ProblemId_ problemId,
                                                                Function<? super ProblemId_,​? extends Solution_> problemFinder,
                                                                Consumer<? super Solution_> bestSolutionConsumer,
                                                                Consumer<? super Solution_> finalBestSolutionConsumer,
                                                                BiConsumer<? super ProblemId_,​? super Throwable> exceptionHandler)

    - #### getSolverStatus

          public SolverStatus getSolverStatus​(ProblemId_ problemId)

      Description copied from interface: `SolverManager`
      Returns if the `Solver` is scheduled to solve, actively solving or not.

      Returns `SolverStatus.NOT_SOLVING` if the solver already terminated or if the problemId was never added. To distinguish between both cases, use `SolverJob.getSolverStatus()` instead. Here, that distinction is not supported because it would cause a memory leak.

      Specified by:
        `getSolverStatus` in interface `SolverManager<Solution_,​ProblemId_>`
      Parameters:
        `problemId` - never null, a value given to `SolverManager.solve(Object, Function, Consumer)` or `SolverManager.solveAndListen(Object, Function, Consumer)`
      Returns:
        never null

    - #### terminateEarly

          public void terminateEarly​(ProblemId_ problemId)

      Description copied from interface: `SolverManager`
      Terminates the solver or cancels the solver job if it hasn't (re)started yet.

      Does nothing if the solver already terminated or the problemId was never added. To distinguish between both cases, use `SolverJob.terminateEarly()` instead. Here, that distinction is not supported because it would cause a memory leak.

      Waits for the termination or cancellation to complete before returning. During termination, a `bestSolutionConsumer` could still be called (on a consumer thread), before this method returns.

      Specified by:
        `terminateEarly` in interface `SolverManager<Solution_,​ProblemId_>`
      Parameters:
        `problemId` - never null, a value given to `SolverManager.solve(Object, Function, Consumer)` or `SolverManager.solveAndListen(Object, Function, Consumer)`

    - #### close

          public void close()

      Description copied from interface: `SolverManager`
      Terminates all solvers, cancels all solver jobs that haven't (re)started yet and discards all queued `ProblemFactChange`s. Releases all thread pool resources.

      No new planning problems can be submitted after calling this method.

      Specified by:
        `close` in interface `AutoCloseable`
      Specified by:
        `close` in interface `SolverManager<Solution_,​ProblemId_>`