Exemple #1
0
        public void TestWS1S_GetAutomatonBDD_eq_GetAutomaton()
        {
            var solver = new CharSetSolver(BitWidth.BV7);
            //var nrOfLabelBits = (int)BitWidth.BV7;
            var isDigit  = solver.MkCharSetFromRegexCharClass(@"\d");
            var isLetter = solver.MkCharSetFromRegexCharClass(@"(c|C)");
            var x        = new Variable("x", false);
            var y        = new Variable("y", false);
            var z        = new Variable("z", false);
            var X        = new Variable("X", false);
            //there are at least two distinct positions x and y
            var xy = new MSOAnd <BDD>(new MSONot <BDD>(new MSOEq <BDD>(x, y)), new MSOAnd <BDD>(new MSOIsSingleton <BDD>(x), new MSOIsSingleton <BDD>(y)));
            //there is a set X containing x and y and all positions z in X have characters that satisfy isWordLetter
            var x_sub_X    = new MSOSubset <BDD>(x, X);
            var y_sub_X    = new MSOSubset <BDD>(y, X);
            var z_sub_X    = new MSOSubset <BDD>(z, X);
            var isletter_z = new MSOPredicate <BDD>(isLetter, z);
            var psi        = new MSOExists <BDD>(X, (x_sub_X & y_sub_X & ~(new MSOExists <BDD>(z, ~((~((new MSOIsSingleton <BDD>(z)) & z_sub_X)) | isletter_z)))));

            var atLeast2w   = xy & psi;
            var atLeast2wEE = new MSOExists <BDD>(x, (new MSOExists <BDD>(y, atLeast2w)));
            var autBDD      = atLeast2w.GetAutomaton(solver);
            var ca          = new CartesianAlgebraBDD <BDD>(solver);
            var autPROD     = atLeast2w.GetAutomaton(ca);
            //autBDD.ShowGraph("autBDD");
            //autPROD.ShowGraph("autPROD");
            var aut_atLeast2wEE1 = BasicAutomata.Restrict(atLeast2wEE.GetAutomaton(ca));
            var aut_atLeast2wEE2 = atLeast2wEE.GetAutomaton(solver);

            //aut_atLeast2wEE1.ShowGraph("aut_atLeast2wEE1");
            //aut_atLeast2wEE2.ShowGraph("aut_atLeast2wEE2");
            Assert.IsTrue(aut_atLeast2wEE1.IsEquivalentWith(aut_atLeast2wEE2));
        }
Exemple #2
0
        public void TestWS1S_SuccDef_GetAutomatonBDD()
        {
            var solver        = new CharSetSolver(BitWidth.BV7);
            var nrOfLabelBits = (int)BitWidth.BV7;
            var x             = new Variable("x", true);
            var y             = new Variable("y", true);
            var z             = new Variable("z", true);
            var xLTy          = new MSOLt <BDD>(x, y);
            var xLTzLTy       = new MSOAnd <BDD>(new MSOLt <BDD>(x, z), new MSOLt <BDD>(z, y));
            var Ez            = new MSOExists <BDD>(z, xLTzLTy);
            var notEz         = new MSONot <BDD>(Ez);
            var xSyDef        = new MSOAnd <BDD>(xLTy, notEz);

            var aut_xSyDef  = xSyDef.GetAutomaton(solver);
            var aut_xLTzLTy = xLTzLTy.GetAutomaton(solver);
            var aut_Ez      = Ez.GetAutomaton(solver).Determinize().Minimize();
            var aut_notEz   = notEz.GetAutomaton(solver);
            var aut_xLTy    = xLTy.GetAutomaton(solver);

            //aut_xSyDef.ShowGraph("aut_xSyDEf");
            //aut_xLTzLTy.ShowGraph("aut_xLTzLTy");
            //aut_Ez.ShowGraph("aut_Ez");
            //aut_notEz.ShowGraph("aut_notEz");

            var xSyPrim     = new MSOSuccN <BDD>(x, y, 1);
            var aut_xSyPrim = xSyPrim.GetAutomaton(solver);
            var equiv       = aut_xSyPrim.IsEquivalentWith(aut_xSyDef);

            Assert.IsTrue(equiv);
        }
