Ejemplo n.º 1
0
        public void TestRanges3()
        {
            CharSetSolver solver = new CharSetSolver(BitWidth.BV16);
            BDD           cond   = solver.MkCharSetFromRegexCharClass(@"\d");
            int           cnt    = cond.CountNodes();

            Pair <uint, uint>[] ranges = solver.ToRanges(cond);
            BDD set     = solver.MkCharSetFromRanges(ranges);
            int nodes   = set.CountNodes();
            var ranges2 = new List <Pair <uint, uint> >(ranges);

            ranges2.Reverse();
            BDD set2    = solver.MkCharSetFromRanges(ranges2);
            int nodes2  = set.CountNodes();
            var ranges3 = solver.ToRanges(set2);
            BDD set3    = solver.MkCharSetFromRanges(ranges3);

            int cnt2 = set2.CountNodes();
            int cnt3 = set3.CountNodes();

            Assert.IsTrue(set2 == set3);

            Assert.AreEqual <int>(nodes, nodes2);
            Assert.AreSame(set, set2);

            set.ToDot("digits.dot");

            //check equivalence
            bool equiv = solver.MkOr(solver.MkAnd(cond, solver.MkNot(set)), solver.MkAnd(set, solver.MkNot(cond))) == solver.False;

            Assert.AreEqual <int>(31, ranges.Length);
        }
Ejemplo n.º 2
0
        public void TestNFA2()
        {
            CharSetSolver solver = new CharSetSolver(BitWidth.BV7);
            var           a      = solver.MkCharConstraint('a');
            var           na     = solver.MkNot(a);
            var           nfa    = Automaton <BDD> .Create(solver, 0, new int[] { 1 }, new Move <BDD>[] { new Move <BDD>(0, 1, solver.True), new Move <BDD>(0, 2, solver.True), new Move <BDD>(2, 1, solver.True), new Move <BDD>(1, 1, a), new Move <BDD>(1, 2, na) });

            var min_nfa = nfa.Minimize();

            nfa.isDeterministic = true; //pretend that nfa is equivalent, causes the deterministic version to be executed that provides the wrong result
            var min_nfa_wrong = nfa.Minimize();

            nfa.isDeterministic           = false;
            min_nfa_wrong.isDeterministic = false;
            //min_nfa.ShowGraph("min_nfa");
            //min_nfa_wrong.ShowGraph("min_nfa_wrong");
            //min_nfa.Determinize().Minimize().ShowGraph("min_nfa1");
            //nfa.Determinize().Minimize().ShowGraph("dfa");
            //nfa.ShowGraph("nfa");
            //min_nfa_wrong.Determinize().Minimize().ShowGraph("min_nfa2");
            Assert.IsFalse(min_nfa.IsEquivalentWith(min_nfa_wrong));
            Assert.IsTrue(min_nfa.IsEquivalentWith(nfa));
            //concrete witness "abab" distinguishes nfa from min_nfa_wrong
            Assert.IsTrue(solver.Convert("^abab$").Intersect(nfa).IsEmpty);
            Assert.IsFalse(solver.Convert("^abab$").Intersect(min_nfa_wrong).IsEmpty);
        }
Ejemplo n.º 3
0
 public override WS1SFormula Normalize(CharSetSolver solver)
 {
     if (phi is WS1SNot)
     {
         return((phi as WS1SNot).phi.Normalize(solver));
     }
     if (phi is WS1SUnaryPred)
     {
         var cphi = phi as WS1SUnaryPred;
         return(new WS1SUnaryPred(cphi.set, solver.MkNot(cphi.pred)));
     }
     return(new WS1SNot(phi.Normalize(solver)));;
 }
Ejemplo n.º 4
0
        public void CheckEquivalence()
        {
            CharSetSolver solver = new CharSetSolver();
            var moves = new List<Move<BDD>>();
            moves.Add(new Move<BDD>(0, 1, solver.True));
            moves.Add(new Move<BDD>(1, 2, solver.True));
            var sfa1 = ThreeAutomaton<BDD>.Create(solver, 0, new int[] { 0 }, new int[] { 2 }, moves);

            var c = solver.MkCharConstraint('c');
            moves = new List<Move<BDD>>();
            moves.Add(new Move<BDD>(0, 1, c));
            moves.Add(new Move<BDD>(1, 2, solver.True));
            moves.Add(new Move<BDD>(0, 3, solver.MkNot(c)));
            moves.Add(new Move<BDD>(3, 2, solver.True));
            var sfa2 = ThreeAutomaton<BDD>.Create(solver, 0, new int[] { 0 }, new int[] { 2 }, moves);

            Assert.IsTrue(sfa1.IsEquivalentWith(sfa2,solver));
        }
Ejemplo n.º 5
0
        public void CheckEquivalence()
        {
            CharSetSolver solver = new CharSetSolver();
            var           moves  = new List <Move <BDD> >();

            moves.Add(new Move <BDD>(0, 1, solver.True));
            moves.Add(new Move <BDD>(1, 2, solver.True));
            var sfa1 = ThreeAutomaton <BDD> .Create(solver, 0, new int[] { 0 }, new int[] { 2 }, moves);

            var c = solver.MkCharConstraint('c');

            moves = new List <Move <BDD> >();
            moves.Add(new Move <BDD>(0, 1, c));
            moves.Add(new Move <BDD>(1, 2, solver.True));
            moves.Add(new Move <BDD>(0, 3, solver.MkNot(c)));
            moves.Add(new Move <BDD>(3, 2, solver.True));
            var sfa2 = ThreeAutomaton <BDD> .Create(solver, 0, new int[] { 0 }, new int[] { 2 }, moves);

            Assert.IsTrue(sfa1.IsEquivalentWith(sfa2, solver));
        }
Ejemplo n.º 6
0
        public void TestIgnoreCaseTransformer_SimpleCases()
        {
            CharSetSolver         solver = new CharSetSolver();
            int                   t      = System.Environment.TickCount;
            IgnoreCaseTransformer ic     = new IgnoreCaseTransformer(solver);
            //simple test first:
            BDD a2c             = solver.MkRangeConstraint('a', 'c');
            BDD a2cA2C          = ic.Apply(a2c);
            BDD a2cA2C_expected = a2c.Or(solver.MkRangeConstraint('A', 'C'));

            Assert.AreEqual <BDD>(a2cA2C, a2cA2C_expected);
            //digits are not changed
            BDD ascii_digits  = solver.MkRangeConstraint('0', '9');
            BDD ascii_digits1 = ic.Apply(ascii_digits);

            Assert.AreEqual <BDD>(ascii_digits1, ascii_digits);
            var tt       = ic.Apply(solver.True);
            var tt_compl = solver.MkNot(tt);

            Assert.AreEqual <BDD>(ic.Apply(solver.True), solver.True);
        }
