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

org.optaplanner.constraint.streams.bavet.BavetConstraintSession 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.HashMap;
import java.util.List;
import java.util.Map;

import org.optaplanner.constraint.streams.bavet.common.AbstractNode;
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.constraint.ConstraintMatchTotal;
import org.optaplanner.core.api.score.constraint.Indictment;

public final class BavetConstraintSession> {

    private final AbstractScoreInliner scoreInliner;
    private final Map, ForEachUniNode> declaredClassToNodeMap;
    private final AbstractNode[] nodes; // Indexed by nodeIndex

    private final Map, List>> effectiveClassToNodeListMap;

    public BavetConstraintSession(AbstractScoreInliner scoreInliner,
            Map, ForEachUniNode> declaredClassToNodeMap,
            AbstractNode[] nodes) {
        this.scoreInliner = scoreInliner;
        this.declaredClassToNodeMap = declaredClassToNodeMap;
        this.nodes = nodes;
        effectiveClassToNodeListMap = new HashMap<>(declaredClassToNodeMap.size());
    }

    public List> findNodeList(Class factClass) {
        return effectiveClassToNodeListMap.computeIfAbsent(factClass, key -> {
            List> nodeList = new ArrayList<>();
            declaredClassToNodeMap.forEach((declaredClass, declaredNode) -> {
                if (declaredClass.isAssignableFrom(factClass)) {
                    nodeList.add(declaredNode);
                }
            });
            return nodeList;
        });
    }

    public void insert(Object fact) {
        Class factClass = fact.getClass();
        List> nodeList = findNodeList(factClass);
        for (ForEachUniNode node : nodeList) {
            node.insert(fact);
        }
    }

    public void update(Object fact) {
        Class factClass = fact.getClass();
        List> nodeList = findNodeList(factClass);
        for (ForEachUniNode node : nodeList) {
            node.update(fact);
        }
    }

    public void retract(Object fact) {
        Class factClass = fact.getClass();
        List> nodeList = findNodeList(factClass);
        for (ForEachUniNode node : nodeList) {
            node.retract(fact);
        }
    }

    public Score_ calculateScore(int initScore) {
        for (AbstractNode node : nodes) {
            node.calculateScore();
        }
        return scoreInliner.extractScore(initScore);
    }

    AbstractScoreInliner getScoreInliner() {
        return scoreInliner;
    }

    public Map> getConstraintMatchTotalMap() {
        return scoreInliner.getConstraintMatchTotalMap();
    }

    public Map> getIndictmentMap() {
        return scoreInliner.getIndictmentMap();
    }

}