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
DECLARE
integer var1 := 1;
integer var2 := 1;
integer setvar (string varName, integer value)
{
if (varName = ‘var1’) var1 := value;
if (varName = ‘var2’) var2 := value;
return value;
}
END;
//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
AS SELECT
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
AS SELECT
EventsIn.ID as ID ,
EventsIn.Value * var1 as NewVal1,
EventsIn.Value * var2 as NewVal2
FROM EventsIn ;
Hello Jeff,
there is a type error in your post, I think. In the setvar definition,
if (varName = 'var1') var1 := value;
it should be:
if (varName = 'var1') value := var1;
because we want to assign the value of 'var1' to 'value'. 😛
Best regards,
Tao
Actually, no, the intended function is as written. I can see where it looks a little odd, but the intent is that the custom function setvar() changes the value of one of the global variables based on the name of the variable passed to it. The 2 global variables are var1 and var2, so calling setvar('var1',10) would set var1 to 10. or calling setvar('var2', 20) would set var2=20.
The fact that I chose setvar to return value is sort of irrelevant to the use. I could have had setvar return a constant. We don't use that value for anything. The purpose of the setvar() function is to alter the value of one of the global variables - we don't actually care about what it returns.