public bool Initialize() { var amountOfTapes = CurrentState.AmountOfTapes; if (States.All(s => s.AmountOfTapes == amountOfTapes || s.AmountOfTapes == 0)) { for (var i = 0; i < amountOfTapes; i++) { Tapes.Add(new TuringTape()); } checkedTransitions = true; } yetToWriteStates.AddRange(States); return(checkedTransitions); }
public void SetDefaultState(Dictionary <string, string> defaultStates) { foreach (KeyValuePair <string, string> pair in defaultStates) { // Состояние всегда должно быть одним из элементов States. if (string.IsNullOrEmpty(pair.Value) || States.All(x => x != pair.Value)) { continue; } if (!_defaultNotesStates.ContainsKey(pair.Key)) { _defaultNotesStates.Add(pair.Key, pair.Value); } _defaultNotesStates[pair.Key] = pair.Value; } Database.SaveDefaultStates(_defaultNotesStates); }
public void Validate() { // Optimization to avoid multiple validations. // It can lead to errors if any property is changed meanwhile... if (_isValid) { return; } this.ValidateObject(); if (States.Count(s => s.Root) != 1) { throw new ValidationException("The flow must have one root state"); } var rootState = States.First(s => s.Root); if (rootState.Input == null || rootState.Input.Bypass) { throw new ValidationException("The root state must expect an input"); } foreach (var state in States) { state.Validate(); if (States.Count(s => s.Id == state.Id) > 1) { throw new ValidationException($"The state id '{state.Id}' is not unique in the flow"); } // Check if there's a direct path loop (without inputs) to this state in the flow. if (state.Outputs != null) { bool CanBeReached(State targetState, Output output, ISet <string> checkedStates) { if (checkedStates.Contains(output.StateId)) { return(false); } var outputState = States.FirstOrDefault(s => s.Id == output.StateId); if (outputState?.Outputs == null || outputState.Outputs.Length == 0) { return(false); } if (outputState.Input != null && !outputState.Input.Bypass) { return(false); } if (outputState.Outputs.Any(o => o.StateId == targetState.Id)) { return(true); } checkedStates.Add(output.StateId); return(outputState.Outputs.Any(o => CanBeReached(targetState, o, checkedStates))); }; foreach (var output in state.Outputs) { if (States.All(s => s.Id != output.StateId)) { throw new ValidationException($"The output state id '{output.StateId}' is invalid"); } if (state.Input == null || state.Input.Bypass) { var checkedStates = new HashSet <string>(); if (CanBeReached(state, output, checkedStates)) { throw new ValidationException( $"There is a loop in the flow starting in the state {state.Id} that does not requires user input"); } } } } } _isValid = true; }
public void Validate() { // Optimization to avoid multiple validations. // It can lead to errors if any property is changed meanwhile... if (_isValid) { return; } this.ValidateObject(); if (States.Count(s => s.Root) != 1) { throw new ValidationException("The flow must have one root state"); } var rootState = States.First(s => s.Root); if (rootState.Input == null || rootState.Input.Bypass) { throw new ValidationException("The root state must expect an input"); } if (rootState.Input.Conditions?.Any() == true) { throw new ValidationException("The root state must not have any conditions"); } if (InputActions != null) { foreach (var inputAction in InputActions) { inputAction.Validate(); } } foreach (var state in States) { state.Validate(); if (States.Count(s => s.Id == state.Id) > 1) { throw new ValidationException($"The state id '{state.Id}' is not unique in the flow"); } // Check if there's a direct path loop (without inputs) to this state in the flow. if (state.Outputs != null) { bool CanBeReached(State targetState, Output output, ISet <string> checkedStates) { if (checkedStates.Contains(output.StateId)) { return(false); } var outputState = States.FirstOrDefault(s => s.Id == output.StateId); if (outputState?.Outputs == null || outputState.Outputs.Length == 0) { return(false); } if (outputState.Input != null && !outputState.Input.Bypass) { return(false); } if (outputState.Outputs.Any(o => o.StateId == targetState.Id)) { return(true); } checkedStates.Add(output.StateId); return(outputState.Outputs.Any(o => CanBeReached(targetState, o, checkedStates))); } ; foreach (var output in state.Outputs) { if (States.All(s => s.Id != output.StateId)) { throw new ValidationException($"The output state id '{output.StateId}' is invalid"); } if (state.Input == null || state.Input.Bypass) { var checkedStates = new HashSet <string>(); if (CanBeReached(state, output, checkedStates)) { throw new ValidationException( $"There is a loop in the flow starting in the state {state.Id} that does not requires user input"); } } } } } if (OutputActions != null) { foreach (var outputAction in OutputActions) { outputAction.Validate(); } } // Try create trace settings from configuration keys if (TraceSettings == null && Configuration != null && Configuration.TryGetValue("TraceMode", out var traceModeValue) && Enum.TryParse <TraceMode>(traceModeValue, true, out var traceMode) && Configuration.TryGetValue("TraceTargetType", out var traceTargetTypeValue) && Enum.TryParse <TraceTargetType>(traceTargetTypeValue, true, out var traceTargetType) && Configuration.TryGetValue("TraceTarget", out var traceTarget)) { TraceSettings = new TraceSettings { Mode = traceMode, TargetType = traceTargetType, Target = traceTarget }; if (Configuration.TryGetValue("TraceSlowThreshold", out var traceSlowThresholdValue) && int.TryParse(traceSlowThresholdValue, out var traceSlowThreshold)) { TraceSettings.SlowThreshold = traceSlowThreshold; } } _isValid = true; }
private bool CheckIfHasNormalForm() { return(States.All(s => s.Value.Transitions.All(t => Transition <A, S> .HasNormalForm(t.SymbolIn, t.StackSymbolsWritten)))); }