// // 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 class. /// public Automate () { } private List> eventids = new List>(); private List> stateids = new List>(); private IEquatable startingstateid ; private List> endingstateids = new List>(); private Dictionary,Dictionary,IEquatable>> transitions = new Dictionary, Dictionary, IEquatable>>(); /// /// Sets the state of the starting. /// /// Starting state identifier. public void SetStartingState(IEquatable startingStateId) { if (endingstateids.Contains (startingStateId)) throw new InvalidOperationException ("endingstateids.Contains (startingStateId)"); if (!stateids.Contains (startingStateId)) stateids.Add (startingStateId); startingstateid = startingStateId; } /// /// Sets the state of the ending. /// /// Ending state identifier. public void SetEndingState(IEquatable endingStateId) { if (startingstateid==endingStateId) throw new InvalidOperationException ("startingstateid==endingStateId"); if (!stateids.Contains (endingStateId)) stateids.Add (endingStateId); if (!endingstateids.Contains (endingStateId)) endingstateids.Add (endingStateId); } /// /// Adds the transition. /// /// Starting state identifier. /// Ending state identifier. /// Event identifier. public void AddTransition( IEquatable startingStateId, IEquatable endingStateId, IEquatable eventId) { if (!stateids.Contains (startingStateId)) stateids.Add (startingStateId); if (!stateids.Contains (endingStateId)) stateids.Add (endingStateId); Dictionary,IEquatable> currentNode = transitions [startingStateId]; if (currentNode == null) { transitions.Add( startingStateId, currentNode = new Dictionary, IEquatable> ()); } currentNode [eventId] = endingStateId; if (!eventids.Contains (eventId)) eventids.Add (eventId); } /// /// Gets or sets the state. /// /// The state. public IEquatable State { get; set; } /// /// Move this instance according the specified eventId. /// /// Event identifier. public void Move(IEquatable eventId) { if (State == null) throw new InvalidOperationException ("Set the current state or reset the automate before"); Dictionary,IEquatable> node = transitions [State]; if (node == null) // no transition found return; IEquatable nextState = node [eventId]; if (nextState == null) // no transition found for this event return; State = nextState; } /// /// Reset the state of this automate to the starting state /// public void Reset() { State = startingstateid; } /// /// Clear all valid transitions from this automate, /// reset all of its properties /// public void FactoryReset() { State = null; startingstateid = null; endingstateids.Clear (); eventids.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 endingstateids.Contains (State); } } }