技术问询:如何用UML状态图建模Moore/Mealy机?输出是否通过动作表示?
Great question! Let’s break down how to model Moore and Mealy machines with UML state diagrams, plus clarify how outputs are represented (and yes, actions are absolutely the way to go for this).
Moore machines have a key trait: outputs depend solely on the current state, not the input that triggered the state change. Here’s how to model this in UML:
- Each state explicitly defines the output associated with being in that state. The most straightforward way is to use a state’s
entryaction: when the system enters the state, the output action executes immediately. For example:- A state labeled
Idlemight haveentry / setSystemOutput(0)— this means as soon as we enterIdle, the output is set to 0.
- A state labeled
- You can also add a textual annotation directly below the state name (like
Output: 0) for documentation purposes, but theentryaction is the executable, model-driven approach. - State transitions only care about input triggers (e.g.,
[input=1]), with no output attached to the transition itself — since output is tied strictly to the target state.
Mealy machines are different: outputs depend on both the current state and the input that triggers the transition. UML state diagrams map this perfectly by attaching outputs to transitions:
- Output actions are placed directly on the transition edge, right after the input trigger (and guard condition, if any). The syntax uses a slash (
/) to separate the trigger from the action.- Example: A transition from
IdletoActivemight be labeled[input=1] / setSystemOutput(1). This means when we receive an input of 1 while inIdle, we switch toActiveand immediately output 1.
- Example: A transition from
- Unlike Moore machines, Mealy outputs are not tied to states — they’re tied to the act of transitioning between states in response to an input.
To answer your second question: Yes, actions are the primary way to implement outputs in these UML state diagrams, and they align perfectly with both machine types:
- For Moore machines: Use
entryactions (as mentioned) because they fire exactly once when entering the state, which matches the Moore behavior of outputting a value for the entire duration of being in the state. You could also usedoactivities, but those run continuously while in the state — which is unnecessary unless your output needs to update repeatedly (rare for basic Moore machines). - For Mealy machines: Transition-level actions are ideal because they execute exactly when the input triggers the transition, which is exactly when a Mealy machine generates its output.
- Actions in UML can be any executable logic: setting a variable, sending a signal to another component, invoking a function, or even printing a value. This makes them flexible enough to model any output requirement for finite state machines.
A quick rule of thumb to avoid confusion:
- Moore = Output lives with the state (use
entryactions or state annotations) - Mealy = Output lives with the transition (use transition actions)
Hope this clears up the mapping between these classic FSMs and UML state diagrams — feel free to ask if you want a concrete example for a specific scenario!
内容的提问来源于stack exchange,提问作者kaiserlautern