Exemple #3
0
        public void BooleanAlgebraZ3_test2()
        {
            var ctx         = new Context();
            var sort        = ctx.IntSort;
            var solver      = new Z3BoolAlg(ctx, sort);
            var alg         = new BDDAlgebra <BoolExpr>(solver);
            var x           = new Variable("x", true);
            var y           = new Variable("y", true);
            var z           = new Variable("z", true);
            var xLTy        = new MSOLt <BoolExpr>(x, y);
            var xLTzLTy     = new MSOAnd <BoolExpr>((new MSOLt <BoolExpr>(x, z)), (new MSOLt <BoolExpr>(z, y)));
            var Ez          = new MSOExists <BoolExpr>(z, xLTzLTy);
            var notEz       = new MSONot <BoolExpr>(Ez);
            var xSyDef      = new MSOAnd <BoolExpr>(xLTy, notEz);
            var aut_xSyDef  = xSyDef.GetAutomaton(alg);
            var aut_xLTzLTy = xLTzLTy.GetAutomaton(alg);
            var aut_Ez      = Ez.GetAutomaton(alg);
            var aut_notEz   = notEz.GetAutomaton(alg);
            var aut_xLTy    = xLTy.GetAutomaton(alg);

            //aut_xSyDef.ShowGraph("aut_xSyDEf");
            //aut_xLTzLTy.ShowGraph("aut_xLTzLTy");
            //aut_Ez.ShowGraph("aut_Ez");
            //aut_notEz.ShowGraph("aut_notEz");

            var xSyPrim     = new MSOSuccN <BoolExpr>(x, y, 1);
            var aut_xSyPrim = xSyPrim.GetAutomaton(alg);
            var equiv       = aut_xSyPrim.IsEquivalentWith(aut_xSyDef);

            Assert.IsTrue(equiv);
        }
Exemple #4
0
        public void TestWS1S_SuccDef_GetAutomaton()
        {
            var solver      = new CharSetSolver(BitWidth.BV7);
            var x           = new Variable("x", true);
            var y           = new Variable("y", true);
            var z           = new Variable("z", true);
            var xLTy        = new MSOLt <BDD>(x, y);
            var xLTzLTy     = new MSOAnd <BDD>(new MSOLt <BDD>(x, z), new MSOLt <BDD>(z, y));
            var Ez          = new MSOExists <BDD>(z, xLTzLTy);
            var notEz       = new MSONot <BDD>(Ez);
            var xSyDef      = new MSOAnd <BDD>(xLTy, notEz);
            var ca          = new CartesianAlgebraBDD <BDD>(solver);
            var aut_xSyDef  = xSyDef.GetAutomaton(ca);
            var aut_xLTzLTy = xLTzLTy.GetAutomaton(ca);
            var aut_Ez      = Ez.GetAutomaton(ca);
            var aut_notEz   = notEz.GetAutomaton(ca);
            var aut_xLTy    = xLTy.GetAutomaton(ca);

            //aut_xSyDef.ShowGraph("aut_xSyDEf");
            //aut_xLTzLTy.ShowGraph("aut_xLTzLTy");
            //aut_Ez.ShowGraph("aut_Ez");
            //aut_notEz.ShowGraph("aut_notEz");

            var xSyPrim     = new MSOSuccN <BDD>(x, y, 1);
            var aut_xSyPrim = xSyPrim.GetAutomaton(ca);
            var equiv       = aut_xSyPrim.IsEquivalentWith(aut_xSyDef);

            Assert.IsTrue(equiv);
        }
