private void AttachState(State state)
		{
			state.Entered += OnStateEntered;
			state.Exited += OnStateExited;
			foreach (State child in state.Substates)
				AttachState(child);
		}
Exemplo n.º 2
0
		public TimeoutEvent(int timeoutInMilliseconds, State state, Action<State> eventPoster)
		{
			this.timeoutPeriod = timeoutInMilliseconds;
			this.eventPoster = eventPoster;
			this.state = state;
			this.state.Entered += OnStateEntered;
			this.state.Exited += OnStateExited;
		}
Exemplo n.º 3
0
		/// <summary>
		/// By returning the current state of the fsm, we nullify this event because nobody responded.
		/// </summary>
		/// <param name="eventToProcess"></param>
		/// <returns></returns>
		public override State ProcessEvent(State originalState, EventInstance eventToProcess)
		{
			Transition transition = transitions.MatchTransition(eventToProcess);
			if (transition != null)
				return TraverseDown(new TransitionEvent(this, transition, eventToProcess));

			return originalState;
		}
Exemplo n.º 4
0
		public static State GetSubstatePath(this State s, State targetState)
		{
			foreach (State substate in s.Substates)
			{
				if (substate.ContainsState(targetState))
					return substate;
			}

			return null;
		}
Exemplo n.º 5
0
		public State(object id, State parent)
		{
			this.id = id;
			this.parent = parent;
			this.substates = new List<State>();
			this.transitions = new TransitionDirector();

			Entered += delegate { };
			Exited += delegate { };
			Traversing += delegate { };
		}
		private void OnStateEntered(object sender, EventArgs args)
		{
			if (finishState != null)
				return;

			StateWatcherAction watcher = sender as StateWatcherAction;
			finishState = watcher.State;

			Cancel();

			// Notify of the completion
			callback(finishState.Id);
		}
Exemplo n.º 7
0
		public void Initialize(StateMachine sm)
		{
			stateMachine = sm;
			stateMachine.Starting += delegate
			                         	{
											Log("Starting state machine");
											if (root == null)
											{
												root = stateMachine.RootNode;
												root.VisitChildren(AttachToState);
											}
			                         	};
			stateMachine.EventPosted += (s, e) => Log("Event posted: {0}", e.Event.Event);
			stateMachine.EventProcessed += (s, e) => Log("Event processed: {0}", e.Event.Event);
		}
Exemplo n.º 8
0
		public void ProcessNextEvent(State currentState)
		{
			// Processing should not be re-entrant, on the same thread or otherwise
			if (NotProcessing == Interlocked.CompareExchange(ref processingIndicator, Processing, NotProcessing))
			{
				EventInstance eventToProcess = events.Dequeue();
				if (eventToProcess != null)
					currentState = currentState.ProcessEvent(currentState, eventToProcess);

				processingIndicator = NotProcessing;

				if (currentState == null || currentState.Substates.Count() > 0)
					throw new InvalidOperationException("Current state [" + currentState + "] is a superstate or null.");

				EventProcessed(this, new StateEventPostedArgs(currentState, eventToProcess));
			}
		}
		public override State Build(State parent)
		{
			RootState root = new RootState();
			((StateBuilderContext)Context).SetRootState(root);

			foreach (IStateBuilder substate in SubStates)
			{
				root.AddChildState(substate.Build(root));
			}

			foreach (Action<State> action in secondPassActions)
			{
				action(root);
			}

			this.VisitChildren(sb => sb.Build(root));

			return root;
		}
		public CompositeStateWatcherAction(
			StateMachine stateMachine, 
			IEnumerable<object> states,
			Action<object> callback)
		{
			this.stateMachine = stateMachine;
			this.stateActions = new List<StateWatcherAction>();
			this.finishState = null;
			this.callback = callback;
			
			foreach (object stateId in states)
			{
				State state = stateMachine[stateId];
				StateWatcherAction action = new StateWatcherAction(state);
				action.Performed += OnStateEntered;

				stateActions.Add(action);
			}
		}
