コード例 #1
0
ファイル: Simplifier.cs プロジェクト: dox0/DotNet471RS3
        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));
            }
        }
コード例 #2
0
 /// <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 })
 {
 }
コード例 #3
0
 protected override bool EquivalentTypeEquals(BoolExpr <T_Identifier> other)
 {
     return(((TreeExpr <T_Identifier>)other).Children.SetEquals(Children));
 }
コード例 #4
0
 protected override bool EquivalentTypeEquals(BoolExpr <T_Identifier> other)
 {
     return(_comparer.Equals(_identifier, ((TermExpr <T_Identifier>)other)._identifier));
 }
コード例 #5
0
 protected override bool EquivalentTypeEquals(BoolExpr <T_Identifier> other)
 {
     return(object.ReferenceEquals(this, other));
 }
コード例 #6
0
 protected abstract bool EquivalentTypeEquals(BoolExpr <T_Identifier> other);
コード例 #7
0
 // (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;
 }
コード例 #8
0
ファイル: Simplifier.cs プロジェクト: dox0/DotNet471RS3
        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);
            }
        }
コード例 #9
0
 internal static IEnumerable <T_Identifier> GetLeaves(BoolExpr <T_Identifier> expression)
 {
     return(GetTerms(expression).Select(term => term.Identifier));
 }
コード例 #10
0
 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));
 }
コード例 #11
0
 internal override BoolExpr <T_Identifier> LocalSimplify(BoolExpr <T_Identifier> expression)
 {
     return(expression.Accept(Simplifier <T_Identifier> .Instance));
 }
コード例 #12
0
 /// <summary>
 /// Performs local simplification appropriate to the current identifier.
 /// </summary>
 internal abstract BoolExpr <T_Identifier> LocalSimplify(BoolExpr <T_Identifier> expression);
コード例 #13
0
 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));
 }
コード例 #14
0
 /// <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));
 }
コード例 #15
0
 /// <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));
 }
コード例 #16
0
ファイル: Sentence.cs プロジェクト: longde123/playscript
 /// <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();
 }
コード例 #17
0
 internal static int CountTerms(BoolExpr <T_Identifier> expression)
 {
     Debug.Assert(null != expression);
     return(expression.Accept(s_instance));
 }
コード例 #18
0
 public bool Equals(BoolExpr <T_Identifier> other)
 {
     return(null != other && ExprType == other.ExprType &&
            EquivalentTypeEquals(other));
 }
コード例 #19
0
ファイル: Converter.cs プロジェクト: dox0/DotNet471RS3
        internal Converter(BoolExpr <T_Identifier> expr, ConversionContext <T_Identifier> context)
        {
            _context = context ?? IdentifierService <T_Identifier> .Instance.CreateConversionContext();

            _vertex = ToDecisionDiagramConverter <T_Identifier> .TranslateToRobdd(expr, _context);
        }
コード例 #20
0
 // (condition --> implies) iff. (!condition OR implies)
 internal Implication(BoolExpr <T_Identifier> condition, BoolExpr <T_Identifier> implies)
     : base(condition.MakeNegated(), implies)
 {
     _condition = condition;
     _implies   = implies;
 }