Exemple #5
0
        public void TestMSO_Eq()
        {
            var solver = new CharSetSolver(BitWidth.BV16);
            //var phi = new MSOTrue();
            MSOFormula<BDD> phi = new MSOExists<BDD>(V1("x"), new MSOExists<BDD>(V1("y"), new MSONot<BDD>(new MSOEq<BDD>(V1("x"), V1("y")))));

            var aut = phi.GetAutomaton(solver);

            var aut2 = solver.RegexConverter.Convert(".{2,}", System.Text.RegularExpressions.RegexOptions.Singleline);
            Assert.IsTrue(aut.IsEquivalentWith(aut2));
        }
Exemple #6
0
        public void TestIntExZ3()
        {
            var solver = new Z3Provider();

            var sort = solver.CharacterSort;
            //var phi = new MSOTrue();
            MSOFormula <Expr> phi = new MSOExists <Expr>(V1("x"),
                                                         new MSOPredicate <Expr>(solver.MkLe(solver.MkVar(0, solver.IntSort), solver.MkInt(0)), V1("x"))
                                                         );

            var aut = phi.GetAutomaton(solver);
        }
Exemple #7
0
        public void TestMSO_FirstC()
        {
            var solver = new CharSetSolver(BitWidth.BV16);
            //var phi = new MSOTrue();
            var x = new Variable("x", true);
            MSOFormula<BDD> phi = new MSOExists<BDD>(x, 
                new MSOAnd<BDD>(new MSOPredicate<BDD>(solver.MkCharConstraint('C'), x), new MSOeqN<BDD>(x,0)));

            var aut = phi.GetAutomaton(solver);
            var aut2 = solver.RegexConverter.Convert("^C");
            Assert.IsTrue(aut2.IsEquivalentWith(aut));
        }
Exemple #8
0
        public void TestMSO_FirstLastZ3() 
        {
            var solver = new CharSetSolver(BitWidth.BV16);
            var x = new Variable("x", true);
            MSOFormula<BDD> phi = new MSOExists<BDD>(x, new MSOAnd<BDD>(new MSOeqN<BDD>(x,0), new MSOLast<BDD>(x)));

            var aut = phi.GetAutomaton(solver);

            var res = solver.Convert("^[\0-\uFFFF]$");
            //solver.ShowGraph(res,"res");
            //solver.SaveAsDgml(res, "res");
            var eq = aut.IsEquivalentWith(res);
            Assert.IsTrue(eq);
        }
        static Automaton <T> CreateAutomaton1 <T>(Func <int, T> f, int bitWidth, IBooleanAlgebra <T> Z)
        {
            Func <int, string, MSOPredicate <T> > pred = (i, s) => new MSOPredicate <T>(f(i), new Variable(s, true));

            MSOFormula <T> phi = new MSOFalse <T>();

            for (int index = 0; index < bitWidth; index++)
            {
                var phi1 = pred(index, "var");
                phi = new MSOOr <T>(phi, phi1);
            }

            phi = new MSOExists <T>(new Variable("var", true), phi);

            var aut = phi.GetAutomaton(Z);

            return(aut);
        }
