Apama Documentation : Developing Apama Applications : Developing Apama Applications in EPL : Defining What Happens When Matching Events Are Found : Using variables : Using local variables
Using local variables
A variable that you declare inside an action is a local variable. You must declare a local variable (specifying its type) and initialize that variable before you can use it.
Although the correlator automatically initializes global variables that were not explicitly assigned a value, the correlator does not do this for local variables. For local variables, you must explicitly assign a value before you can use the variable.
If you try to inject an EPL file that declares a local variable and you have not initialized the value of that local variable before you try to use it, the correlator terminates injection of that file and generates a message such as the following: Local variable ‘var2' might not have been initialized. EPL requires explicit assignment of values to local variables as a way of achieving the best performance.
When you declare a variable in an action, you can use that variable only in that action. You can declare a variable anywhere in an action, but you can use it only after you declare it and initialize it.
For example,
action anAction(integer a) returns integer {
   integer i;
   integer j;
   i := 10;
   j := a;
   return j + i;
}
You can use the local action variables, i and j in the action, anAction(), after you initialize them. The following generates an error:
action anAction2(integer a) returns integer {
   i := 10; // error, reference to undeclared variable i
   j := a; // error, reference to undeclared variable j
   integer i;
   integer j;
   i := 2;
   j := 5;
   return j + i;
}
Suppose that an action scope variable has the same name as a monitor scope variable. Within that action, after declaration of the action scope variable, any references to the variable resolve to the action scope variable. In other words, a local action variable always hides a global variable of the same name.
Consider again the definition for anAction2() in the previous code fragment, but with i and j variables declared in the monitor scope. The first use of i and j resolves successfully to the values of the i and j monitor scope variables. The second use occurs after the local declaration and initialization of i and j. That use resolves to the local (within the action) occurrence. This results in the following values:
*Global variable i is set to 10.
*Local variable i is set to 2.
*Global variable j is set to the value of a.
*Local variable j is set to 5.
Since you must explicitly initialize local variables before you can use them, the following example is invalid because j and i are not initialized to any value before they are used.
action anAction3(integer a) returns integer {
   integer i;
  integer j;
   return j + i; // error, i and j were not initialised
}
It is possible to initialize a variable on the same line as its declaration, as follows:
action anAction4(integer a) returns integer {
   integer i := 10;
   integer j := a;
   return j + i;
}
It is also possible to initialize a local variable by coassigning to it in an event listener. For example, the following is correct:
action onload() {
   Event e;
   on all Event():e {
     log e.toString();
   }
}
You can also initialize a local variable by coassigning to it from a stream. For example:
action onload() {
   float f;
   from x in all X() select x.f : f {
      log f.toString();
   }
}
Copyright © 2013-2017 Software AG, Darmstadt, Germany.

Product LogoContact Support   |   Community   |   Feedback