3.4. Data
While the rule flow is designed specifically to allow you to create process control flows, you also have to plan it from a data perspective. Throughout the execution of a process, data is retrieved, stored, passed on and used.
To store run-time data while a process is executing, use variables. A variable is defined by a name and a data type. This could be something very basic, such as Boolean, int, or String, or it could be any kind of Object sub-class.
Define variables inside a variable scope. The top-level scope is that for the process itself. Sub-scopes can be defined via a composite node. Variables that are defined in sub-scopes can only be accessed by nodes within that scope.
Whenever a variable is accessed, the process will search for the appropriate definitive variable scope.
You are allowed to nest variable scopes. A node will always search for a variable in its parent container. If the variable cannot be found, it will look in that one's parent container, and so on, until the process instance itself is reached. If the variable cannot be found, a read access yields null, and a write access produces an error message, with the process continuing its execution.
You can use variables in these ways:
- you can set process-level variables when starting a process by providing a map of parameters to the invocation of the
startProcessmethod. These parameters are then set as variables on the process scope. - actions can access variables directly. They do so by using the name of the variable as a parameter name:
// call method on the process variable "person" person.setAge(10);
// call method on the process variable "person" person.setAge(10);Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can change the value of a variable via the knowledge context:kcontext.setVariable(variableName, value);
kcontext.setVariable(variableName, value);Copy to Clipboard Copied! Toggle word wrap Toggle overflow - you can make WorkItem and SubFlow nodes pass the value of parameters to the "outside world" by mapping the variable to one of the work item parameters. To do so, either use a parameter mapping or interpolate it into a String parameter, using
#{expression}. You can also copy a WorkItem's output to a variable via a result mapping. - various other nodes can also access data. Event nodes, for example, can store the data associated with an event in a variable. Exception handlers can read error data from a specific variable. Check the properties of the different node types for more information.
Finally, every process and rule can access globals. These are globally-defined variables that are considered immutable with regard to rule evaluation and data in the knowledge session.
You can access the knowledge session via the actions in the knowledge context:
kcontext.getKnowledgeRuntime().insert( new Person(...) );
kcontext.getKnowledgeRuntime().insert( new Person(...) );