001package org.clafer.ast;
002
003import org.clafer.common.Check;
004
005/**
006 * An immutable constraint. Can be either hard or soft.
007 *
008 * @author jimmy
009 */
010public class AstConstraint {
011
012    private static int idFactory = 0;
013    private final int id = idFactory++;
014    private final AstClafer context;
015    private AstBoolExpr expr;
016    private final boolean soft;
017
018    public AstConstraint(AstClafer context, AstBoolExpr expr) {
019        this(context, expr, false);
020    }
021
022    public AstConstraint(AstClafer context, AstBoolExpr expr, boolean soft) {
023        this.context = Check.notNull(context);
024        this.expr = Check.notNull(expr);
025        this.soft = soft;
026    }
027
028    public AstClafer getContext() {
029        return context;
030    }
031
032    public boolean isHard() {
033        return !isSoft();
034    }
035
036    public boolean isSoft() {
037        return soft;
038    }
039
040    public AstBoolExpr getExpr() {
041        return expr;
042    }
043
044    @Override
045    public boolean equals(Object obj) {
046        return this == obj;
047    }
048
049    @Override
050    public int hashCode() {
051        return id;
052    }
053
054    @Override
055    public String toString() {
056        return isHard() ? "[" + expr + "]" : "(" + expr + ")";
057    }
058}