As a newbie am trying to develop a state machine using Visio for a cd writer. below are the device operations/transactions and attached, is a diagram of what I've done so far, am unsure if its accurately represented.
Device operation
Load button- causes the drawer to open and to shut if open(load an empty cdr)
Burn button- starts recording document on the cdr, green light comes on in the burning process
and goes off when completed. Once cdr is burned writer stops.
Verify button- verifies the document previously recorded on the cdr, green light comes on in the
process and goes off when completed, then device stops
Cancel button- stops process anytime during recording or verifying
Cancel button- no effect if cd writer is empty or not busy verifying or recording When powered up- CD Writer will ensure the drawer is closed
Burn button – has no effect when cd writer is empty and during recording or verifying process.
Verifying can only be started when the CD Writer is not busy recording.
Visually your diagram looks like a state machine and states have good-sounding names - it's a good start. :)
The first issue I see there is the transition specification. It is definitelly not correct. State transitions in UML are specify in the folowing format:
event [guard] /action
where:
event (or trigger) is an external on internal "signal" that starts the transition. It can be a button activated by a user, an elapsed timer, a detected error, etc. It can even be omitted.
guard is a logical condition that should be fulfilled in order to start the transition. It is usually an expression returning a boolean value of tru or false. It can also be omitted.
action is a kind of side-effect, something that is executed when the transition is triggered. Ic can also be omitted.
Getting back to your diagram I would say that...
most of the labels on your transitions should not carrry "/" as it indicates an action. There are mostly manual triggers, like "load" (pressing the button to open the drawer), "Cancel", "butn", etc.
Consider some events that are triggered internally, like "burning done", or "CD loaded"
You can add some guard conditions in the situations where possible
I would remove all the triggers with no effect (like cancel with no CD in). It makes the diagram simplier with no loss of information
States LOADED and IDLE in your case are kind of strange, weak. It is not clear what makes them different (see the example below)
Here is a diagram that I find a bit more acurate (see notes for additional comments):
You need to specify explicit events as triggers of the transitions.
In the current state machine, each transition (except the one leaving the initial vertex) has an effect, but not a trigger. More accurately, they are triggered by the default completion event, and are therefore automatic.
Moreover, since all transitions react to the same event, your state machine is non-deterministic. For instance, in the state loaded, the transitions to Recording, empty, and loaded all react to the completion event. When the completion event is dispatched, these transitions are said to be conflicting, and one of them is selected non-deterministically. I am sure this is no what you want.
Read the UML Specification, and define triggers for your transitions.
You don't need to depict the transitions from "empty" to "empty" as transitions without any actions or state transitions would not need to be drawn in the Statemachine diagrams. (State Transition tables are often used for the checks of any missing transitions for such cases.)
"loaded" and "Idle" can be represented in one as a same state
To represent the "green light", I would write "turn on the green light" as entry action in Recording and "turn off the green light" as exit
Here's the diagram I drew. The status of tray (whether its opened or closed) should be considered in the actual model, but its not on my sample diagram below.
Related
I have a state machine diagram for an application that has a UI and a thread that runs parallel. I model this using a state with 2 parallel regions. Now I want when that thread gives some message, for example an error, that the state of the UI changes, for example to an error screen state.
I was wondering what the correct method to do this is, as I was not able to find any example of this situation online.
The options that I thought of were creating a transition from every state to this error state when the error state is called, which in a big diagram would mean a lot of transitions. Or by just creating a transition from the thread to the error state (As shown in the image), which could possible be unclear.
Example of second option
You don't need a transition from every state. Just use a transition from the State State to the Error window state. It will then fire no matter what states are currently active.
Such a transition will also leave the Some process state. Since you didn't define initial states in the orthogonal regions, the right region of the state machine will then be in an undefined state. Some people allow this to happen, but for me it is not good practice. So, I suggest to add initial states to both regions. Then it is clear, which sub state will become active, when the State state is entered for any reason. The fork would also not be necessary then.
How you have done it in your example would also be Ok. However, since the Some process state is left by this transition, this region is now in an undefined state. Again, the solution to this is to have an initial state here. I would only use such a transition between regions, when it contains more than one state and the On error event shall not trigger a transition in all of the states.
If the Some process state is only there to allow for concurrent execution, there is an easier way to achieve this: The State state can have a do behavior, that is running while the sub states are active. Orthogonal regions should be used wisely, since they add a lot complexity.
I have never created/understood completely how to create a state machine diagram based on detailed description so I wanted to start somewhere. The automated booth is for student to return books automatically.
Description:
To return a book, it must be placed on a small conveyor belt and then the user needs to press the check-in button on the touch screen.
The chip reader in the machine then detects which book is being returned.
If no book is detected, the conveyor belt returns the item to the user. If a book is detected, the screen displays all the loan information on the screen and the user can confirm the return or cancel it.
If cancelled, the book is returned to the user. If confirmed, the book is taken into the library and the booth prints a physical receipt for the user. Additionally, the machine has a maintenance mode; no books can be returned until the maintenance is completed.
The Diagram:
I am not sure if I am describing all the states that the machine has or if I am fundamentally missing the guide lines of state machine diagram rules. I would appreciate any feedback as I am still learning.
In most cases, a state is a situation in which nothing happens and the machine is waiting for an external event to happen. The event may then be a reason to go to another state. Along the transition arrow, you write event [ condition ] / action. In your case, event is something the user does and action is something the machine does in response to the event.
States in which the machine is waiting are in your case: "Idle", "Loan information displayed" and "Maintenance". Although "Out of service" and "Self test" seem logical states, the description does not mention them, so I would not include them.
In some cases, you may wish to use a state to reflect the situation that the machine is busy doing something. The event may then be the completion of the work or an external interruption. In your case, Scan for book may be such a state, but you could also regard scanning as an action, for example user presses button / scan for book.
Both options are valid, but let's choose for Scan for book as a state (although I think "Busy scanning" would be a more state-like name).
From the description, I assume the machine goes from Idle to Scan for book when the user presses the button. So instead of Request book [check-in button pressed], I would write user presses check-in button.
State Scan for book is left when the machine has finished scanning. Along the arrow towards the decision diamond, you could write finished scanning. If it is a valid book, the machine will show the loan information and enter the state Loan information displayed. This transition is [valid book] / display loan information. Alternatively, you could write display loan information as an entry action inside the Loan information displayed state.
If the item is not valid, the machine returns the item. The transition is [else] / return item and this transition goes from the diamond to the Idle state.
In the same way, you can model transitions between Idle and Maintenance and between Idle and Loan information displayed.
I am not 100% sure what 'do' does in an UML statechart diagram.
Is my thinking correct:
If a transition happens, 'entry' will be executed first and then 'do' will be executed.
And if we are leaving the state only 'exit' will be executed.
Does this logic apply to reflexive transitions as well?
And what if a trigger happens, that does not set off a transition? Does this only execute 'do'?
The UML Specification tells us:
14.2.3.4.3 State entry, exit, and doActivity Behaviors
. . .
A State may also have an associated doActivity Behavior. This Behavior commences execution when the State is entered (but only after the State entry Behavior has completed) and executes concurrently with any other Behaviors that may be associated with the State, until:
it completes (in which case a completion event is generated) or
the State is exited, in which case execution of the doActivity Behavior is aborted.
The execution of a doActivity Behavior of a State is not affected by the firing of an internal transition of that State.
You :
Does this logic apply to reflexive transitions as well?
As said by the last sentence above that depends if the reflexive transition is internal or external.
To add to bruno's answer something more specific.
As stated in the UML specification (and quoted by bruno in his answer) the do activity is performed while the system is in a certain state (after entry activities were completed). It is performed during the whole time the system is in the state. The do activities cease to be performed only in two situations:
The transition taking you out of the State is triggered; in such case, the do activities are interrupted, exit activities if any, are performed and then the transition occurs. The transition can be reflexive (i.e. once completed the system returns to the same state), in which case first entry activities will be carried out and once they are completed, the do activities will start again.
The do activities finish on its own; in such case, once the do activities finish it's time to leave the state. So the eventual exit activities are performed and the transition is performed. A valid system should have one or more transitions without a triggering event (if more than one then they should have conditions allowing to uniquely determine which one will be used) that would be followed once the exit activities (or do if there was no exit) completes.
Let's have a more tangible example. Consider a washing machine. There are two interesting states in it, one is filling with water, the other one is drum rolling.
For the filling with water state, one can expect the following activities:
entry: open water valve
do: monitor water level
exit: close water valve
Now, once the washing machine gets into the state, the valve is being open (entry) and immediately afterwards the machine starts the do: to monitor the level of water (as water now flows in freely). Once the set water level for the current program and step is reached, the event "water level reached" is sent, initiating next transition. As a result the do activity is stopped (there is no more point in monitoring the water level) and the exit is carried out (valve is being closed), followed by the transition to the next state (e.g. heating or drum rolling depending on the program stage).
As you can clearly see, in such case the do activity is interrupted once the transition trigger is captured.
For the drum rolling the situation is quite different though. For the sake of simplicity we will have just one *do( activity, with no entry or exit activities:
do: rotate the drum for n seconds with n speed
When the washing machine enters this state, it immediately starts rolling the drum and (unless interrupted by some exceptional event) keeps doing it for the predefined amount of time. Once that time elapses, the do activity interrupts immediately and the transition to the next state happens. In such case, it is the do activity that controls when the next state transition occurs, at least in the typical scenario.
I have implemented a simple state machine for an embedded system using C's switch statement. I know it would be better if I used function pointers with a look up table but I'm saving that as a next step.
My state machine has the following states:
Startup (the initial state)
Startup Error.
Idle (the system only checks for input in this state. It does not update the display or anything else. It's just 'idle').
Check (this is the actual application)
Program
Copy to Internal Memory
When the system starts up, it enter the startup state which configures the ports, initializes the display and does a hand-shake with the ICs connected on the SPI bus to ensure everything is A-OK. If so, the system enters the Idle state. If not, it enters the Startup Error state, displays an error on the LCD, flags a variable and then enters the Idle state.
In the Idle state, the program polls 3 pins on the microcontroller to check if one of the 3 buttons (Check, Program, Copy to Mem.) is pressed. Depending on which button is pressed, it enters the appropriate state, executes some code, updates the LCD and then returns back to the Idle state. NOTE: The system does NOT care if a button is pressed if there was a hardware fault in the system. The Startup Error state flags a variable called hardware_fault which, if set, ensures that the Idle state does not bother polling any of the input buttons.
This is the first time I'm implementing a state machine and I was just unsure if this was a good design. I haven't really seen any examples of FSMs where they poll for input in an Idle state. Instead, it seems, most examples are rather sequential in nature (like a counter, for instance). So, my question is, is my design reasonable? It DOES work but as everyone here knows, there is bad design and then there is good design.
You should refactor your code away from a switch statement and use a transition table as you mention. Code around a switch doesn't scale, is messy and quickly becomes difficult to read and update.
To answer your question, I would say that most state machines are actually like yours: they react to events (in your case, the poll). If they are purely sequential, without events at all, they are for specialized usages, like implementing a regex...
As a note, the Idle state is equivalent to the runtime core of a state machine library: it intercepts events and posts them to the state machine. With a state machine library, it would be "hidden" from the client code, while a bare bones implementation like yours has to do event polling explicitly.
And now a design critique: one should avoid global variables in general, and especially so in a state machine. State Idle should not know about a hardware_fault global variable. With state machines, one should strive to "embed" global variables in the state machine itself, by adding states (when it makes sense!). A very good introduction to (hierarchical) state machines and explanation of the rationale is here.
Using the UML notation (see here for a tutorial), your state machine is:
An easy refactoring to remove the hardware_fault dependency all together is:
That is, we just stay forever in the StartupError state, without transitioning to Idle.
The ε (epsilon) represents an empty transition, that is, a transition that is taken as soon as the activity associated with the source node is over, without any event. This is what you mean by "sequential" state machines.
I am also including the sources to the first diagram (the second one is very similar), generated with the very easy and powerful PlantUML.
#startuml
skinparam shadowing false
title Embedded v1
state Startup
state StartupError
state Idle
state Program
state Check
state Copy
Startup : entry/ init HW
StartupError: entry/\n display error\n hw_fault = true
Idle : do/ if not hw_fault: poll
Program : do/ program
Check : do/ check
Copy : do/ copy
[*] -> Startup
Startup -> StartupError : ε [error]
Startup --> Idle : ε [not error]
StartupError --> Idle : ε
Idle --> Program : program_btn
Idle --> Check : check_btn
Idle --> Copy : copy_btn
Program --> Idle : ε
Check --> Idle : ε
Copy --> Idle : ε
#enduml
I have been using State machine based design tools for some time, and have seen UML modeling tools that allow you to execute your logic (call functions, do other stuff) inside a state. However, after spending a couple days with IAR VisualState, it appears that you cannot execute your logic inside a state without a trigger. I am confused as it does not make sense TO HAVE A TRIGGER for every single action inside a state !
Here is what I expect from a state chart tool:
If I enter StateA, upon entering the state I set my values in entry section, then I would like to call a function (I just want to call it, NO TRIGGER), and inside that function, I want to trigger an event based on some logic, and that event would trigget state transition from StateA to StateB or StateC.
Is there something wrong with this expectation? Is it possible in VisualSTATE?
Help is greatly appreciated.
VisualSTATE imposes the event-driven paradigm, just like any Graphical User Interface program. Anything and everything that happens in such systems is triggered by an event. The system then responds by performing actions (computation) and possibly by changing the state (state transition).
Probably the most difficult aspect of event-driven systems is the inversion of control, that is, your (state machine) code is called only when there is an event to process. Otherwise, your code is not even active. This means that you are not in control, the events are. Your job is to respond to events.
Perhaps before you play with visualSTATE, you could pick up any book on GUI programming for Windows (Visual Basic is a good starting point) and build a couple of event-driven applications. After you do this, the philosophy behind visualSTATE will become much clearer.
Create 3 states: A, B, C where state A is a default state.
By entering state A, call action function [that sets you
variables a and b following some algorithm], followed by ^Signal1.
Entry/ action()^Signal1
Make a transition driven by Signal1 [will serve you as an event] from state A with 2 guards:
a <= b, transition to state C
a > b, transition to state B