UML State Machine ShallowHistory - uml

I'd like to know whether I have correctly understood the ShallowHistory syntax or not.
Is this the right way to use it?
In the UML spec. it is said it can be used instead of the initial psuedo-state. I guess that in that case there would be no way to reset the State1's memory while in this case the transition from State0 always starts from State1.1. Am I right?

Your interpretation seems correct. From the Superstructure:
Upon entering a composite state, the following cases are differentiated:
• Default entry: Graphically, this is indicated by an incoming transition that terminates on the outside edge of the composite state. In this case, the default entry rule is applied (see Semantic variation point (default entry rule)).
And
Semantic variation point (default entry rule)
If a transition terminates on an enclosing state and the enclosed regions do not have an initial pseudostate, the interpretation of this situation is a semantic variation point.
In some interpretations, this is considered an ill-formed model. That is, in those cases the initial pseudostate is mandatory. An alternative interpretation allows this situation and it means that, when such a transition is taken, the state machine stays in the composite state, without entering any of the regions or their substates.
And finally:
Shallow history entry: If the transition terminates on a shallow history pseudostate, the active substate becomes the most recently active substate prior to this entry, unless the most recently active substate is the final state or if this is the first entry into this state. In the latter two cases, the default history state is entered. This is the substate that is target of the transition originating from the history pseudostate. (If no such transition is specified, the situation is ill-defined and its handling is not defined.) If the active substate determined by history is a composite state, then it proceeds with its
default entry.
Note that from the last paragraph, it seems you should ALWAYS have a transition from the H pseudostate, at least to the same state pointed by the initial pseudostate, otherwise you may have an ill-defined machine.
I didn't find where it says that you can use H* instead of the initial pseudo-state. Where did you see this?

Related

Handling Failures in State diagram

I have a system with 3 states. I wanted to handle failures. That is, when the system reboots, it doesn't know the state it's in. Is the following state diagram correct?
This not a valid UML State Machine Diagram for several reasons:
The start node is the wrong symbol. It should be a bullet.
The arrows fork. Each arrow (transition) should begin and end on a node.
The Y and N don't have square brackets.
Regarding the semantics:
The decisions don't have meaningful text (should refer to previously stored state). They may be combined to one decision "storedState = " which has four outgoing transitions guarded as [S1], [S2], [S3] and [empty].
The actions to store the state in persistent storage, in order to be restored in case of crash, are not present.
In case all decisions yield N, the object is destroyed immediately, instead of ending in some default state.
I don't understand the intention of A1, A2 and A3.
Perhaps it would be good to first show the diagram without reboot logic and then tell us what you try to add to that diagram to handle the failures.

Is a state without a direct transition allowed in UML state diagram?

