internal override BoolExpr <T_Identifier> VisitNot(NotExpr <T_Identifier> expression) { BoolExpr <T_Identifier> child = expression.Child.Accept(this); switch (child.ExprType) { case ExprType.Not: return(((NotExpr <T_Identifier>)child).Child); case ExprType.True: return(FalseExpr <T_Identifier> .Value); case ExprType.False: return(TrueExpr <T_Identifier> .Value); default: return(base.VisitNot(expression)); } }
/// <summary> /// Initialize a new Not expression with the given child. /// </summary> /// <param name="child"></param> internal NotExpr(BoolExpr <T_Identifier> child) : base(new BoolExpr <T_Identifier>[] { child }) { }
protected override bool EquivalentTypeEquals(BoolExpr <T_Identifier> other) { return(((TreeExpr <T_Identifier>)other).Children.SetEquals(Children)); }
protected override bool EquivalentTypeEquals(BoolExpr <T_Identifier> other) { return(_comparer.Equals(_identifier, ((TermExpr <T_Identifier>)other)._identifier)); }
protected override bool EquivalentTypeEquals(BoolExpr <T_Identifier> other) { return(object.ReferenceEquals(this, other)); }
protected abstract bool EquivalentTypeEquals(BoolExpr <T_Identifier> other);
// (left iff. right) iff. (left --> right AND right --> left) internal Equivalence(BoolExpr <T_Identifier> left, BoolExpr <T_Identifier> right) : base(new Implication(left, right), new Implication(right, left)) { _left = left; _right = right; }
private BoolExpr <T_Identifier> SimplifyTree(TreeExpr <T_Identifier> tree) { bool isAnd = ExprType.And == tree.ExprType; Debug.Assert(isAnd || ExprType.Or == tree.ExprType); // Get list of simplified children, flattening nested And/Or expressions List <BoolExpr <T_Identifier> > simplifiedChildren = new List <BoolExpr <T_Identifier> >(tree.Children.Count); foreach (BoolExpr <T_Identifier> child in tree.Children) { BoolExpr <T_Identifier> simplifiedChild = child.Accept(this); // And(And(A, B), C) iff. And(A, B, C) // Or(Or(A, B), C) iff. Or(A, B, C) if (simplifiedChild.ExprType == tree.ExprType) { simplifiedChildren.AddRange(((TreeExpr <T_Identifier>)simplifiedChild).Children); } else { simplifiedChildren.Add(simplifiedChild); } } // Track negated children separately to identify tautologies and contradictions Dictionary <BoolExpr <T_Identifier>, bool> negatedChildren = new Dictionary <BoolExpr <T_Identifier>, bool>(tree.Children.Count); List <BoolExpr <T_Identifier> > otherChildren = new List <BoolExpr <T_Identifier> >(tree.Children.Count); foreach (BoolExpr <T_Identifier> simplifiedChild in simplifiedChildren) { switch (simplifiedChild.ExprType) { case ExprType.Not: negatedChildren[((NotExpr <T_Identifier>)simplifiedChild).Child] = true; break; case ExprType.False: // False And A --> False if (isAnd) { return(FalseExpr <T_Identifier> .Value); } // False || A --> A (omit False from child collections) break; case ExprType.True: // True Or A --> True if (!isAnd) { return(TrueExpr <T_Identifier> .Value); } // True And A --> A (omit True from child collections) break; default: otherChildren.Add(simplifiedChild); break; } } List <BoolExpr <T_Identifier> > children = new List <BoolExpr <T_Identifier> >(); foreach (BoolExpr <T_Identifier> child in otherChildren) { if (negatedChildren.ContainsKey(child)) { // A && !A --> False, A || !A --> True if (isAnd) { return(FalseExpr <T_Identifier> .Value); } else { return(TrueExpr <T_Identifier> .Value); } } children.Add(child); } foreach (BoolExpr <T_Identifier> child in negatedChildren.Keys) { children.Add(child.MakeNegated()); } if (0 == children.Count) { // And() iff. True if (isAnd) { return(TrueExpr <T_Identifier> .Value); } // Or() iff. False else { return(FalseExpr <T_Identifier> .Value); } } else if (1 == children.Count) { // Or(A) iff. A, And(A) iff. A return(children[0]); } else { // Construct simplified And/Or expression TreeExpr <T_Identifier> result; if (isAnd) { result = new AndExpr <T_Identifier>(children); } else { result = new OrExpr <T_Identifier>(children); } return(result); } }
internal static IEnumerable <T_Identifier> GetLeaves(BoolExpr <T_Identifier> expression) { return(GetTerms(expression).Select(term => term.Identifier)); }
internal static BoolExpr <DomainConstraint <T_Variable, T_Element> > EliminateNot <T_Variable, T_Element>(BoolExpr <DomainConstraint <T_Variable, T_Element> > expression) { return(expression.Accept(NonNegatedDomainConstraintTreeVisitor <T_Variable, T_Element> .Instance)); }
internal override BoolExpr <T_Identifier> LocalSimplify(BoolExpr <T_Identifier> expression) { return(expression.Accept(Simplifier <T_Identifier> .Instance)); }
/// <summary> /// Performs local simplification appropriate to the current identifier. /// </summary> internal abstract BoolExpr <T_Identifier> LocalSimplify(BoolExpr <T_Identifier> expression);
internal override BoolExpr <DomainConstraint <T_Variable, T_Element> > LocalSimplify(BoolExpr <DomainConstraint <T_Variable, T_Element> > expression) { expression = NegationPusher.EliminateNot <T_Variable, T_Element>(expression); return(expression.Accept(Simplifier <DomainConstraint <T_Variable, T_Element> > .Instance)); }
/// <summary> /// Adds an equivalence to this KB, of the form: /// /// left iff. right /// </summary> /// <param name="left">Left operand</param> /// <param name="right">Right operand</param> internal void AddEquivalence(BoolExpr <T_Identifier> left, BoolExpr <T_Identifier> right) { AddFact(new Equivalence(left, right)); }
/// <summary> /// Adds the given implication to this KB, where implication is of the form: /// /// condition --> implies /// </summary> /// <param name="condition">Condition</param> /// <param name="implies">Entailed expression</param> internal void AddImplication(BoolExpr <T_Identifier> condition, BoolExpr <T_Identifier> implies) { AddFact(new Implication(condition, implies)); }
/// <summary> /// Initialize a new normal form node representing the given expression. Caller must /// ensure the expression is logically equivalent to the node. /// </summary> /// <param name="expr">Expression logically equivalent to this node.</param> protected NormalFormNode(BoolExpr <T_Identifier> expr) { _expr = expr.Simplify(); }
internal static int CountTerms(BoolExpr <T_Identifier> expression) { Debug.Assert(null != expression); return(expression.Accept(s_instance)); }
public bool Equals(BoolExpr <T_Identifier> other) { return(null != other && ExprType == other.ExprType && EquivalentTypeEquals(other)); }
internal Converter(BoolExpr <T_Identifier> expr, ConversionContext <T_Identifier> context) { _context = context ?? IdentifierService <T_Identifier> .Instance.CreateConversionContext(); _vertex = ToDecisionDiagramConverter <T_Identifier> .TranslateToRobdd(expr, _context); }
// (condition --> implies) iff. (!condition OR implies) internal Implication(BoolExpr <T_Identifier> condition, BoolExpr <T_Identifier> implies) : base(condition.MakeNegated(), implies) { _condition = condition; _implies = implies; }