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

org.optaplanner.constraint.streams.bavet.BavetConstraintSessionFactory 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;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.optaplanner.constraint.streams.bavet.common.AbstractNode;
import org.optaplanner.constraint.streams.bavet.common.BavetAbstractConstraintStream;
import org.optaplanner.constraint.streams.bavet.common.NodeBuildHelper;
import org.optaplanner.constraint.streams.bavet.uni.ForEachUniNode;
import org.optaplanner.constraint.streams.common.inliner.AbstractScoreInliner;
import org.optaplanner.core.api.score.Score;
import org.optaplanner.core.api.score.stream.Constraint;
import org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor;
import org.optaplanner.core.impl.score.definition.ScoreDefinition;

public final class BavetConstraintSessionFactory> {

    private final SolutionDescriptor solutionDescriptor;
    private final List> constraintList;

    public BavetConstraintSessionFactory(SolutionDescriptor solutionDescriptor,
            List> constraintList) {
        this.solutionDescriptor = solutionDescriptor;
        this.constraintList = constraintList;
    }

    // ************************************************************************
    // Node creation
    // ************************************************************************

    public BavetConstraintSession buildSession(boolean constraintMatchEnabled,
            Solution_ workingSolution) {
        ScoreDefinition scoreDefinition = solutionDescriptor.getScoreDefinition();
        AbstractScoreInliner scoreInliner = AbstractScoreInliner.buildScoreInliner(scoreDefinition,
                constraintMatchEnabled);

        Score_ zeroScore = scoreDefinition.getZeroScore();
        Set> constraintStreamSet = new LinkedHashSet<>();
        Map constraintWeightMap = new HashMap<>(constraintList.size());
        for (BavetConstraint constraint : constraintList) {
            Score_ constraintWeight = constraint.extractConstraintWeight(workingSolution);
            // Filter out nodes that only lead to constraints with zero weight.
            // Note: Node sharing happens earlier, in BavetConstraintFactory#share(Stream_).
            if (!constraintWeight.equals(zeroScore)) {
                // Relies on BavetConstraintFactory#share(Stream_) occurring for all constraint stream instances
                // to ensure there are no 2 equal ConstraintStream instances (with different child stream lists).
                constraint.collectActiveConstraintStreams(constraintStreamSet);
                constraintWeightMap.put(constraint, constraintWeight);
            }
        }
        NodeBuildHelper buildHelper = new NodeBuildHelper<>(constraintStreamSet, constraintWeightMap, scoreInliner);
        // Build constraintStreamSet in reverse order to create downstream nodes first
        // so every node only has final variables (some of which have downstream node method references).
        List> reversedConstraintStreamList = new ArrayList<>(constraintStreamSet);
        Collections.reverse(reversedConstraintStreamList);
        for (BavetAbstractConstraintStream constraintStream : reversedConstraintStreamList) {
            constraintStream.buildNode(buildHelper);
        }
        List nodeList = buildHelper.destroyAndGetNodeList();
        Map, ForEachUniNode> declaredClassToNodeMap = new LinkedHashMap<>();
        for (AbstractNode node : nodeList) {
            if (node instanceof ForEachUniNode) {
                ForEachUniNode forEachUniNode = (ForEachUniNode) node;
                ForEachUniNode old = declaredClassToNodeMap.put(forEachUniNode.getForEachClass(), forEachUniNode);
                if (old != null) {
                    throw new IllegalStateException("Impossible state: For class (" + forEachUniNode.getForEachClass()
                            + ") there are 2 nodes (" + forEachUniNode + ", " + old + ").");
                }
            }
        }
        return new BavetConstraintSession<>(scoreInliner, declaredClassToNodeMap, nodeList.toArray(new AbstractNode[0]));
    }

}