Skip to main content

7. Behaviours

A behaviour is what an instance of a concept can do: a name, arguments, an optional return type and a body. Behaviours are declared in models, concepts and events.

7.1 Syntax​

behaviour_decl =
annotation* visibility? "create" "(" formal_arguments? ")" throws? body (* anonymous creation scheme *)
| annotation* visibility? "create" "::" lidentifier "(" formal_arguments? ")" throws? body (* named creation scheme *)
| annotation* visibility? "delete" "(" formal_arguments? ")" throws? body (* anonymous deletion scheme *)
| annotation* visibility? "delete" "::" lidentifier "(" formal_arguments? ")" throws? body (* named deletion scheme *)
| annotation* visibility? "abstract"? type? lidentifier "(" formal_arguments? ")" throws? body (* method *)
| annotation* "listen" reference_type "from" expression throws? body (* event listener *)
| annotation* visibility? type? lidentifier "(" formal_arguments? ")"
"with" behaviour_ref fml_parameters? throws? body (* technology-specific *)

throws = "throws" reference_type ( "," reference_type )* (* §8.10 *)

behaviour_ref = uidentifier | cidentifier "::" uidentifier

formal_arguments = formal_argument ( "," formal_argument )*
formal_argument = "required"? ( primitive_type | reference_type ) lidentifier ( "=" expression )?

body = block | ";"
block = "{" block_statement* "}"

The kind of a behaviour is determined by its introducer:

IntroducerKindRuns
create(…), create::n(…)creation schemewhen an instance is created with new
delete(…), delete::n(…)deletion schemewhen an instance is deleted with delete
type? name(…)action scheme (a method)when called
listen E from srcevent listenerwhen an event of type E is fired by src
… with Kind(…)technology-specific behaviouras defined by the technology

7.1.1 Methods​

public String greet() {
return "hello";
}

public String greet(String who) {
return "hello " + who;
}

public remember(String text) {
last = text;
}

A method has an optional visibility, an optional return type, a name and arguments. When the return type is omitted, as in remember, the behaviour acts on the instance and returns nothing. A method may be overloaded: two methods have the same name when their arguments differ (§7.4).

A method is called on an instance: talker.greet() from a script or from another unit, this.greet() from a behaviour of the same instance (§9.6.3).

7.1.2 Abstract methods​

abstract followed by a signature and ; declares a method without a body. Each concept that extends the declaring concept gives the body; a call goes to the body of the concept of the instance (§5.3.1).

abstract String sound();

A body of ; is the only form that declares a bodyless behaviour. For a method that is not marked abstract, ; yields a behaviour with an empty control graph.

7.2 Arguments​

An argument has a type, a name, optionally required and optionally a default value.

  • The type is a primitive type or a reference type (§4). A technology-specific type is not accepted as the type of a formal argument.
  • required marks the argument as mandatory: the application that asks for the arguments (a dialog, a palette) insists on a value for it.
  • A default value, = expression, is the value the argument takes when a call omits it (§7.4), and the value it starts from in such a dialog. Only trailing arguments can be omitted: an argument with a default value may be left out of a call only if every argument after it is left out too.
create(required String label, int capacity = 10) {
this.label = label;
this.capacity = capacity;
}
Implementation note

A default value does not allow a call to omit the argument: new Shelf("x") against create(required String label, int capacity = 10) does not resolve, is reported cannot find constructor, and yields null at run time (CORE-D-4). Every argument is passed at every call. To offer a shorter call, declare a second, named creation scheme.

7.2.1 Reading the arguments​

An argument is designated by its name in the body of its behaviour, as a local variable is, and can be assigned:

public int add(int a, int b) {
return a + b;
}

An argument hides a property of the concept of the same name, as a local variable does (§9.6.3): this.label reaches the property, label the argument. That is the usual form of a creation scheme (this.label = label;). A local variable cannot have the name of an argument: validation reports it.

Deprecated. Before 3.0 (CORE-F-14) an argument could only be read as parameters.a, and a bare name resolved to nothing. parameters.a is still evaluated, for the FML written before, but validation reports it as deprecated and proposes to rewrite it as a. FML written with the old notation that assigns a property of the name of an argument (label = parameters.label;) must be migrated: label now designates the argument, so the assignment must read this.label = label;.

7.3 Creation and deletion schemes​

7.3.1 Creation​

A creation scheme initialises a new instance. It runs when the instance is created with new (§9.7). A concept may declare several; at most one is anonymous, the others are named with create::name, and a new chooses among them by name (new Shelf::withDefaultCapacity(l)).

create(required String label, int capacity = 10) {
this.label = label;
this.capacity = capacity;
}

create::withDefaultCapacity(String label) {
this.label = label;
capacity = 10;
}

