Class ModelEvaluator


  • public class ModelEvaluator
    extends Object
    A ModelEvaluator exposes a previously prepared/trained/calculated model to non-PO modules (Groovy logics). The idea is that the internals / implementation details of the model are shielded, and that only the behaviour intended to be exposed is accessible to the outside world.

    Specifically, the evaluate(Map) method executes the model's logic in the evaluation calculation context. Depending on the ModelType definition, this could be a dedicated evaluation logic, or elements embedded in the model's main logic. In addition to evaluation, there is also a number of helper methods, which simplify getting objects related to (and usually generated by) a model.

    Example: The standard Segmentation's evaluation logic determines the segment for a given set of (segmentation attribute, value) pairs, and returns the PolicyRecord for that segment, valid on a given date. The record contains the guidance and key metrics for that segment that an external application can translate to, for example, a target price for a product quoted to a customer. The evaluation logic is seen as implementation detail to the model. But the standard way of calling it, by means of a ModelEvaluator is not, as it represents the contract between the model (or ModelType more generally) and users (clients) of the model.

    
     def eval = api.modelEvaluator(modelUniqueName)
     def levels = eval.params["Segmentation Levels"]
     def input = levels.collectEntries{ api.stringUserEntry(it) }
     def record = eval.evaluate(input)
     return record
     

    • Constructor Detail

      • ModelEvaluator

        public ModelEvaluator​(DMModel model,
                              FormulaEngineContext context)
    • Method Detail

      • evaluate

        public Map<String,​Object> evaluate​(Map<String,​Object> inputs)
        Executes a model's evaluation logic, taking a map of input parameters. The expected inputs, as well as the returned result, are entirely dependent on the model, more specifically the ModelType of the model.
        Parameters:
        inputs - Map of input param (name,value) pairs
        Returns:
        Map containing one or two entries. One entry that is always there has the key "results". Its value is the evaluation logic's calculation results as a map of (element name, result) pairs. If there was any error evaluating the logic, the result map will also contain an entry with the key "errors", and as value the error messages (in format).
      • evaluateTree

        public String evaluateTree​(ModelFormulaContext.TreeNode tree,
                                   Map<String,​Object> input)
        Determines the node in a model tree that complies to the given input, starting from a given TreeNode. Usually the starting TreeNode is the root node of the tree, but this is not mandatory. Each node in the tree defines a Filter. When the filters of the starting node's children do not exclude the given input, the evaluation is passed on to their descendants, where again the input is tested against those nodes' filter criteria. When the traversal reaches a dead-end, the output of the last successfully matched node is returned. The tree is expected to be constructed in such a way that the result is unambivalent, but this is the responsibility of the model.

        Example: In the below tree, the root node has 2 children, one with the filter Filter.equal("Region", "EMEA") and one with Filter.equal("Region", "APAC"). The former has itself 1 child with the filter Filter.equal("Channel", "Retail"), the latter has 2 with filters Filter.equal("Channel", "Retail") and Filter.equal("Channel", "Industry"). When given an input [Region:"APAC", Channel:"Retail"], the root node's first child is rejected but the second child's filter is passed (Region == "APAC"). The evaluation is then passed to that node's first child, where it also passes (Channel == "Retail"), and given that this is a leave node, its output is returned up the call stack. Note that in the Segmentation model, a node's output is the name of the segment represented by that tree node (e.g. 'S000004').

         Segments
              |
                 + EMEA
                 |    |
                 |    + Retail
                 |
                 + APAC
                      |
                          + Retail
                          |
                          + Industry
         

        Parameters:
        tree - The node from which to start the evaluation
        input - Map of input param (name,value) pairs
        Returns:
        The value of the output field of the last matched FilterNode