Search code examples
javadroolsoptaplanner

Can I tell Optaplanner (with constraint streams) not to recalculate join if it doesn't depend on planning variable?


I'm using fromUniquePair​(Class<A> fromClass, BiJoiner<A,​A> joiner) at the start of my constraint stream. The fromClass is a planning entity, representing a time slot. I need a stream of all pairs of time slots that overlap, to later check that their planning variables don't conflict. The time slot doesn't move in time based on any planning variable, so the result of this fromUniquePair join doesn't change while optimising.

However, whenever a planning variable changes, the time slot gets compared to all other time slots to see if a pair needs to be added or removed from the stream. This is unnecessary because the result of this joiner doesn't depend on the planning variables in any way. I think the majority of time is spent on doing just this, as it needs to check thousands of other time slots every time a planning variable gets changed, while it only needs to do a few quick checks on the planning variable itself.

Is there a way to tell Optaplanner not to do this?


Solution

  • What you are asking for here can be compared to the property reactivity feature in Drools. Unfortunately, Constraint Streams does not support that, nor is there a way it ever could - Constraint Streams can not see inside the filter lambdas or method references, and therefore it does not know which fields you access. This is a price we pay for a better abstraction.

    I do not think flattenLast() will do anything in this regard. The entity you do from(...) on will still be reevaluated every time it is changed, and then all the flattened data would be re-evaluated too. That said, maybe I have misunderstood your intentions and if you update your original question with additional information, I'll reconsider.