what the UML way to describe a master/slave state machine interaction - uml

My system has two state machines, one master, one slave. And the states between the master and the slave are not one to one. The possible interactions are as follows.
If the master transits to certain master state, it will notify the slave to transit to certain slave state
The slave may request the master to enter certain master state; if master succeeds, the master will notify the slave to enter certain slave state.
The slave may request certain data from master
What is the proper UML diagram to describe those interactions? Statechart diagram is limited to state transition only and can't describe the request from the slave(either state transition or data); Interaction diagram is limited to class/object and can't applied on state.

You can describe those interactions using two UML state machine diagrams, one for the master and one for the slave. For example, to specify that a transition from state Idle to state Busy of the slave takes place upon receipt of signal Req from master, you draw a transition from Idle to Busy with transition label Req in the slave's state machine diagram. To specify that master sends signal Req when it enters state X, you write entry/Req inside the state symbol of X in the master's state machine diagram.
Alternatively, you may draw elements for sending and receiving signals (see sections 14.2.4.8.2 to 14.2.4.8.5 of UML 2.5.1 specification):
For example, to go from Idle to Busy upon receipt of signal Req with parameter id:
You also described the situation where the slave requests certain data from the master. For this, you need a third diagram (or a textual specification) of the interface provided by the master and required by the slave, for example:
In the slave's state machine diagram, you can call an operation defined in the interface. For example, to call the read operation of master when the slave enters state Busy and store it in local variable x, write entry/x:=master.read() inside the state symbol X.
For an introduction to the UML state machine diagram, please refer to wikipedia