Exemplo n.º 11
0
		public virtual State ProcessEvent(State originalState, EventInstance eventToProcess)
		{
			Transition transition = transitions.MatchTransition(eventToProcess);
			if (transition != null)
			{
				var transitionEvent = new TransitionEvent(originalState, transition, eventToProcess);

				// If originating from substate
				if (!this.Equals(originalState))
					return originalState.TraverseUp(transitionEvent);
				else
				{
					Exit(transitionEvent);
					return parent.TraverseUp(transitionEvent);
				}
			}

			// Not handled, pass it up the chain
			return parent.ProcessEvent(originalState, eventToProcess);
		}
Exemplo n.º 12
0
		public virtual State Build(State parent)
		{
			// Build is called twice, first time around we build just the state
			if (state == null)
			{
				state = CreateState(Id, parent);

				foreach (IBaseStateBuilder substate in substates)
				{
					state.AddChildState(substate.Build(state));
				}
			}
			else
			{
				foreach (Action<State> action in secondPassActions)
				{
					action(state);
				}
			}

			return state;
		}
		public SingleStateEventInstance(State targetState, object eventTarget)
			: base(eventTarget)
		{
			this.targetState = targetState;
		}
Exemplo n.º 14
0
		public void AddChildState(State substate)
		{
			substates.Add(substate);
		}
Exemplo n.º 15
0
		public HistoryState(State parent, HistoryFetchStrategy historyStrategy)
			: base(new HistoryStateId(parent.Id), parent)
		{
			historyFetcher = historyStrategy;
			Parent.Exited += OnParentExit;
		}
Exemplo n.º 16
0
		public Transition(State sourceState, object eventTarget, State targetState)
		{
			this.sourceState = sourceState;
			this.eventTarget = eventTarget;
			this.targetState = targetState;
		}
Exemplo n.º 17
0
		public State Build(State parent)
		{
			throw new InvalidOperationException("Not valid to Build from this context");
		}
Exemplo n.º 18
0
		public void PostTimeout(State state, object transitionEvent)
		{
			stateMachine.PostEvent(new SingleStateEventInstance(state, transitionEvent));
		}
Exemplo n.º 19
0
		public StateEventPostedArgs(State state, EventInstance eventPosted)
			: base(state)
		{
			Event = eventPosted;
		}
		public StateWatcherAction(State state)
		{
			State = state;
			enterAction = new EnterAction(State, Perform);
		}
Exemplo n.º 21
0
		private void AttachToState(State state)
		{
			state.Entered += delegate { Log("Entered {0}", state); };
			state.Exited += delegate { Log("Exited {0}", state); };
		}
Exemplo n.º 22
0
		public EnterAction(State state, Action<TransitionReceipt> action)
		{
			this.state = state;
			this.state.Entered += OnStateEntered;
			this.Action = action;
		}
Exemplo n.º 23
0
		public ExitAction(State state, Action<TransitionReceipt> action)
		{
			this.state = state;
			this.state.Exited += OnStateExited;
			this.Action = action;
		}
Exemplo n.º 24
0
		public virtual State CreateState(object stateId, State parent)
		{
			if (parent == null)
				throw new InvalidOperationException("Parent may not be null for state: " + stateId);
			return new State(stateId, parent);
		}
Exemplo n.º 25
0
		public void PostTimeout(State state)
		{
			stateMachine.PostEvent(new SingleStateEventInstance(state, TimerBuilder.TimeoutEvent));
		}
Exemplo n.º 26
0
		public GuardedTransition(State sourceState, object eventTarget, State targetState, Func<bool> guard)
			: base(sourceState, eventTarget, targetState)
		{
			this.guard = guard;
		}
		public State Build(State parent)
		{
			HistoryState state = new HistoryState(parent, historyStrategy);

			return state;
		}
Exemplo n.º 28
0
		public StateEventArgs(State state)
		{
			State = state;
		}
Exemplo n.º 29
0
		public TransitionEvent(State sourceState, Transition transition, EventInstance eventInstance)
		{
			this.sourceState = sourceState;
			this.transition = transition;
			this.eventInstance = eventInstance;
		}
Exemplo n.º 30
0
		protected virtual void OnParentExit(object sender, StateTransitionEventArgs args)
		{
			lastState = historyFetcher(this, args.TransitionEvent);
		}