-
Notifications
You must be signed in to change notification settings - Fork 0
Case switching
The ? operator, when appended to a scalar variable typed with an enumeration, designates a decision based on the variable value, much like a case/switch statement that you might find in a programming language.
Since Scrall does not support literal values, cases are only represented by enumeration values. Consequently, you must specify a case for each possible value of an enumeration type.
vehicle traversing intersection ?
traffic signal? {
_red : take photo -> camera
: conserve power -> camera // yellow and green
}
In the above example, there is one explicit case red and then an “other” case which designates all other possible enumeration values. The traffic signal variable must have an enumeration type. You could also have done this for the second case:
_green, _yellow : conserve power -> camera
It is generally best practice to avoid use of the “other” case since its use makes it unclear that all other options were fully considered by the modeler.
Here, all possible values are made explicit:
valve position? {
_open : close -> my valve
_closed : open -> my valve
}
In the following example cases and guards and sequence tokens are combined:
dir? {
// update attribute value only if input dir is different
_up:
!Calling up? Calling up.set() <new call>
_down:
!Calling down? Calling down.set() <new call>
}
<new call> /R38/Bank Level.Request cabin()
Each case uses a local attribute value Calling up or Calling down as a guard. If the input value and attribute value match, the local attribute is set to true (via the .set boolean operation) and the new call sequence token is enabled. Otherwise, no change occurs and no further action is necessary. If the sequence token is enabled, the Request cabin() method is invoked.
If 'dir' is not of type Direction, there will be a static error. Therefore, there is no need for a default or fall-through case.
Consider cases on an enumeration with only two possible outcomes:
valve position? { .open : close -> my valve .closed : open -> my valve }
Now let’s say that we change the switch variable to a boolean scalar named valve open. You can then use a shorter guard expression:
valve open ? close -> : open -> my valve
Either formulation works.
A superclass instance’s activity may behave differently depending on the associated subclass instance. The ? symbol can be postfixed on a generalization relationship name to generate a case for each modeled subclass name on that relationship.
You can then switch based on the subclass as shown:
R1? {
_Fixed Wing Aircraft: // must be a subclass name on R1
// land on runway
_Rotary Wing Aircraft:
// land on helipad
}
If no instance is specified, the local instance is assumed. Otherwise, make the source instance explicit:
some aircraft/R1? {
// subclass cases
}
Copyright 2020, 2021, 2022, 2023, 2025 © Leon Starr under MIT Open Source License
- Why they are problematic
- Instance attribute creation values
- Boolean values
- Special values
- Enumerated values
- Action block
- Statement
- Single line action
- Multiple dependent actions on a single line
- An action spread across multiple lines
- A conditional group of single line actions
- Comments
- Finding instances
- Attribute access
- Creation and deletion
- Subclass migration
- Creating a table from a class
- Creating a table with a definition
- Converting a table into a class
- Set operations on tables
- Set comparisons on tables
- Join
- Rename
- Extend
- Aggregation
- Rank
- Image
- Input values
- Signatures and name doubling
- Output values
- Execution order
- Sequential execution
- Conditional execution
- Signals
- Scrall has no for_each action
- Iteration