Ejemplo n.º 7
0
        public void CheckDeMorgan()
        {
            CharSetSolver solver = new CharSetSolver();
            var moves = new List<Move<BDD>>();
            var c = solver.MkCharConstraint('a');

            moves.Add(new Move<BDD>(0, 1, c));
            moves.Add(new Move<BDD>(1, 2, solver.True));
            var sfa1 = ThreeAutomaton<BDD>.Create(solver, 0, new int[] { 0 }, new int[] { 2 }, moves);

            moves = new List<Move<BDD>>();
            moves.Add(new Move<BDD>(0, 1, c));
            moves.Add(new Move<BDD>(1, 2, solver.True));
            moves.Add(new Move<BDD>(0, 3, solver.MkNot(c)));
            moves.Add(new Move<BDD>(3, 2, solver.True));
            var sfa2 = ThreeAutomaton<BDD>.Create(solver, 0, new int[] { 0 }, new int[] { 2 }, moves);

            var inters = sfa1.Intersect(sfa2,solver);
            var union = sfa1.Union(sfa2, solver);

            var u2 = sfa1.MkComplement().Intersect(sfa1.MkComplement(), solver).MkComplement();
        }
Ejemplo n.º 8
0
 public void TestNFA2()
 {
     CharSetSolver solver = new CharSetSolver(BitWidth.BV7);
     var a = solver.MkCharConstraint('a');
     var na = solver.MkNot(a);
     var nfa = Automaton<BDD>.Create(solver, 0, new int[] { 1 }, new Move<BDD>[] { new Move<BDD>(0, 1, solver.True), new Move<BDD>(0, 2, solver.True), new Move<BDD>(2, 1, solver.True), new Move<BDD>(1, 1, a), new Move<BDD>(1, 2, na) });
     var min_nfa = nfa.Minimize();
     nfa.isDeterministic = true; //pretend that nfa is equivalent, causes the deterministic version to be executed that provides the wrong result
     var min_nfa_wrong = nfa.Minimize();
     nfa.isDeterministic = false;
     min_nfa_wrong.isDeterministic = false;
     //min_nfa.ShowGraph("min_nfa");
     //min_nfa_wrong.ShowGraph("min_nfa_wrong");
     //min_nfa.Determinize().Minimize().ShowGraph("min_nfa1");
     //nfa.Determinize().Minimize().ShowGraph("dfa");
     //nfa.ShowGraph("nfa");
     //min_nfa_wrong.Determinize().Minimize().ShowGraph("min_nfa2");
     Assert.IsFalse(min_nfa.IsEquivalentWith(min_nfa_wrong));
     Assert.IsTrue(min_nfa.IsEquivalentWith(nfa));
     //concrete witness "abab" distinguishes nfa from min_nfa_wrong
     Assert.IsTrue(solver.Convert("^abab$").Intersect(nfa).IsEmpty);
     Assert.IsFalse(solver.Convert("^abab$").Intersect(min_nfa_wrong).IsEmpty);
 }
Ejemplo n.º 9
0
        public void CheckDeMorgan()
        {
            CharSetSolver solver = new CharSetSolver();
            var           moves  = new List <Move <BDD> >();
            var           c      = solver.MkCharConstraint('a');

            moves.Add(new Move <BDD>(0, 1, c));
            moves.Add(new Move <BDD>(1, 2, solver.True));
            var sfa1 = ThreeAutomaton <BDD> .Create(solver, 0, new int[] { 0 }, new int[] { 2 }, moves);


            moves = new List <Move <BDD> >();
            moves.Add(new Move <BDD>(0, 1, c));
            moves.Add(new Move <BDD>(1, 2, solver.True));
            moves.Add(new Move <BDD>(0, 3, solver.MkNot(c)));
            moves.Add(new Move <BDD>(3, 2, solver.True));
            var sfa2 = ThreeAutomaton <BDD> .Create(solver, 0, new int[] { 0 }, new int[] { 2 }, moves);

            var inters = sfa1.Intersect(sfa2, solver);
            var union  = sfa1.Union(sfa2, solver);

            var u2 = sfa1.MkComplement().Intersect(sfa1.MkComplement(), solver).MkComplement();
        }
