protected static void AssertTransition(IReadOnlyDfa <StateSet <string>, char> dfa, string from, char on, string to) { var fromSet = ParseStateSet(from); var toSet = ParseStateSet(to); Assert.True(dfa.TryGetTransition(fromSet, on, out var gotTo)); AssertEqual(toSet, gotTo); }
/// <summary> /// Implements <see cref="IReadOnlyFiniteAutomaton{TState, TSymbol}.Accepts(IEnumerable{TSymbol})"/> for DFAs. /// </summary> /// <typeparam name="TState">The state type.</typeparam> /// <typeparam name="TSymbol">The symbol type.</typeparam> /// <param name="dfa">The DFA to check acceptance on.</param> /// <param name="input">The input to check acceptance for.</param> /// <returns>True, if the <paramref name="input"/> is accepted by <paramref name="dfa"/>.</returns> public static bool Accepts <TState, TSymbol>(IReadOnlyDfa <TState, TSymbol> dfa, IEnumerable <TSymbol> input) { var currentState = dfa.InitialState; foreach (var symbol in input) { if (!dfa.TryGetTransition(currentState, symbol, out var destinationState)) { return(false); } currentState = destinationState; } return(dfa.AcceptingStates.Contains(currentState)); }
protected static void AssertTransition <T>(IReadOnlyDfa <T, char> dfa, T from, char on, T to) { Assert.True(dfa.TryGetTransition(from, on, out var gotTo)); Assert.Equal(to, gotTo); }
/// <summary> /// Minimizes the DFA. /// </summary> /// <typeparam name="TState">The state type.</typeparam> /// <typeparam name="TSymbol">The symbol type.</typeparam> /// <param name="dfa">The DFA to minimize.</param> /// <param name="differentiatePairs">The pairs of states to differentiate.</param> /// <returns>The minimized DFA.</returns> public static IDfa <StateSet <TState>, TSymbol> Minimize <TState, TSymbol>( this IReadOnlyDfa <TState, TSymbol> dfa, IEnumerable <(TState, TState)> differentiatePairs) =>
/// <summary> /// Minimizes the DFA. /// </summary> /// <typeparam name="TState">The state type.</typeparam> /// <typeparam name="TSymbol">The symbol type.</typeparam> /// <param name="dfa">The DFA to minimize.</param> /// <returns>The minimized DFA.</returns> public static IDfa <StateSet <TState>, TSymbol> Minimize <TState, TSymbol>(this IReadOnlyDfa <TState, TSymbol> dfa) => dfa.Minimize(StateCombiner <TState> .ToSetCombiner(dfa.StateComparer), Enumerable.Empty <(TState, TState)>());
/// <summary> /// Minimizes the DFA. /// </summary> /// <typeparam name="TState">The state type.</typeparam> /// <typeparam name="TSymbol">The symbol type.</typeparam> /// <typeparam name="TResultState">The resulting state type.</typeparam> /// <param name="dfa">The DFA to minimize.</param> /// <param name="combiner">The state combiner to use.</param> /// <returns>The minimized DFA.</returns> public static IDfa <TResultState, TSymbol> Minimize <TState, TSymbol, TResultState>( this IReadOnlyDfa <TState, TSymbol> dfa, IStateCombiner <TState, TResultState> combiner) => dfa.Minimize(combiner, Enumerable.Empty <(TState, TState)>());
/// <summary> /// Minimizes the DFA. /// </summary> /// <typeparam name="TState">The state type.</typeparam> /// <typeparam name="TSymbol">The symbol type.</typeparam> /// <typeparam name="TResultState">The resulting state type.</typeparam> /// <param name="dfa">The DFA to minimize.</param> /// <param name="combiner">The state combiner to use.</param> /// <param name="differentiate">The list of states to differentiate from every other state.</param> /// <returns>The minimized DFA.</returns> public static IDfa <TResultState, TSymbol> Minimize <TState, TSymbol, TResultState>( this IReadOnlyDfa <TState, TSymbol> dfa, IStateCombiner <TState, TResultState> combiner, IEnumerable <TState> differentiate) => dfa.Minimize(combiner, differentiate.SelectMany(s1 => dfa.States.Select(s2 => (s1, s2))));