-
Notifications
You must be signed in to change notification settings - Fork 0
Starting from a class
We start with the name of some class and then apply criteria to select out a subset of instances belonging to that class.
The fundamental form for selection is:
<class-name>( <cardinality symbol>, <filter-criteria> )
So we have a class name followed by required parentheses containing an optional cardinality symbol and optional selection criteria. The comma is required only if both items are present.
There are two cardinality symbols, 1 and * to select either one or all instances of a class. To select all instances, for example:
Airport(*) // get all instances of the Airport class
You can assign the selected instance references to a labeled instance flow like so:
airports ..= Airport(*) // all instances assigned to the airports flow
But you don’t have to assign the instance set to a labeled flow. You might, for example, nest the selection inside some expression that expects a set of input instance references.
The 1 cardinality symbol ensures that no more than one instance reference is produced. If no criteria is supplied, an arbitrary instance is selected or zero instances are returned if there are no instances in the class. Here we select an arbitrary instance:
some airport .= Airport(1)
If a class has no instances, the empty set is returned regardless of requested cardinality.
Any positive integer value greater than or equal to zero may be specified as a cardinality. Note that the 1 and * values are keywords since they are the primary use cases. In rare circumstances, it may be desirable to supply a value using a scalar variable. Here we select a specific quantity of instances.
aircraft group ..= Aircraft( group qty )
The cardinality of the result in the above example will be equal to or less than the supplied cardinality.
Criteria can be specified to filter the instance population of a class and return the corresponding instance references.
airports in my country ..= Airport( Country : my country )
Only those instances of Airport whose value for the Country attribute match the value of the my country scalar variable are selected.
More generally, each instance of Airport is compared against the supplied critiera. The set of all instances that meet the critiera are output.
In the context of a filter criteria expression, the : symbol reads as “is”, “equals” or “matches” and the ; symbol reads as “and”. You can always substitute == and the and keyword, but these may add visual clutter, especially when multiple attributes are involved.
To improve readability and, or, and not are defined as keywords in Scrall. (You can also use ! in place of not). These keywords are familiar and short and quick to type. So, you can name a variable thisAndThat or this_and_that or this andthat, but this and that is a really bad idea.
Supply an identifier to guarantee at most one instance selection:
origin airport .= Airport( Code : origin code )
where Code is considered unique among Airport instances, or
next point .= Point( x: xloc; y: yloc )
where each Point is understood to be unique by x y location.
In either case, zero or one instance will always be returned. Remember that the empty set can be returned if you select based on an identifier value.
Filter criteria can use the usual comparison operators if they are defined for the attributes involved in the comparison. For example, you can use these symbols: >, >=, <, <=, =, !=. Be careful, the data type of the compared values must support the desired comparison.
high aircraft ..= Aircraft( Altitude > high alt )
high and fast aircraft ..= Aircraft( Altitude > high alt and Airspeed > min fast speed )
Min and max comparisons are represented by the ^+ and ^- unary operators. They can only be applied to attributes with data types that support ordering operations.
fastest aircraft ..= Aircraft( ^+Airspeed )
slowest aircraft ..= Aircraft ^-Airspeed )
allbut fastest aircraft ..= Aircraft(*) - Aircraft(^+Airspeed )
Note the usage of the multiple instance reference assigner. Multiple aircraft may be flying at the same speed! If you only want one, specify 1 cardinality
More than one ordering comparison may be used and applied in the left to right order:
next tasks ..= Task(^-Time submitted)(^+Priority)
The selection above is the set of Tasks with the highest priority and then, among those, the set with the earliest submission time. So the ordering is significant. Note that the first ordering applied is rightmost.
In the previous example, multiple instances may be selected. There could be many Tasks at the same Priority, submitted at the same time, for example. But it is possible to select an arbitrary instance among those selected:
next task .= Task(1, ^-Time submitted)(^+Priority)
Here, both cardinality and selection criteria are specified. In this case, zero or one instance will be selected, but the zero case would only occur when the cardinality of Task is also zero.
You could also do:
next task ..= Task(^-Time submitted)(1, ^+Priority)
Only one highest priority tasks are chosen and then fed into the the time submitted ordering. If there are multiple highest priority tasks (all at the same priority) one or more of these may share the same least time submitted values and, hence, multiple instances could be assigned to next task.
For activities that return a value, such as a class method, you may want to return at most one instance.
Let’s say, for example, the earlier example was in a class method that returns at most one task instance reference. In that case you could write this:
=>> Task(1, ^-Time submitted)(^+Priority)
By ensuring that the leftmost selection specifies a 1 cardinality, we know that at most one instance reference will be output.
The =>> symbol means “return”. The calling action should be able to handle a returned empty set.
A method that returns a value may be used just like an attribute in a selection expression.
If a class method returns a scalar value of a type that supports ordering operations, the ^+ and ^- unary max min can be applied. Here’s an example where the lowest duration returned by a method is used as the selection criteria.
cabin to call .= Cabin(1, ^-Estimated delay( Destination: Floor ) )
Here, each instance of Cabin invokes its Estimated delay( Destination: Floor ) method to yield an order-able time duration value. A reference to the instance returning the least value is assigned to the instance set variable.
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