handlers.computed_value_handler
Watch fields for value changes, and when one of the watched fields changes, set another field to a computed value.
This allows for lookups and mappings between multiple fields. Note that this functionality goes beyond the options
in ComputedFieldHandler; there, only lookups and mappings are possible. Here we have full computation available.
Configuration:
fs_types: list of fact sheet types, where this handler should run
depends_on_fields: a list of fields to be monitored for changes; if any of these fields change, the computation
will be performed
field_name: name of the field to be set
update_when_empty: also set the value of field_name when no new value or an empty value was computed
data: auxiliary data used during lookup and threshold operations
code: list of lines of code that will be executed during the computation
The code is called with two local variables: event and fs, which contain the update event and the currently
updated fact sheet, respectively. Additionally, there are two functions available: lookup and threshold. These
use the supplied data from the configuration.
The lookup function pulls the specified field from the fact sheet and applies the mapping as defined in data to
it, then returns the mapped value. For example, if in data, there is a field
"criterion1": {"low": 1, "medium": 2, "high": 3}, then the lookup("criterion1") will fetch the value of the
field criterion1 from the currently updating fact sheet, look up that value in the defined mapping and return the
value of that mapping. I.e., if fs['criterion1'] == "medium", the return value would be 2.
Similarly, the threshold function looks up the value of the given name in the fact sheet and applies a range
threshold using the auxiliary data. If for example, fs['criterion3'] == 2500, and
"criterion3": [1000, 2000, 3000], then the result of the thresholding would be threshold('criterion3') == 2.
Values that are smaller than the smallest value in the given range will result in 0.
Example:
{
"fs_types": ["Application"],
"depends_on_fields": ["criterion1", "criterion2", "criterion3"],
"field_name": "updateThisField",
"update_when_empty": False,
"data": {
"criterion1": {"low": 1, "medium": 2, "high": 3},
"criterion2": {"little": 1, "some": 2, "most": 3},
"criterion3": [1000, 2000, 3000],
},
"code": [
"value1 = lookup('criterion1')",
"value2 = lookup('criterion2')",
"value3 = threshold('criterion3')",
"return value1 * value2 * value3"]
}
This would look up values for criterion1 and criterion2, and map them according to the table; it would then
apply a threshold function to criterion3, and finally set updateThisField to the product of the resulting
values.