Search code examples
optaplanner

Optaplanner missing a no-args #countDistinct method overload


I'm just starting with Optaplanner and am trying to recreate the following example from the documentation (https://docs.optaplanner.org/7.37.0.Final/optaplanner-docs/html_single/index.html#_collecting_countdistinct):

private Constraint roomCount(ConstraintFactory factory) {
    return factory.from(Talk.class)
            .groupBy(Talk::getRoom, countDistinct())
            .penalize("roomCount",
                    HardSoftScore.ONE_SOFT,
                    (room, count) -> ...);
}

... But there is no no-args #countDistinct() method overload in the ConstraintCollectors class. There are ony only method overloads that accept a Function/BiFunction/TriFunction/QuadFunction respectively.

Am I importing the wrong class, or are there undocumented signature changes?


Solution

  • Good catch, that looks like an oversight in the docs. What you want to do is most easily achieved like so:

    .groupBy(Talk::getRoom, countDistinct(Function.identity()))
    

    The no-argument overload could possibly be added to the API, a syntactic sugar for what I've just shown. I'll look into it when I find some time.

    Wrt. your question about undocumented signature changes - no, that would not happen. The API remains strictly backwards compatible between major versions of OptaPlanner - we actually use Revapi at build time to enforce that for us. What you're pointing out is most likely just an oversight during doc review.

    Thanks for taking the time to point this out.