Esempio n. 1
0
        /// <summary>
        /// Returns a new automaton that accepts strings representing decimal
        /// non-negative integers in the given interval.
        /// </summary>
        /// <param name="min"> minimal value of interval </param>
        /// <param name="max"> maximal value of interval (both end points are included in the
        ///          interval) </param>
        /// <param name="digits"> if >0, use fixed number of digits (strings must be prefixed
        ///          by 0's to obtain the right length) - otherwise, the number of
        ///          digits is not fixed </param>
        /// <exception cref="IllegalArgumentException"> if min>max or if numbers in the
        ///              interval cannot be expressed with the given fixed number of
        ///              digits </exception>
        public static Automaton MakeInterval(int min, int max, int digits)
        {
            Automaton a = new Automaton();
            string    x = Convert.ToString(min);
            string    y = Convert.ToString(max);

            if (min > max || (digits > 0 && y.Length > digits))
            {
                throw new System.ArgumentException();
            }
            int d;

            if (digits > 0)
            {
                d = digits;
            }
            else
            {
                d = y.Length;
            }
            StringBuilder bx = new StringBuilder();

            for (int i = x.Length; i < d; i++)
            {
                bx.Append('0');
            }
            bx.Append(x);
            x = bx.ToString();
            StringBuilder by = new StringBuilder();

            for (int i = y.Length; i < d; i++)
            {
                by.Append('0');
            }
            by.Append(y);
            y = by.ToString();
            ICollection <State> initials = new List <State>();

            a.Initial = Between(x, y, 0, initials, digits <= 0);
            if (digits <= 0)
            {
                List <StatePair> pairs = new List <StatePair>();
                foreach (State p in initials)
                {
                    if (a.Initial != p)
                    {
                        pairs.Add(new StatePair(a.Initial, p));
                    }
                }
                BasicOperations.AddEpsilons(a, pairs);
                a.Initial.AddTransition(new Transition('0', a.Initial));
                a.deterministic = false;
            }
            else
            {
                a.deterministic = true;
            }
            a.CheckMinimizeAlways();
            return(a);
        }