Question

I am currently working on a Java project that uses Optaplanner and drools for a Constraint Satisfaction problem.

The solving works fine. But after the solver has given me a solution, I want to evaluate the solution: I want to know which constraints have been violated, i.e. which rules have been fired and how many times.

Is this possible and how do I start with this?

Était-ce utile?

La solution

See docs section Reusing the score calculation outside the Solver. That provide all the data you need in a simple way.

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();
        ...
    }
}

See Laune's answer for more advanced drools specific techniques/tricks.

Autres conseils

The firing of rules can be observed by implementing an org.kie.api.event.rule.AgendaEventListener and attaching it with KieSession.addEventListener(AgendaEventListener listener).

The rule engine is not equipped to explain why some rule has not fired.

It depends on the complexity of the rules you have, but one way is to write rules that evaluate the constraints individually and accumulate a "set of met", after which "all met" can be determined and the complement of an incomplete set is the answer to your question.

Another way is to have rules that fire when one of the patterns or constraints is not met. For instance, in addition to

rule abc when A() B() C() then ... end

you have to write

rule not_a when not A() then ... end
rule a_not_B when A() not B() then ... end
rule ab_not_c when A() B() not C() then ... end 

This, however, will not tell you all missing conditions: if not_a fires, also B and C could be mismatched.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top