/*===============================================================================
* StateEngineEvent.cs
*
* Change history
* When Who Comment
* ---------- ------ ----------------------------------
* 2006-07-18 RMa Added Priority and ArrivalTime.
* 2006-03-02 RMa FXCop updates: validate eventtype in constructor.
* 2006-02-03 RMA FXCop updates: Changed from Event -> StateEngineEvent. (Event is a keyword)
* 2006-01-27 RMa Removed interface Event. Use Event class instead.
* 2006-01-09 RMa Added ToString() method.
* 2005-12-05 RMa This header added.
*
---------------------------------------------------------------------------------*/
using System;
namespace Wayne.Lib.StateEngine
{
///
/// Event class that implements the Event interface. It is the base
/// class to be used to send events to states in the state machine.
///
public class StateEngineEvent
{
#region Fields
private bool handled;
private object type;
private DateTime arrivalTime = DateTime.MinValue;
private int priority;
private uint sequenceNumber;
#endregion
#region Construction
///
/// Initializes a new instance of the StateEngine event with the default priority 0.
///
/// Object identifying the event.
public StateEngineEvent(object eventType)
: this(eventType, 0)
{
}
///
/// Initializes a new instance of the StateEngine event with the specified priority
///
/// Object identifying the event.
/// Priority specifier, Lower number specifies that it will be handled sooner than higher numbers. Default priority is 0.
public StateEngineEvent(object eventType, int priority)
{
this.type = eventType;
this.priority = priority;
}
#endregion
#region Properties
///
/// Indicates if the event has been handled. If the state is handled, it is the application's responsiblity
/// to set this flag. the State engine never changes it.
///
public bool Handled
{
get { return handled; }
set { handled = value; }
}
///
/// Defines the type of the event. Can be any reference or value
/// type (through boxing). Recommended is use of a user-defined enumeration.
/// The EventType is a arbitary type, preferrably enums, but any type can be used.
///
public object Type
{
get { return type; }
}
///
/// Specifies the priority of this event. High priority is defined by lower integers. Highest priority is thereby Int.Min.
/// Default priority is 0.
///
public int Priority
{
get { return priority; }
set { priority = value; }
}
///
/// Specifies the time the event was sent into the state machine. This is used to determine
/// the handling order when resending events to states.
///
internal DateTime ArrivalTime
{
get { return arrivalTime; }
set { arrivalTime = value; }
}
///
/// Specifies an arrival sequence number that is assigned when the event is sent to a state machine.
///
internal uint SequenceNumber
{
get { return sequenceNumber; }
set { sequenceNumber = value; }
}
#endregion
#region Debug methods
///
/// Presents the class as a string.
///
///
public virtual string ToString(string format, IFormatProvider provider)
{
return String.Format(provider, "Event(type={0}), Prio={1}, Arrived={2}, Sq={3}", type, priority, arrivalTime.ToString("HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture), sequenceNumber);
}
///
/// Presents the class as a string using the specified culture-specific format information.
///
///
public virtual string ToString(IFormatProvider provider)
{
return ToString("", provider);
}
///
/// Presents the class as a string using a format string.
///
///
public virtual string ToString(string format)
{
return ToString(format, System.Globalization.CultureInfo.InvariantCulture);
}
///
/// Presents the class as a string using a format string and the specified culture-specific format information.
///
///
public override string ToString()
{
return ToString("", System.Globalization.CultureInfo.InvariantCulture);
}
#endregion
}
}