Guard Conditions in SysML v2¶
A guard condition is a boolean expression that controls whether a state machine
transition fires. The condition must evaluate to true for the transition to
occur.
Canonical Keyword: if¶
The official OMG SysML v2 specification uses the keyword if for guard
conditions. The word guard exists only as a metamodel enum value name
(TransitionFeatureKind::guard) — it is not a keyword in the concrete
syntax.
Correct syntax¶
Mandatory keyword order¶
transitionkeywordfirst <source-state>accept <trigger>(optional — must come beforeif)if <guard-expression>(optional — must come afteracceptand beforethen)do <effect>(optional)then <target-state>
Examples¶
Guard with signal trigger and effect¶
From the official OMG Annex A Vehicle Model:
transition off_To_starting
first off
accept ignitionCmd : IgnitionCmd via ignitionCmdPort
if ignitionCmd.ignitionOnOff == IgnitionOnOff::on and brakePedalDepressed
do send new StartSignal() to controller
then starting;
Guard with trigger only¶
From the OMG validation examples:
transition 'off-starting'
first off
accept 'Vehicle Start Signal'
if vehicle1_c1.'brake pedal depressed'
do send new 'Start Signal'() to vehicle1_c1.vehicleController
then starting;
Guard-only transition (no trigger, no effect)¶
Guard with compound condition¶
From Advent of SysML v2:
accept StartMission via hmi
if reindeerCount == 9 and totalPower >= 36 [SI::kW]
do send new Command("Gee-up!") via eyelet
then mission;
Guard on a trigger-less transition¶
Change trigger (not a guard, but achieves similar effect)¶
Action-level conditional succession¶
Guard conditions also work at the action flow level, not just state transitions:
Common Mistakes¶
Mistake 1: Using guard instead of if¶
// WRONG — 'guard' is not a keyword in the spec
transition first S1 guard condition then S2;
// CORRECT
transition first S1 if condition then S2;
(The sysmlpy parser accepts both if and guard as a compatibility
extension, but the canonical keyword is if.)
Mistake 2: Putting if after then¶
// WRONG — guard must come BEFORE the target
transition first S1 then condition guard S2;
// CORRECT — guard comes before then
transition first S1 if condition then S2;
Mistake 3: Putting accept after if¶
// WRONG — accept before if per spec
transition first off if enabled accept TurnOn then on;
// CORRECT
transition first off accept TurnOn if enabled then on;
References¶
- OMG SysML v2 specification, Section 8.2.2.18.3 "Transition Usages"
- OMG Issue SYSML2_-411 — confirms
acceptmust precedeifin concrete syntax - OMG SysML v2 Release: https://github.com/Systems-Modeling/SysML-v2-Release
- Advent of SysML v2: https://github.com/sensmetry/advent-of-sysml-v2
- Sensmetry SysML Cheatsheet: https://sensmetry.com/sysml-cheatsheet/