예제 #1
0
        /// <summary>
        /// Returns an automaton that accepts the union of the languages of the given automata.
        /// </summary>
        /// <param name="automatons">The l.</param>
        /// <returns>
        /// An automaton that accepts the union of the languages of the given automata.
        /// </returns>
        /// <remarks>
        /// Complexity: linear in number of states.
        /// </remarks>
        public static Automaton Union(IList <Automaton> automatons)
        {
            var ids = new HashSet <int>();

            foreach (Automaton a in automatons)
            {
                ids.Add(RuntimeHelpers.GetHashCode(a));
            }

            bool hasAliases = ids.Count != automatons.Count;
            var  s          = new State();

            foreach (Automaton b in automatons)
            {
                if (b.IsEmpty)
                {
                    continue;
                }

                Automaton bb = b;
                bb = hasAliases ? bb.CloneExpanded() : bb.CloneExpandedIfRequired();

                s.AddEpsilon(bb.Initial);
            }

            var automaton = new Automaton();

            automaton.Initial         = s;
            automaton.IsDeterministic = false;
            automaton.ClearHashCode();
            automaton.CheckMinimizeAlways();
            return(automaton);
        }
예제 #2
0
        /// <summary>
        /// Returns an automaton that accepts the union of the empty string and the language of the
        /// given automaton.
        /// </summary>
        /// <param name="a">The automaton.</param>
        /// <remarks>
        /// Complexity: linear in number of states.
        /// </remarks>
        /// <returns>An automaton that accepts the union of the empty string and the language of the
        /// given automaton.</returns>
        public static Automaton Optional(Automaton a)
        {
            a = a.CloneExpandedIfRequired();
            var s = new State();

            s.AddEpsilon(a.Initial);
            s.Accept          = true;
            a.Initial         = s;
            a.IsDeterministic = false;
            a.ClearHashCode();
            a.CheckMinimizeAlways();
            return(a);
        }
예제 #3
0
        /// <summary>
        /// Returns a (deterministic) automaton that accepts the complement of the language of the
        /// given automaton.
        /// </summary>
        /// <param name="a">The automaton.</param>
        /// <returns>A (deterministic) automaton that accepts the complement of the language of the
        /// given automaton.</returns>
        /// <remarks>
        /// Complexity: linear in number of states (if already deterministic).
        /// </remarks>
        public static Automaton Complement(Automaton a)
        {
            a = a.CloneExpandedIfRequired();
            a.Determinize();
            a.Totalize();
            foreach (State p in a.GetStates())
            {
                p.Accept = !p.Accept;
            }

            a.RemoveDeadTransitions();
            return(a);
        }
예제 #4
0
        public static Automaton Concatenate(Automaton a1, Automaton a2)
        {
            if (a1.IsSingleton && a2.IsSingleton)
            {
                return(BasicAutomata.MakeString(a1.Singleton + a2.Singleton));
            }

            if (BasicOperations.IsEmpty(a1) || BasicOperations.IsEmpty(a2))
            {
                return(BasicAutomata.MakeEmpty());
            }

            bool deterministic = a1.IsSingleton && a2.IsDeterministic;

            if (a1 == a2)
            {
                a1 = a1.CloneExpanded();
                a2 = a2.CloneExpanded();
            }
            else
            {
                a1 = a1.CloneExpandedIfRequired();
                a2 = a2.CloneExpandedIfRequired();
            }

            foreach (State s in a1.GetAcceptStates())
            {
                s.Accept = false;
                s.AddEpsilon(a2.Initial);
            }

            a1.IsDeterministic = deterministic;
            a1.ClearHashCode();
            a1.CheckMinimizeAlways();
            return(a1);
        }
