Allocating events
Note: The principles described here apply to variables of any type, not just to any event type or any reference type.
When writing monitors consider when and where to declare and populate event variables. You can declare event variables at the monitor level or inside an action. Event variables that you declare at the monitor level are similar to global variables.
Events are reference types. This means that, for example, a variable of event type Foo is not an instance of Foo. The variable is a reference to an instance of Foo.
You cannot initialize the fields of a monitor-level variable. You can, however, initialize a monitor-level instance of the event that the variable refers to. For example:
Foo a := Foo(1, 2.3);
This instantiates a Foo event and specifies that a refers to that event. Now suppose you declare the following:
Foo b := a;
This does not instantiate a new Foo event. It only initializes b as an alias for a.
When you declare an event at the monitor level, the correlator can automatically use default values for the event's fields. You can, but you do not have to, initialize field values. This is because the correlator implicitly transforms a statement such as this:
Foo a;
into this:
Foo a := new Foo;
Before you use a locally declared event variable in an action, you must either assign it to an existing event of the same type, or you must specify the new operator to create a new event to assign to the variable. Note that each event field of an event created using new initially has the default value for that event field type.
The following code illustrates these points:
event Foo
{
integer i,
float x;
}
monitor Bar
Foo a; // Global (monitor-level) declaration.
// The correlator creates a Foo event with default
// values for fields.
action onload() {
a.i := 10; // Assign non-default value.
a.x := 20.0; // Assign non-default value.
Foo b; // Local (in an action) declaration.
// The correlator does not create an event yet.
b := new Foo; // Create a default Foo event and assign
// it to local event.
b.i := 10; // Assign a non-default value.
b.x := 20.0; // Assign a non-default value.
Foo c := a; // You can assign a locally declared event to
// reference an existing event.
// Variables a and c alias the same event.
c.i := 123 // The value of a.i is now also 123.
Foo d := Foo(15,30.0);
// Create an event and also initialize it.
}