org.optaplanner.constraint.streams.bavet.uni.BavetAbstractUniConstraintStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of optaplanner-constraint-streams-bavet Show documentation
Show all versions of optaplanner-constraint-streams-bavet Show documentation
OptaPlanner solves planning problems.
This lightweight, embeddable planning engine implements powerful and scalable algorithms
to optimize business resource scheduling and planning.
This module contains implementation of Constraint streams (Bavet).
package org.optaplanner.constraint.streams.bavet.uni;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import org.optaplanner.constraint.streams.bavet.BavetConstraintFactory;
import org.optaplanner.constraint.streams.bavet.bi.BavetGroupBiConstraintStream;
import org.optaplanner.constraint.streams.bavet.bi.BavetJoinBiConstraintStream;
import org.optaplanner.constraint.streams.bavet.bi.BiTuple;
import org.optaplanner.constraint.streams.bavet.common.BavetAbstractConstraintStream;
import org.optaplanner.constraint.streams.bavet.quad.BavetGroupQuadConstraintStream;
import org.optaplanner.constraint.streams.bavet.quad.QuadTuple;
import org.optaplanner.constraint.streams.bavet.tri.BavetGroupTriConstraintStream;
import org.optaplanner.constraint.streams.bavet.tri.TriTuple;
import org.optaplanner.constraint.streams.common.RetrievalSemantics;
import org.optaplanner.constraint.streams.common.ScoreImpactType;
import org.optaplanner.constraint.streams.common.bi.BiJoinerComber;
import org.optaplanner.constraint.streams.common.uni.InnerUniConstraintStream;
import org.optaplanner.core.api.score.Score;
import org.optaplanner.core.api.score.stream.Constraint;
import org.optaplanner.core.api.score.stream.bi.BiConstraintStream;
import org.optaplanner.core.api.score.stream.bi.BiJoiner;
import org.optaplanner.core.api.score.stream.quad.QuadConstraintStream;
import org.optaplanner.core.api.score.stream.tri.TriConstraintStream;
import org.optaplanner.core.api.score.stream.uni.UniConstraintCollector;
import org.optaplanner.core.api.score.stream.uni.UniConstraintStream;
public abstract class BavetAbstractUniConstraintStream extends BavetAbstractConstraintStream
implements InnerUniConstraintStream {
protected final List> childStreamList = new ArrayList<>(2);
public BavetAbstractUniConstraintStream(BavetConstraintFactory constraintFactory,
RetrievalSemantics retrievalSemantics) {
super(constraintFactory, retrievalSemantics);
}
public List> getChildStreamList() {
return childStreamList;
}
// ************************************************************************
// Stream builder methods
// ************************************************************************
public > Stream_ shareAndAddChild(
Stream_ stream) {
return constraintFactory.share(stream, childStreamList::add);
}
// ************************************************************************
// Filter
// ************************************************************************
@Override
public BavetAbstractUniConstraintStream filter(Predicate predicate) {
return shareAndAddChild(
new BavetFilterUniConstraintStream<>(constraintFactory, this, predicate));
}
// ************************************************************************
// Join
// ************************************************************************
@Override
@SafeVarargs
public final BiConstraintStream join(UniConstraintStream otherStream,
BiJoiner... joiners) {
BiJoinerComber joinerComber = BiJoinerComber.comb(joiners);
return join(otherStream, joinerComber);
}
@Override
public final BiConstraintStream join(UniConstraintStream otherStream, BiJoinerComber joinerComber) {
BavetAbstractUniConstraintStream other = assertBavetUniConstraintStream(otherStream);
BavetJoinBridgeUniConstraintStream leftBridge =
new BavetJoinBridgeUniConstraintStream<>(constraintFactory, this, true);
BavetJoinBridgeUniConstraintStream rightBridge =
new BavetJoinBridgeUniConstraintStream<>(constraintFactory, other, false);
BavetJoinBiConstraintStream joinStream =
new BavetJoinBiConstraintStream<>(constraintFactory, leftBridge, rightBridge,
joinerComber.getMergedJoiner());
leftBridge.setJoinStream(joinStream);
rightBridge.setJoinStream(joinStream);
joinStream = constraintFactory.share(joinStream, joinStream_ -> {
// Connect the bridges upstream, as it is an actual new join.
getChildStreamList().add(leftBridge);
other.getChildStreamList().add(rightBridge);
});
if (joinerComber.getMergedFiltering() == null) {
return joinStream;
} else {
return joinStream.filter(joinerComber.getMergedFiltering());
}
}
// ************************************************************************
// If (not) exists
// ************************************************************************
@SafeVarargs
@Override
public final UniConstraintStream ifExists(Class otherClass, BiJoiner... joiners) {
if (getRetrievalSemantics() == RetrievalSemantics.STANDARD) {
return ifExists(constraintFactory.forEach(otherClass), joiners);
} else {
// Calls fromUnfiltered() for backward compatibility only
return ifExists(constraintFactory.fromUnfiltered(otherClass), joiners);
}
}
@SafeVarargs
@Override
public final UniConstraintStream ifExistsIncludingNullVars(Class otherClass, BiJoiner... joiners) {
if (getRetrievalSemantics() == RetrievalSemantics.STANDARD) {
return ifExists(constraintFactory.forEachIncludingNullVars(otherClass), joiners);
} else {
return ifExists(constraintFactory.fromUnfiltered(otherClass), joiners);
}
}
@SafeVarargs
public final UniConstraintStream ifExists(UniConstraintStream otherStream, BiJoiner... joiners) {
return ifExistsOrNot(true, otherStream, joiners);
}
@SafeVarargs
@Override
public final UniConstraintStream ifNotExists(Class otherClass, BiJoiner... joiners) {
if (getRetrievalSemantics() == RetrievalSemantics.STANDARD) {
return ifNotExists(constraintFactory.forEach(otherClass), joiners);
} else {
// Calls fromUnfiltered() for backward compatibility only
return ifNotExists(constraintFactory.fromUnfiltered(otherClass), joiners);
}
}
@SafeVarargs
@Override
public final UniConstraintStream ifNotExistsIncludingNullVars(Class otherClass, BiJoiner... joiners) {
if (getRetrievalSemantics() == RetrievalSemantics.STANDARD) {
return ifNotExists(constraintFactory.forEachIncludingNullVars(otherClass), joiners);
} else {
return ifNotExists(constraintFactory.fromUnfiltered(otherClass), joiners);
}
}
@SafeVarargs
public final UniConstraintStream ifNotExists(UniConstraintStream otherStream, BiJoiner... joiners) {
return ifExistsOrNot(false, otherStream, joiners);
}
private UniConstraintStream ifExistsOrNot(boolean shouldExist, UniConstraintStream otherStream,
BiJoiner[] joiners) {
BavetAbstractUniConstraintStream other = assertBavetUniConstraintStream(otherStream);
BiJoinerComber joinerComber = BiJoinerComber.comb(joiners);
BavetIfExistsBridgeUniConstraintStream parentBridgeB = other.shareAndAddChild(
new BavetIfExistsBridgeUniConstraintStream<>(constraintFactory, other));
return constraintFactory.share(
new BavetIfExistsUniConstraintStream<>(constraintFactory, this, parentBridgeB,
shouldExist, joinerComber.getMergedJoiner(), joinerComber.getMergedFiltering()),
childStreamList::add);
}
// ************************************************************************
// Group by
// ************************************************************************
@Override
public UniConstraintStream groupBy(
UniConstraintCollector collector) {
UniGroupNodeConstructor> nodeConstructor =
(inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group0Mapping1CollectorUniNode<>(inputStoreIndex,
collector, tupleLifecycle, outputStoreSize);
return buildUniGroupBy(nodeConstructor);
}
private UniConstraintStream buildUniGroupBy(UniGroupNodeConstructor> nodeConstructor) {
BavetUniGroupBridgeUniConstraintStream bridge = shareAndAddChild(
new BavetUniGroupBridgeUniConstraintStream<>(constraintFactory, this, nodeConstructor));
return constraintFactory.share(
new BavetGroupUniConstraintStream<>(constraintFactory, bridge),
bridge::setGroupStream);
}
@Override
public BiConstraintStream groupBy(
UniConstraintCollector collectorA,
UniConstraintCollector collectorB) {
UniGroupNodeConstructor> nodeConstructor =
(inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group0Mapping2CollectorUniNode<>(inputStoreIndex,
collectorA, collectorB, tupleLifecycle, outputStoreSize);
return buildBiGroupBy(nodeConstructor);
}
private BiConstraintStream
buildBiGroupBy(UniGroupNodeConstructor> nodeConstructor) {
BavetBiGroupBridgeUniConstraintStream bridge = shareAndAddChild(
new BavetBiGroupBridgeUniConstraintStream<>(constraintFactory, this, nodeConstructor));
return constraintFactory.share(
new BavetGroupBiConstraintStream<>(constraintFactory, bridge),
bridge::setGroupStream);
}
@Override
public
TriConstraintStream
groupBy(UniConstraintCollector collectorA,
UniConstraintCollector collectorB,
UniConstraintCollector collectorC) {
UniGroupNodeConstructor> nodeConstructor =
(inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group0Mapping3CollectorUniNode<>(inputStoreIndex,
collectorA, collectorB, collectorC, tupleLifecycle, outputStoreSize);
return buildTriGroupBy(nodeConstructor);
}
private TriConstraintStream
buildTriGroupBy(UniGroupNodeConstructor> nodeConstructor) {
BavetTriGroupBridgeUniConstraintStream bridge = shareAndAddChild(
new BavetTriGroupBridgeUniConstraintStream<>(constraintFactory, this, nodeConstructor));
return constraintFactory.share(
new BavetGroupTriConstraintStream<>(constraintFactory, bridge),
bridge::setGroupStream);
}
@Override
public
QuadConstraintStream
groupBy(UniConstraintCollector collectorA,
UniConstraintCollector collectorB,
UniConstraintCollector collectorC,
UniConstraintCollector collectorD) {
UniGroupNodeConstructor> nodeConstructor =
(inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group0Mapping4CollectorUniNode<>(inputStoreIndex,
collectorA, collectorB, collectorC, collectorD, tupleLifecycle, outputStoreSize);
return buildQuadGroupBy(nodeConstructor);
}
private QuadConstraintStream
buildQuadGroupBy(UniGroupNodeConstructor> nodeConstructor) {
BavetQuadGroupBridgeUniConstraintStream bridge = shareAndAddChild(
new BavetQuadGroupBridgeUniConstraintStream<>(constraintFactory, this, nodeConstructor));
return constraintFactory.share(
new BavetGroupQuadConstraintStream<>(constraintFactory, bridge),
bridge::setGroupStream);
}
@Override
public UniConstraintStream groupBy(Function groupKeyMapping) {
UniGroupNodeConstructor> nodeConstructor =
(inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group1Mapping0CollectorUniNode<>(groupKeyMapping,
inputStoreIndex, tupleLifecycle, outputStoreSize);
return buildUniGroupBy(nodeConstructor);
}
@Override
public
TriConstraintStream groupBy(Function groupKeyMapping,
UniConstraintCollector collectorB,
UniConstraintCollector collectorC) {
UniGroupNodeConstructor> nodeConstructor =
(inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group1Mapping2CollectorUniNode<>(groupKeyMapping,
inputStoreIndex, collectorB, collectorC, tupleLifecycle, outputStoreSize);
return buildTriGroupBy(nodeConstructor);
}
@Override
public
QuadConstraintStream
groupBy(Function groupKeyMapping,
UniConstraintCollector collectorB,
UniConstraintCollector collectorC,
UniConstraintCollector collectorD) {
UniGroupNodeConstructor> nodeConstructor =
(inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group1Mapping3CollectorUniNode<>(groupKeyMapping,
inputStoreIndex, collectorB, collectorC, collectorD, tupleLifecycle, outputStoreSize);
return buildQuadGroupBy(nodeConstructor);
}
@Override
public BiConstraintStream groupBy(
Function groupKeyMapping,
UniConstraintCollector collector) {
UniGroupNodeConstructor> nodeConstructor =
(inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group1Mapping1CollectorUniNode<>(groupKeyMapping,
inputStoreIndex, collector, tupleLifecycle, outputStoreSize);
return buildBiGroupBy(nodeConstructor);
}
@Override
public BiConstraintStream groupBy(
Function groupKeyAMapping, Function groupKeyBMapping) {
UniGroupNodeConstructor> nodeConstructor =
(inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group2Mapping0CollectorUniNode<>(groupKeyAMapping,
groupKeyBMapping, inputStoreIndex, tupleLifecycle, outputStoreSize);
return buildBiGroupBy(nodeConstructor);
}
@Override
public TriConstraintStream groupBy(
Function groupKeyAMapping, Function groupKeyBMapping,
UniConstraintCollector collector) {
UniGroupNodeConstructor> nodeConstructor =
(inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group2Mapping1CollectorUniNode<>(groupKeyAMapping,
groupKeyBMapping, inputStoreIndex, collector, tupleLifecycle, outputStoreSize);
return buildTriGroupBy(nodeConstructor);
}
@Override
public
QuadConstraintStream groupBy(
Function groupKeyAMapping, Function groupKeyBMapping,
UniConstraintCollector collectorC,
UniConstraintCollector collectorD) {
UniGroupNodeConstructor> nodeConstructor =
(inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group2Mapping2CollectorUniNode<>(groupKeyAMapping,
groupKeyBMapping, inputStoreIndex, collectorC, collectorD, tupleLifecycle, outputStoreSize);
return buildQuadGroupBy(nodeConstructor);
}
@Override
public TriConstraintStream groupBy(
Function groupKeyAMapping, Function groupKeyBMapping,
Function groupKeyCMapping) {
UniGroupNodeConstructor> nodeConstructor =
(inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group3Mapping0CollectorUniNode<>(groupKeyAMapping,
groupKeyBMapping, groupKeyCMapping, inputStoreIndex, tupleLifecycle, outputStoreSize);
return buildTriGroupBy(nodeConstructor);
}
@Override
public
QuadConstraintStream
groupBy(Function groupKeyAMapping, Function groupKeyBMapping,
Function groupKeyCMapping,
UniConstraintCollector collectorD) {
UniGroupNodeConstructor> nodeConstructor =
(inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group3Mapping1CollectorUniNode<>(groupKeyAMapping,
groupKeyBMapping, groupKeyCMapping, inputStoreIndex, collectorD, tupleLifecycle, outputStoreSize);
return buildQuadGroupBy(nodeConstructor);
}
@Override
public QuadConstraintStream
groupBy(Function groupKeyAMapping, Function groupKeyBMapping,
Function groupKeyCMapping, Function groupKeyDMapping) {
UniGroupNodeConstructor> nodeConstructor =
(inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group4Mapping0CollectorUniNode<>(groupKeyAMapping,
groupKeyBMapping, groupKeyCMapping, groupKeyDMapping, inputStoreIndex, tupleLifecycle, outputStoreSize);
return buildQuadGroupBy(nodeConstructor);
}
// ************************************************************************
// Operations with duplicate tuple possibility
// ************************************************************************
@Override
public UniConstraintStream map(Function mapping) {
throw new UnsupportedOperationException();
}
@Override
public UniConstraintStream flattenLast(Function> mapping) {
BavetFlattenLastBridgeUniConstraintStream bridge = shareAndAddChild(
new BavetFlattenLastBridgeUniConstraintStream<>(constraintFactory, this, mapping));
return constraintFactory.share(
new BavetFlattenLastUniConstraintStream<>(constraintFactory, bridge),
bridge::setFlattenLastStream);
}
// ************************************************************************
// Penalize/reward
// ************************************************************************
@Override
public final Constraint impactScore(String constraintPackage, String constraintName, Score> constraintWeight,
ScoreImpactType impactType) {
BavetScoringUniConstraintStream stream = shareAndAddChild(
new BavetScoringUniConstraintStream<>(constraintFactory, this));
return buildConstraint(constraintPackage, constraintName, constraintWeight,
impactType, stream);
}
@Override
public final Constraint impactScore(String constraintPackage, String constraintName, Score> constraintWeight,
ToIntFunction matchWeigher, ScoreImpactType impactType) {
BavetScoringUniConstraintStream stream = shareAndAddChild(
new BavetScoringUniConstraintStream<>(constraintFactory, this, matchWeigher));
return buildConstraint(constraintPackage, constraintName, constraintWeight,
impactType, stream);
}
@Override
public final Constraint impactScoreLong(String constraintPackage, String constraintName, Score> constraintWeight,
ToLongFunction matchWeigher, ScoreImpactType impactType) {
BavetScoringUniConstraintStream stream = shareAndAddChild(
new BavetScoringUniConstraintStream<>(constraintFactory, this, matchWeigher));
return buildConstraint(constraintPackage, constraintName, constraintWeight,
impactType, stream);
}
@Override
public final Constraint impactScoreBigDecimal(String constraintPackage, String constraintName,
Score> constraintWeight, Function matchWeigher, ScoreImpactType impactType) {
BavetScoringUniConstraintStream stream = shareAndAddChild(
new BavetScoringUniConstraintStream<>(constraintFactory, this, matchWeigher));
return buildConstraint(constraintPackage, constraintName, constraintWeight,
impactType, stream);
}
@Override
public final Constraint impactScoreConfigurable(String constraintPackage, String constraintName,
ScoreImpactType impactType) {
BavetScoringUniConstraintStream stream = shareAndAddChild(
new BavetScoringUniConstraintStream<>(constraintFactory, this));
return buildConstraintConfigurable(constraintPackage, constraintName,
impactType, stream);
}
@Override
public final Constraint impactScoreConfigurable(String constraintPackage, String constraintName,
ToIntFunction matchWeigher, ScoreImpactType impactType) {
BavetScoringUniConstraintStream stream = shareAndAddChild(
new BavetScoringUniConstraintStream<>(constraintFactory, this, matchWeigher));
return buildConstraintConfigurable(constraintPackage, constraintName,
impactType, stream);
}
@Override
public final Constraint impactScoreConfigurableLong(String constraintPackage, String constraintName,
ToLongFunction matchWeigher, ScoreImpactType impactType) {
BavetScoringUniConstraintStream stream = shareAndAddChild(
new BavetScoringUniConstraintStream<>(constraintFactory, this, matchWeigher));
return buildConstraintConfigurable(constraintPackage, constraintName,
impactType, stream);
}
@Override
public final Constraint impactScoreConfigurableBigDecimal(String constraintPackage, String constraintName,
Function matchWeigher, ScoreImpactType impactType) {
BavetScoringUniConstraintStream stream = shareAndAddChild(
new BavetScoringUniConstraintStream<>(constraintFactory, this, matchWeigher));
return buildConstraintConfigurable(constraintPackage, constraintName,
impactType, stream);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy