9. System Declaration¶
A system is declared as follows:
<system> ::= system <word> "{" <system_elt>* "}"
<system_elt> ::= <instance> | <assignment>
The first word
is the system’s name. A system is composed of instance
declarations and assignments. Assignments are used to assign an instance to
an instance’s reference slot. The following illustrates a system declaration:
system name {
// body
}
9.1. Instance declaration¶
The syntax to declare an instance in a system is:
<instance> ::= <path> [ "[" digit* "]" ] <word> ";"
The first word
is the instance’s class name and the second is the
instance’s name. For example, if we have a class A
we could declare
the following instance:
A an_instance;
We may want to declare arrays of instances. To do so we need to add [n]
as a suffix to the instance’s type, where n
is the number of instances
that the array should contain. if n = 0
then we can simply write []
.
// An empty array of instances
A_class[] a_name;
// A array of 5 instances
A_class[5] another_name;
You can also specify values for parameters when instantiating a class (see Section 5.3 on how to parameterize CPTs). The syntax to do so is:
<instance> ::= <path> <word> "(" <parameters> ")" ";"
<parameters> ::= instanceParameter ("," instanceParameter)*
<instanceParameter> ::= <word>"="(<integer>|<float>)
An example:
// We declare an instance of A_class where a_param equals 0.001
A_class a_name(a_param=0.001);
9.2. Assignment¶
<assignment> ::= <path> += <word> ";" |
<path> = <word> ";"
It is possible to add instances into an array, using the +=
operator:
// Declaring some instances
A_class x;
A_class y;
A_class z;
// An empty array of instances
A_class[] array;
// Adding instances to array
array += x;
array += y;
array += z;
Reference assignment is done using the =
operator:
class A {
boolean X {[0.5, 0.5]};
}
class B {
A myRef;
}
system S {
// declaring two instances
A a;
B b;
// Assigning b's reference to a
b.myRef = a;
}
In the case of multiple references, we can either use the =
to assign
a whole array or the +=
operator to add instances one by one:
class A {
boolean X {[0.5, 0.5]};
}
class B {
A myRef[];
}
system S1 {
// declaring an array of five instances of A.
A[5] a;
// declaring an instance of B
B b;
// Assigning b's reference to a
b.myRef = a;
}
// An alternative declaration
system S2 {
// declaring three instances of A
A a1;
A a2;
A a3;
// declaring an instance of B
B b;
// Assigning b's reference to a
b.myRef += a1;
b.myRef += a2;
b.myRef += a3;
}