001package org.clafer.ast.compiler;
002
003import java.util.Collections;
004import java.util.Map;
005import org.clafer.ast.AstClafer;
006import org.clafer.ast.AstConstraint;
007import org.clafer.ast.AstException;
008import org.clafer.ast.AstModel;
009import org.clafer.ast.AstRef;
010import org.clafer.ast.analysis.Analysis;
011import org.clafer.common.Check;
012import org.clafer.ir.IrBoolVar;
013import org.clafer.ir.IrIntVar;
014import org.clafer.ir.IrSetVar;
015import org.clafer.objective.Objective;
016
017/**
018 *
019 * @author jimmy
020 */
021public class AstSolutionMap {
022
023    private final AstModel model;
024    private final Map<AstClafer, IrSetVar[]> siblingVars;
025    private final Map<AstRef, IrIntVar[]> refVars;
026    private final Map<AstConstraint, IrBoolVar> softVars;
027    private final IrIntVar sumSoftVar;
028    private final Map<Objective, IrIntVar> objectiveVars;
029    private final Analysis analysis;
030
031    AstSolutionMap(AstModel model,
032            Map<AstClafer, IrSetVar[]> sibling,
033            Map<AstRef, IrIntVar[]> refVars,
034            Map<AstConstraint, IrBoolVar> softVars,
035            IrIntVar sumSoftVar,
036            Map<Objective, IrIntVar> objectiveVars,
037            Analysis analysis) {
038        this.model = Check.notNull(model);
039        this.siblingVars = Check.notNull(sibling);
040        this.refVars = Check.notNull(refVars);
041        this.softVars = Check.notNull(softVars);
042        this.sumSoftVar = Check.notNull(sumSoftVar);
043        this.objectiveVars = Check.notNull(objectiveVars);
044        this.analysis = Check.notNull(analysis);
045    }
046
047    public AstModel getModel() {
048        return model;
049    }
050
051    public Analysis getAnalysis() {
052        return analysis;
053    }
054
055    /**
056     * Returns the sibling variables associated with the Clafer.
057     *
058     * @param clafer the Clafer
059     * @return the sibling variables associated with the Clafer
060     */
061    public IrSetVar[] getSiblingVars(AstClafer clafer) {
062        return notNull(clafer + " not part of the AST solution", siblingVars.get(clafer));
063    }
064
065    /**
066     * Returns the reference variables associated to the reference.
067     *
068     * @param ref the reference
069     * @return the reference variables associated to the reference
070     */
071    public IrIntVar[] getRefVars(AstRef ref) {
072        return notNull(ref + " not part of the AST solution", refVars.get(ref));
073    }
074
075    /**
076     * Returns the soft variable associated to the constraint.
077     *
078     * @param constraint the constraint
079     * @return the soft variable associated to the constraint
080     */
081    public IrBoolVar getSoftVar(AstConstraint constraint) {
082        return notNull(constraint + " not a compiled soft constraint", softVars.get(constraint));
083    }
084
085    public IrBoolVar[] getSoftVars() {
086        return softVars.values().toArray(new IrBoolVar[softVars.size()]);
087    }
088
089    public Map<AstConstraint, IrBoolVar> getSoftVarsMap() {
090        return Collections.unmodifiableMap(softVars);
091    }
092
093    /**
094     * Returns the variable equal to the sum of the soft variables.
095     *
096     * @return the variable equal to the sum of the soft variables
097     */
098    public IrIntVar getSumSoftVar() {
099        return sumSoftVar;
100    }
101
102    /**
103     * Returns the variable associated to the objective.
104     *
105     * @param objective the objective
106     * @return the variable associated to the objective
107     */
108    public IrIntVar getObjectiveVar(Objective objective) {
109        return notNull(objective + " not a compiled objective", objectiveVars.get(objective));
110    }
111
112    private static <T> T notNull(String message, T t) {
113        if (t == null) {
114            throw new AstException(message);
115        }
116        return t;
117    }
118}