Apama 10.15.0 | Developing Apama Applications | Developing Apama Applications in EPL | Defining What Happens When Matching Events Are Found | Using variables | Specifying named constant values
 
Specifying named constant values
In a monitor or in an event type definition, you can specify a named boolean, decimal, float, integer, or string value as constant. The format for doing this is as follows:
constant type name := literal;
Element
Description
type
Specify boolean, decimal, float, integer, or string. This is the type of the constant value.
name
Specify an identifier for the constant. This name must be unique within its scope — monitor, event, or action.
literal
Specify the value of the constant. The type of the value must be the type that you specify for the constant.
Benefits of using constants include:
*Using a named constant can often be better than using a literal because it lets you define that constant in a single place. There is no chance of one instance becoming incorrect when the value is changed elsewhere. An alternative to using a constant would be to define a variable to contain the value. The disadvantage with this approach is that someone could accidentally assign a new value to the "constant", which would cause errors.
*A named constant can make code easier to read because the name can be meaningful in a way that a magic number, such as 42, is not.
*Constants appear in memory once. For example, spawning multiple copies of a monitor that contains a constant does not consume memory to store extra copies of the constant. A non-constant variable takes up space in memory for every copy of the event or monitor in the correlator.
You can refer to a declared constant in any code in the event or monitor being defined. When you define a constant in an event you can refer to it from outside the event by qualifying the name of the constant with the event name, for example, MyEvent.myConstant.
Following is an example of specifying and using a constant:
event Paper {
   constant float GOLDEN := 1.61803398874;
   float width;
   action getLength() {
      return GOLDEN * width;
   }
   action getWidth() {
      return width;
   }
}
You cannot declare a constant in an action.