What you describe is the interaction between the master and the slave.
There is no single solution to that and it is really difficult to have at the same picture both all possible states and the logic of the transaction (actually I don't think it is possible at all).
The main diagrams to be considered here would be:
Sequence diagram depicting the process of requesting/changing states (you may have few showing various triggers for the change and use alt/opt to show further behaviour)
Activity diagram depicting how the process of state changing is handled
Interaction overview diagram with state changes being shown at the "activity" level and negotiation between the master and the slave in the "sequence" parts.
Especially the last one is an interesting option here as it combines possibilities of activity and sequence diagrams. Usually it's an overkill but might be the best idea for your specific case.
In addition to those you may also use the state machine diagram (two, one for master and one for slave) but they will not be able to show the interaction between the two entities. It might be useful though to explain what are the available states.
If it is not clear feel free to comment this answer asking for more details. I will try building example of each of those diagrams then (activity diagram example you already have in the answer by www.admiraalit.nl).

Related

Clarification on FSM machine diagram

I have a simple state machine like the above. As far as, I looked over the online resources (example - x-sate) the state machine could have three different things "State", "Action Or Event" and "Side effect".
So Basically the event comes from outside of the state machine like User clicks, Reboot triggered etc., and the state machine should change its state based on those events. Also, each state could perform some side effects, but the side effect should not affect the state machine (for example, perform a cleanup task before Reboot)
Questions:
Is it allowed for a state to perform some operations(side effects) which could change the state of the state machine(fetch A, B, C in the above diagram) without any external event or action?

How can a parallel region change state of the other region in UML state machine diagram

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.

How to use a state chart as the flow chart for an agent

I have two processes I want to juxtapose. The first is a Manual workflow that is well represented by the Process library. The second is a software System that performs the same work, but is better modelled as a state transition system (e.g. s/w component level).
Now in AnyLogic, state models are for agents, that can run through processes with animations (counts), or move across space. What if I want to use a state chart to run an agent through? so I have a System state chart/agent and a Job state chart/agent?
I want Jobs from Population A to go through the Manual process flow chart and Jobs from Population B to go through the System state flow chart, so I can juxtapose the processing costs. I then calculate various delays and resource allocations for each of the Jobs going through and compare them.
Can anyone explain how to setup a state chart as the base process, another agent will go through? Is this even possible?
Please help
Thanks
This will not work as you would like it to, for these reasons:
You can't send an Agent into a flowchart. (Not sure how AnyLogic is handling it internally, maybe a generic token, or no flow at all, just changes to the state).
In AnyLogic there can only be one state active (simple or combined state) per state chart, so you can't represent a population with several members.
Agents can't be in more then one flow at a time, so even if it would be possible to insert an Agent into a statechart, this limitation would also apply.
The conclusion of this is: State charts are suitable for modeling individual behaviour (inside one Agent), whereas process flows can be both used for individual behaviour (inside one Agent, running a dummy Agent through) as well as for groups (multiple Agents running through process).
The normal use case would be to add the state chart to the Agent type running through your process flow (as you already noted in your question), applying the changes caused by the state chart to the individual agent.

State machine program design in FreeRTOS - Task managing the state changes

Following on the discussion at State machine program design in FreeRTOS - vTaskStartScheduler in a switch statement, I have a state machine with 4 states, each of them with different tasks reading sensor data.
When a sensor data reaches a threshold, its task has to cause a state change.
The idea is to have a superior task which controls the states (and suspends/resumes corresponding tasks) with a switch statement. My question is: how should the sensor tasks communicate the STATE with the superior task?
One proposed solution is to have a set_state function which is called by the event generating task, but I read that having global variables in FreeRTOS is discouraged. I thought to implement it through Queues:
1- Task1 detects sensor threshold, and sends STATE to a Queue.
2- Superior task is blocked waiting to receive data from Queue. When it receives STATE, the switch statement processes the state change.
If this approach is correct, my doubt relates on how and where should be STATE defined (global, or just existing in the stack of each task, or the superior task...)
STATE should exist only where it needs to, 'in' the superior task. I'm presuming you're using C, so declare STATE as a static variable in "superior_task.c" (or whatever it's called). This means it can then only be affected within that file - the C equivalent to a private member variable in C++.
Then, if your inferior tasks need to affect a state change, they post a state change event to a queue managed by the superior task. When the queue is processed, the superior state makes the change to the private STATE variable.
If other tasks need to know what the state is for their own processing, they can use an accessor for the private variable, e.g., State get_state() { return STATE; }. As Martin says though, the other tasks shouldn't need to know the state, as otherwise there's inter-dependencies between tasks that shouldn't exist.

UML state machine: How to exit orthogonal child regions?

Base on Wikipedia, I can have a hierarchical state decomposition, where I have multiple orthogonal regions, which can change state independently.
The diagram shows how the orthogonal regions are entered. I assume that the entry happens in parallel in all regions. What I want to know is, how do you express the exit? If each region has an exit, does the global parent state exits when the first child region exit, or when they have all exited? I want to express that the exit happens when they have all exited. And how do you express that the global parent state transitions to the next global state because all children regions have exited? Is that always implicit?
My concrete problem is that my FSM starts in the Initializing global state. It sends multiple queries to multiple external systems asynchronously in parallel. Only once it has received the answer to each query, it can then proceed. So I model each asynchronous parallel query as an orthogonal region inside the global Initializing state. Each child region can change state independently. When all child regions have reached an end-state, I can move on.
The FSM will be implemented in an Actor framework, where asynchronous messages (events) are the only way to communicate.
Ideally, I'd like the answer to point to an example image, as such things are difficult to express in words.
A completion transition from the orthogonal state will do the trick, since it will be taken when all of the orthogonal regions have finished.
When an composite state (state with inner states and/or orthogonal regions) is entered, each region starts at its initial state, to a different state in each region using fork pseudonodes, or to the last state of each region if the state is entered through a history pseudostate. Exiting the composite state can be done through a transition that starts at one of the inner states, which exists all of the orthogonal regions from their current state. If you want to exit when a the machine has come to a set of states in different regions, you can also use join pseudostates.
As you can see, the possibilities are almost endless. I can't add an image yet, but I'll do it later when I have a good UML editor at hand.
An now the example. Suppose you have the following state machine:
If both parallel regions finish (arrive at a final node), then the completion transition will take the machine from S1 to S5. If the
If the machine is currently at S3 and receives e1, it will exit S1 and go to S6.
If the machine is in S7 and S8 and they both finish, it will go to the join and then to S6.
This is the way I know state machines work.

Resources