How to change variable (parameter) values in a running project
Users often come across the need to change a variable value in a running project. Actually, what they usually ask is how to change the value of a parameter – but we need to be clear on terminology:
CCL has variables and parameters. Parameter values are set when the project is started and can’t be changed while the project is running. The values of variables, however, can be changed at any time….so we’re really talking about variables here.
Now the question here is how do you let a user or an application change a variable value. This is a bit different from having an event source where the events themselves affect the values of variables. Let’s take a simple example: let’s say you have an ESP project that generates an alert whenever the moving average of an event stream is greater than n, but you want an application (or a user, via an application) to be able to change the value of n without restarting the project. How do you do that?
First, declare a variable (or several variables) in your ESP project. Then, to change the value of that variable from outside the project, there are a couple of approaches:
1. You can use the ESP “command and control” utility: esp_cnc. This is a command line utility that can be executed manually or from a script or application. Here’s an example of the command to set the value of the variable “var1” in project1 to 10, where this project was started from the ESP Studio on the local cluster. Change the path as appropriate for your install location and substitute the password you create for the local cluster for <pwd> below.
C:\Sybase\ESP-5_1\bin\esp_cnc -C setParam -p localhost:9786/default/project1 -c studio:<pwd> -P var1:10
2. The other approach is to send new values in as events via an Input Stream. This may be easier if you are building an application that already uses the ESP SDK to send events to ESP and/or subscribe to output from ESP. Below is a simple example of an ESP project that defines two global variables – var1 and var2 and watches for new values for them on an input stream.
//declare 2 global variables and a global setvar() function that changes their values
integer var1 := 1;
integer var2 := 1;
integer setvar (string varName, integer value)
if (varName = ‘var1’) var1 := value;
if (varName = ‘var2’) var2 := value;
//this stream receives events that are new values for the global variables
CREATE INPUT STREAM varValues
SCHEMA (varName string, value integer);
//this stream calls setvar() to change the value of global variables var1 or var2 based on an incoming event
CREATE LOCAL STREAM Stream1
varValues.varName as varName ,
setvar(varValues.varName, varValues.value) as value
FROM varValues ;
//this input stream receives “real” events
CREATE INPUT WINDOW EventsIn
SCHEMA ( ID integer , Value integer )
PRIMARY KEY ( ID ) ;
//this is just a demonstration of using the global variables var1 and var2 and applying them to incoming events
CREATE OUTPUT WINDOW OutWindow
PRIMARY KEY DEDUCED
EventsIn.ID as ID ,
EventsIn.Value * var1 as NewVal1,
EventsIn.Value * var2 as NewVal2
FROM EventsIn ;