Exemplo n.º 1
0
            internal void Refine(IBooleanAlgebra <TPredicate> solver, TPredicate other)
            {
                if (!StackHelper.TryEnsureSufficientExecutionStack())
                {
                    StackHelper.CallOnEmptyStack(Refine, solver, other);
                    return;
                }

                TPredicate thisAndOther = solver.And(_pred, other);

                if (solver.IsSatisfiable(thisAndOther))
                {
                    // The predicates overlap, now check if this is contained in other
                    TPredicate thisMinusOther = solver.And(_pred, solver.Not(other));
                    if (solver.IsSatisfiable(thisMinusOther))
                    {
                        // This is not contained in other, minterms may need to be split
                        if (_left is null)
                        {
                            Debug.Assert(_right is null);
                            _left  = new PartitionTree(thisAndOther);
                            _right = new PartitionTree(thisMinusOther);
                        }
                        else
                        {
                            Debug.Assert(_right is not null);
                            _left.Refine(solver, other);
                            _right.Refine(solver, other);
                        }
                    }
                }
            }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a leaf with LeafCondition pred.
        /// If simplify=true, checks if pred is unsat (returns False) or valid (returns True).
        /// Assumes that if simplify=false then pred is neither unsat nor valid.
        /// </summary>
        public BDG <T, S> MkLeaf(T pred, bool simplify = false)
        {
            BDG <T, S> val;

            if (simplify)
            {
                if (!MkLeafCache1.TryGetValue(pred, out val))
                {
                    if (!leafAlgebra.IsSatisfiable(pred))
                    {
                        val = _False;
                    }
                    else if (!leafAlgebra.IsSatisfiable(leafAlgebra.MkNot(pred)))
                    {
                        val = _True;
                    }
                    else
                    {
                        val = new BDG <T, S>(this, default(S), pred, null, null);
                    }
                    MkLeafCache1[pred] = val;
                }
            }
            else
            {
                if (!MkLeafCache2.TryGetValue(pred, out val))
                {
                    val = new BDG <T, S>(this, default(S), pred, null, null);
                    MkLeafCache2[pred] = val;
                }
            }
            return(val);
        }
