4.2. Solver Configuration
4.2.1. Solver Configuration by XML
Build a Solver instance with the SolverFactory. Configure the SolverFactory with a solver configuration XML file, provided as a classpath resource (as defined by ClassLoader.getResource()):
SolverFactory<NQueens> solverFactory = SolverFactory.createFromXmlResource(
"org/optaplanner/examples/nqueens/solver/nqueensSolverConfig.xml");
Solver<NQueens> solver = solverFactory.buildSolver();
In a typical project (following the Maven directory structure), that solverConfig XML file would be located at $PROJECT_DIR/src/main/resources/org/optaplanner/examples/nqueens/solver/nqueensSolverConfig.xml. Alternatively, a SolverFactory can be created from a File, an InputStream or a Reader with methods such as SolverFactory.createFromXmlFile(). However, for portability reasons, a classpath resource is recommended.
On some environments (OSGi, JBoss modules, …), classpath resources (such as the solver config, score DRL’s and domain classes) in your JARs might not be available to the default ClassLoader of the optaplanner-core JAR. In those cases, provide the ClassLoader of your classes as a parameter:
SolverFactory<NQueens> solverFactory = SolverFactory.createFromXmlResource(
".../nqueensSolverConfig.xml", getClass().getClassLoader());
When using Workbench or Execution Server or to take advantage of Drools’s KieContainer features, provide the KieContainer as a parameter:
KieServices kieServices = KieServices.Factory.get();
KieContainer kieContainer = kieServices.newKieContainer(
kieServices.newReleaseId("org.nqueens", "nqueens", "1.0.0"));
SolverFactory<NQueens> solverFactory = SolverFactory.createFromKieContainerXmlResource(
kieContainer, ".../nqueensSolverConfig.xml");
Both a Solver and a SolverFactory have a generic type called Solution_, which is the class representing a planning problem and solution.
A solver configuration XML file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<solver>
<!-- Define the model -->
<solutionClass>org.optaplanner.examples.nqueens.domain.NQueens</solutionClass>
<entityClass>org.optaplanner.examples.nqueens.domain.Queen</entityClass>
<!-- Define the score function -->
<scoreDirectorFactory>
<scoreDefinitionType>SIMPLE</scoreDefinitionType>
<scoreDrl>org/optaplanner/examples/nqueens/solver/nQueensScoreRules.drl</scoreDrl>
</scoreDirectorFactory>
<!-- Configure the optimization algorithms (optional) -->
<termination>
...
</termination>
<constructionHeuristic>
...
</constructionHeuristic>
<localSearch>
...
</localSearch>
</solver>Notice the three parts in it:
- Define the model.
- Define the score function.
- Optionally configure the optimization algorithm(s).
These various parts of a configuration are explained further in this manual.
Planner makes it relatively easy to switch optimization algorithm(s) just by changing the configuration. There is even a Benchmarker which allows you to play out different configurations against each other and report the most appropriate configuration for your use case.
4.2.2. Solver Configuration by Java API
A solver configuration can also be configured with the SolverConfig API. This is especially useful to change some values dynamically at runtime. For example, to change the running time based on user input, before building the Solver:
SolverFactory<NQueens> solverFactory = SolverFactory.createFromXmlResource(
"org/optaplanner/examples/nqueens/solver/nqueensSolverConfig.xml");
TerminationConfig terminationConfig = new TerminationConfig();
terminationConfig.setMinutesSpentLimit(userInput);
solverFactory.getSolverConfig().setTerminationConfig(terminationConfig);
Solver<NQueens> solver = solverFactory.buildSolver();
Every element in the solver configuration XML is available as a *Config class or a property on a *Config class in the package namespace org.optaplanner.core.config. These *Config classes are the Java representation of the XML format. They build the runtime components (of the package namespace org.optaplanner.core.impl) and assemble them into an efficient Solver.
The SolverFactory is only multi-thread safe after it’s configured. So the getSolverConfig() method is not thread-safe. To configure a SolverFactory dynamically for each user request, build a SolverFactory as base during initialization and clone it with the cloneSolverFactory() method for a user request:
private SolverFactory<NQueens> base;
public void init() {
base = SolverFactory.createFromXmlResource(
"org/optaplanner/examples/nqueens/solver/nqueensSolverConfig.xml");
base.getSolverConfig().setTerminationConfig(new TerminationConfig());
}
// Called concurrently from different threads
public void userRequest(..., long userInput)
SolverFactory<NQueens> solverFactory = base.cloneSolverFactory();
solverFactory.getSolverConfig().getTerminationConfig().setMinutesSpentLimit(userInput);
Solver<NQueens> solver = solverFactory.buildSolver();
...
}4.2.3. Solver Configuration in Business Central
To configure your solver, you can use the Solver Editor in Business Central. To learn about Business Central, see chapter 1.4. Business Central from Red Hat JBoss BPM Suite User Guide.
You must have the role plannermgmt to enable the Business Resource Planner settings in Business Central.
Click New Item → Solver configuration to create the Solver Editor in your Business Central. Note that you can also use other assets, for example DRL rules, or guided decision tables.
Figure 4.1. Solver Configuration Editor

The solver editor creates a basic solver configuration. You can run it, together with DRL rules, planner entities, and planner solution, in the Intelligent Process Server or in plain Java code after the KJAR is deployed.
Once you define your solver in the editor, you can click Source and view it in the XML format:
<solver xStreamId="1">
<scanAnnotatedClasses xStreamId="2"/>
<scoreDirectorFactory xStreamId="3">
<scoreDefinitionType>HARD_SOFT</scoreDefinitionType>
</scoreDirectorFactory>
<termination xStreamId="4">
<secondsSpentLimit>0</secondsSpentLimit>
<minutesSpentLimit>0</minutesSpentLimit>
<hoursSpentLimit>5</hoursSpentLimit>
<daysSpentLimit>0</daysSpentLimit>
</termination>
</solver>Use the Validate button to validate the solver configuration. This will actually build a Solver, so most issues in your project will present themselves then, without the need to deploy and run it.
By default, the solver configuration will automatically scan for all planning entities and a planning solution classes. If none are found (or too many), validation will fail.
Data Object Tab
To model planning entities and planning solutions used in Business Resource Planner, use data objects. To learn more about data objects, see chapter 4.14. Data models from Red Hat JBoss BPM Suite User Guide. To designate a data object as a planner entity or solution, see the following steps:
Figure 4.2. Data Object Tab

- Click on the label of your data object and then on the OptaPlanner tool window to set the object to be a Planning Entity or a Planning Solution.
- Click a specific data object field and then on the OptaPlanner tool window to set the relations between Business Resource Planner variables and solver.
- The OptaPlanner tool window allows you to change the planner settings based on the context, that is this window changes its content based on your selection.
4.2.4. Annotations Configuration
4.2.4.1. Automatic Scanning for Annotations
Instead of the declaring the classes that have a @PlanningSolution or @PlanningEntity manually:
<solver> <!-- Define the model --> <solutionClass>org.optaplanner.examples.nqueens.domain.NQueens</solutionClass> <entityClass>org.optaplanner.examples.nqueens.domain.Queen</entityClass> ... </solver>
Planner can find scan the classpath and find them automatically:
<solver> <!-- Define the model --> <scanAnnotatedClasses/> ... </solver>
If there are multiple models in your classpath (or just to speed up scanning), specify the packages to scan:
<solver>
<!-- Define the model -->
<scanAnnotatedClasses>
<packageInclude>org.optaplanner.examples.cloudbalancing</packageInclude>
</scanAnnotatedClasses>
...
</solver>This will find all solution and entity classes in the package or subpackages.
If scanAnnotatedClasses is not specified, the org.reflections transitive Maven dependency can be excluded.
4.2.4.2. Annotation Alternatives
Planner needs to be told which classes in your domain model are planning entities, which properties are planning variables, etc. There are several ways to deliver this information:
- Add class annotations and JavaBean property annotations on the domain model (recommended). The property annotations must be on the getter method, not on the setter method. Such a getter does not need to be public.
- Add class annotations and field annotations on the domain model. Such a field does not need to be public.
- No annotations: externalize the domain configuration in an XML file. This is not yet supported.
This manual focuses on the first manner, but every feature supports all 3 manners, even if it’s not explicitly mentioned.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.