/// <summary> /// Remove a state from the list /// </summary> /// <typeparam name="Q"></typeparam> /// <param name="states"></param> /// <param name="key"></param> public static void RemoveTranistionFunction <Q, S>(this List <TransitionFunction <NfaState <Q>, S, NfaState <Q> > > tf, NfaState <Q> state, S symbol) { tf.Remove( tf.Find( t => t.From.Id.Equals(state.Id) && ((symbol == null && t.TransitionSymbols == null) || t.TransitionSymbols.Equals(symbol)))); }
/// <summary> /// Override the GetNextStates methdo in the base /// class to add the lambda feature /// It gets all the next states of the specified state /// and that's reachable through the symbol <see cref="S"/> /// or the lambda, lambda is represented as null. /// </summary> /// <param name="state">The state for which to get the next states</param> /// <param name="symbol">The connector symbol</param> /// <returns>List of all next states if any, empty list otherwise</returns> public override List <NfaState <Q> > GetNextStates(NfaState <Q> state, S symbol) { var list = new List <NfaState <Q> >(); if (state != null && IsExists(state.Id)) { IEnumerable <TransitionFunction <NfaState <Q>, S, NfaState <Q> > > transitions = (from t in TransitionTable where t.From.Id.Equals(state.Id) && t.TransitionSymbols != null && t.TransitionSymbols.Contains( symbol) select t).Union( from t in TransitionTable.OfType <PredicateTransitionFunction <NfaState <Q>, S, NfaState <Q> > >() where t.From.Id.Equals(state.Id) && t.PredicateLambda != null && t.PredicateLambda(symbol) select(TransitionFunction <NfaState <Q>, S, NfaState <Q> >) t ).Distinct(); foreach (var tf in transitions) { list.Add(tf.To); // For each one check to see of there are some states // reachable via lambda List <NfaState <Q> > nextLambdaStates = GetNextLambdaStates(tf.To); // add the states reachable via lambda if (nextLambdaStates != null) { list.AddRange(nextLambdaStates); } } // Search for lambda transtion from the state and other states List <NfaState <Q> > nextStateLambdaStates = GetNextLambdaStates(state); if (nextStateLambdaStates != null) { // if found, iterate over them and check if // they have a transition with the same symbol foreach (var st in nextStateLambdaStates) { list.AddRange(from t in TransitionTable where t.From.Id.Equals(st.Id) && t.TransitionSymbols != null && t.TransitionSymbols.Contains(symbol) select t.To); } } } else { return(null); } return(list); }
/// <summary> /// Add new state, this method check if the /// state <see cref="T"/> exists, if not /// add the state. /// </summary> /// <param name="state"></param> public void AddState(NfaState <Q> state) { if (!IsExists(state.Id)) { if (state.StateType == StateType.StartState) { CurrentStates.Add(state); } States.Add(state); } }
/// <summary> /// Get all the next states of the specified state /// </summary> /// <param name="state">The state for which to get the next states</param> /// <returns>List of all next states if any, empty list otherwise</returns> public List <NfaState <Q> > GetNextStates(NfaState <Q> state) { var list = new List <NfaState <Q> >(); if (state != null && IsExists(state.Id)) { list.AddRange(from s in TransitionTable.FindAll(tf => tf.From.Id.Equals(state.Id)) select s.To); } else { return(null); } return(list); }
/// <summary> /// Set state type /// </summary> /// <param name="key">The state key</param> /// <param name="type">The type to be set</param> public void SetStateType(Q key, StateType type) { NfaState <Q> state = null; if ((state = GetState(key)) != null) { state.StateType = type; if (state.StateType == StateType.StartState) { CurrentStates.Add(state); } else { CurrentStates.RemoveState(key); } } else { throw new Exception("State doesn't exists"); } }
/// <summary> /// Get all the next states of the specified state /// and that's reachable through the symbol <see cref="S"/> /// </summary> /// <param name="state">The state for which to get the next states</param> /// <param name="symbol">The connector symbol</param> /// <returns>List of all next states if any, empty list otherwise</returns> public virtual List <NfaState <Q> > GetNextStates(NfaState <Q> state, S symbol) { var list = new List <NfaState <Q> >(); if (state != null && IsExists(state.Id)) { list.AddRange((from s in TransitionTable.FindAll(tf => tf.From.Id.Equals(state.Id) && tf.TransitionSymbols.Contains(symbol)) select s.To).Union( from t in TransitionTable.OfType <PredicateTransitionFunction <NfaState <Q>, S, NfaState <Q> > >() where t.From.Id.Equals(state.Id) && t.PredicateLambda != null && t.PredicateLambda(symbol) select t.To ).Distinct()); } else { return(null); } return(list); }