Convert Charts Between Mealy and Moore Semantics
Mealy and Moore machines are often considered the basic, industry-standard paradigms for modeling finite-state machines. You can create charts that implement pure Mealy or Moore semantics as a subset of Stateflow® chart semantics. Mealy and Moore semantics are not supported in standalone Stateflow charts in MATLAB®. For more information, see Overview of Mealy and Moore Machines.
A best practice is to not change from one Stateflow chart type to another in the middle of development. You cannot automatically convert the semantics of the original chart to conform to the design rules of the new chart type. Changing chart type usually requires you to redesign your chart to achieve the equivalent behavior of the original chart, so that both charts produce the same sequence of outputs given the same sequence of inputs. Sometimes, however, there is no way to translate specific behaviors without violating chart definitions.
This table lists a summary of what happens when you change chart types mid-design.
From | To | Result |
---|---|---|
Mealy | Classic | Mealy charts retain their semantics when changed to Classic type. |
Classic | Mealy | If the Classic chart defines its output at every time step and conforms to Mealy semantic rules, the Mealy chart exhibits behavior equivalent to the original Classic chart. |
Moore | Classic | State actions in the Moore chart behave as
entry and during actions
because they are not labeled. The Classic chart exhibits behavior
that is not equivalent to the original Moore chart. Requires
redesign. |
Classic | Moore | Actions that are unlabeled in the Classic chart
(entry and during actions
by default) behave as during and
exit actions. The Moore chart exhibits
behavior that is not equivalent to the original Classic chart.
Requires redesign. |
Mealy | Moore | Mealy and Moore rules about placement of actions are mutually exclusive. Converting chart types between Mealy and Moore semantics does not produce equivalent behavior. Requires redesign. |
Moore | Mealy |
Transform Chart from Mealy to Moore Semantics
This example shows how to use a Mealy chart to model a vending machine, as described in Model a Vending Machine by Using Mealy Semantics.
openExample("sf_mealy_vending_machine")
In the Mealy chart, each state represents one of the three possible coin inputs:
nickel, dime, or no coin. Each condition action computes output (whether soda is
released) based on input (the coin received) as the chart transitions to the next
state. If you change the chart type to Moore
, you get a
compile-time diagnostic message indicating that the chart violates Moore chart
semantics. According to Moore semantics, the chart output soda
cannot depend on the value of the input coin
.
To convert the chart to valid Moore semantics, redesign your chart by moving the
logic that computes the output out of the transitions and into the states. In the
Moore chart, each state must represent both coins received and the soda release
condition (soda = 0
or soda = 1
). As a result,
the redesigned chart requires more states.
openExample("sf_moore_vending_machine")
Before considering the value of coin
, the Moore chart must
compute the value of soda
according to the active state. As a
result, there is a delay in releasing soda, even if the machine receives enough
money to cover the cost.
Compare Semantics of Vending Machines
This table compares the semantics of the charts before and after the transformation.
Mealy Vending Machine | Moore Vending Machine |
---|---|
Uses three states | Uses five states |
Computes outputs in condition actions | Computes outputs in state actions |
Updates output based on input | Updates output before evaluating input, requiring an extra time step to release the soda |
For this vending machine, Mealy is a better modeling paradigm because there is no delay in releasing the soda once sufficient coins are received. By contrast, the Moore vending machine requires an extra time step before producing the soda. Therefore, it is possible for the Moore vending machine to produce a can of soda at the same time step that it accepts a coin toward the next purchase. In this situation, the delivery of a soda can appear to be in response to this coin, but actually occurs because the vending machine received the purchase price in previous time steps.
Transform Chart from Moore to Mealy Semantics
This example shows how to use a Moore chart to model a traffic light, as described in Model a Traffic Light by Using Moore Semantics.
openExample("sf_moore_traffic_light")
In the Moore chart, each state represents the colors of the traffic light in two
opposite directions and the duration of the current color. Each state action
computes output (the colors of the traffic light) regardless of input (if there are
cars waiting at a sensor). If you change the chart type to
Mealy
, you get a compile-time diagnostic message
indicating that the chart violates Mealy chart semantics. According to Mealy
semantics, the chart cannot compute its outputs in state actions.
To convert the chart to valid Mealy semantics, redesign your chart by moving the
logic that computes the output out of the states and into the transitions. The
redesigned Mealy chart consists of five states, just like the Moore chart. In most
transitions, a temporal logic expression or the Boolean input
sens
guards a condition action computing the outputs
y1
and y2
. The only exceptions are:
The default transition, which computes the initial outputs without a guarding condition.
The transition from the
Stop
state to theStopForTraffic
state, which does not compute new outputs.
openExample("sf_mealy_traffic_light")
In the same time step, the Mealy chart evaluates the temporal logic expressions
and the input signal sens
, and computes the value of the outputs
y1
and y2
. As a result, in the Mealy
chart, the output changes one time step before the corresponding change occurs in
the original Moore chart. In the Simulink® model, you can compensate for the anticipated changes in output by
adding a Delay block to each output signal.
Compare Semantics of Traffic Light Controllers
This table compares the semantics of the charts before and after the transformation.
Moore Traffic Light Controller | Mealy Traffic Light Controller |
---|---|
Uses five states | Uses five states |
Computes outputs in state actions | Computes outputs in condition actions |
Updates output before evaluating input | Updates output based on input, requiring a Delay block in each output signal |