private FAState GetOptimalState(FAState sourceState, IEnumerable <FATransition> filterTransition, FiniteAutomata DFA) { FAState selectedState = null; IEnumerable <FAState> finalStateFilter = filterTransition.SelectMany(x => x.ToState.Where(y => y.IsFinalState)); if (finalStateFilter.Count() == 1) { selectedState = finalStateFilter.First(); } else { bool xLoop = filterTransition.All(x => x.FromState == sourceState && x.ToState.Any(y => y.StateName == sourceState.StateName) && x.FromState.IsFinalState); if (xLoop) { string emptySName = "Empty"; FAState emptySet = new FAState(emptySName); if (!DFA.States.Any(x => x.StateName == emptySet.StateName)) { _ = DFA.AddState(emptySet); foreach (char symbol in DFA.Alphabet) { _ = DFA.AddTransition(symbol, emptySName, emptySName); } } selectedState = emptySet; return(selectedState); } IEnumerable <FAState> selfRefStates = filterTransition.Where(x => x.ToState.Any(y => y == sourceState && x.FromState == sourceState) && !x.Direction) .Select(x => x.FromState); FAState selfRefSt = selfRefStates.FirstOrDefault(); if (selfRefSt != null) { FAState updState = new FAState(selfRefSt.StateName, selfRefSt.IsInitialState); DFA.UpdateState(updState); selectedState = updState; return(selectedState); } IEnumerable <FAState> selfReferenceFilter = filterTransition.SelectMany(x => x.ToState.Where(y => y != sourceState)); if (selfReferenceFilter.Count() == 1) { selectedState = selfReferenceFilter.First(); } else { selectedState = filterTransition.First().FromState; } } return(selectedState); }
public void Should_ReturnTrue_When_UpdateState_WithFAState() { List <char> alphabet = new List <char>() { 'a' }; FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.TwoWayDFA, alphabet); FAState state = new FAState("State Name", isInitialState: true, isFinalState: true); automata.AddState(state); state = new FAState("State Name", isInitialState: true, isFinalState: false); Assert.True(automata.UpdateState(state)); }