StateMachineResume Method |
Namespace: StaMa
Exception | Condition |
---|---|
ArgumentNullException | The inputStream parameter is null |
ArgumentException | The contents of the inputStream are not compatible with the current StateMachineTemplate structure, e.g. states or regions might have been added to or removed from the StateMachineTemplate since the content was created. |
IOException | The inputStream is corrupt. |
The Resume(Stream, Boolean) method is an alternative to the Startup and leaves the state machine in the operable state that accepts events through the SendTriggerEvent(Object).
It is assumed that the contents of the given inputStream can be read and that the structure of the state machine template has not changed since the contents were written. Compatibility of the structure is tested by comparing a signature computed from the current StateMachineTemplate structure with the signature that was computed and embedded when the state was saved. The default algorithm for computing the signature is the platform string hash code algorithm applied to a string that describes the entire StateMachineTemplate structure. Applications might change the signature algorithm by setting the SerializationSignatureGenerator property.
The following code shows how to use the SaveState and Resume methods:
StateMachineTemplate t = new StateMachineTemplate(); t.Region("State1", false); t.State("State1"); t.Transition("T1", "State2", "Event1"); t.EndState(); t.State("State2"); t.Transition("T2", "State1", "Event2"); t.EndState(); t.EndRegion(); // Create a state machine and start it StateMachine stateMachine1 = t.CreateStateMachine(); stateMachine1.Startup(); // stateMachine1 is now ready for receiving events stateMachine1.SendTriggerEvent("Event1"); // Check that stateMachine1 has executed transition "T1" to state "State2" if (stateMachine1.ActiveStateConfiguration.ToString() != t.CreateStateConfiguration("State2").ToString()) { throw new Exception("stateMachine1 in unexpected state"); } // Save the current state of stateMachine1 MemoryStream stream = new MemoryStream(); stateMachine1.SaveState(stream); stream.Flush(); // Might write to persistent storage when using FileStream instead // For demonstration purposes reset the MemoryStream to enable reading from it // Real applications would open a FileStream stream.Position = 0; // Create a new state machine using the same structure and resume from saved state StateMachine stateMachine2 = t.CreateStateMachine(); stateMachine2.Resume(stream, false); // stateMachine2 is ready for receiving events stateMachine2.SendTriggerEvent("Event2"); // Check that stateMachine2 has executed transition "T2" to state "State1" if (stateMachine2.ActiveStateConfiguration.ToString() != t.CreateStateConfiguration("State1").ToString()) { throw new Exception("stateMachine2 in unexpected state"); }