Is a state without a direct transition allowed in UML state diagram (SD) like the below drawn with StarUML?
State1 is not directly involved in any transition so I'm in doubt wether this is allowed in UML / desirable. I think in my application I'm actually modeling multiple objects in a single SD.
In short
Based on the UML 2.5 specifications, this kind of diagram is perfectly valid. Nevertheless, by deduction, we can understand that it is not the best approach.
The details : why it is valid
According to the definitions (section 14.2.3.4.1):
State1 is a composite state made of exactly one region.
State2 and State3 are simple states, and in this case they are also direct substates of State1
A first answer to your question is suggested by the rules on entering a state (section 14.2.3.4.5):
Explicit entry: If the incoming Transition or its continuations
terminate on a directly contained substate of the composite State,
then that substate becomes active and ...
This is also reinforced in the rules related to regions (section 14.2.3.2), and more precisely to their activation:
Either a region starts with its own local initial pseudo-event (that is automatically activated when the enclosing state is activated, or one its "orthogonal" regions (i.e. concurrent in the same composite state) is activated.
Or a region starts at an explicit state (substate) if the region is activated by an entering transition:
an explicit activation occurs when a Region is entered by a Transition
terminating on one of the Region’s contained Vertices.
So your diagram is perfectly valid, with an explicit transition from initial state to the substate State2.
The details: why it is not recommended
First of all, it is suggested (section 14.2.4.5.1) that it may help in some case to hide the decomposition of a composite state:
With explicit activation, such hiding would require to draw a variation of the model, showing a transition from initial to State1 instead of the direct transition to State2.
With default activation, you'd have only one model: initial to State1 and State1::initial to State2. Up to you to show or hide the details. Or to zoom into the region ignoring its context.
Then, if you'd later need to extend your composite state with multiple "orthogonal" regions:
with default activation, you'd just add additional regions with their own default activation.
with explicit activation, you'd have an asymmetry between one (explicitly activated) region, and the other regions (that require default activation).
alternatively, you could consider having multiple explicit activations. But this is not well supported: first there is no transition with multiple targets, second, multiple transitions from the (external) source state to the different (internal) target substates, would be ambiguous in regard of the semantics and execution model of transitions.
The UML specs warn that if default activations are missing, one should consider the model as ill-defined, or that the region will never start. So it's safer to use a systematic approach, and always use default activation.
State1 subsumes State2, so an indirect transition does exist for State1. That diagram is equivalent to having a transition to State1 with a default transition to State2, which would be overly messy.
The diagram is a valid UML state machine diagram of a single object (not two objects, as you suspected). However, State1 is not useful, because the object will always be in State1 during its entire lifetime. While in State1, it will either be in State2 or in State3 as well.

In a UML state machine, can an initial pseudostate have incoming transitions?

In UML 2.5.1, the initial pseudostate of a state machine is defined as follows:
An initial Pseudostate represents a starting point for a Region; that
is, it is the point from which execution of its contained behavior
commences when the Region is entered via default activation. It is the
source for at most one Transition, which may have an associated effect
Behavior, but not an associated trigger or guard. There can be at
most one initial Vertex in a Region.
In other words, a UML state machine should almost always contain exactly one initial pseudostate, which should have exactly one outgoing transition.
However, can an initial pseudostate have incoming transitions as well? For example:
I cannot find anything forbidding it in the UML specification, yet I cannot find any example online where this case happen, therefore I was wondering whether or not I overlooked anything.
EDIT: To go into more detail, if we look into the OCL constraints stated in the specification, we can only find the following one that affects outgoing transitions (section 14.5.6.7):
inv: (kind = PseudostateKind::initial) implies (outgoing->size() <= 1)
but I cannot find any constraint regarding incoming transitions
EDIT2: I have just realized that my model is wrong! Considering this sentence of the specification (cited above): "It is the source for at most one Transition, which may have an associated effect Behavior, but not an associated trigger or guard."
Therefore the transition between init and s1 should actually have zero triggers, instead of having e1 as a trigger.
Note that while this does not invalidate the initial question.
I see nothing in the UML 2.5.1 Specification that prohibits a transition whose target is the initial pseudostate.
Such a transition would be meaningless at best and confusing at worst, which is likely why no examples are found.
Edit: see the comments!
On p. 423 UML 2.5:
15.7.18 InitialNode [Class]
15.7.18.4 Constraints
• no_incoming_edges
An InitialNode has no incoming ActivityEdges.
inv: incoming->isEmpty()
N.B. If you intend to have a self-transition for e1 then why not just using that? The Initial can anyway have only on singular outgoing edge, namely to the first state (here s1).
No this is not allowed. And why would one Do that? As you already stated in the cited text,it can only have one outgoing edge without any guard. So what is the added value, as you cannot reuse anything.
I think the text is pretty clear as-is: "[An initial Pseudostate] is the point from which execution of its contained behavior commences when the Region is entered via default activation." If you connect a transition back around to the initial psuedostate, the initial psuedostate is no longer "the point from which execution of its contained behavior commences," it is something else, and is therefore undefined.

Finite State Machine: One State to Multiple States

I'm writing a simple finite state machine and realized that there are situations where an event can take a state to more than one possible results. Basically, from state A, if Event E happens, the state could be either C or D.
I'm currently using the Javascript Finite State Machine code written here: https://github.com/jakesgordon/javascript-state-machine
From the documentation I don't see an obvious way that makes this possible. More so, I feel like maybe this is actually a flow in my original design.
Essentially, in a Finite State Machine, should there be a situation where a transition happens, and based on some logic result in one of multiple states (1 to many), or should it be that we check the logic to see which transition needs to takes place (1 to 1)?
Congratulations, you've just discovered non-deterministic finite state machines! The ideas are similar to that of a deterministic state machine, except that there may be multiple ways to transition from a state given the same input symbol. How this is actually done is unspecified (randomness, user input, branch out and run them all at once, etc.).

UML behavioral state diagram: entry and exit point ownership implications for orthogonal states

In UML (let's take specification 2.4.1), when considering orthogonal composite states, entry and exit points belong either to the (enclosing) composite state or to the region the entry / exit point is drawn in. So which is it?
I cannot find this explicitly in the specification, but this is what the spec says about the subject (taken from paragraph 15.3.8 of the superstructure specification): "An entry point pseudostate is an entry point of a state machine or composite state. In each region of the state machine or composite state it has at most a single transition to a vertex within the same region."
From this I infer that entry points belong to the region they are applied to. If so, how do I model entry / exit actions for the enclosing state? Do I have to repeat a 'state-wide entry action' on every entry point I use? This seems cumbersome and redundant when using many regions. Or do I have to create a 'greater' state that has its own entry / exit points (or 'entry / ...' and 'exit / ...' lines), which in turn encloses the composite state which contains the regions? This seems rather complex. Can somebody clarify this for me with a visual example?
If you want to model actions that are executed before entering the orthogonal regions, yes: you need to create an enclosing state (See Fig. 15.35 "Orthogonal state with regions" in 2.4.1 superstructure).
From clause 15.3.11:
A composite state either contains one region or is decomposed into two
or more orthogonal regions. Each region has a set of mutually
exclusive disjoint subvertices and a set of transitions. A given state
may only be decomposed in one of these two ways.
On the other side, if all you need are entry/exit pseudostates, you can avoid that additional complexity. See the paragraph about Composite state, its Description and Semantic variation point (default entry rule) in clause 15.3.11.

Resources