Ejemplo n.º 10
0
        STModel ConvertReplace(replace repl)
        {
            //create a disjunction of all the regexes
            //each case terminated by the identifier
            int K = 0; //max pattern length
            //HashSet<int> finalReplacers = new HashSet<int>();

            //for efficieny keep lookup tables of character predicates to sets
            Dictionary <Expr, BDD> predLookup = new Dictionary <Expr, BDD>();

            Automaton <BDD> previouspatterns = Automaton <BDD> .MkEmpty(css);

            Automaton <BV2> N = Automaton <BV2> .MkFull(css2);

            var hasNoEndAnchor = new HashSet <int>();

            for (int i = 0; i < repl.CaseCount; i++)
            {
                replacecase rcase = repl.GetCase(i);
                var         pat   = "^" + rcase.Pattern.val;
                var         M     = css.Convert("^" + rcase.Pattern.val, System.Text.RegularExpressions.RegexOptions.Singleline).Determinize().Minimize();

                #region check that the pattern is a feasible nonempty sequence
                if (M.IsEmpty)
                {
                    throw new BekParseException(string.Format("Semantic error: pattern {0} is infeasible.", rcase.Pattern.ToString()));
                }
                int _K;
                if (!M.CheckIfSequence(out _K))
                {
                    throw new BekParseException(string.Format("Semantic error: pattern {0} is not a sequence.", rcase.Pattern.ToString()));
                }
                if (_K == 0)
                {
                    throw new BekParseException(string.Format("Semantic error: empty pattern {0} is not allowed.", rcase.Pattern.ToString()));
                }
                K = Math.Max(_K, K);
                #endregion

                var liftedMoves   = new List <Move <BV2> >();
                var st            = M.InitialState;
                var newFinalState = M.MaxState + 1;
                var endAnchor     = css.MkCharConstraint((char)i);

                //lift the moves to BV2 moves, adding end-markers
                while (!M.IsFinalState(st))
                {
                    var mv         = M.GetMoveFrom(st);
                    var pair_cond  = new BV2(mv.Label, css.False);
                    var liftedMove = new Move <BV2>(mv.SourceState, mv.TargetState, pair_cond);
                    liftedMoves.Add(liftedMove);
                    if (M.IsFinalState(mv.TargetState))
                    {
                        var end_cond = new BV2(css.False, endAnchor);
                        if (M.IsLoopState(mv.TargetState))
                        {
                            hasNoEndAnchor.Add(i);
                            //var loop_cond = css2.MkNot(end_cond);
                            //var loopMove = new Move<BV2>(mv.TargetState, mv.TargetState, loop_cond);
                            //liftedMoves.Add(loopMove);
                        }
                        var endMove = new Move <BV2>(mv.TargetState, newFinalState, end_cond);
                        liftedMoves.Add(endMove);
                    }
                    st = mv.TargetState;
                }
                var N_i = Automaton <BV2> .Create(css2, M.InitialState, new int[] { newFinalState }, liftedMoves);

                //Microsoft.Automata.Visualizer.ToDot(N_i, "N" + i , "C:\\Automata\\Docs\\Papers\\Bex\\N" + i +".dot", x => "(" + css.PrettyPrint(x.First) + "," + css.PrettyPrint(x.Second) + ")");

                N = N.Intersect(N_i.Complement());

                #region other approach: disallow overlapping patterns

                //Visualizer.ShowGraph(M2.Complement(css2), "M2", lab => { return "<" + css.PrettyPrint(lab.First) + "," + css.PrettyPrint(lab.Second) + ">"; });

                //note: keep here the original pattern, add only the start anchor to synchronize prefixes
                //var thispattern = css.Convert("^" + rcase.Pattern.val, System.Text.RegularExpressions.RegexOptions.Singleline).Determinize(css).Minimize(css);

                //var thispattern1 = thispattern.Minus(previouspatterns, css);
                //Visualizer.ShowGraph(thispattern1, "test", css.PrettyPrint);

                //#region check that thispattern does not overlap with any previous pattern
                //var common = thispattern.Intersect(previouspatterns, css);
                //if (!(common.IsEmpty))
                //{
                //    int j = 0;
                //    while ((j < i) && css.Convert("^" + repl.GetCase(j).Pattern.val,
                //        System.Text.RegularExpressions.RegexOptions.Singleline).Determinize(css).Intersect(thispattern, css).IsEmpty)
                //        j++;

                //    throw new BekParseException(rcase.id.line, rcase.id.pos, string.Format("Semantic error: pattern {0} overlaps pattern {1}.",
                //        rcase.Pattern.ToString(), repl.GetCase(j).Pattern.ToString()));
                //}
                //previouspatterns = previouspatterns.Union(thispattern).RemoveEpsilons(css.MkOr); //TBD: better union
                //#endregion

                #endregion
            }

            N = N.Complement().Minimize();
            //Microsoft.Automata.Visualizer.ShowGraph(N, "N", x => "<" + css.PrettyPrint(x.First) + "," + css.PrettyPrint(x.Second) + ">");
            //Microsoft.Automata.Visualizer.ToDot(N, "N","C:\\Automata\\Docs\\Papers\\Bex\\N.dot", x => "(" + css.PrettyPrint(x.First) + "," + css.PrettyPrint(x.Second) + ")");

            var D = new Dictionary <int, int>();
            var G = new Dictionary <int, BDD>();

            #region compute distance from initial state and compute guard unions
            var S = new Stack <int>();
            D[N.InitialState] = 0;
            G[N.InitialState] = css.False;
            S.Push(N.InitialState);
            while (S.Count > 0)
            {
                var q = S.Pop();
                foreach (var move in N.GetMovesFrom(q))
                {
                    G[q] = css.MkOr(G[q], move.Label.Item1);
                    var p = move.TargetState;
                    var d = D[q] + 1;
                    if (!(N.IsFinalState(p)) && !D.ContainsKey(p))
                    {
                        D[p] = d;
                        G[p] = css.False;
                        S.Push(p);
                    }
                    if (!(N.IsFinalState(p)) && D[p] != d)
                    {
                        throw new BekException(string.Format("Unexpected error, inconsitent distances {0} and {1} to state {2}", D[p], d, p));
                    }
                }
            }

            #endregion

            #region check that outputs do not have out of bound variables
            foreach (var fs in N.GetFinalStates())
            {
                foreach (var move in N.GetMovesTo(fs))
                {
                    if (move.Label.Item2.IsEmpty)
                    {
                        throw new BekException("Internal error: missing end anchor");
                    }

                    //if (!css.IsSingleton(move.Condition.Second))
                    //{
                    //    var one = (int)css.GetMin(move.Condition.Second);
                    //    var two = (int)css.GetMax(move.Condition.Second);
                    //    throw new BekParseException(repl.GetCase(two).id.line, repl.GetCase(two).id.pos, string.Format("Ambiguous replacement patterns {0} and {1}.", repl.GetCase(one).Pattern, repl.GetCase(two).Pattern));
                    //}

                    //pick the minimum case identifer when there are several, essentially pick the earliest case
                    int id = (int)css.GetMin(move.Label.Item2);

                    int           distFromRoot = D[move.SourceState];
                    var           e            = repl.GetCase(id).Output;
                    HashSet <int> vars         = new HashSet <int>();
                    foreach (var v in e.GetBoundVars())
                    {
                        if (v.GetVarId() >= distFromRoot)
                        {
                            throw new BekParseException(v.line, v.pos, string.Format("Syntax error: pattern variable '{0}' is out ouf bounds, valid range is from '#0' to '#{1}']", v.name, distFromRoot - 1));
                        }
                    }
                }
            }
            #endregion

            int finalState = N.FinalState;
            K = K - 1; //this many registers are needed

            var zeroChar       = stb.Solver.MkCharExpr('\0');
            var STmoves        = new List <Move <Rule <Expr> > >();
            var STstates       = new HashSet <int>();
            var STdelta        = new Dictionary <int, List <Move <Rule <Expr> > > >();
            var STdeltaInv     = new Dictionary <int, List <Move <Rule <Expr> > > >();
            var FinalSTstates  = new HashSet <int>();
            var STdeletedMoves = new HashSet <Move <Rule <Expr> > >();
            Action <Move <Rule <Expr> > > STmovesAdd = r =>
            {
                var p = r.SourceState;
                var q = r.TargetState;
                STmoves.Add(r);
                if (STstates.Add(p))
                {
                    STdelta[p]    = new List <Move <Rule <Expr> > >();
                    STdeltaInv[p] = new List <Move <Rule <Expr> > >();
                }
                if (STstates.Add(q))
                {
                    STdelta[q]    = new List <Move <Rule <Expr> > >();
                    STdeltaInv[q] = new List <Move <Rule <Expr> > >();
                }
                if (r.Label.IsFinal)
                {
                    FinalSTstates.Add(p);
                }
                STdelta[p].Add(r);
                STdeltaInv[q].Add(r);
            };
            var regsorts = new Sort[K];
            for (int j = 0; j < K; j++)
            {
                regsorts[j] = stb.Solver.CharSort;
            }
            var regsort = stb.Solver.MkTupleSort(regsorts);
            var regvar  = stb.MkRegister(regsort);
            var initialRegisterValues = new Expr[K];
            for (int j = 0; j < K; j++)
            {
                initialRegisterValues[j] = zeroChar;
            }
            var initialRegister = stb.Solver.MkTuple(initialRegisterValues);

            Predicate <int> IsCaseEndState = s => { return(N.OutDegree(s) == 1 && N.GetMoveFrom(s).Label.Item1.IsEmpty); };

            #region compute the forward moves and the completion moves
            var V = new HashSet <int>();
            S.Push(N.InitialState);
            while (S.Count > 0)
            {
                var p = S.Pop();

                #region forward moves
                foreach (var move in N.GetMovesFrom(p))
                {
                    var q = move.TargetState;
                    //this move occurs if p has both an end-move and a non-end-move
                    //note that if p is an case-end-state then it is never pushed to S
                    if (N.IsFinalState(q))
                    {
                        continue;
                    }

                    var  distance = D[p];
                    Expr chExpr;
                    Expr chPred;
                    MkExprPred(move.Label.Item1, out chExpr, out chPred);
                    predLookup[chPred] = move.Label.Item1;

                    Expr[] regUpds = new Expr[K];
                    for (int i = 0; i < K; i++)
                    {
                        if (i == distance)
                        {
                            regUpds[i] = chExpr;
                        }
                        else //if (i < distance)
                        {
                            regUpds[i] = stb.Solver.MkProj(i, regvar);
                        }
                        //else
                        //    regUpds[i] = zeroChar;
                    }
                    Expr regExpr = stb.Solver.MkTuple(regUpds);
                    var  moveST  = stb.MkRule(p, q, chPred, regExpr); //there are no yields
                    STmovesAdd(moveST);

                    if (V.Add(q) && !IsCaseEndState(q))
                    {
                        S.Push(q);
                    }
                }
                #endregion


                #region completion is only enabled if there exists an else case
                if (repl.HasElseCase)
                {
                    var guards  = G[p];
                    var guards0 = G[N.InitialState];

                    #region nonmatching cases to the initial state
                    var nomatch = css.MkNot(css.MkOr(guards, guards0));

                    if (!nomatch.IsEmpty)
                    {
                        Expr chExpr;
                        Expr nomatchPred;
                        MkExprPred(nomatch, out chExpr, out nomatchPred);
                        predLookup[nomatchPred] = nomatch;

                        var else_yields_list = new List <Expr>();
                        for (int i = 0; i < D[p]; i++)
                        {
                            else_yields_list.AddRange(GetElseYieldInstance(repl.ElseOutput, stb.Solver.MkProj(i, regvar)));
                        }
                        else_yields_list.AddRange(GetElseYieldInstance(repl.ElseOutput, stb.MkInputVariable(stb.Solver.CharSort)));

                        var else_yields = else_yields_list.ToArray();
                        var resetMove   = stb.MkRule(p, N.InitialState, nomatchPred, initialRegister, else_yields);
                        STmovesAdd(resetMove);
                    }
                    #endregion

                    #region matching cases via the initial state
                    foreach (var move0 in N.GetMovesFrom(N.InitialState))
                    {
                        var g0    = move0.Label.Item1;
                        var match = css.MkAnd(css.MkNot(guards), g0);
                        if (!match.IsEmpty)
                        {
                            Expr chExpr;
                            Expr matchPred;
                            MkExprPred(match, out chExpr, out matchPred);
                            predLookup[matchPred] = match;


                            var resetYieldsList = new List <Expr>();
                            //for all unprocessed inputs produce the output yield according to the else case
                            for (int i = 0; i < D[p]; i++)
                            {
                                resetYieldsList.AddRange(GetElseYieldInstance(repl.ElseOutput, stb.Solver.MkProj(i, regvar)));
                            }
                            var resetYields = resetYieldsList.ToArray();

                            Expr[] regupd = new Expr[K];
                            regupd[0] = chExpr;
                            for (int j = 1; j < K; j++)
                            {
                                regupd[j] = zeroChar;
                            }
                            var regupdExpr = stb.Solver.MkTuple(regupd);
                            var resetMove  = stb.MkRule(p, move0.TargetState, matchPred, regupdExpr, resetYields);
                            STmovesAdd(resetMove);
                        }
                    }
                    #endregion
                }
                #endregion
            }

            #endregion

            foreach (var last_move in N.GetMovesTo(N.FinalState))
            {
                //i is the case identifier
                int i = (int)css.GetMin(last_move.Label.Item2);

                if (hasNoEndAnchor.Contains(i))
                {
                    #region this corresponds to looping back to the initial state on the given input
                    //the final outputs produced after a successful pattern match

                    #region compute the output terms

                    int distFromRoot = D[last_move.SourceState];
                    Func <ident, Expr> registerMap = id =>
                    {
                        // --- already checked I think ---
                        if (!id.IsVar || id.GetVarId() >= distFromRoot)
                        {
                            throw new BekParseException(id.Line, id.Pos, string.Format("illeagal variable '{0}' in output", id.name));
                        }

                        if (id.GetVarId() == distFromRoot - 1) //the last reg update refers to the current variable
                        {
                            return(stb.MkInputVariable(stb.Solver.CharSort));
                        }
                        else
                        {
                            return(stb.Solver.MkProj(id.GetVarId(), regvar));
                        }
                    };
                    Expr[] yields;
                    var    outp = repl.GetCase(i).Output;
                    if (outp is strconst)
                    {
                        var s = ((strconst)outp).val;
                        yields = Array.ConvertAll(s.ToCharArray(),
                                                  c => this.str_handler.iter_handler.expr_handler.Convert(new charconst("'" + StringUtility.Escape(c) + "'"), registerMap));
                    }
                    else //must be an explicit list construct
                    {
                        if (!(outp is functioncall) || !((functioncall)outp).id.name.Equals("string"))
                        {
                            throw new BekParseException("Invalid pattern output.");
                        }

                        var s = ((functioncall)outp).args;
                        yields = Array.ConvertAll(s.ToArray(),
                                                  e => this.str_handler.iter_handler.expr_handler.Convert(e, registerMap));
                    }
                    #endregion

                    //shortcut all the incoming transitions to the initial state
                    foreach (var move in STdeltaInv[last_move.SourceState])
                    {
                        //go to the initial state, i.e. the matching raps around
                        int         p       = move.SourceState;
                        int         q0      = N.InitialState;
                        List <Expr> yields1 = new List <Expr>(move.Label.Yields); //incoming yields are
                        yields1.AddRange(yields);
                        var rule = stb.MkRule(p, q0, move.Label.Guard, initialRegister, yields1.ToArray());
                        STmovesAdd(rule);
                        //STdeletedMoves.Add(move);
                        STmoves.Remove(move); //the move has been replaced
                    }
                    #endregion
                }
                else
                {
                    #region this is the end of the input stream case

                    #region compute the output terms

                    int distFromRoot = D[last_move.SourceState];
                    Func <ident, Expr> registerMap = id =>
                    {
                        if (!id.IsVar || id.GetVarId() >= distFromRoot)
                        {
                            throw new BekParseException(id.Line, id.Pos, string.Format("illeagal variable '{0}' in output", id.name));
                        }

                        return(stb.Solver.MkProj(id.GetVarId(), regvar));
                    };
                    Expr[] yields;
                    var    outp = repl.GetCase(i).Output;
                    if (outp is strconst)
                    {
                        var s = ((strconst)outp).val;
                        yields = Array.ConvertAll(s.ToCharArray(),
                                                  c => this.str_handler.iter_handler.expr_handler.Convert(new charconst("'" + c.ToString() + "'"), registerMap));
                    }
                    else //must be an explicit list construct
                    {
                        if (!(outp is functioncall) || !((functioncall)outp).id.name.Equals("string"))
                        {
                            throw new BekParseException("Invalid pattern output.");
                        }

                        var s = ((functioncall)outp).args;
                        yields = Array.ConvertAll(s.ToArray(),
                                                  e => this.str_handler.iter_handler.expr_handler.Convert(e, registerMap));
                    }
                    #endregion

                    int p    = last_move.SourceState;
                    var rule = stb.MkFinalOutput(p, stb.Solver.True, yields);
                    STmovesAdd(rule);
                    #endregion
                }
            }

            if (repl.HasElseCase)
            {
                #region final completion (upon end of input) for all non-final states
                foreach (var p in STstates)
                {
                    if (!FinalSTstates.Contains(p) && !IsCaseEndState(p)) //there is no final rule for p, so add the default one
                    {
                        Expr[] finalYields;
                        finalYields = new Expr[D[p]];
                        for (int i = 0; i < finalYields.Length; i++)
                        {
                            finalYields[i] = stb.Solver.MkProj(i, regvar);
                        }
                        var p_finalMove = stb.MkFinalOutput(p, stb.Solver.True, finalYields);
                        STmovesAdd(p_finalMove);
                    }
                }
                #endregion
            }
            else
            {
                //in this case there is a final rule from the initial state
                var q0_finalMove = stb.MkFinalOutput(N.InitialState, stb.Solver.True);
                STmovesAdd(q0_finalMove);
            }

            var resST  = stb.MkST(name, initialRegister, stb.Solver.CharSort, stb.Solver.CharSort, regsort, N.InitialState, STmoves);
            var resSTb = new STModel(stb.Solver, name, stb.Solver.CharSort, stb.Solver.CharSort, regsort, initialRegister, N.InitialState);

            //create STb from the moves, we use here the knowledge that the ST is deterministic
            //we also use the lookuptable of conditions to eliminate dead code


            //resST.ShowGraph();

            //resST.ToDot("C:\\Automata\\Docs\\Papers\\Bex\\B.dot");

            #region compute the rules of the resulting STb

            //V.Clear();
            //S.Push(resST.InitialState);
            //V.Add(resST.InitialState);

            foreach (var st in resST.GetStates())
            {
                var condUnion = css.False;
                var st_moves  = new List <Move <Rule <Expr> > >();
                foreach (var move in resST.GetNonFinalMovesFrom(st))
                {
                    condUnion = css.MkOr(condUnion, predLookup[move.Label.Guard]);
                    st_moves.Add(move);
                }

                BranchingRule <Expr> st_rule;
                if (st_moves.Count > 0)
                {
                    //collect all rules with singleton guards and put them into a switch statement
                    var st_rules1 = new List <KeyValuePair <Expr, BranchingRule <Expr> > >();
                    var st_moves2 = new List <Move <Rule <Expr> > >();
                    foreach (var move in st_moves)
                    {
                        if (css.ComputeDomainSize(predLookup[move.Label.Guard]) == 1)
                        {
                            var v = stb.Solver.MkNumeral(css.Choose(predLookup[move.Label.Guard]), stb.Solver.CharSort);
                            var r = new BaseRule <Expr>(new Sequence <Expr>(move.Label.Yields),
                                                        move.Label.Update, move.TargetState);
                            st_rules1.Add(new KeyValuePair <Expr, BranchingRule <Expr> >(v, r));
                        }
                        else
                        {
                            st_moves2.Add(move);
                        }
                    }
                    BranchingRule <Expr> defaultcase = new UndefRule <Expr>("reject");
                    //make st_moves2 into an ite rule
                    if (st_moves2.Count > 0)
                    {
                        for (int j = st_moves2.Count - 1; j >= 0; j--)
                        {
                            var r = new BaseRule <Expr>(new Sequence <Expr>(st_moves2[j].Label.Yields),
                                                        st_moves2[j].Label.Update, st_moves2[j].TargetState);
                            if (j == (st_moves2.Count - 1) && condUnion.IsFull)
                            {
                                defaultcase = r;
                            }
                            else
                            {
                                defaultcase = new IteRule <Expr>(st_moves2[j].Label.Guard, r, defaultcase);
                            }
                        }
                    }
                    else if (condUnion.IsFull)
                    {
                        defaultcase = st_rules1[st_rules1.Count - 1].Value;
                        st_rules1.RemoveAt(st_rules1.Count - 1);
                    }
                    if (st_rules1.Count == 0)
                    {
                        st_rule = defaultcase;
                    }
                    else
                    {
                        st_rule = new SwitchRule <Expr>(stb.MkInputVariable(stb.Solver.CharSort), defaultcase, st_rules1.ToArray());
                    }
                }
                else
                {
                    st_rule = new UndefRule <Expr>("reject");
                }

                resSTb.AssignRule(st, st_rule);

                var st_finalrules = new List <Rule <Expr> >(resST.GetFinalRules(st));
                if (st_finalrules.Count > 1)
                {
                    throw new BekException("Unexpected error: multiple final rules per state.");
                }
                if (st_finalrules.Count > 0)
                {
                    resSTb.AssignFinalRule(st, new BaseRule <Expr>(new Sequence <Expr>(st_finalrules[0].Yields), initialRegister, st));
                }
            }

            resSTb.ST = resST;
            resST.STb = resSTb;
            #endregion

            return(resSTb);
        }
