Inferencing listeners can be used to infer values when it is not convenient (or not possible) to use rules or custom functions. The topic Customize the inferencing cycle with custom functions and inferencing listeners describes when it is appropriate to use an inferencing listener.
An inferencing listener is a hook in the inferencing cycle of the determinations engine that is called before and after each inference cycle. If it modifies the session after the inference cycle, the inferencing cycle is repeated and the inferencing listener is called again, and so on. This process repeats until the inferencing listener stops making changes to the session, and in this way can be used to augment the inferencing cycle by having the inferencing listener ‘infer’ values (by setting them) and automatically re-evaluating the rules that depend on those changed values.
Note: It is not recommended to create rules and inferencing listeners that are highly dependent on each other. This would cause the inferencing cycle to repeat itself many times while waiting for the session to stabilise and this may cause performance degradation. To discourage this practice, an exception is thrown if the inferencing cycle fails to stabilise after a small number of times (currently 20).
Important Note: for Web Determinations, entity instances should only be created using the Interview Engine's API methods.
Unlike event-based inferencing listeners (see Example: Handle rulebase events with an inferencing listener), this kind of inferencing listener shouldn’t be triggered by a rulebase event. This is because rulebase events are raised when a condition becomes true, and in this case the ‘inferred’ result should update when dependencies change, not when a condition becomes true.
An inferencing listener should instead be structured to look at dependent attributes and make changes to the session to make it consistent with these dependencies. If the session is already consistent with the dependent attributes, then it should do nothing.
In the following example, an inferencing listener is used to create a number of entity instances based on the value of a numeric attribute. It expects an attribute with the public name of “num_children”, and an entity called “child”. After each inferencing cycle, the value of num_children is compared to the number of instances of the entity “child”. If there are fewer than requested, it creates new instances, if there are more, some are deleted.
This approach of updating the session is not strictly required but it is strongly encouraged because it ensures that the inferencing listener will work in the debugger and regression tester and allow the num_children attribute to be changed by the user.
Click on the appropriate link to view either the Java or the C# example: