Transitions¶
Transitions represent the path between two other objects and can be used to connects states and/or junctions together. Transitions are what make a system dynamic as they allow a system to move from one state to another.
In Weld, transitions are represented by an arrow from a source object to a destination. You can draw a transition by selecting the transition tool from the toolbar, then using left click hold to draw the transition between the source and destination.
Once you draw the transition between the source and destination, you can specify the transition characteristics by selecting the transition and navigating to the properties tab, where you can find the condition label of the transition.
Condition Label¶
The condition label follows a strict notation that determines the :
- Transition Event/Message guard
- Transition Condition guard
- Condition Actions
- Transition Actions
Event/Message Guard¶
The Event/Message Guards are the first gatekeepers of the transition which have to evaluate to a boolean True
for the transition to be valid.
The Event/Message Guards come inside the parenthesis after the "@", as in @(event_name && message_name)
. An event evaluates to True
if its flag has been set and a message evaluates to True
if it has at least one data in the queue.
Caution
Please note that:
- you should only use event and/or message names inside the event guard. Using any other statement or builtin API inside the guards will result in an error.
- Event/message guards DO NOT clear/discard events or messages. They are simply indicators on whether or not an event or message is available.
The events and messages in the event guard can be chained together just like the conditions inside an if
statement. For example, @((event_1 && event2) || message_1)
is a valid guard the evaluates to true
if event_1 and event_2 flags are set, or there is at least one message in message_1 queue.
Tip
The Event/Message Guard of a transition should evaluate to True
in order for transition conditions to be evaluated. If the Event/Message Guard yields a False
, the transition will be rendered invalid and the conditions will not be evaluated.
Condition Guard¶
The condition guard, is a conditional statement that should evaluate to a boolean true
for the transition to be valid. You can logically chain any type of statement inside a condition guard, just like you would inside an if
statement.
There are two ways to add condition guard to a transition:
- Using the
if(condition)
notation where you list the transition conditions inside an if statement - If the transition has no Guards and no Actions, you can just use the transition label to add your conditions. Meaning that you you do not have to use the
if(condition)
notation and you can just add the condition chain as the transition label.
Condition Actions¶
Condition Actions are snippets of code that get executed as soon as the a transition's guards and conditions evaluate to true
and they can be listed in curly braces {}
after if(condition)
statement. The code inside the first curly brackets in the transition label is assumed to be condition actions. So, if a transition has no guard and no condition, you can just list the actions inside a curly bracket and separate the lines using semicolons.
Note
Condition Actions get executed as soon as the transition's guards and conditions are evaluated to true
, regardless of what the transition's destination.
Transition Actions¶
Transition Actions are snippets of code that are executed after a transition has resulted in a state to become active. Transition actions basically become the Entry actions of the destination state and are executed before the actions of that state.
Transition actions are denoted in the second curly brackets {}
in the transition label. So, if you have a transition that has no condition actions but a Transition Action, you have to include an empty curly bracket, as the condition actions, i.e. if(condition){}{transition_action();}
To explain the Transition Actions further, lets assume we have two transitions chained together using a Junction and they lead the state machine from state_1 to state_2. If the first transition is rendered to be valid and the second is invalid, the transition actions of none of the transitions are executed, because the chain is not ended with a state. However, if both transitions are valid, the Transition Conditions of the transition are executed in order and before the entry actions of the destination state.
Note
Condition and transition actions, just like regular c code, can be separated using semicolons ;
. Everything between two semicolons will be evaluated as a single line of code.
Transition Priority¶
Each transition is Weld has a priority that denoted its order of evaluation with "1" being the highest priority. If there are several transitions going out of a source object, the highest priority route is evaluated first and it it does not end in a Destination State, the lower priority routes are evaluated after that. As soon an one transition is valid and it results in a state being activated, the lower priority transitions will be ignored.