001package org.clafer.compiler;
002
003import java.util.Set;
004import org.clafer.ast.AstClafer;
005import org.clafer.common.Check;
006
007/**
008 * Defines the options for the Clafer compiler. Start with the default options
009 * and configure it to your needs. For example,
010 * {@code ClaferOptions.Default.preferSmallerInstances()} will prefer smaller
011 * instances and use default settings for all the other option.
012 *
013 * @author jimmy
014 */
015public class ClaferOption {
016
017    private final ClaferSearchStrategy strategy;
018    /**
019     * If true then basic symmetry breaking, else full symmetry breaking.
020     */
021    private final boolean basicSymmetryBreaking;
022    /**
023     * If true then basic optimizations else full optimizations.
024     */
025    private final boolean basicOptimizations;
026    /**
027     * The branching heuristic will use the priorities to determine the order of
028     * branching.
029     */
030    private final Set<AstClafer>[] branchingPriority;
031
032    /**
033     * Use the default options.
034     */
035    public static final ClaferOption Optimized = new ClaferOption(ClaferSearchStrategy.PreferSmallerInstances, false, false);
036    public static final ClaferOption Basic = new ClaferOption(ClaferSearchStrategy.PreferSmallerInstances, true, true);
037    public static final ClaferOption Default = Optimized;
038
039    private ClaferOption(ClaferSearchStrategy strategy, boolean basicSymmetryBreaking, boolean basicOptimizations, Set<AstClafer>[] branchingPriority) {
040        this.strategy = Check.notNull(strategy);
041        this.basicSymmetryBreaking = basicSymmetryBreaking;
042        this.basicOptimizations = basicOptimizations;
043        this.branchingPriority = branchingPriority;
044    }
045
046    private ClaferOption(ClaferSearchStrategy strategy, boolean basicSymmetryBreaking, boolean basicOptimizations) {
047        this(strategy, basicSymmetryBreaking, basicOptimizations, new Set[0]);
048    }
049
050    public ClaferSearchStrategy getStrategy() {
051        return strategy;
052    }
053
054    public ClaferOption setStrategy(ClaferSearchStrategy strategy) {
055        return new ClaferOption(strategy, basicSymmetryBreaking, basicOptimizations);
056    }
057
058    public boolean isBasicSymmetryBreaking() {
059        return basicSymmetryBreaking;
060    }
061
062    public boolean isFullSymmetryBreaking() {
063        return !basicSymmetryBreaking;
064    }
065
066    public ClaferOption basicSymmetryBreaking() {
067        return new ClaferOption(strategy, true, basicOptimizations);
068    }
069
070    public ClaferOption fullSymmetryBreaking() {
071        return new ClaferOption(strategy, false, basicOptimizations);
072    }
073
074    public boolean isBasicOptimizations() {
075        return basicOptimizations;
076    }
077
078    public boolean isFullOptimizations() {
079        return !basicOptimizations;
080    }
081
082    public ClaferOption basicOptimizations() {
083        return new ClaferOption(strategy, basicSymmetryBreaking, true);
084    }
085
086    public ClaferOption fullOptimizations() {
087        return new ClaferOption(strategy, basicSymmetryBreaking, false);
088    }
089
090    public Set<AstClafer>[] getBranchingPriority() {
091        return branchingPriority;
092    }
093
094    /**
095     * Specify priorities of Clafers for branching.
096     *
097     * For example, consider the priorities [{a,b}, {c,d}, {e}]. This forces the
098     * branching heuristic to branch on either a or b. Once both a and b are
099     * assigned, then the branching heuristic will branch on either c or d. Once
100     * all a, b, c, and d are assigned then the branching heuristic will branch
101     * on e. Once all a, b, c, d, and e are assigned, then the branching
102     * heuristic will branch on the remaining Clafers.
103     *
104     * Assumes that the sets in this array are all pairwise disjoint.
105     *
106     * @param branchingPriority
107     * @return
108     */
109    public ClaferOption setBranchingPriority(Set<AstClafer>... branchingPriority) {
110        return new ClaferOption(strategy, basicSymmetryBreaking, basicOptimizations, branchingPriority);
111    }
112
113    @Override
114    public String toString() {
115        return strategy + "\n"
116                + (basicSymmetryBreaking ? "perform only basic symmetry breaking\n" : "perform full symmetry breaking\n")
117                + (basicOptimizations ? "perform only basic optimizations\n" : "perform full optimizations\n");
118    }
119}