public bool RemoveState(FSMState <TInput, TOutput> state) { bool result = false; if (stateSet.Contains(state)) { try { // Удаляем все переходы, которые ведут в удаляемое состояние foreach (var transition in Transitions.Values) { TransitionRes <TInput, TOutput> destState = transition.destinationStates.FirstOrDefault(ds => ds.DestState.StateCore == state.StateCore); while (destState != null) { transition.destinationStates.Remove(destState); destState = transition.destinationStates.FirstOrDefault( ds => ds.DestState.StateCore == state.StateCore); } } // Чистим пустые переходы var tr = Transitions.Values.FirstOrDefault(t => t.destinationStates.Count == 0); while (tr != null) { Transitions.Remove(tr.ToString()); tr = Transitions.Values.FirstOrDefault(t => t.destinationStates.Count == 0); } // Удаляем все переходы, исходящие из удаляемого состояния var tr1 = Transitions.Values.FirstOrDefault(t => t.SourceState.StateCore == state.StateCore); while (tr1 != null) { Transitions.Remove(tr1.ToString()); tr1 = Transitions.Values.FirstOrDefault(t => t.SourceState.StateCore == state.StateCore); } // Собсно мочим само состояние stateSet.Remove(state); state.FSM = null; state.Outgoing.Clear(); result = true; } catch (Exception e) { Debug.WriteLine(e.Message); } } if (result) { SyncInitialState(); } return(result); }
public TransitionRes <TInput, TOutput> GetDestination(double probability) { TransitionRes <TInput, TOutput> result = null; double sum = 0.0; foreach (var destinationState in destinationStates) { if ((probability >= sum) && (probability <= sum + destinationState.Probability)) { result = destinationState; break; } sum += destinationState.Probability; } return(result); }
public TransitionRes <TInput, TOutput> GetTransRes(double probability) { TransitionRes <TInput, TOutput> result = null; double sum = 0.0; foreach (var pair in SubTransitions) { if ((probability >= sum) && (probability <= sum + pair.Value)) { result = pair.Key; break; } sum += pair.Value; } return(result); }
public bool RemoveOutput(TOutput output) { bool result = false; if (outputSet.Contains(output)) { try { // Удаляем все переходы, которые ведут к удаляемому символу foreach (var transition in Transitions.Values) { TransitionRes <TInput, TOutput> dest = transition.destinationStates.FirstOrDefault(ds => ds.Output.KeyName == output.KeyName); while (dest != null) { transition.destinationStates.Remove(dest); dest = transition.destinationStates.FirstOrDefault( ds => ds.Output.KeyName == output.KeyName); } } // Чистим пустые переходы var tr = Transitions.Values.FirstOrDefault(t => t.destinationStates.Count == 0); while (tr != null) { Transitions.Remove(tr.ToString()); tr = Transitions.Values.FirstOrDefault(t => t.destinationStates.Count == 0); } // Собсно мочим сам выходной символ outputSet.Remove(output); result = true; } catch (Exception e) { Debug.WriteLine(e.Message); } } return(result); }
public bool AddTransitionRes(FSMState <TInput, TOutput> destState, TOutput output, double probability) { if (destState == null) { throw new ArgumentNullException("destState"); } if ((probability <= 0) || (probability > 1)) { throw new ArgumentException("Probability must be in (0;1]", "probability"); } double available = 0; foreach (var pair in SubTransitions) { available += pair.Value; } if (probability + available > 1) { throw new ArgumentException("Sum probability must be in (0;1]", "probability"); } bool result = false; var transRes = new TransitionRes <TInput, TOutput>() { DestState = destState, Output = output, // Probability = probability }; if (!SubTransitions.ContainsKey(transRes)) { SubTransitions.Add(transRes, probability); result = true; } return(result); }
public bool AddDestination(FSMState <TInput, TOutput> state, TOutput output, double probability) { if (state == null) { throw new ArgumentNullException("state"); } if ((probability <= 0) || (probability > 1)) { throw new ArgumentException("Probability must be in (0;1]", "probability"); } TransitionRes <TInput, TOutput> tr = new TransitionRes <TInput, TOutput>(this) { DestState = state, Output = output, Probability = probability }; bool result = true; if (!destinationStates.Contains(tr)) { double available = 0; foreach (var destinationState in destinationStates) { available += destinationState.Probability; } if (probability + available > 1) { throw new ArgumentException("Sum probability must be in (0;1]", "probability"); } destinationStates.Add(tr); } else { var v = destinationStates.First(d => d.KeyName == tr.KeyName); double available = 0; foreach (var destinationState in destinationStates) { if (destinationState.KeyName != v.KeyName) { available += destinationState.Probability; } else { available += probability; } } if (available > 1) { throw new ArgumentException("Sum probability must be in (0;1]", "probability"); } v.Probability = probability; } return(result); }
public bool ChangeValues(FSMState <TInput, TOutput> newDestState, TOutput newOutput, double newProbability) { if (newDestState == null) { throw new ArgumentNullException("newDestState"); } if (newOutput == null) { throw new ArgumentNullException("newOutput"); } if (newDestState.FSM != DestState.FSM) { throw new ArgumentException("newDestState"); } bool result = false; TransitionRes <TInput, TOutput> trR = new TransitionRes <TInput, TOutput>() { DestState = newDestState, Output = newOutput }; double avProb = 0; foreach (var destinationState in Transition.destinationStates) { if (destinationState.KeyName != this.KeyName) { avProb += destinationState.Probability; } else { avProb += newProbability; } } if (avProb > 1) { throw new ArgumentException("Probability must be in (0;1]", "newProbability"); } if (trR.KeyName == this.KeyName) { this.Probability = newProbability; result = true; } else { var existTrRes = Transition.destinationStates.FirstOrDefault(t => t.KeyName == trR.KeyName); if (existTrRes == null) { this.DestState = newDestState; this.Output = newOutput; this.Probability = newProbability; result = true; } else { throw new ArgumentException("Перехода с указанными параметрами уже существует"); } } return(result); }