All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.optaplanner.constraint.streams.bavet.quad.BavetAbstractQuadConstraintStream Maven / Gradle / Ivy

Go to download

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).

There is a newer version: 10.0.0
Show newest version
package org.optaplanner.constraint.streams.bavet.quad;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import org.optaplanner.constraint.streams.bavet.BavetConstraintFactory;
import org.optaplanner.constraint.streams.bavet.bi.BavetGroupBiConstraintStream;
import org.optaplanner.constraint.streams.bavet.bi.BiTuple;
import org.optaplanner.constraint.streams.bavet.common.BavetAbstractConstraintStream;
import org.optaplanner.constraint.streams.bavet.tri.BavetGroupTriConstraintStream;
import org.optaplanner.constraint.streams.bavet.tri.TriTuple;
import org.optaplanner.constraint.streams.bavet.uni.BavetAbstractUniConstraintStream;
import org.optaplanner.constraint.streams.bavet.uni.BavetGroupUniConstraintStream;
import org.optaplanner.constraint.streams.bavet.uni.BavetIfExistsBridgeUniConstraintStream;
import org.optaplanner.constraint.streams.bavet.uni.UniTuple;
import org.optaplanner.constraint.streams.common.RetrievalSemantics;
import org.optaplanner.constraint.streams.common.ScoreImpactType;
import org.optaplanner.constraint.streams.common.penta.PentaJoinerComber;
import org.optaplanner.constraint.streams.common.quad.InnerQuadConstraintStream;
import org.optaplanner.core.api.function.QuadFunction;
import org.optaplanner.core.api.function.QuadPredicate;
import org.optaplanner.core.api.function.ToIntQuadFunction;
import org.optaplanner.core.api.function.ToLongQuadFunction;
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.penta.PentaJoiner;
import org.optaplanner.core.api.score.stream.quad.QuadConstraintCollector;
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.UniConstraintStream;