Exemplo n.º 3
0
            internal void Refine(TPredicate other)
            {
                if (_left is null && _right is null)
                {
                    // If this is a leaf node create left and/or right children for the new predicate
                    TPredicate thisAndOther = _solver.And(_pred, other);
                    if (_solver.IsSatisfiable(thisAndOther))
                    {
                        // The predicates overlap, now check if this is contained in other
                        TPredicate thisMinusOther = _solver.And(_pred, _solver.Not(other));
                        if (_solver.IsSatisfiable(thisMinusOther))
                        {
                            // This is not contained in other, both children are needed
                            _left = new PartitionTree(_solver, thisAndOther, null, null);

                            // The right child corresponds to a conjunction with a negation, which matches thisMinusOther
                            _right = new PartitionTree(_solver, thisMinusOther, null, null);
                        }
                        else // [[this]] subset of [[other]]
                        {
                            // Other implies this, so populate the left child with this
                            _left = new PartitionTree(_solver, _pred, null, null);
                        }
                    }
                    else // [[this]] subset of [[not(other)]]
                    {
                        // negation of other implies this, so populate the right child with this
                        _right = new PartitionTree(_solver, _pred, null, null); //other must be false
                    }
                }
Exemplo n.º 4
0
 internal void Refine(PRED psi)
 {
     if (left == null && right == null)
     {
         #region leaf
         var phi_and_psi = solver.MkAnd(phi, psi);
         if (solver.IsSatisfiable(phi_and_psi))
         {
             var phi_min_psi = solver.MkAnd(phi, solver.MkNot(psi));
             if (solver.IsSatisfiable(phi_min_psi))
             {
                 left  = new PartitonTree <PRED>(solver, nr + 1, this, phi_and_psi, null, null);
                 right = new PartitonTree <PRED>(solver, nr + 1, this, phi_min_psi, null, null);
             }
             else // [[phi]] subset of [[psi]]
             {
                 left = new PartitonTree <PRED>(solver, nr + 1, this, phi, null, null); //psi must true
             }
         }
         else // [[phi]] subset of [[not(psi)]]
         {
             right = new PartitonTree <PRED>(solver, nr + 1, this, phi, null, null); //psi must be false
         }
         #endregion
     }
     else if (left == null)
     {
         right.Refine(psi);
     }
     else if (right == null)
     {
         left.Refine(psi);
     }
     else
     {
         #region nonleaf
         var phi_and_psi = solver.MkAnd(phi, psi);
         if (solver.IsSatisfiable(phi_and_psi))
         {
             var phi_min_psi = solver.MkAnd(phi, solver.MkNot(psi));
             if (solver.IsSatisfiable(phi_min_psi))
             {
                 left.Refine(psi);
                 right.Refine(psi);
             }
             else // [[phi]] subset of [[psi]]
             {
                 left.ExtendLeft(); //psi is true
                 right.ExtendLeft();
             }
         }
         else // [[phi]] subset of [[not(psi)]]
         {
             left.ExtendRight();
             right.ExtendRight(); //psi is false
         }
         #endregion
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// The sink state will be the state with the largest id.
        /// </summary>
        public ThreeAutomaton <S> MakeTotal()
        {
            var aut = this;

            int deadState = aut.maxState + 1;

            var newMoves = new List <Move <S> >();

            foreach (int state in aut.States)
            {
                var cond = algebra.MkNot(algebra.MkOr(aut.EnumerateConditions(state)));
                if (algebra.IsSatisfiable(cond))
                {
                    newMoves.Add(Move <S> .Create(state, deadState, cond));
                }
            }
            if (newMoves.Count == 0)
            {
                return(this);
            }

            newMoves.Add(Move <S> .Create(deadState, deadState, algebra.True));
            newMoves.AddRange(GetMoves());

            return(ThreeAutomaton <S> .Create(algebra, aut.initialState, aut.rejectingStateSet, aut.acceptingStateSet, newMoves));
        }
Exemplo n.º 6
0
            internal void Refine(IBooleanAlgebra <S> solver, S newSet)
            {
                var set_cap_newSet = solver.MkAnd(set, newSet);

                if (!solver.IsSatisfiable(set_cap_newSet))
                {
                    return; //set is disjoint from newSet
                }
                if (solver.AreEquivalent(set, set_cap_newSet))
                {
                    return; //set is a subset of newSet
                }
                var set_minus_newSet = solver.MkAnd(set, solver.MkNot(newSet));


                if (left == null) //leaf
                {
                    left  = new PartTree(set_cap_newSet, null, null);
                    right = new PartTree(set_minus_newSet, null, null);
                }
                else
                {
                    left.Refine(solver, newSet);
                    right.Refine(solver, newSet);
                }
            }
Exemplo n.º 7
0
            private IEnumerable <Move <T> > GetMovesFrom_(int p, int i, T pred, SimpleList <int> stateList)
            {
                Sequence <int> stateSeq = stateSeqs[p];

                foreach (var move in nfas[i].GetMovesFrom(stateSeq[i]))
                {
                    var psi = solver.MkAnd(pred, move.Label);
                    if (solver.IsSatisfiable(psi))
                    {
                        var extendedStateList = stateList.Append(move.TargetState);
                        if (i == K - 1)
                        {
                            //this was the final element of the state sequence
                            var qSeq = new Sequence <int>(extendedStateList);
                            int q;
                            if (!stateLookup.TryGetValue(qSeq, out q))
                            {
                                q = stateSeqs.Count;
                                stateLookup[qSeq] = q;
                                stateSeqs.Add(qSeq);
                            }
                            yield return(Move <T> .Create(p, q, psi));
                        }
                        else
                        {
                            //continue in the rest of the state sequence
                            foreach (var res in GetMovesFrom_(p, i + 1, psi, extendedStateList))
                            {
                                yield return(res);
                            }
                        }
                    }
                }
            }
Exemplo n.º 8
0
        private IteBag <T> OpInContext2(BagOpertion op, T context, IteBag <T> leaf, IteBag <T> bag2)
        {
            if (bag2.IsLeaf)
            {
                var newleaf = MkLeaf(ApplyOp(op, leaf.Count, bag2.Count));
                return(newleaf);
            }

            IteBag <T> bag;
            var        key = new Tuple <BagOpertion, T, IteBag <T>, IteBag <T> >(op, context, leaf, bag2);

            if (opCache.TryGetValue(key, out bag))
            {
                return(bag);
            }

            var context_t = algebra.MkAnd(context, bag2.Predicate);

            if (algebra.IsSatisfiable(context_t))
            {
                var context_f = algebra.MkAnd(context, algebra.MkNot(bag2.Predicate));
                if (algebra.IsSatisfiable(context_f))
                {
                    bag = MkNode(bag2.Predicate, OpInContext2(op, context_t, leaf, bag2.TrueCase), OpInContext2(op, context_f, leaf, bag2.FalseCase));
                }
                else //~IsSat(context & ~ bag2.Predicate) ---> IsValid(context ==> bag2.Predicate)
                {
                    bag = OpInContext2(op, context, leaf, bag2.TrueCase);
                }
            }
            else //~IsSat(context & bag2.Predicate) ---> IsValid(context ==> ~ bag2.Predicate)
            {
                bag = OpInContext2(op, context, leaf, bag2.FalseCase);
            }
            opCache[key] = bag;
            return(bag);
        }
Exemplo n.º 9
0
        public static ThreeAutomaton <S> MkProduct(ThreeAutomaton <S> aut1, ThreeAutomaton <S> aut2, IBooleanAlgebra <S> solver, bool inters)
        {
            var a = aut1.MakeTotal();
            var b = aut2.MakeTotal();

            var stateIdMap = new Dictionary <Tuple <int, int>, int>();
            var initPair   = new Tuple <int, int>(a.InitialState, b.InitialState);
            var frontier   = new Stack <Tuple <int, int> >();

            frontier.Push(initPair);
            stateIdMap[initPair] = 0;

            var delta = new Dictionary <int, List <Move <S> > >();

            delta[0] = new List <Move <S> >();
            var states = new List <int>();

            states.Add(0);

            var accStates = new List <int>();
            var rejStates = new List <int>();

            if (inters)
            {
                if (a.IsFinalState(a.InitialState) && b.IsFinalState(b.InitialState))
                {
                    accStates.Add(0);
                }
                else
                if (a.IsRejectingState(a.InitialState) || b.IsRejectingState(b.InitialState))
                {
                    rejStates.Add(0);
                }
            }
            else
            {
                if (a.IsRejectingState(a.InitialState) && b.IsRejectingState(b.InitialState))
                {
                    rejStates.Add(0);
                }
                else
                if (a.IsFinalState(a.InitialState) || b.IsFinalState(b.InitialState))
                {
                    accStates.Add(0);
                }
            }

            int n = 1;

            while (frontier.Count > 0)
            {
                var currPair       = frontier.Pop();
                int source         = stateIdMap[currPair];
                var outTransitions = delta[source];

                foreach (var t1 in a.GetMovesFrom(currPair.Item1))
                {
                    foreach (var t2 in b.GetMovesFrom(currPair.Item2))
                    {
                        var cond = solver.MkAnd(t1.Label, t2.Label);
                        if (!solver.IsSatisfiable(cond))
                        {
                            continue; //ignore the unsatisfiable move
                        }
                        Tuple <int, int> targetPair = new Tuple <int, int>(t1.TargetState, t2.TargetState);
                        int target;
                        if (!stateIdMap.TryGetValue(targetPair, out target))
                        {
                            //state has not yet been found
                            target = n;
                            n     += 1;
                            stateIdMap[targetPair] = target;
                            states.Add(target);
                            delta[target] = new List <Move <S> >();
                            frontier.Push(targetPair);

                            if (inters)
                            {
                                if (a.IsFinalState(t1.TargetState) && b.IsFinalState(t2.TargetState))
                                {
                                    accStates.Add(target);
                                }
                                else
                                if (a.IsRejectingState(t1.TargetState) || b.IsRejectingState(t2.TargetState))
                                {
                                    rejStates.Add(target);
                                }
                            }
                            else
                            {
                                if (a.IsRejectingState(t1.TargetState) && b.IsRejectingState(t2.TargetState))
                                {
                                    rejStates.Add(target);
                                }
                                else
                                if (a.IsFinalState(t1.TargetState) || b.IsFinalState(t2.TargetState))
                                {
                                    accStates.Add(target);
                                }
                            }
                        }
                        outTransitions.Add(Move <S> .Create(source, target, cond));
                    }
                }
            }

            var incomingTransitions = new Dictionary <int, List <Move <S> > >();

            foreach (int state in states)
            {
                incomingTransitions[state] = new List <Move <S> >();
            }
            foreach (int state in states)
            {
                foreach (Move <S> t in delta[state])
                {
                    incomingTransitions[t.TargetState].Add(t);
                }
            }


            return(ThreeAutomaton <S> .Create(aut1.algebra, 0, rejStates, accStates, EnumerateMoves(delta)));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Based on MinimizeMoore method/technique.
        /// </summary>
        void Initialize()
        {
            var fa = aut;

            var currLayer = new SimpleStack <Tuple <int, int> >();
            var nextLayer = new SimpleStack <Tuple <int, int> >();

            //any pair of states (p,q) where one state is final and the other is not are distinguished by
            //the empty list represented by null, this set of distinguishing sequences is trivially suffix-closed
            foreach (var p in fa.GetStates())
            {
                if (!fa.IsFinalState(p))
                {
                    foreach (var q in fa.GetFinalStates())
                    {
                        var pair = MkPair(p, q);
                        if (!distinguisher.ContainsKey(pair))
                        {
                            //the empty sequence distinguishes the states
                            distinguisherConcrete[pair] = "";
                            //List<Pair> pairs;
                            //if (!distinguishingStringsMap.TryGetValue("", out pairs))
                            //{
                            //    pairs = new List<Tuple<int, int>>();
                            //    distinguishingStringsMap[""] = pairs;
                            //    distinguishingStringsSeq.Add("");
                            //}
                            //pairs.Add(pair);
                            distinguisher[pair] = null;
                            nextLayer.Push(pair);
                        }
                    }
                }
            }

            //if a target pair of states is distinguished by some sequence
            //then any pair entering those states is also distinguished by extending that sequence
            //work breadth-first to maintain shortest witnesses
            while (nextLayer.IsNonempty)
            {
                var tmp = currLayer;
                currLayer = nextLayer;
                nextLayer = new SimpleStack <Tuple <int, int> >();
                while (currLayer.IsNonempty)
                {
                    var targetpair = currLayer.Pop();
                    foreach (var m1 in fa.GetMovesTo(targetpair.Item1))
                    {
                        foreach (var m2 in fa.GetMovesTo(targetpair.Item2))
                        {
                            if (m1.SourceState != m2.SourceState)
                            {
                                var psi = solver.MkAnd(m1.Label, m2.Label);
                                if (solver.IsSatisfiable(psi))
                                {
                                    var sourcepair = MkPair(m1.SourceState, m2.SourceState);
                                    if (!distinguisher.ContainsKey(sourcepair))
                                    {
                                        //add a new distinguishing sequence for the source pair
                                        //it extends a sequence of the target pair
                                        //thus the sequences remain suffix-closed
                                        if (concretize != null)
                                        {
                                            #region when concretization function is given precompute concrete distinguishers
                                            char c;
                                            if (!selectedCharacterMap.TryGetValue(psi, out c))
                                            {
                                                c = concretize(psi);
                                                selectedCharacterMap[psi] = c;
                                            }
                                            var s = c + distinguisherConcrete[targetpair];
                                            distinguisherConcrete[sourcepair] = s;

                                            //List<Pair> pairs;
                                            //if (!distinguishingStringsMap.TryGetValue(s, out pairs))
                                            //{
                                            //    pairs = new List<Tuple<int, int>>();
                                            //    distinguishingStringsMap[s] = pairs;
                                            //    distinguishingStringsSeq.Add(s);
                                            //}
                                            //pairs.Add(sourcepair);
                                            #endregion
                                        }

                                        var list = new ConsList <T>(psi, distinguisher[targetpair]);
                                        distinguisher[sourcepair] = list;

                                        nextLayer.Push(sourcepair);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 11
0
 /// <summary>
 /// Returns true iff the first or the second component of the pair predicate is satisfiable.
 /// </summary>
 public bool IsSatisfiable(Pair <S, T> predicate)
 {
     return(first.IsSatisfiable(predicate.First) || second.IsSatisfiable(predicate.Second));
 }
Exemplo n.º 12
0
        /// <summary>Enumerates all target states from the given source state</summary>
        /// <param name="sourceState">must be a an integer between 0 and StateCount-1</param>
        /// <param name="input">must be a value that acts as a minterm for the transitions emanating from the source state</param>
        /// <param name="context">reflects the immediate surrounding of the input and is used to determine nullability of anchors</param>
        public IEnumerable <int> EnumerateTargetStates(int sourceState, S input, uint context)
        {
            Debug.Assert(sourceState >= 0 && sourceState < _transitionFunction.Length);

            // First operate in a mode assuming no Union happens by finding the target leaf state if one exists
            Transition transition = _transitionFunction[sourceState];

            while (transition._kind != TransitionRegexKind.Union)
            {
                switch (transition._kind)
                {
                case TransitionRegexKind.Leaf:
                    // deadend and unexplored are negative
                    if (transition._leaf >= 0)
                    {
                        Debug.Assert(transition._leaf < _transitionFunction.Length);
                        yield return(transition._leaf);
                    }
                    // The single target (or no target) state was found, so exit the whole enumeration
                    yield break;

                case TransitionRegexKind.Conditional:
                    Debug.Assert(transition._test is not null && transition._first is not null && transition._second is not null);
                    // Branch according to the input condition in relation to the test condition
                    if (_solver.IsSatisfiable(_solver.And(input, transition._test)))
                    {
                        // in a conditional transition input must be exclusive
                        Debug.Assert(!_solver.IsSatisfiable(_solver.And(input, _solver.Not(transition._test))));
                        transition = transition._first;
                    }
                    else
                    {
                        transition = transition._second;
                    }
                    break;

                default:
                    Debug.Assert(transition._kind == TransitionRegexKind.Lookaround && transition._look is not null && transition._first is not null && transition._second is not null);
                    // Branch according to nullability of the lookaround condition in the given context
                    transition = transition._look.IsNullableFor(context) ?
                                 transition._first :
                                 transition._second;
                    break;
                }
            }

            // Continue operating in a mode where several target states can be yielded
            Debug.Assert(transition._first is not null && transition._second is not null);
            Stack <Transition> todo = new();

            todo.Push(transition._second);
            todo.Push(transition._first);
            while (todo.TryPop(out _))
            {
                switch (transition._kind)
                {
                case TransitionRegexKind.Leaf:
                    // dead-end
                    if (transition._leaf >= 0)
                    {
                        Debug.Assert(transition._leaf < _transitionFunction.Length);
                        yield return(transition._leaf);
                    }
                    break;

                case TransitionRegexKind.Conditional:
                    Debug.Assert(transition._test is not null && transition._first is not null && transition._second is not null);
                    // Branch according to the input condition in relation to the test condition
                    if (_solver.IsSatisfiable(_solver.And(input, transition._test)))
                    {
                        // in a conditional transition input must be exclusive
                        Debug.Assert(!_solver.IsSatisfiable(_solver.And(input, _solver.Not(transition._test))));
                        todo.Push(transition._first);
                    }
                    else
                    {
                        todo.Push(transition._second);
                    }
                    break;

                case TransitionRegexKind.Lookaround:
                    Debug.Assert(transition._look is not null && transition._first is not null && transition._second is not null);
                    // Branch according to nullability of the lookaround condition in the given context
                    todo.Push(transition._look.IsNullableFor(context) ? transition._first : transition._second);
                    break;


                default:
                    Debug.Assert(transition._kind == TransitionRegexKind.Union && transition._first is not null && transition._second is not null);
                    todo.Push(transition._second);
                    todo.Push(transition._first);
                    break;
                }
            }
        }
Exemplo n.º 13
0
 /// <summary>
 /// Returns true iff the first or the second component of the pair predicate is satisfiable.
 /// </summary>
 public bool IsSatisfiable(Tuple <S, T> predicate)
 {
     return(first.IsSatisfiable(predicate.Item1) || second.IsSatisfiable(predicate.Item2));
 }