Ejemplo n.º 11
0
        public void TestRanges3()
        {
            CharSetSolver solver = new CharSetSolver(BitWidth.BV16);
            BDD cond = solver.MkCharSetFromRegexCharClass(@"\d");
            int cnt = cond.CountNodes();
            Pair<uint, uint>[] ranges = solver.ToRanges(cond);
            BDD set = solver.MkCharSetFromRanges(ranges);
            int nodes = set.CountNodes();
            var ranges2 = new List<Pair<uint, uint>>(ranges);
            ranges2.Reverse();
            BDD set2 = solver.MkCharSetFromRanges(ranges2);
            int nodes2 = set.CountNodes();
            var ranges3 = solver.ToRanges(set2);
            BDD set3 = solver.MkCharSetFromRanges(ranges3);

            int cnt2 = set2.CountNodes();
            int cnt3 = set3.CountNodes();
            Assert.IsTrue(set2 == set3);

            Assert.AreEqual<int>(nodes, nodes2);
            Assert.AreSame(set,set2);

            set.ToDot("digits.dot");

            //check equivalence
            bool equiv = solver.MkOr(solver.MkAnd(cond, solver.MkNot(set)), solver.MkAnd(set, solver.MkNot(cond))) == solver.False;

            Assert.AreEqual<int>(31, ranges.Length);
        }
        // looks for an edit at depth "depth" 
        // returns false and null in bestScript if no edit is found at depth "depth"
        // returns false and not null in bestScript if found
        // returns true if timeout
        internal static bool GetDFAEditScriptTimeout(
            Automaton<BDD> dfa1, Automaton<BDD> dfa2,
            HashSet<char> al, CharSetSolver solver,
            List<long> editScriptHash, List<DFAEdit> editList,
            int depth, long timeout, Stopwatch sw,
            Pair<IEnumerable<string>, IEnumerable<string>> tests,
            double dfa1density,
            int totalCost,
            DFAEditScript bestScript, Dictionary<int, int> stateNamesMapping)
        {
            // check timer
            if (sw.ElapsedMilliseconds > timeout)
                return true;

            //Compute worst case distance, call finalScript with this value?
            int dist = (dfa1.StateCount + dfa2.StateCount) * (al.Count + 1);

            //Stop if no more moves left
            if (depth == 0)
            {
                if (DFAUtilities.ApproximateMNEquivalent(tests, dfa1density, dfa2, al, solver) && dfa2.IsEquivalentWith(dfa1, solver))
                    //check if totalCost < finalScript cost and replace if needed
                    if (bestScript.script == null || totalCost < bestScript.GetCost())
                        bestScript.script = ObjectCopier.Clone<List<DFAEdit>>(editList);
                return false;
            }

            DFAEdit edit = null;

            #region Flip one move target state
            foreach (var move in dfa2.GetMoves())
            {
                //Creaty copy of the moves without current move
                var movesWithoutCurrMove = dfa2.GetMoves().ToList();
                movesWithoutCurrMove.Remove(move);

                //Redirect every ch belonging to move condition
                foreach (var c in solver.GenerateAllCharacters(move.Label, false))
                {
                    long hash = IntegerUtil.PairToInt(move.SourceState, c - 97) + dfa2.StateCount;

                    if (CanAdd(hash, editScriptHash))
                    {
                        editScriptHash.Insert(0, hash);

                        //Local copy of moves
                        var newMoves = movesWithoutCurrMove.ToList();
                        var newMoveCondition = solver.MkCharConstraint(false, c);

                        #region Remove ch from current move
                        var andCond = solver.MkAnd(move.Label, solver.MkNot(newMoveCondition));
                        //add back move without ch iff satisfiable
                        if (solver.IsSatisfiable(andCond))
                            newMoves.Add(new Move<BDD>(move.SourceState, move.TargetState, andCond));
                        #endregion

                        #region Redirect c to a different state
                        foreach (var state in dfa2.States)
                            if (state != move.TargetState)
                            {
                                var newMovesComplete = newMoves.ToList();
                                newMovesComplete.Add(new Move<BDD>(move.SourceState, state, newMoveCondition));
                                var dfa2new = Automaton<BDD>.Create(dfa2.InitialState, dfa2.GetFinalStates(), newMovesComplete);

                                edit = new DFAEditMove(stateNamesMapping[move.SourceState], stateNamesMapping[state], c);
                                editList.Insert(0, edit);
                                if (GetDFAEditScriptTimeout(dfa1, dfa2new, al, solver, editScriptHash, editList, depth - 1, timeout, sw, tests, dfa1density, totalCost + edit.GetCost(), bestScript, stateNamesMapping))
                                    return true;
                                editList.RemoveAt(0);
                            }
                        #endregion

                        editScriptHash.RemoveAt(0);

                    }
                }
            }
            #endregion

            #region Flip one state from fin to non fin
            foreach (var state in dfa2.States)
            {
                if (CanAdd(state, editScriptHash))
                {
                    //flip its final non final status
                    editScriptHash.Insert(0, state);

                    var newFinalStates = new HashSet<int>(dfa2.GetFinalStates());
                    Automaton<BDD> dfa2new = null;
                    if (dfa2.GetFinalStates().Contains(state))
                    {
                        edit = new DFAEditState(stateNamesMapping[state], false);
                        editList.Insert(0, edit);
                        newFinalStates.Remove(state);
                        dfa2new = Automaton<BDD>.Create(dfa2.InitialState, newFinalStates, dfa2.GetMoves());
                    }
                    else
                    {
                        edit = new DFAEditState(stateNamesMapping[state], true);
                        editList.Insert(0, edit);
                        newFinalStates.Add(state);
                        dfa2new = Automaton<BDD>.Create(dfa2.InitialState, newFinalStates, dfa2.GetMoves());
                    }

                    if (GetDFAEditScriptTimeout(dfa1, dfa2new, al, solver, editScriptHash, editList, depth - 1, timeout, sw, tests, dfa1density, totalCost + edit.GetCost(), bestScript, stateNamesMapping))
                        return true;

                    editScriptHash.RemoveAt(0);
                    editList.RemoveAt(0);
                }
            }
            #endregion

            return false;
        }
