State Machine Actor

From LabVIEW Wiki
Jump to: navigation, search

The State Machine Actor (SMA) design pattern is completely implemented in Messages, it does not override the Actor Core. It sends the first Message on Pre Launch Init Method. Each subsequent Message sends the next Message in the state.

Because it doesn't override the Actor Core the pattern does not have a UI of its own. It could be paired with another Actor that implements the UI and sends Messages to it to update UI elements. The paired Actor would follow one of the other patterns.

Collatz Sequence

Below is an example of the SMA pattern, of where the Actor has an Initialization and three States which solve the Collatz Sequence. The Collatz Sequence is a sequence of positive numbers given any starting number where the next number in the sequence is calculated by:

n --> n/2 (n is even)
n --> 3n+1 (n is odd)

The sequence always finishes when n = 1.

Code

Translating the Collatz Sequence into these states creates:

  • State 1 checks n for even or odd, then decides to send State 2 or State 3 Messages
  • State 2 calculates n/2. If n = 1 the send Stop Message, else send State 1 Message
  • State 3 calculates 3n+1. If n = 1 the send Stop Message, else send State 1 Message

The final sequence would be sent with the Handle Last Ack, if launched as a nested Actor. However, in this case the launcher started a User Event and is waiting for the final sequence. The Stop Code is overridden to send the User Even.

Launcher

Launcher

Pre Launch Init

Pre Launch Init

Initialization State

Most of the time this code should be handled in the Pre Launch Init. Another initialization step might not be necessary.

Initialization State

State 1

State 1

State 2

State 2

State 3

State 3

Stop Core

Stop Core

External Links