// // Automate.cs // // Author: // Paul Schneider // // Copyright (c) 2015 Paul Schneider // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . using System; using System.Collections.Generic; namespace Yavsc.Model.WorkFlow { /// /// Automate. /// public class Automate { /// /// Initializes a new instance of the Automate class. /// public Automate () { } private Dictionary> transitions = new Dictionary>(); private List letters = new List(); private List states = new List(); /// /// Gets or sets the state. /// /// The state. public TState State { get; set; } /// /// Gets or sets the starting state. /// /// The state. public TState StartingState { get; set; } /// /// Adds the transition. /// /// Start. /// End. /// Letter. public virtual void AddTransition( TState start, TState end, TLetter letter) { if (!states.Contains (start)) states.Add (start); if (!states.Contains (end)) states.Add (end); if (!letters.Contains (letter)) letters.Add (letter); Dictionary node = null; if (!transitions.ContainsKey(start)) transitions.Add( start, node = new Dictionary ()); else node = transitions [start]; if (node.ContainsKey (letter)) throw new NotImplementedException ("Automates indéterministes"); node.Add(letter, end); } /// /// Move this instance according the specified eventId. /// /// Event identifier. public void Aggregate(TLetter eventId) { if (State == null) throw new InvalidOperationException ("Set the current state or reset the automate before"); Dictionary node = transitions [State]; if (node == null) // no transition found from this state // it is final. throw new FinalStateException(); TState nextState = node [eventId]; if (nextState == null) // no transition found for this event throw new InvalidLetterException(eventId); State = nextState; } /// /// Reset the state of this automate to the starting state /// public void Reset() { State = StartingState; } /// /// Clear all valid transitions from this automate, /// reset all of its properties /// public void FactoryReset() { State = default(TState); StartingState = default(TState); letters.Clear (); transitions.Clear (); } /// /// Determines whether this instance is in final state. /// /// true if this instance is in final state; otherwise, false. public bool IsInFinalState() { return !transitions.ContainsKey(this.State); } } }