Skip to content

Reading an attribute

Leon Starr edited this page Sep 5, 2021 · 2 revisions

The dot notation is used to read or write an attribute.

If you are referencing the attribute of an instance in an instance set (variable or expression), you need to use the dot notation to access any of its attributes. For example:

cabin height = /R1/Cabin.Height

Attributes of the local instance need not be qualified. Assuming we have an instance of Aircraft and a single instance of some other Aircraft in a target variable we could write:

closing speed = target.Air speed - Air speed

The Speed element is understood to represent the local Aircraft instance as distinct from the target Aircraft’s speed.

This means that no local variables may be named to match any local attribute. So, if you have a class called Aircraft with attributes Tail number, Altitude, Airspeed and Heading, you cannot use any of those names for local variables.

Assignment to a scalar variable

A scalar variable can be assigned an attribute value from an instance reference set that contains exactly one instance. Let’s say that an instance of Shaft wants to save the height of its Cabin.

cabin height = /R1/Cabin.Height

If the RHS yields zero or many instances, there will be a runtime error. The above example is taken from the Elevator Case Study where there is an unconditional to-one association and it is assumed that all instances have been created at initialization. Therefore, there is no need to check in this case.

If, on the other hand, it is possible for the RHS to yield the empty set, you should verify that this is not the case before making the scalar variable assignment.

Here, an Aircraft instance has a 1:0..1 association with an assigned runway. So we must verify that the my runway variable is not empty before performing the scalar assignment.

my runway .= /taxi to/Runway
my runway? takeoff heading = my runway.Heading : norunway -> me

Or you could do this:

found runway = /taxi to/Runway
// implicit conversion to boolean
(found runway)? takeoff heading = my runway.Heading
(!found runway)? norunway -> me

Note the use of the = assignment operator instead of .=. Since the empty set is false and one or more instances true, we can perform an implicit conversion to a boolean scalar variable. There isn’t much point in doing this here since you could just evaluate the instance set as we did in the previous example. But, in a more complex activity, you may find a need for a boolean variable assigned in this manner.

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