Ejemplo n.º 13
0
 public override WS1SFormula Normalize(CharSetSolver solver)
 {
     if (phi is WS1SNot)
     {
         return (phi as WS1SNot).phi.Normalize(solver);
     }
     if (phi is WS1SUnaryPred)
     {
         var cphi = phi as WS1SUnaryPred;
         return new WS1SUnaryPred(cphi.set, solver.MkNot(cphi.pred));
     }
     return new WS1SNot(phi.Normalize(solver)); ;
 }
        // looks for an edit at depth "depth"
        // returns false and null in bestScript if no edit is found at depth "depth"
        // returns false and not null in bestScript if found
        // returns true if timeout
        internal bool GetNFAEditScriptTimeout(
            int depth, long lastEditHash,
            Automaton <BDD> currentNfa2,
            List <NFAEdit> editList, int scriptCost,
            NFAEditScript bestScript)
        {
            // if timeout return true
            if (sw.ElapsedMilliseconds > timeout)
            {
                return(true);
            }


            //Stop if no more moves left
            if (depth == 0)
            {
                if (DFAUtilities.ApproximateMNEquivalent(tests, nfa1density, currentNfa2, al, solver) && currentNfa2.IsEquivalentWith(nfa1, solver))
                {
                    //check if totalCost < finalScript cost and replace if needed
                    if (bestScript.script == null || scriptCost < bestScript.GetCost())
                    {
                        bestScript.script = ObjectCopier.Clone <List <NFAEdit> >(editList);
                    }
                }
                return(false);
            }

            NFAEdit edit = null;

            long thisEditHash = 0;

            #region Flip one state from fin to non fin
            foreach (var state in currentNfa2.States)
            {
                thisEditHash = state;
                if (CanAdd(thisEditHash, lastEditHash))
                {
                    //flip its final non final status

                    var             newFinalStates = new HashSet <int>(currentNfa2.GetFinalStates());
                    Automaton <BDD> nfa2new        = null;
                    if (currentNfa2.GetFinalStates().Contains(state))
                    {
                        edit = new NFAEditState(state, false);
                        editList.Insert(0, edit);
                        newFinalStates.Remove(state);
                        nfa2new = Automaton <BDD> .Create(currentNfa2.InitialState, newFinalStates, currentNfa2.GetMoves());
                    }
                    else
                    {
                        edit = new NFAEditState(state, true);
                        editList.Insert(0, edit);
                        newFinalStates.Add(state);
                        nfa2new = Automaton <BDD> .Create(currentNfa2.InitialState, newFinalStates, currentNfa2.GetMoves());
                    }

                    if (GetNFAEditScriptTimeout(depth - 1, thisEditHash, nfa2new, editList, scriptCost + edit.GetCost(), bestScript))
                    {
                        return(true);
                    }

                    editList.RemoveAt(0);
                }
            }
            #endregion

            #region Change transition from source state
            currentNfa2 = NFAUtilities.normalizeMoves(currentNfa2, solver);
            foreach (var sourceState in currentNfa2.States)
            {
                HashSet <int> unreachedStates = new HashSet <int>(currentNfa2.States);
                foreach (var moveFromSource in currentNfa2.GetMovesFrom(sourceState))
                {
                    // take all chars in alphabet
                    foreach (var c in al)
                    {
                        long moveHash = currentNfa2.StateCount + IntegerUtil.TripleToInt(sourceState, moveFromSource.TargetState, alphabetMap[c]);
                        thisEditHash = currentNfa2.StateCount + moveHash;
                        if (CanAdd(thisEditHash, lastEditHash))
                        {
                            BDD cCond   = solver.False;
                            BDD newCond = solver.False;

                            //skip epsilon moves
                            if (moveFromSource.Label != null)
                            {
                                // if c in move, remove it and recursion
                                if (solver.Contains(moveFromSource.Label, c))
                                {
                                    cCond   = solver.MkNot(solver.MkCharConstraint(false, c));
                                    newCond = solver.MkAnd(moveFromSource.Label, cCond);
                                }
                                else // if c not in move, add it and recursion
                                {
                                    cCond   = solver.MkCharConstraint(false, c);
                                    newCond = solver.MkOr(moveFromSource.Label, cCond);
                                }

                                var newMoves = new List <Move <BDD> >(currentNfa2.GetMoves());
                                newMoves.Remove(moveFromSource);
                                newMoves.Add(new Move <BDD>(sourceState, moveFromSource.TargetState, newCond));
                                var nfa2new = Automaton <BDD> .Create(currentNfa2.InitialState, currentNfa2.GetFinalStates(), newMoves);

                                edit = new NFAEditMove(sourceState, moveFromSource.TargetState, c);
                                editList.Insert(0, edit);

                                if (GetNFAEditScriptTimeout(depth - 1, thisEditHash, nfa2new, editList, scriptCost + edit.GetCost(), bestScript))
                                {
                                    return(true);
                                }

                                editList.RemoveAt(0);
                            }
                        }
                    }

                    unreachedStates.Remove(moveFromSource.TargetState);
                }

                foreach (var targetState in unreachedStates)
                {
                    //try adding a symbol not in transition
                    foreach (var c in al)
                    {
                        long moveHash = IntegerUtil.TripleToInt(sourceState, targetState, alphabetMap[c]);
                        thisEditHash = currentNfa2.StateCount + moveHash;

                        var moveCond = solver.MkCharConstraint(false, c);
                        var newMoves = new List <Move <BDD> >(currentNfa2.GetMoves());
                        newMoves.Add(new Move <BDD>(sourceState, targetState, moveCond));
                        var nfa2new = Automaton <BDD> .Create(currentNfa2.InitialState, currentNfa2.GetFinalStates(), newMoves);

                        edit = new NFAEditMove(sourceState, targetState, c);
                        editList.Insert(0, edit);

                        //TODO put correct hash
                        if (GetNFAEditScriptTimeout(depth - 1, thisEditHash, nfa2new, editList, scriptCost + edit.GetCost(), bestScript))
                        {
                            return(true);
                        }

                        editList.RemoveAt(0);
                    }
                }
            }
            #endregion

            return(false);
        }