Exemple #10
0
        public void TestCountNoProduct2()
        {
            int       maxvars = 13;
            var       solver  = new CharSetSolver(BitWidth.BV7);
            Stopwatch sw      = new Stopwatch();
            var       tries   = 1;
            var       times   = new long[maxvars - 1];
            var       x       = new Variable("x", true);
            var       y       = new Variable("y", true);

            for (int k = 0; k < tries; k++)
            {
                for (int vars = 8; vars <= maxvars; vars++)
                {
                    MSOFormula <BDD> phi = new MSOTrue <BDD>();
                    for (int i = 1; i < vars; i++)
                    {
                        phi = new MSOAnd <BDD>(phi, new MSOLt <BDD>(new Variable("x" + i, true), new Variable("x" + (i + 1), true)));
                        phi = new MSOAnd <BDD>(phi, new MSOPredicate <BDD>(solver.MkCharConstraint('a'), new Variable("x" + i, true)));
                        phi = new MSOOr <BDD>(phi, new MSOPredicate <BDD>(solver.MkCharConstraint('k'), new Variable("x" + i, true)));
                    }
                    phi = new MSOAnd <BDD>(phi, new MSOPredicate <BDD>(solver.MkCharConstraint('a'), new Variable("x" + vars, true)));
                    phi = new MSOOr <BDD>(phi,
                                          new MSOExists <BDD>(y,
                                                              new MSOPredicate <BDD>(solver.MkCharConstraint('c'), y)));
                    for (int i = vars; i >= 1; i--)
                    {
                        phi = new MSOExists <BDD>(new Variable("x" + i, true), phi);
                    }

                    sw.Restart();
                    //var aut1 = phi.GetAutomaton(new CartesianAlgebraBDD<BDD>(solver));
                    var aut1 = phi.GetAutomaton(solver);
                    sw.Stop();
                    times[vars - 2] += sw.ElapsedMilliseconds;
                    //Console.WriteLine("States {0} Trans {1}",aut1.StateCount,aut1.MoveCount);
                    if (k == tries - 1)
                    {
                        TestContext.WriteLine(string.Format("{0} variables; {1} ms", vars, times[vars - 2] / tries));
                    }
                    //solver.ShowGraph(aut1, "a"+vars);
                }
            }
        }
Exemple #11
0
        Automaton <T> CreateAutomaton2 <T>(Func <int, T> f, int bitWidth, IBooleanAlgebra <T> Z)
        {
            Func <int, Variable, MSOPredicate <T> > pred = (i, s) => new MSOPredicate <T>(f(i), s);

            MSOFormula <T> phi = new MSOFalse <T>();

            for (int index = 0; index < bitWidth; index++)
            {
                MSOFormula <T> phi1 = pred(index, V1("var"));
                phi1 = new MSOExists <T>(V1("var"), phi1);
                phi  = new MSOOr <T>(phi, phi1);
            }

            phi = new MSOExists <T>(V1("var"), phi);

            var aut = phi.GetAutomaton(Z);

            return(aut);
        }
Exemple #12
0
        public void TestCount()
        {
            int       maxvars = 10;
            var       solver  = new CharSetSolver(BitWidth.BV16);
            Stopwatch sw      = new Stopwatch();
            var       tries   = 1;
            var       times   = new long[maxvars - 1];

            for (int k = 0; k < tries; k++)
            {
                for (int vars = 8; vars <= maxvars; vars++)
                {
                    MSOFormula <BDD> phi = new MSOTrue <BDD>();
                    for (int i = 1; i < vars; i++)
                    {
                        phi = new MSOAnd <BDD>(phi, new MSOLt <BDD>(V1("x" + i), V1("x" + (i + 1))));
                        phi = new MSOAnd <BDD>(phi, new MSOPredicate <BDD>(solver.MkCharConstraint('a'), V1("x" + i)));
                    }
                    phi = new MSOAnd <BDD>(phi, new MSOPredicate <BDD>(solver.MkCharConstraint('a'), V1("x" + vars)));
                    phi = new MSOOr <BDD>(phi,
                                          new MSOExists <BDD>(V1("y"),
                                                              new MSOPredicate <BDD>(solver.MkCharConstraint('c'), V1("y"))));
                    for (int i = vars; i >= 1; i--)
                    {
                        phi = new MSOExists <BDD>(V1("x" + i), phi);
                    }


                    sw.Restart();
                    //var aut1 = phi.GetAutomaton(new CartesianAlgebraBDD<BDD>(solver));
                    var aut1 = phi.GetAutomaton(solver);
                    //aut1.ShowGraph();
                    sw.Stop();
                    times[vars - 2] += sw.ElapsedMilliseconds;
                    if (k == tries - 1)
                    {
                        TestContext.WriteLine(string.Format("{0} variables; {1} ms", vars, times[vars - 2] / tries));
                    }
                }
            }
        }
Exemple #13
0
        public void TestMSO_Pred()
        {
            var solver = new CharSetSolver(BitWidth.BV16);
            var x = new Variable("x", true);
            var pred = new MSOPredicate<BDD>(solver.MkCharConstraint( 'c'), x);
            MSOFormula<BDD> phi = new MSOExists<BDD>(x, pred);

            var ca = new CartesianAlgebraBDD<BDD>(solver);
            var pred_aut = pred.GetAutomaton(ca);
            //pred_aut.ShowGraph("pred_aut");

            var aut = phi.GetAutomaton(solver);
            for (int i = 0; i < 10; i++)
            {
                var s = solver.GenerateMember(aut);
                Assert.IsTrue(System.Text.RegularExpressions.Regex.IsMatch(s, "c"), "regex mismatch");
            }
            var aut2 = solver.RegexConverter.Convert("c", System.Text.RegularExpressions.RegexOptions.Singleline);
            //aut2.ShowGraph("aut2");
            //aut.ShowGraph("aut");
            Assert.IsTrue(aut2.IsEquivalentWith(aut), "automata not equialent");
        }
Exemple #14
0
        public void TestMSO_Le()
        {
            var solver = new CharSetSolver(BitWidth.BV16);
            //var phi = new MSOTrue();
            var x = new Variable("x", true);
            var y = new Variable("y", true);
            var z = new Variable("z", true);
            MSOFormula<BDD> phi = new MSOExists<BDD>(z, new MSOExists<BDD>(x, new MSOExists<BDD>(y,
                new MSOAnd<BDD>(
                    new MSOLt<BDD>(x, y),
                    new MSOLt<BDD>(y, z) 
                    )
            ))); 


            var aut = phi.GetAutomaton(solver);

            var aut2 = solver.RegexConverter.Convert(".{3,}", System.Text.RegularExpressions.RegexOptions.Singleline);
            Assert.IsTrue(aut.IsEquivalentWith(aut2));

            //solver.ShowGraph(aut, "SFA");
        }
Exemple #15
0
        public static void Run()
        {
            var sw = new Stopwatch();

            //ex x1 x2... a(x1) /\ a(x2).../\ x1<x2...
            using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(@"..\mso-minterm-p1.csv"))
            {
                file.WriteLine("k, old, cartesian, trie, minterm");
                for (int size = 2; size < kminterm; size++)
                {
                    var solver           = new CharSetSolver(BitWidth.BV64);
                    MSOFormula <BDD> phi = new MSOTrue <BDD>();

                    //x1<x2 /\...
                    for (int k = 1; k < size; k++)
                    {
                        var leq = new MSOSuccN <BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true), 1);
                        phi = new MSOAnd <BDD>(phi, leq);
                    }

                    //ai(xi) /\ ...
                    for (int k = 0; k < size; k++)
                    {
                        var axk = new MSOPredicate <BDD>(solver.MkBitTrue(k), new Variable("x" + k, true));
                        phi = new MSOAnd <BDD>(phi, axk);
                    }
                    phi = new MSOAnd <BDD>(new MSOeqN <BDD>(new Variable("x" + 0, true), 0), phi);
                    for (int k = size - 1; k >= 0; k--)
                    {
                        phi = new MSOExists <BDD>(new Variable("x" + k, true), phi);
                    }

                    //Old
                    sw.Restart();
                    for (int t = 0; t < numTests; t++)
                    {
                        var aut = phi.GetAutomaton(solver);
                    }

                    var told = sw.ElapsedMilliseconds;

                    //BDD
                    sw.Restart();
                    for (int t = 0; t < numTests; t++)
                    {
                        var aut = phi.GetAutomaton(new CartesianAlgebraBDD <BDD>(solver));
                    }
                    sw.Stop();

                    var t1 = sw.ElapsedMilliseconds;

                    //Trie
                    sw.Restart();
                    for (int t = 0; t < numTests; t++)
                    {
                        var aut = phi.GetAutomaton(new BDDAlgebra <BDD>(solver), false);
                    }

                    var t2 = sw.ElapsedMilliseconds;

                    //Tminterm
                    solver = new CharSetSolver(BitWidth.BV64);
                    BDD[] predicates = new BDD[size];
                    solver.GenerateMinterms();
                    for (int k = 0; k < size; k++)
                    {
                        predicates[k] = solver.MkBitTrue(k);
                    }

                    var t3 = 60000L * numTests;
                    if (size <= maxmint)
                    {
                        sw.Restart();
                        for (int t = 0; t < numTests; t++)
                        {
                            var mint = solver.GenerateMinterms(predicates).ToList();
                        }
                        sw.Stop();
                        t3 = sw.ElapsedMilliseconds;
                    }

                    file.WriteLine(size + ", " + (double)told / numTests + ", " + (double)t1 / numTests + ", " + (double)t2 / numTests + ", " + (double)t3 / numTests);
                    Console.WriteLine(size + ", " + (double)told / numTests + ", " + (double)t1 / numTests + ", " + (double)t2 / numTests + ", " + (double)t3 / numTests);
                }
            }

            //ex x1 x2... a(x1) /\ a(x2)...
            using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(@"..\mso-minterm-p2.csv"))
            {
                file.WriteLine("k, old, cartesian, trie, minterm");
                for (int size = 2; size < 10; size++)
                {
                    // Tsolve force
                    var solver = new CharSetSolver();

                    MSOFormula <BDD> phi = new MSOTrue <BDD>();

                    for (int k = 0; k < size; k++)
                    {
                        var axk = new MSOPredicate <BDD>(solver.MkBitTrue(k), new Variable("x" + k, true));
                        phi = new MSOAnd <BDD>(phi, axk);
                    }
                    for (int k = size - 1; k >= 0; k--)
                    {
                        phi = new MSOExists <BDD>(new Variable("x" + k, true), phi);
                    }

                    //Old
                    sw.Restart();
                    for (int t = 0; t < numTests; t++)
                    {
                        var aut = phi.GetAutomaton(solver);
                    }

                    var told = sw.ElapsedMilliseconds;

                    //Cartesian
                    var t1 = 60000L * numTests;
                    if (size <= 7)
                    {
                        sw.Restart();
                        for (int t = 0; t < numTests; t++)
                        {
                            phi.GetAutomaton(new CartesianAlgebraBDD <BDD>(solver));
                        }
                        sw.Stop();

                        t1 = sw.ElapsedMilliseconds;
                    }


                    //Trie
                    var t2 = 60000L * numTests;
                    sw.Restart();
                    for (int t = 0; t < numTests; t++)
                    {
                        phi.GetAutomaton(new BDDAlgebra <BDD>(solver), false);
                    }
                    sw.Stop();

                    t2 = sw.ElapsedMilliseconds;

                    //Tminterm
                    solver = new CharSetSolver(BitWidth.BV64);
                    BDD[] predicates = new BDD[size];
                    solver.GenerateMinterms();
                    for (int k = 0; k < size; k++)
                    {
                        predicates[k] = solver.MkBitTrue(k);
                    }

                    var t3 = 60000L * numTests;
                    if (size <= maxmint)
                    {
                        sw.Restart();
                        for (int t = 0; t < numTests; t++)
                        {
                            var mint = solver.GenerateMinterms(predicates).ToList();
                        }
                        sw.Stop();
                        t3 = sw.ElapsedMilliseconds;
                    }

                    file.WriteLine(size + ", " + (double)told / numTests + ", " + (double)t1 / numTests + ", " + (double)t2 / numTests + ", " + (double)t3 / numTests);
                    Console.WriteLine(size + ", " + (double)told / numTests + ", " + (double)t1 / numTests + ", " + (double)t2 / numTests + ", " + (double)t3 / numTests);
                }
            }
        }