A creation scheme reaches that of its parent concept with super(args) (anonymous) or super.name(args) (named) (§5.3.2). A model that declares only a named creation scheme is instantiated by name (new Journal::start("Diary") with (name = "diary")); new Journal() does not resolve against it.

A concept that declares no creation scheme can be created without arguments.

7.3.2 Deletion​

delete() {
container.last = "note deleted";
}

A deletion scheme runs when an instance is deleted with delete (§10.4). A concept may declare several, told apart by name. Which of them delete x runs when there are several is not specified by this document; the closing clause of a matching set runs the first one that the concept declares (§10.9), which is why a concept that is closed by a matching set should declare exactly one.

Implementation note

Deleting an instance does not remove it from the multi-valued properties that reference it: take it out first (books.remove(book); delete book;) (CORE-D-10).

7.4 Calling a behaviour​

A call is an expression target.name(arguments) (§9.6.4). The behaviour called is selected by

  1. the name, and
  2. the number of arguments, which lies between the number of declared arguments without a trailing default value and the number of declared arguments (§7.2; currently it must be exactly the number of declared arguments, CORE-D-4), and
  3. the types of the arguments, to choose among behaviours of the same name and number.

The lookup starts at the concept of the instance the call is made on and goes up the inheritance graph; the most specific matching behaviour is called.

A behaviour of the same instance is called on this. A bare call, twin(), does not resolve: it is an invalid binding, an execution error when it runs (§9.8):

public String twin() {
return "twin";
}

public String bareCall() {
return twin();
}

public String thisCall() {
return this.twin();
}

bareCall() fails, thisCall() returns "twin". The rule holds for behaviours of a model called from the model.

A behaviour that is recursive calls itself the same way (this.fact(n - 1)).

Implementation note

Signatures are compared by parameter names: f(int x) and f(String x) are reported as duplicates, g(int x) and g(int y) are not. Among overloads, the first declared one whose parameters accept the arguments is called, not the most specific. The return type of an override and the implementation of an inherited abstract behaviour are not checked (CORE-D-49).

7.5 Return​

A return statement ends the behaviour and gives its value (§8.8). A return inside a loop ends the behaviour, which is how a search stops early.

7.6 Instantiation from a behaviour​

Inside a behaviour, new Shelf(…) creates an instance of the concept Shelf in the nearest object that can hold it, found by climbing the containment tree from the current instance: in a behaviour of a concept, a concept nested in it is created in the current instance, a concept of the model in the model instance, and a concept of the same kind as the current one beside it. container.new Tool(…) starts the climb from the container instead (chapter 14).

7.7 Event listeners​

listener = annotation* "listen" reference_type "from" expression body

A listener has no name and no visibility. It belongs to the instance that holds it: the instance listens to the source designated by from for as long as the instance exists. It runs when an event of the given type is fired by the object the from expression designates, which is typed as a virtual model instance. Inside the body, the variable evt is the event that was fired:

listen ShelfCreated from this {
lastCreatedShelf = evt.label;
}

The event type must resolve to an event or concept type; otherwise the semantic issue Unexpected event type is reported.

A listener with a body of ; is accepted and does nothing.

Implementation note

A listener inherited from a parent concept is registered twice for an instance attached to the engine in bulk, as happens when it is loaded, and then runs twice per event (CORE-D-55). Declare the listener in the concrete concept.

7.8 Technology-specific behaviours​

A behaviour declared with with Kind(parameters) is a behaviour of a kind that a technology adapter defines. It is how a model hooks into what happens in a federated resource: a drop on a diagram, a navigation, a synchronization. The Kind and its parameters are resolved among the behaviour classes of the adapters declared with use (§3.4), or by TA::Kind.

dropShape(required String label) with DropScheme(targetType = Diagram) {
log "dropped";
}

The fully qualified form with DIAGRAM::DropScheme(…) selects the adapter explicitly. The core platform defines no non-deprecated kind of this family; in particular the former synchronization scheme is deprecated and a behaviour named synchronization() { … } is a plain method.

Whether a kind resolves depends on the adapters installed (§3.4); when it does not, the unit is built with the behaviour unresolved and the problem is reported.

7.9 Annotations on behaviours​

Annotations precede a behaviour. @UI(…) describes how the application asks for the arguments (§11.4); any other annotation is kept (§11.2). A listen behaviour has annotations but no visibility.

7.10 Bodies​

A body is either a block or ;. A block is a sequence of block statements (chapter 8). Within a body

  • the variables in scope are, from innermost to outermost, the local variables, the properties of the concept, those of the enclosing concepts and model, and the built-in names this, container, parameters, super and, in a listener, evt (§9.6.3);
  • the instructions that act on models are the edition actions of chapter 10.

A body is not required to be non-empty. A creation scheme with an empty body creates an instance with its initial values and nothing else.