org.optaplanner.constraint.streams.bavet.BavetConstraintFactory Maven / Gradle / Ivy
Show all versions of optaplanner-constraint-streams-bavet Show documentation
package org.optaplanner.constraint.streams.bavet;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import org.optaplanner.constraint.streams.bavet.common.BavetAbstractConstraintStream;
import org.optaplanner.constraint.streams.bavet.uni.BavetAbstractUniConstraintStream;
import org.optaplanner.constraint.streams.bavet.uni.BavetForEachUniConstraintStream;
import org.optaplanner.constraint.streams.common.InnerConstraintFactory;
import org.optaplanner.constraint.streams.common.RetrievalSemantics;
import org.optaplanner.core.api.score.stream.uni.UniConstraintStream;
import org.optaplanner.core.impl.domain.constraintweight.descriptor.ConstraintConfigurationDescriptor;
import org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor;
public final class BavetConstraintFactory
extends InnerConstraintFactory> {
private final SolutionDescriptor solutionDescriptor;
private final String defaultConstraintPackage;
private final Map, BavetAbstractConstraintStream> sharingStreamMap =
new HashMap<>(256);
public BavetConstraintFactory(SolutionDescriptor solutionDescriptor) {
this.solutionDescriptor = solutionDescriptor;
ConstraintConfigurationDescriptor configurationDescriptor = solutionDescriptor
.getConstraintConfigurationDescriptor();
if (configurationDescriptor == null) {
Package pack = solutionDescriptor.getSolutionClass().getPackage();
defaultConstraintPackage = (pack == null) ? "" : pack.getName();
} else {
defaultConstraintPackage = configurationDescriptor.getConstraintPackage();
}
}
public > Stream_ share(Stream_ stream) {
return share(stream, t -> {
});
}
/**
* Enables node sharing.
* If a constraint already exists in this factory, it replaces it by the old copy.
* {@link BavetAbstractConstraintStream} implement equals/hashcode ignoring child streams.
*
* {@link BavetConstraintSessionFactory#buildSession(boolean, Object)} relies on this occurring for all streams.
*
* This must be called before the stream receives child streams.
*
* @param stream never null
* @param consumer never null
* @param the {@link BavetAbstractConstraintStream} subclass
* @return never null
*/
public > Stream_ share(Stream_ stream,
Consumer consumer) {
return (Stream_) sharingStreamMap.computeIfAbsent(stream, k -> {
consumer.accept(stream);
return stream;
});
}
// ************************************************************************
// from
// ************************************************************************
@Override
public UniConstraintStream forEachIncludingNullVars(Class sourceClass) {
assertValidFromType(sourceClass);
return share(new BavetForEachUniConstraintStream<>(this, sourceClass, RetrievalSemantics.STANDARD));
}
@Override
public BavetAbstractUniConstraintStream fromUnfiltered(Class fromClass) {
assertValidFromType(fromClass);
return share(new BavetForEachUniConstraintStream<>(this, fromClass, RetrievalSemantics.LEGACY));
}
// ************************************************************************
// Getters/setters
// ************************************************************************
@Override
public SolutionDescriptor getSolutionDescriptor() {
return solutionDescriptor;
}
@Override
public String getDefaultConstraintPackage() {
return defaultConstraintPackage;
}
}