//-------------------------------------------------------------------------------- /// <summary> /// Register a new event /// </summary> /// <param name="name">The name of the event</param> public FSMEvent registerEvent(string name) { if (registered_events.ContainsKey(name)) { return(registered_events[name]); } FSMEvent newEvent = new FSMEvent(next_event++, name); registered_events[name] = newEvent; return(newEvent); }
//-------------------------------------------------------------------------------- /// <summary> /// Add a transition. /// </summary> /// <param name="source">The source state name.</param> /// <param name="target">The target state name.</param> /// <param name="eventName">The event name that triggers this transition.</param> /// <param name="predicate">The guard predicate for this transition.</param> /// <param name="action">An action callback to be called if this transition is triggered.</param> public void addTransition(string source, string target, FSMEvent trigger, Guard guard, Action <FSMEvent, FSMState> action) { if (!states.ContainsKey(source)) { throw new ArgumentException("No such source state found"); } if (!states.ContainsKey(target)) { throw new ArgumentException("No such target state found"); } states[source].addTransition(new FSMTransition(trigger, states[target], guard, action)); }
//------------------------------------------------------------------------------------- /// <summary> /// Trigger an event on this state /// This will execute a transition to a different ( or the same ) state. /// </summary> /// <param name="trigger">The event we want to trigger</param> /// <returns>The new state we are in</returns> public FSMState triggerEvent(FSMEvent trigger) { if (!transitions.ContainsKey(trigger)) { return(null); } foreach (FSMTransition transition in transitions[trigger]) { if (transition.checkGuard()) { fireExitAction(); transition.fireAction(); FSMState newState = transition.Target; newState.fireEnterAction(); return(newState); } } return(null); }
//-------------------------------------------------------------------------------- /// <summary> /// Add a transition. /// </summary> /// <param name="source">The source state name.</param> /// <param name="target">The target state name.</param> /// <param name="eventName">The event name that triggers this transition.</param> /// <param name="action">An action callback to be called if this transition is triggered.</param> public void addTransition(string source, string target, FSMEvent trigger, Action <FSMEvent, FSMState> action) { addTransition(source, target, trigger, null, action); }
//-------------------------------------------------------------------------------- /// <summary> /// Add a transition. /// </summary> /// <param name="source">The source state name.</param> /// <param name="target">The target state name.</param> /// <param name="eventName">The event name that triggers this transition.</param> /// <param name="guard">The guard predicate for this transition.</param> public void addTransition(string source, string target, FSMEvent trigger, Guard guard) { addTransition(source, target, trigger, guard, null); }
//-------------------------------------------------------------------------------- /// <summary> /// Add a transition. /// </summary> /// <param name="source">The source state name.</param> /// <param name="target">The target state name.</param> /// <param name="eventName">The event name that triggers this transition.</param> public void addTransition(string source, string target, FSMEvent trigger) { addTransition(source, target, trigger, null, null); }