private static void AssertAutomaton(Automaton a) { Automaton clone = (Automaton)a.Clone(); // complement(complement(a)) = a Automaton equivalent = BasicOperations.Complement(BasicOperations.Complement(a)); Assert.IsTrue(BasicOperations.SameLanguage(a, equivalent)); // a union a = a equivalent = BasicOperations.Union(a, clone); Assert.IsTrue(BasicOperations.SameLanguage(a, equivalent)); // a intersect a = a equivalent = BasicOperations.Intersection(a, clone); Assert.IsTrue(BasicOperations.SameLanguage(a, equivalent)); // a minus a = empty Automaton empty = BasicOperations.Minus(a, clone); Assert.IsTrue(BasicOperations.IsEmpty(empty)); // as long as don't accept the empty string // then optional(a) - empty = a if (!BasicOperations.Run(a, "")) { //System.out.println("test " + a); Automaton optional = BasicOperations.Optional(a); //System.out.println("optional " + optional); equivalent = BasicOperations.Minus(optional, BasicAutomata.MakeEmptyString()); //System.out.println("equiv " + equivalent); Assert.IsTrue(BasicOperations.SameLanguage(a, equivalent)); } }
/// <summary> /// Returns an automaton that accepts between <paramref name="min"/> and /// <paramref name="max"/> (including both) concatenated repetitions of the language /// of the given automaton. /// <para/> /// Complexity: linear in number of states and in <paramref name="min"/> and /// <paramref name="max"/>. /// </summary> public static Automaton Repeat(Automaton a, int min, int max) { if (min > max) { return(BasicAutomata.MakeEmpty()); } max -= min; a.ExpandSingleton(); Automaton b; if (min == 0) { b = BasicAutomata.MakeEmptyString(); } else if (min == 1) { b = (Automaton)a.Clone(); } else { IList <Automaton> @as = new List <Automaton>(); while (min-- > 0) { @as.Add(a); } b = Concatenate(@as); } if (max > 0) { Automaton d = (Automaton)a.Clone(); while (--max > 0) { Automaton c = (Automaton)a.Clone(); foreach (State p in c.GetAcceptStates()) { p.AddEpsilon(d.initial); } d = c; } foreach (State p in b.GetAcceptStates()) { p.AddEpsilon(d.initial); } b.deterministic = false; //b.clearHashCode(); b.ClearNumberedStates(); b.CheckMinimizeAlways(); } return(b); }
/// <summary> /// Returns an automaton that accepts the concatenation of the languages of the /// given automata. /// <para/> /// Complexity: linear in total number of states. /// </summary> public static Automaton Concatenate(IList <Automaton> l) { if (l.Count == 0) { return(BasicAutomata.MakeEmptyString()); } bool all_singleton = true; foreach (Automaton a in l) { if (!a.IsSingleton) { all_singleton = false; break; } } if (all_singleton) { StringBuilder b = new StringBuilder(); foreach (Automaton a in l) { b.Append(a.singleton); } return(BasicAutomata.MakeString(b.ToString())); } else { foreach (Automaton a in l) { if (BasicOperations.IsEmpty(a)) { return(BasicAutomata.MakeEmpty()); } } HashSet <int> ids = new HashSet <int>(); foreach (Automaton a in l) { ids.Add(a.GetHashCode()); } bool has_aliases = ids.Count != l.Count; Automaton b = l[0]; if (has_aliases) { b = b.CloneExpanded(); } else { b = b.CloneExpandedIfRequired(); } ISet <State> ac = b.GetAcceptStates(); bool first = true; foreach (Automaton a in l) { if (first) { first = false; } else { if (a.IsEmptyString) { continue; } Automaton aa = a; if (has_aliases) { aa = aa.CloneExpanded(); } else { aa = aa.CloneExpandedIfRequired(); } ISet <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.deterministic = false; //b.clearHashCode(); b.ClearNumberedStates(); b.CheckMinimizeAlways(); return(b); } }