Ejemplo n.º 1
0
        internal BoolExpr <T_Identifier> ExpensiveSimplify(out Converter <T_Identifier> converter)
        {
            ConversionContext <T_Identifier> conversionContext = IdentifierService <T_Identifier> .Instance.CreateConversionContext();

            converter = new Converter <T_Identifier>(this, conversionContext);
            if (converter.Vertex.IsOne())
            {
                return((BoolExpr <T_Identifier>)TrueExpr <T_Identifier> .Value);
            }
            if (converter.Vertex.IsZero())
            {
                return((BoolExpr <T_Identifier>)FalseExpr <T_Identifier> .Value);
            }
            return(BoolExpr <T_Identifier> .ChooseCandidate(this, converter.Cnf.Expr, converter.Dnf.Expr));
        }
Ejemplo n.º 2
0
        internal override BoolExpr <T_Identifier> VisitNot(NotExpr <T_Identifier> expression)
        {
            BoolExpr <T_Identifier> boolExpr = expression.Child.Accept <BoolExpr <T_Identifier> >((Visitor <T_Identifier, BoolExpr <T_Identifier> >) this);

            switch (boolExpr.ExprType)
            {
            case ExprType.Not:
                return(((NotExpr <T_Identifier>)boolExpr).Child);

            case ExprType.True:
                return((BoolExpr <T_Identifier>)FalseExpr <T_Identifier> .Value);

            case ExprType.False:
                return((BoolExpr <T_Identifier>)TrueExpr <T_Identifier> .Value);

            default:
                return(base.VisitNot(expression));
            }
        }
Ejemplo n.º 3
0
        private static BoolExpr <T_Identifier> ChooseCandidate(
            params BoolExpr <T_Identifier>[] candidates)
        {
            int num1 = 0;
            int num2 = 0;
            BoolExpr <T_Identifier> boolExpr1 = (BoolExpr <T_Identifier>)null;

            foreach (BoolExpr <T_Identifier> candidate in candidates)
            {
                BoolExpr <T_Identifier> boolExpr2 = candidate.Simplify();
                int num3 = boolExpr2.GetTerms().Distinct <TermExpr <T_Identifier> >().Count <TermExpr <T_Identifier> >();
                int num4 = boolExpr2.CountTerms();
                if (boolExpr1 == null || num3 < num1 || num3 == num1 && num4 < num2)
                {
                    boolExpr1 = boolExpr2;
                    num1      = num3;
                    num2      = num4;
                }
            }
            return(boolExpr1);
        }
Ejemplo n.º 4
0
 protected override bool EquivalentTypeEquals(BoolExpr <T_Identifier> other)
 {
     return(((TreeExpr <T_Identifier>)other).Children.SetEquals(Children));
 }
Ejemplo n.º 5
0
 protected abstract bool EquivalentTypeEquals(BoolExpr <T_Identifier> other);
 internal static IEnumerable <T_Identifier> GetLeaves(BoolExpr <T_Identifier> expression)
 {
     return(LeafVisitor <T_Identifier> .GetTerms(expression).Select <TermExpr <T_Identifier>, T_Identifier>((Func <TermExpr <T_Identifier>, T_Identifier>)(term => term.Identifier)));
 }
Ejemplo n.º 7
0
 /// <summary>
 ///     Performs local simplification appropriate to the current identifier.
 /// </summary>
 internal abstract BoolExpr <T_Identifier> LocalSimplify(BoolExpr <T_Identifier> expression);
Ejemplo n.º 8
0
 /// <summary>
 ///     Initialize a new Not expression with the given child.
 /// </summary>
 /// <param name="child"> </param>
 internal NotExpr(BoolExpr <T_Identifier> child)
     : base(new[] { child })
 {
 }
Ejemplo n.º 9
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));
 }
Ejemplo n.º 10
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;
 }
Ejemplo n.º 11
0
 internal Equivalence(BoolExpr <T_Identifier> left, BoolExpr <T_Identifier> right)
     : base((BoolExpr <T_Identifier>) new KnowledgeBase <T_Identifier> .Implication(left, right), (BoolExpr <T_Identifier>) new KnowledgeBase <T_Identifier> .Implication(right, left))
 {
     this._left  = left;
     this._right = right;
 }
Ejemplo n.º 12
0
        internal Converter(BoolExpr <T_Identifier> expr, ConversionContext <T_Identifier> context)
        {
            _context = context ?? IdentifierService <T_Identifier> .Instance.CreateConversionContext();

            _vertex = ToDecisionDiagramConverter <T_Identifier> .TranslateToRobdd(expr, _context);
        }
Ejemplo n.º 13
0
        private BoolExpr <T_Identifier> SimplifyTree(TreeExpr <T_Identifier> tree)
        {
            bool flag = ExprType.And == tree.ExprType;
            List <BoolExpr <T_Identifier> > boolExprList1 = new List <BoolExpr <T_Identifier> >(tree.Children.Count);

            foreach (BoolExpr <T_Identifier> child in tree.Children)
            {
                BoolExpr <T_Identifier> boolExpr = child.Accept <BoolExpr <T_Identifier> >((Visitor <T_Identifier, BoolExpr <T_Identifier> >) this);
                if (boolExpr.ExprType == tree.ExprType)
                {
                    boolExprList1.AddRange((IEnumerable <BoolExpr <T_Identifier> >)((TreeExpr <T_Identifier>)boolExpr).Children);
                }
                else
                {
                    boolExprList1.Add(boolExpr);
                }
            }
            Dictionary <BoolExpr <T_Identifier>, bool> dictionary    = new Dictionary <BoolExpr <T_Identifier>, bool>(tree.Children.Count);
            List <BoolExpr <T_Identifier> >            boolExprList2 = new List <BoolExpr <T_Identifier> >(tree.Children.Count);

            foreach (BoolExpr <T_Identifier> boolExpr in boolExprList1)
            {
                switch (boolExpr.ExprType)
                {
                case ExprType.Not:
                    dictionary[((NotExpr <T_Identifier>)boolExpr).Child] = true;
                    continue;

                case ExprType.True:
                    if (!flag)
                    {
                        return((BoolExpr <T_Identifier>)TrueExpr <T_Identifier> .Value);
                    }
                    continue;

                case ExprType.False:
                    if (flag)
                    {
                        return((BoolExpr <T_Identifier>)FalseExpr <T_Identifier> .Value);
                    }
                    continue;

                default:
                    boolExprList2.Add(boolExpr);
                    continue;
                }
            }
            List <BoolExpr <T_Identifier> > boolExprList3 = new List <BoolExpr <T_Identifier> >();

            foreach (BoolExpr <T_Identifier> key in boolExprList2)
            {
                if (dictionary.ContainsKey(key))
                {
                    if (flag)
                    {
                        return((BoolExpr <T_Identifier>)FalseExpr <T_Identifier> .Value);
                    }
                    return((BoolExpr <T_Identifier>)TrueExpr <T_Identifier> .Value);
                }
                boolExprList3.Add(key);
            }
            foreach (BoolExpr <T_Identifier> key in dictionary.Keys)
            {
                boolExprList3.Add(key.MakeNegated());
            }
            if (boolExprList3.Count == 0)
            {
                if (flag)
                {
                    return((BoolExpr <T_Identifier>)TrueExpr <T_Identifier> .Value);
                }
                return((BoolExpr <T_Identifier>)FalseExpr <T_Identifier> .Value);
            }
            if (1 == boolExprList3.Count)
            {
                return(boolExprList3[0]);
            }
            return(!flag ? (BoolExpr <T_Identifier>) new OrExpr <T_Identifier>((IEnumerable <BoolExpr <T_Identifier> >)boolExprList3) : (BoolExpr <T_Identifier>) new AndExpr <T_Identifier>((IEnumerable <BoolExpr <T_Identifier> >)boolExprList3));
        }
 protected override bool EquivalentTypeEquals(BoolExpr <T_Identifier> other)
 {
     return(this._comparer.Equals(this._identifier, ((TermExpr <T_Identifier>)other)._identifier));
 }
Ejemplo n.º 15
0
 internal virtual void AddFact(BoolExpr <T_Identifier> fact)
 {
     this._facts.Add(fact);
     this._knowledge = this._context.Solver.And(this._knowledge, new Converter <T_Identifier>(fact, this._context).Vertex);
 }
Ejemplo n.º 16
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;
 }
Ejemplo n.º 17
0
 internal void AddImplication(BoolExpr <T_Identifier> condition, BoolExpr <T_Identifier> implies)
 {
     this.AddFact((BoolExpr <T_Identifier>) new KnowledgeBase <T_Identifier> .Implication(condition, implies));
 }
Ejemplo n.º 18
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));
 }
Ejemplo n.º 19
0
 internal void AddEquivalence(BoolExpr <T_Identifier> left, BoolExpr <T_Identifier> right)
 {
     this.AddFact((BoolExpr <T_Identifier>) new KnowledgeBase <T_Identifier> .Equivalence(left, right));
 }
Ejemplo n.º 20
0
 internal static IEnumerable <T_Identifier> GetLeaves(BoolExpr <T_Identifier> expression)
 {
     return(GetTerms(expression).Select(term => term.Identifier));
 }
Ejemplo n.º 21
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));
 }
Ejemplo n.º 22
0
 internal override BoolExpr <DomainConstraint <T_Variable, T_Element> > LocalSimplify(
     BoolExpr <DomainConstraint <T_Variable, T_Element> > expression)
 {
     expression = NegationPusher.EliminateNot(expression);
     return(expression.Accept(Simplifier <DomainConstraint <T_Variable, T_Element> > .Instance));
 }
Ejemplo n.º 23
0
 public bool Equals(BoolExpr <T_Identifier> other)
 {
     return(null != other && ExprType == other.ExprType &&
            EquivalentTypeEquals(other));
 }
Ejemplo n.º 24
0
 internal override BoolExpr <T_Identifier> LocalSimplify(BoolExpr <T_Identifier> expression)
 {
     return(expression.Accept(Simplifier <T_Identifier> .Instance));
 }
Ejemplo n.º 25
0
 internal static int CountTerms(BoolExpr <T_Identifier> expression)
 {
     DebugCheck.NotNull(expression);
     return(expression.Accept(_instance));
 }