Ejemplo n.º 15
0
        // looks for an edit at depth "depth"
        // returns false and null in bestScript if no edit is found at depth "depth"
        // returns false and not null in bestScript if found
        // returns true if timeout
        internal static bool GetDFAEditScriptTimeout(
            Automaton <BDD> dfa1, Automaton <BDD> dfa2,
            HashSet <char> al, CharSetSolver solver,
            List <long> editScriptHash, List <DFAEdit> editList,
            int depth, long timeout, Stopwatch sw,
            Pair <IEnumerable <string>, IEnumerable <string> > tests,
            double dfa1density,
            int totalCost,
            DFAEditScript bestScript, Dictionary <int, int> stateNamesMapping)
        {
            // check timer
            if (sw.ElapsedMilliseconds > timeout)
            {
                return(true);
            }

            //Compute worst case distance, call finalScript with this value?
            int dist = (dfa1.StateCount + dfa2.StateCount) * (al.Count + 1);

            //Stop if no more moves left
            if (depth == 0)
            {
                if (DFAUtilities.ApproximateMNEquivalent(tests, dfa1density, dfa2, al, solver) && dfa2.IsEquivalentWith(dfa1, solver))
                {
                    //check if totalCost < finalScript cost and replace if needed
                    if (bestScript.script == null || totalCost < bestScript.GetCost())
                    {
                        bestScript.script = ObjectCopier.Clone <List <DFAEdit> >(editList);
                    }
                }
                return(false);
            }

            DFAEdit edit = null;

            #region Flip one move target state
            foreach (var move in dfa2.GetMoves())
            {
                //Creaty copy of the moves without current move
                var movesWithoutCurrMove = dfa2.GetMoves().ToList();
                movesWithoutCurrMove.Remove(move);

                //Redirect every ch belonging to move condition
                foreach (var c in solver.GenerateAllCharacters(move.Label, false))
                {
                    long hash = IntegerUtil.PairToInt(move.SourceState, c - 97) + dfa2.StateCount;

                    if (CanAdd(hash, editScriptHash))
                    {
                        editScriptHash.Insert(0, hash);

                        //Local copy of moves
                        var newMoves         = movesWithoutCurrMove.ToList();
                        var newMoveCondition = solver.MkCharConstraint(false, c);

                        #region Remove ch from current move
                        var andCond = solver.MkAnd(move.Label, solver.MkNot(newMoveCondition));
                        //add back move without ch iff satisfiable
                        if (solver.IsSatisfiable(andCond))
                        {
                            newMoves.Add(new Move <BDD>(move.SourceState, move.TargetState, andCond));
                        }
                        #endregion

                        #region Redirect c to a different state
                        foreach (var state in dfa2.States)
                        {
                            if (state != move.TargetState)
                            {
                                var newMovesComplete = newMoves.ToList();
                                newMovesComplete.Add(new Move <BDD>(move.SourceState, state, newMoveCondition));
                                var dfa2new = Automaton <BDD> .Create(dfa2.InitialState, dfa2.GetFinalStates(), newMovesComplete);

                                edit = new DFAEditMove(stateNamesMapping[move.SourceState], stateNamesMapping[state], c);
                                editList.Insert(0, edit);
                                if (GetDFAEditScriptTimeout(dfa1, dfa2new, al, solver, editScriptHash, editList, depth - 1, timeout, sw, tests, dfa1density, totalCost + edit.GetCost(), bestScript, stateNamesMapping))
                                {
                                    return(true);
                                }
                                editList.RemoveAt(0);
                            }
                        }
                        #endregion

                        editScriptHash.RemoveAt(0);
                    }
                }
            }
            #endregion

            #region Flip one state from fin to non fin
            foreach (var state in dfa2.States)
            {
                if (CanAdd(state, editScriptHash))
                {
                    //flip its final non final status
                    editScriptHash.Insert(0, state);

                    var             newFinalStates = new HashSet <int>(dfa2.GetFinalStates());
                    Automaton <BDD> dfa2new        = null;
                    if (dfa2.GetFinalStates().Contains(state))
                    {
                        edit = new DFAEditState(stateNamesMapping[state], false);
                        editList.Insert(0, edit);
                        newFinalStates.Remove(state);
                        dfa2new = Automaton <BDD> .Create(dfa2.InitialState, newFinalStates, dfa2.GetMoves());
                    }
                    else
                    {
                        edit = new DFAEditState(stateNamesMapping[state], true);
                        editList.Insert(0, edit);
                        newFinalStates.Add(state);
                        dfa2new = Automaton <BDD> .Create(dfa2.InitialState, newFinalStates, dfa2.GetMoves());
                    }

                    if (GetDFAEditScriptTimeout(dfa1, dfa2new, al, solver, editScriptHash, editList, depth - 1, timeout, sw, tests, dfa1density, totalCost + edit.GetCost(), bestScript, stateNamesMapping))
                    {
                        return(true);
                    }

                    editScriptHash.RemoveAt(0);
                    editList.RemoveAt(0);
                }
            }
            #endregion

            return(false);
        }