Skip to content

Instance flow

Leon Starr edited this page Nov 6, 2023 · 1 revision

Instance flows

An instance reference is defined as a set of one or more values corresponding to each attribute of a class identifier. There is no concept of an internally managed handle or object id as these would leak implementation artifacts into the language. Set theory does not define or specify handles.

So the only way to refer to an instance of a class is by supplying a complete set of values for one of that class’s identifiers. Later, when we talk informally about assigning an instance to a variable we really mean assigning an instance reference.

An instance flow conveys a set of zero or more instance references typed by a some class. It corresponds to the metamodel definitions of both a Labled Flow and an Instance Flow.

An instance flow is implicitly declared to refer to instances of a particular class by virtue of its initial assignment.

Instance reference assignment

The .=, ..= operators assign a value to (flow a value into) an instance flow.

The intended cardinality (number of instance references) is signified by the choice of assignment operator. We say “intended” because the choice is generally determined by the operation on the RHS which produces the value. For readability though, it is helpful to indicate the desired cardinality so that, at a glance, the reader grasps the intended content of the variable. It is also helpful to force the modeler to be as explicit as possible about their intentions to reduce the likelihood of error.

In all cases, the LHS is an instance set variable or an input flow expecting the intended cardinality.

Let’s examine each of these operators.

Assign zero or one instance

The .= assignment (assign-zero-one) operator requires an expression on the RHS which produces one or zero instance references. An error will result if the expression can (or does) yield more than one instance.

You would use this operator when you traverse a relationship path along a 1:1 association. Or, you would use this operator if you select an instance of a class with an identifier. You should not use the assign-zero-one operator in any case where you could legally select more than one instance.

Example: single instance assignment

Selection using values supplied for an identifier is a common idiom in Scrall. Consequently, the syntax has been streamlined for this action.

Here, a local Pilot instance locates its related Aircraft instance using an aircraft id value passed in as an input parameter.

my aircraft .= Aircraft( Tail number: ^aircraft id ) // ^ indicates input parameter

Assuming that an identifier of the Aircraft class is Tail number, it is known that the selection must yield zero or one instance. Use of the .= assignment operator implicitly defines my aircraft as an instance set variable. The type of the variable is established by the class name used in the selection expression on the RHS.

If the aircraft is not found, zero instances are assigned and the my aircraft variable holds the empty set of Aircraft instances. You cannot, in some later assignment, assign instances of a different class to this variable. The empty set evaluates to false in a predicate:

my aircraft .= Aircraft( Tail number: ^aircraft id )
my aircraft? {
   // at least one aircraft
} : {
    // no aircraft
}

Or you can explicitly compare against the empty set of Aircraft instances:

( my aircraft != Aircraft(0) )? { // if not empty
}

In this selection example the class has a multiple attribute identifier:

assigned runway .= Runway( Number: directed runway; Airport: local airport code ) // ; shorthand for AND

Once again, one or zero instance will be assigned.

Assign zero, one or many instances

The ..= assignment (assign-many) operator assigns any number of instance references, including zero, from the same class.

You might use this operator to select instances based on criteria that could apply to multiple instances. You would also use this operator if you traverse associations that lead to multiple instances.

You can use ..= in any situation where a .= would work without any error occurring. But it is preferable to be as precise as possible about your intentions.

Example: Multiple instance assignment

Multiple instances may be selected and assigned with the ..= operator.

low flying aircraft ..= /R3/Control Zone.Aircraft( Altitude < low alt )

All instances of Aircraft whose Altitude attribute value is below the specified altitude are assigned to the low flying aircraft variable. This could be zero, one or many instances.

The Aircraft selection below may return zero, one or many instances:

aircraft to land ..= Aircraft( Zone: Code; (Altitude < landing altitude AND Ready to land) )

In the statement above, the set of all Aircraft instances such that each instance has a value for its Zone attribute matching the value of the local instance’s Code attribute (think of it as self.Code, if that helps) and an Altitude attribute value less than the value of the landing altitude scalar variable and a true value for the Ready to land attribute is assigned to the LHS.

Assign at most one

To assign at most one, just use the .= assignment operator in conjunction with a selection quantity of at most one.

Example: Arbitrary instance assignment

To select any instance of the Aircraft class or zero if none exists, you can specify this assignment:

some aircraft .= Aircraft(1) // select at most one

Here are some examples of sorting based on some criteria and then choosing one of the highest or lowest ranking instances:

some high flying aircraft .= Aircraft(1, Altitude: > high alt )
lowest fastest aircraft .= Aircraft(1, ^-Altitude; ^+Airspeed )
fastest lowest aircraft .= Aircraft(1, ^+Airspeed; ^-Alititude )

In all of these examples the RHS may yield zero or multiple instances.

Assigning the empty set

Using any instance set assignment operator, you can initialize an instance flow to an empty set of some class as shown:

pilots aircraft .= Aircraft(0)  // Empty set is assigned

In this case the single instance assignment operator was used since it reflects the usage of the variable on the LHS. Remember though, that the LHS variable can only be assigned instance references for Aircraft in the future since the variable is declared, implicitly, as holding Aircraft instance references.

Introduction

Model semantics

Flows (as Variables)

Constants and literals

Structure of an activity

Accessing the class model

Data flow


Grammar and parsing notes

Components

Clone this wiki locally