예제 #5
0
 /// <summary>
 /// Returns an automaton that accepts the union of the empty string and the language of the 
 /// given automaton.
 /// </summary>
 /// <param name="a">The automaton.</param>
 /// <remarks>
 /// Complexity: linear in number of states.
 /// </remarks>
 /// <returns>An automaton that accepts the union of the empty string and the language of the 
 /// given automaton.</returns>
 public static Automaton Optional(Automaton a)
 {
     a = a.CloneExpandedIfRequired();
     var s = new State();
     s.AddEpsilon(a.Initial);
     s.Accept = true;
     a.Initial = s;
     a.IsDeterministic = false;
     a.ClearHashCode();
     a.CheckMinimizeAlways();
     return a;
 }
예제 #6
0
        public static Automaton Concatenate(Automaton a1, Automaton a2)
        {
            if (a1.IsSingleton && a2.IsSingleton)
            {
                return BasicAutomata.MakeString(a1.Singleton + a2.Singleton);
            }

            if (BasicOperations.IsEmpty(a1) || BasicOperations.IsEmpty(a2))
            {
                return BasicAutomata.MakeEmpty();
            }

            bool deterministic = a1.IsSingleton && a2.IsDeterministic;
            if (a1 == a2)
            {
                a1 = a1.CloneExpanded();
                a2 = a2.CloneExpanded();
            }
            else
            {
                a1 = a1.CloneExpandedIfRequired();
                a2 = a2.CloneExpandedIfRequired();
            }

            foreach (State s in a1.GetAcceptStates())
            {
                s.Accept = false;
                s.AddEpsilon(a2.Initial);
            }

            a1.IsDeterministic = deterministic;
            a1.ClearHashCode();
            a1.CheckMinimizeAlways();
            return a1;
        }
예제 #7
0
        /// <summary>
        /// Returns a (deterministic) automaton that accepts the complement of the language of the 
        /// given automaton.
        /// </summary>
        /// <param name="a">The automaton.</param>
        /// <returns>A (deterministic) automaton that accepts the complement of the language of the 
        /// given automaton.</returns>
        /// <remarks>
        /// Complexity: linear in number of states (if already deterministic).
        /// </remarks>
        public static Automaton Complement(Automaton a)
        {
            a = a.CloneExpandedIfRequired();
            a.Determinize();
            a.Totalize();
            foreach (State p in a.GetStates())
            {
                p.Accept = !p.Accept;
            }

            a.RemoveDeadTransitions();
            return a;
        }
예제 #8
0
        public static Automaton Concatenate(IList <Automaton> l)
        {
            if (l.Count == 0)
            {
                return(BasicAutomata.MakeEmptyString());
            }

            bool allSingleton = l.All(a => a.IsSingleton);

            if (allSingleton)
            {
                var b = new StringBuilder();
                foreach (Automaton a in l)
                {
                    b.Append(a.Singleton);
                }

                return(BasicAutomata.MakeString(b.ToString()));
            }
            else
            {
                if (l.Any(a => a.IsEmpty))
                {
                    return(BasicAutomata.MakeEmpty());
                }

                var ids = new HashSet <int>();
                foreach (Automaton a in l)
                {
                    ids.Add(RuntimeHelpers.GetHashCode(a));
                }

                bool      hasAliases = ids.Count != l.Count;
                Automaton b          = l[0];
                b = hasAliases ? b.CloneExpanded() : b.CloneExpandedIfRequired();

                var  ac    = b.GetAcceptStates();
                bool first = true;
                foreach (Automaton a in l)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        if (a.IsEmptyString())
                        {
                            continue;
                        }

                        Automaton aa = a;
                        aa = hasAliases ? aa.CloneExpanded() : aa.CloneExpandedIfRequired();

                        HashSet <State> ns = aa.GetAcceptStates();
                        foreach (State s in ac)
                        {
                            s.Accept = false;
                            s.AddEpsilon(aa.Initial);
                            if (s.Accept)
                            {
                                ns.Add(s);
                            }
                        }

                        ac = ns;
                    }
                }

                b.IsDeterministic = false;
                b.ClearHashCode();
                b.CheckMinimizeAlways();
                return(b);
            }
        }