public abstract class BavetAbstractQuadConstraintStream
        extends BavetAbstractConstraintStream
        implements InnerQuadConstraintStream {

    protected final List> childStreamList = new ArrayList<>(2);

    public BavetAbstractQuadConstraintStream(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 BavetAbstractQuadConstraintStream filter(QuadPredicate predicate) {
        return shareAndAddChild(
                new BavetFilterQuadConstraintStream<>(constraintFactory, this, predicate));
    }

    // ************************************************************************
    // If (not) exists
    // ************************************************************************

    @SafeVarargs
    @Override
    public final  QuadConstraintStream ifExists(Class otherClass, PentaJoiner... 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  QuadConstraintStream ifExistsIncludingNullVars(Class otherClass,
            PentaJoiner... joiners) {
        if (getRetrievalSemantics() == RetrievalSemantics.STANDARD) {
            return ifExists(constraintFactory.forEachIncludingNullVars(otherClass), joiners);
        } else {
            return ifExists(constraintFactory.fromUnfiltered(otherClass), joiners);
        }
    }

    @SafeVarargs
    public final  QuadConstraintStream ifExists(UniConstraintStream otherStream,
            PentaJoiner... joiners) {
        return ifExistsOrNot(true, otherStream, joiners);
    }

    @SafeVarargs
    @Override
    public final  QuadConstraintStream ifNotExists(Class otherClass, PentaJoiner... 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  QuadConstraintStream ifNotExistsIncludingNullVars(Class otherClass,
            PentaJoiner... joiners) {
        if (getRetrievalSemantics() == RetrievalSemantics.STANDARD) {
            return ifNotExists(constraintFactory.forEachIncludingNullVars(otherClass), joiners);
        } else {
            return ifNotExists(constraintFactory.fromUnfiltered(otherClass), joiners);
        }
    }

    @SafeVarargs
    public final  QuadConstraintStream ifNotExists(UniConstraintStream otherStream,
            PentaJoiner... joiners) {
        return ifExistsOrNot(false, otherStream, joiners);
    }

    private  QuadConstraintStream ifExistsOrNot(boolean shouldExist,
            UniConstraintStream otherStream, PentaJoiner[] joiners) {
        BavetAbstractUniConstraintStream other = assertBavetUniConstraintStream(otherStream);
        PentaJoinerComber joinerComber = PentaJoinerComber.comb(joiners);
        BavetIfExistsBridgeUniConstraintStream parentBridgeD = other.shareAndAddChild(
                new BavetIfExistsBridgeUniConstraintStream<>(constraintFactory, other));
        return constraintFactory.share(
                new BavetIfExistsQuadConstraintStream<>(constraintFactory, this, parentBridgeD,
                        shouldExist, joinerComber.getMergedJoiner(), joinerComber.getMergedFiltering()),
                childStreamList::add);
    }

    // ************************************************************************
    // Group by
    // ************************************************************************

    @Override
    public  UniConstraintStream groupBy(
            QuadConstraintCollector collector) {
        QuadGroupNodeConstructor> nodeConstructor =
                (inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group0Mapping1CollectorQuadNode<>(inputStoreIndex,
                        collector, tupleLifecycle, outputStoreSize);
        return buildUniGroupBy(nodeConstructor);
    }

    private  UniConstraintStream
            buildUniGroupBy(QuadGroupNodeConstructor> nodeConstructor) {
        BavetUniGroupBridgeQuadConstraintStream bridge = shareAndAddChild(
                new BavetUniGroupBridgeQuadConstraintStream<>(constraintFactory, this, nodeConstructor));
        return constraintFactory.share(
                new BavetGroupUniConstraintStream<>(constraintFactory, bridge),
                bridge::setGroupStream);
    }

    @Override
    public  BiConstraintStream groupBy(
            QuadConstraintCollector collectorA,
            QuadConstraintCollector collectorB) {
        QuadGroupNodeConstructor> nodeConstructor =
                (inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group0Mapping2CollectorQuadNode<>(inputStoreIndex,
                        collectorA, collectorB, tupleLifecycle, outputStoreSize);
        return buildBiGroupBy(nodeConstructor);
    }

    private  BiConstraintStream
            buildBiGroupBy(QuadGroupNodeConstructor> nodeConstructor) {
        BavetBiGroupBridgeQuadConstraintStream bridge = shareAndAddChild(
                new BavetBiGroupBridgeQuadConstraintStream<>(constraintFactory, this, nodeConstructor));
        return constraintFactory.share(
                new BavetGroupBiConstraintStream<>(constraintFactory, bridge),
                bridge::setGroupStream);
    }

    @Override
    public 
            TriConstraintStream
            groupBy(QuadConstraintCollector collectorA,
                    QuadConstraintCollector collectorB,
                    QuadConstraintCollector collectorC) {
        QuadGroupNodeConstructor> nodeConstructor =
                (inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group0Mapping3CollectorQuadNode<>(inputStoreIndex,
                        collectorA, collectorB, collectorC, tupleLifecycle, outputStoreSize);
        return buildTriGroupBy(nodeConstructor);
    }

    private  TriConstraintStream
            buildTriGroupBy(QuadGroupNodeConstructor> nodeConstructor) {
        BavetTriGroupBridgeQuadConstraintStream bridge = shareAndAddChild(
                new BavetTriGroupBridgeQuadConstraintStream<>(constraintFactory, this, nodeConstructor));
        return constraintFactory.share(
                new BavetGroupTriConstraintStream<>(constraintFactory, bridge),
                bridge::setGroupStream);
    }

    @Override
    public 
            QuadConstraintStream
            groupBy(QuadConstraintCollector collectorA,
                    QuadConstraintCollector collectorB,
                    QuadConstraintCollector collectorC,
                    QuadConstraintCollector collectorD) {
        QuadGroupNodeConstructor> nodeConstructor =
                (inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group0Mapping4CollectorQuadNode<>(inputStoreIndex,
                        collectorA, collectorB, collectorC, collectorD, tupleLifecycle, outputStoreSize);
        return buildQuadGroupBy(nodeConstructor);
    }

    private  QuadConstraintStream
            buildQuadGroupBy(QuadGroupNodeConstructor> nodeConstructor) {
        BavetQuadGroupBridgeQuadConstraintStream bridge = shareAndAddChild(
                new BavetQuadGroupBridgeQuadConstraintStream<>(constraintFactory, this, nodeConstructor));
        return constraintFactory.share(
                new BavetGroupQuadConstraintStream<>(constraintFactory, bridge),
                bridge::setGroupStream);
    }

    @Override
    public  UniConstraintStream groupBy(QuadFunction groupKeyMapping) {
        QuadGroupNodeConstructor> nodeConstructor =
                (inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group1Mapping0CollectorQuadNode<>(groupKeyMapping,
                        inputStoreIndex, tupleLifecycle, outputStoreSize);
        return buildUniGroupBy(nodeConstructor);
    }

    @Override
    public 
            TriConstraintStream groupBy(QuadFunction groupKeyMapping,
                    QuadConstraintCollector collectorB,
                    QuadConstraintCollector collectorC) {
        QuadGroupNodeConstructor> nodeConstructor =
                (inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group1Mapping2CollectorQuadNode<>(groupKeyMapping,
                        inputStoreIndex, collectorB, collectorC, tupleLifecycle, outputStoreSize);
        return buildTriGroupBy(nodeConstructor);
    }

    @Override
    public 
            QuadConstraintStream
            groupBy(QuadFunction groupKeyMapping,
                    QuadConstraintCollector collectorB,
                    QuadConstraintCollector collectorC,
                    QuadConstraintCollector collectorD) {
        QuadGroupNodeConstructor> nodeConstructor =
                (inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group1Mapping3CollectorQuadNode<>(groupKeyMapping,
                        inputStoreIndex, collectorB, collectorC, collectorD, tupleLifecycle, outputStoreSize);
        return buildQuadGroupBy(nodeConstructor);
    }

    @Override
    public  BiConstraintStream groupBy(
            QuadFunction groupKeyMapping,
            QuadConstraintCollector collector) {
        QuadGroupNodeConstructor> nodeConstructor =
                (inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group1Mapping1CollectorQuadNode<>(groupKeyMapping,
                        inputStoreIndex, collector, tupleLifecycle, outputStoreSize);
        return buildBiGroupBy(nodeConstructor);
    }

    @Override
    public  BiConstraintStream groupBy(
            QuadFunction groupKeyAMapping,
            QuadFunction groupKeyBMapping) {
        QuadGroupNodeConstructor> nodeConstructor =
                (inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group2Mapping0CollectorQuadNode<>(groupKeyAMapping,
                        groupKeyBMapping, inputStoreIndex, tupleLifecycle, outputStoreSize);
        return buildBiGroupBy(nodeConstructor);
    }

    @Override
    public  TriConstraintStream groupBy(
            QuadFunction groupKeyAMapping,
            QuadFunction groupKeyBMapping,
            QuadConstraintCollector collector) {
        QuadGroupNodeConstructor> nodeConstructor =
                (inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group2Mapping1CollectorQuadNode<>(groupKeyAMapping,
                        groupKeyBMapping, inputStoreIndex, collector, tupleLifecycle, outputStoreSize);
        return buildTriGroupBy(nodeConstructor);
    }

    @Override
    public 
            QuadConstraintStream groupBy(
                    QuadFunction groupKeyAMapping,
                    QuadFunction groupKeyBMapping,
                    QuadConstraintCollector collectorC,
                    QuadConstraintCollector collectorD) {
        QuadGroupNodeConstructor> nodeConstructor =
                (inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group2Mapping2CollectorQuadNode<>(groupKeyAMapping,
                        groupKeyBMapping, inputStoreIndex, collectorC, collectorD, tupleLifecycle, outputStoreSize);
        return buildQuadGroupBy(nodeConstructor);
    }

    @Override
    public  TriConstraintStream groupBy(
            QuadFunction groupKeyAMapping, QuadFunction groupKeyBMapping,
            QuadFunction groupKeyCMapping) {
        QuadGroupNodeConstructor> nodeConstructor =
                (inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group3Mapping0CollectorQuadNode<>(groupKeyAMapping,
                        groupKeyBMapping, groupKeyCMapping, inputStoreIndex, tupleLifecycle, outputStoreSize);
        return buildTriGroupBy(nodeConstructor);
    }

    @Override
    public 
            QuadConstraintStream
            groupBy(QuadFunction groupKeyAMapping,
                    QuadFunction groupKeyBMapping,
                    QuadFunction groupKeyCMapping,
                    QuadConstraintCollector collectorD) {
        QuadGroupNodeConstructor> nodeConstructor =
                (inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group3Mapping1CollectorQuadNode<>(groupKeyAMapping,
                        groupKeyBMapping, groupKeyCMapping, inputStoreIndex, collectorD, tupleLifecycle, outputStoreSize);
        return buildQuadGroupBy(nodeConstructor);
    }

    @Override
    public  QuadConstraintStream
            groupBy(QuadFunction groupKeyAMapping,
                    QuadFunction groupKeyBMapping,
                    QuadFunction groupKeyCMapping,
                    QuadFunction groupKeyDMapping) {
        QuadGroupNodeConstructor> nodeConstructor =
                (inputStoreIndex, tupleLifecycle, outputStoreSize) -> new Group4Mapping0CollectorQuadNode<>(groupKeyAMapping,
                        groupKeyBMapping, groupKeyCMapping, groupKeyDMapping, inputStoreIndex, tupleLifecycle, outputStoreSize);
        return buildQuadGroupBy(nodeConstructor);
    }

    // ************************************************************************
    // Operations with duplicate tuple possibility
    // ************************************************************************

    @Override
    public  UniConstraintStream map(QuadFunction mapping) {
        throw new UnsupportedOperationException();
    }

    @Override
    public  QuadConstraintStream flattenLast(Function> mapping) {
        BavetFlattenLastBridgeQuadConstraintStream bridge = shareAndAddChild(
                new BavetFlattenLastBridgeQuadConstraintStream<>(constraintFactory, this, mapping));
        return constraintFactory.share(
                new BavetFlattenLastQuadConstraintStream<>(constraintFactory, bridge),
                bridge::setFlattenLastStream);
    }

    // ************************************************************************
    // Penalize/reward
    // ************************************************************************

    @Override
    public final Constraint impactScore(String constraintPackage, String constraintName, Score constraintWeight,
            ScoreImpactType impactType) {
        BavetScoringQuadConstraintStream stream = shareAndAddChild(
                new BavetScoringQuadConstraintStream<>(constraintFactory, this));
        return buildConstraint(constraintPackage, constraintName, constraintWeight,
                impactType, stream);
    }

    @Override
    public final Constraint impactScore(String constraintPackage, String constraintName, Score constraintWeight,
            ToIntQuadFunction matchWeigher, ScoreImpactType impactType) {
        BavetScoringQuadConstraintStream stream = shareAndAddChild(
                new BavetScoringQuadConstraintStream<>(constraintFactory, this, matchWeigher));
        return buildConstraint(constraintPackage, constraintName, constraintWeight,
                impactType, stream);
    }

    @Override
    public final Constraint impactScoreLong(String constraintPackage, String constraintName,
            Score constraintWeight, ToLongQuadFunction matchWeigher, ScoreImpactType impactType) {
        BavetScoringQuadConstraintStream stream = shareAndAddChild(
                new BavetScoringQuadConstraintStream<>(constraintFactory, this, matchWeigher));
        return buildConstraint(constraintPackage, constraintName, constraintWeight,
                impactType, stream);
    }

    @Override
    public final Constraint impactScoreBigDecimal(String constraintPackage, String constraintName,
            Score constraintWeight, QuadFunction matchWeigher, ScoreImpactType impactType) {
        BavetScoringQuadConstraintStream stream = shareAndAddChild(
                new BavetScoringQuadConstraintStream<>(constraintFactory, this, matchWeigher));
        return buildConstraint(constraintPackage, constraintName, constraintWeight,
                impactType, stream);
    }

    @Override
    public final Constraint impactScoreConfigurable(String constraintPackage, String constraintName,
            ScoreImpactType impactType) {
        BavetScoringQuadConstraintStream stream = shareAndAddChild(
                new BavetScoringQuadConstraintStream<>(constraintFactory, this));
        return buildConstraintConfigurable(constraintPackage, constraintName,
                impactType, stream);
    }

    @Override
    public final Constraint impactScoreConfigurable(String constraintPackage, String constraintName,
            ToIntQuadFunction matchWeigher, ScoreImpactType impactType) {
        BavetScoringQuadConstraintStream stream = shareAndAddChild(
                new BavetScoringQuadConstraintStream<>(constraintFactory, this, matchWeigher));
        return buildConstraintConfigurable(constraintPackage, constraintName,
                impactType, stream);
    }

    @Override
    public final Constraint impactScoreConfigurableLong(String constraintPackage, String constraintName,
            ToLongQuadFunction matchWeigher, ScoreImpactType impactType) {
        BavetScoringQuadConstraintStream stream = shareAndAddChild(
                new BavetScoringQuadConstraintStream<>(constraintFactory, this, matchWeigher));
        return buildConstraintConfigurable(constraintPackage, constraintName,
                impactType, stream);
    }

    @Override
    public final Constraint impactScoreConfigurableBigDecimal(String constraintPackage, String constraintName,
            QuadFunction matchWeigher, ScoreImpactType impactType) {
        BavetScoringQuadConstraintStream stream = shareAndAddChild(
                new BavetScoringQuadConstraintStream<>(constraintFactory, this, matchWeigher));
        return buildConstraintConfigurable(constraintPackage, constraintName,
                impactType, stream);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy