public override Regexp Normalize()
        {
            var disj = GetDisjuncts();

            disj.Sort();
            Regexp re = null;

            foreach (var d in disj)
            {
                if (re == null)
                {
                    re = d.Normalize();
                }
                else
                {
                    re = new REUnion(d.Normalize(), re);
                }
            }

            if (re.GetDisjuncts().Count == disj.Count)
            {
                return(re);
            }
            else
            {
                return(re.Normalize());
            }
        }
 private static bool CorrectOnPosSet(Regexp regexp, IEnumerable <string> testSet)
 {
     foreach (var test in testSet)
     {
         if (!regexp.HasModel(test))
         {
             return(false);
         }
     }
     return(true);
 }
        private static Automaton <BDD> getDfa(Regexp regexp)
        {
            var re = regexp.Normalize();

            if (!memoDfa.Keys.Contains(re.ToString()))
            {
                memoDfa[re.ToString()] = re.getDFA(alph, solver);
            }

            return(memoDfa[re.ToString()]);
        }
        public override Regexp Normalize()
        {
            var r1 = r.Normalize();

            Regexp dj = null;

            foreach (var d in r1.GetDisjuncts())
            {
                Regexp sol = null;
                if (!(d is REStar) && !(d is REPlus) && !(d is REQMark))
                {
                    sol = d;
                }
                else
                {
                    if (d is REStar)
                    {
                        var v = d as REStar;
                        sol = v.r;
                    }
                    else
                    {
                        if (d is REPlus)
                        {
                            var v = d as REPlus;
                            sol = v.r;
                        }
                        else
                        {
                            var v = d as REQMark;
                            sol = v.r;
                        }
                    }
                }
                if (dj == null)
                {
                    dj = sol;
                }
                else
                {
                    dj = new REUnion(sol, dj);
                }
            }
            if (dj.GetDisjuncts().Count != r.GetDisjuncts().Count)
            {
                return(new REStar(dj).Normalize());
            }

            return(new REStar(dj));
        }
        public static IEnumerable <Regexp> SynthesizeRegexp(HashSet <char> alphabet, Automaton <BDD> dfa, CharSetSolver s, StringBuilder sb, long timeout)
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"..\..\..\regexpenum.txt"))
            {
                solver    = s;
                numStates = dfa.StateCount;
                alph      = alphabet;

                #region test variables
                StringBuilder sb1             = new StringBuilder();
                int           lim             = 0;
                Stopwatch     membershipTimer = new Stopwatch();
                Stopwatch     equivTimer      = new Stopwatch();
                timer = new Stopwatch();
                timer.Start();
                #endregion


                #region TestSets for equiv
                var mytests  = DFAUtilities.MyHillTestGeneration(alphabet, dfa, solver);
                var posMN    = mytests.First;
                var negMN    = mytests.Second;
                var tests    = DFAUtilities.GetTestSets(dfa, alphabet, solver);
                var positive = tests.First;
                var negative = tests.Second;
                foreach (var t in posMN)
                {
                    positive.Remove(t);
                }
                foreach (var t in negMN)
                {
                    negative.Remove(t);
                }
                #endregion

                #region Sigma Star
                bool fst = true;
                foreach (var c in alph)
                {
                    if (fst)
                    {
                        fst       = false;
                        sigmaStar = new RELabel(c);
                    }
                    else
                    {
                        sigmaStar = new REUnion(sigmaStar, new RELabel(c));
                    }
                }

                sigmaPlus = new REPlus(sigmaStar);
                sigmaStar = new REStar(sigmaStar);
                #endregion

                #region Accessories vars
                maxWidthC     = 0;
                maxSigmaStarC = 0;
                var isSubset             = true;
                HashSet <string> visited = new HashSet <string>();
                HashSet <string> newReg  = new HashSet <string>();
                currUnionEls = new Dictionary <string, Automaton <BDD> >();
                memoDfa      = new Dictionary <string, Automaton <BDD> >();
                List <Regexp> subsetReg = new List <Regexp>();
                #endregion

                for (maxWidth = 1; true; maxWidth++)
                {
                    newReg       = new HashSet <string>();
                    maxSigmaStar = 2;

                    foreach (var regexp in EnumerateRegexp())
                    {
                        #region run for at most timeout
                        if (timer.ElapsedMilliseconds > timeout)
                        {
                            sb.AppendLine("| Timeout");
                            timer.Stop();
                            yield break;
                        }
                        #endregion

                        var re = regexp.Normalize();

                        if (!(visited.Contains(re.ToString())))
                        {
                            visited.Add(re.ToString());

                            sb1 = new StringBuilder();
                            sb1.Append(re.ToString());
                            file.WriteLine(sb1);
                            lim++;

                            #region Membership test
                            membershipTimer.Start();
                            isSubset = CorrectOnNegSet(regexp, negMN);
                            membershipTimer.Stop();
                            #endregion

                            #region equivalence check
                            if (isSubset)
                            {
                                membershipTimer.Start();
                                if (CorrectOnNegSet(regexp, negative))
                                {
                                    if (CorrectOnPosSet(regexp, posMN) && CorrectOnPosSet(regexp, positive))
                                    {
                                        membershipTimer.Stop();
                                        equivTimer.Start();
                                        var rDfa = getDfa(regexp);
                                        memoDfa[regexp.ToString()] = rDfa;

                                        if (rDfa.IsEquivalentWith(dfa, solver))
                                        {
                                            isSubset = false;
                                            equivTimer.Stop();
                                            timer.Stop();

                                            sb.Append("| ");
                                            regexp.ToString(sb);
                                            sb.AppendLine("|");
                                            sb.AppendLine(string.Format("| elapsed time:    \t {0} ms", timer.ElapsedMilliseconds));
                                            sb.AppendLine(string.Format("| equivalence cost:\t {0} ms", equivTimer.ElapsedMilliseconds));
                                            sb.AppendLine(string.Format("| membership cost: \t {0} ms", membershipTimer.ElapsedMilliseconds));
                                            sb.AppendLine(string.Format("| attempts:        \t {0}", lim));
                                            yield return(regexp);
                                        }
                                        else
                                        {
                                            Console.WriteLine("used dfa");
                                            equivTimer.Stop();
                                        }
                                    }
                                    else
                                    {
                                        membershipTimer.Stop();
                                    }
                                }
                                else
                                {
                                    membershipTimer.Stop();
                                    isSubset = false;
                                }
                            }
                            #endregion

                            //#region Subsets
                            //if (isSubset)
                            //{
                            //    foreach (var reg1 in subsetReg)
                            //    {
                            //        var union = (reg1.CompareTo(regexp) > 0) ? (new REUnion(reg1, regexp)) : (new REUnion(regexp, reg1));
                            //        visited.Add(union.ToString());
                            //        sb1 = new StringBuilder();
                            //        sb1.Append(union + " From union");
                            //        file.WriteLine(sb1);
                            //        lim++;

                            //        membershipTimer.Start();
                            //        if (CorrectOnPosSet(union, posMN) && CorrectOnPosSet(union, positive))
                            //        {
                            //            membershipTimer.Stop();

                            //            equivTimer.Start();
                            //            var rDfa = getDfa(union);
                            //            memoDfa[union.ToString()] = rDfa;

                            //            if (rDfa.IsEquivalentWith(dfa, solver))
                            //            {
                            //                equivTimer.Stop();
                            //                timer.Stop();

                            //                sb.Append("| ");
                            //                union.ToString(sb);
                            //                sb.AppendLine("|");
                            //                sb.AppendLine(string.Format("| elapsed time:    \t {0} ms", timer.ElapsedMilliseconds));
                            //                sb.AppendLine(string.Format("| equivalence cost:\t {0} ms", equivTimer.ElapsedMilliseconds));
                            //                sb.AppendLine(string.Format("| membership cost: \t {0} ms", membershipTimer.ElapsedMilliseconds));
                            //                sb.AppendLine(string.Format("| attempts:        \t {0}", lim));
                            //                yield return union;
                            //            }
                            //            else
                            //            {
                            //                Console.WriteLine("used dfa");
                            //                equivTimer.Stop();
                            //            }
                            //        }
                            //        else
                            //        {
                            //            membershipTimer.Stop();
                            //        }
                            //    }
                            //    subsetReg.Add(regexp);
                            //}
                            //#endregion
                        }
                    }

                    visited = new HashSet <string>(visited.Union(newReg));
                }
            }
        }
 public REUnion(Regexp left, Regexp right)
 {
     this.left  = left;
     this.right = right;
     this.str   = string.Format(@"(({0})|({1}))", left.str, right.str);
 }
 public REQMark(Regexp rexp)
 {
     this.r   = rexp;
     this.str = string.Format(@"({0})?", rexp.str);
 }
 public REPlus(Regexp rexp)
 {
     this.r   = rexp;
     this.str = string.Format(@"({0})+", rexp.str);
 }
 public REStar(Regexp rexp)
 {
     this.r   = rexp;
     this.str = string.Format(@"({0})*", rexp.str);
 }
 public REConcatenation(Regexp left, Regexp right)
 {
     this.left  = left;
     this.right = right;
     this.str   = string.Format(@"{0}{1}", left.str, right.str);
 }