Show Table of Contents
5.5. Explaining the Score: Using Score Calculation Outside the Solver
Other parts of your application, for example your webUI, might need to calculate the score too. Do that by reusing the
ScoreDirectorFactory of the Solver to build a separate ScoreDirector for that webUI:
ScoreDirectorFactory scoreDirectorFactory = solver.getScoreDirectorFactory(); ScoreDirector guiScoreDirector = scoreDirectorFactory.buildScoreDirector();
Then use it when you need to calculate the
Score of a Solution:
guiScoreDirector.setWorkingSolution(solution); Score score = guiScoreDirector.calculateScore();
To explain in the GUI what entities are causing which part of the
Score, get the ConstraintMatch objects from the ScoreDirector:
for (ConstraintMatchTotal constraintMatchTotal : guiScoreDirector.getConstraintMatchTotals()) {
String constraintName = constraintMatchTotal.getConstraintName();
Number weightTotal = constraintMatchTotal.getWeightTotalAsNumber();
for (ConstraintMatch constraintMatch : constraintMatchTotal.getConstraintMatchSet()) {
List<Object> justificationList = constraintMatch.getJustificationList();
Number weight = constraintMatch.getWeightAsNumber();
...
}
}Note
Drools score calculation supports constraint matches automatically, but incremental Java score calculation requires requires implementing an extra interface (see that section).

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.