private static void InitializeEvents() { Type machineType = typeof(T); foreach (PropertyInfo propertyInfo in machineType.GetProperties(BindingFlags.Static | BindingFlags.Public)) { if (IsPropertyABasicEvent(propertyInfo)) { BasicEvent <T> value = SetPropertyValue(propertyInfo, x => new BasicEvent <T>(x.Name)); _events.Add(value.Name, value); } else if (IsPropertyATypedEvent(propertyInfo)) { Type eventType = typeof(DataEvent <,>).MakeGenericType(typeof(T), propertyInfo.PropertyType.GetGenericArguments()[0]); ConstructorInfo ctor = eventType.GetConstructors()[0]; var name = Expression.Parameter(typeof(string), "name"); var newExp = Expression.New(ctor, name); Func <string, object> creator = Expression.Lambda <Func <string, object> >(newExp, new[] { name }).Compile(); PropertyInfo eventProperty = propertyInfo; SetPropertyValue(propertyInfo, x => creator(eventProperty.Name)); } } }
/// <summary> /// Send an event within the current state /// </summary> /// <param name="raised">The event to raise</param> public virtual void RaiseEvent(Event raised) { BasicEvent <T> eevent = BasicEvent <T> .GetEvent(raised); _currentState.RaiseEvent(this as T, eevent, null); _anyState.RaiseEvent(this as T, eevent, null); }
public bool Inspect <T>(BasicEvent <T> state) where T : StateMachine <T> { Append(string.Format("When {0} Occurs", state.Name)); return(true); }
public State(string name) { _name = name; _enter = new BasicEvent <T>(Name + ":Enter"); _leave = new BasicEvent <T>(Name + ":Leave"); _actions = new MultiDictionary <Event, StateEventAction <T> >(true); }
public bool Inspect <T>(BasicEvent <T> eevent) where T : StateMachine <T> { _currentEventVertex = GetEventVertex(eevent.Name, () => eevent.Name, typeof(Event), typeof(void)); _edges.Add(new Edge(_currentStateVertex, _currentEventVertex, eevent.Name)); return(true); }
public static BasicEvent <T> GetEvent(Event input) { BasicEvent <T> result = input as BasicEvent <T>; if (result == null) { throw new ArgumentException("The event is not valid for this state machine " + input.Name); } return(result); }
public void RaiseEvent(T instance, BasicEvent <T> eevent, object value) { if (!_actions.ContainsKey(eevent)) { return; } foreach (var action in _actions[eevent]) { action.Execute(instance, eevent, value); } }
private void BindEventAction(int all, int i, FastProperty <T, int> property) { var e = _sources[i]; var eevent = BasicEvent <T> .GetEvent(e); var eventAction = new BasicEventAction <T>(eevent); int flag = 1 << i; eventAction.Then(x => { int value = property.Get(x) | flag; property.Set(x, value); if (value == all) { x.RaiseEvent(_target); } }); _bindEventAction(eventAction); }
/// <summary> /// Defines an actions to take when an event is raised within a state /// </summary> /// <param name="raised"></param> /// <returns></returns> protected static BasicEventAction <T> When(Event raised) { BasicEvent <T> eevent = BasicEvent <T> .GetEvent(raised); return(new BasicEventAction <T>(eevent)); }