Logic Service


Contents

Overview

The logic service provides a mechanism for consuming data from the OpenMRS API, including not only discrete data, but also logical or "derived" data elements that combine information. For example, imagine height and weight are stored as discrete data points and body mass index (BMI) is determined by a calculation using height and weight. Using the logic service, you could obtain height, weight, and BMI without the need to know where/how the information is stored within the system or whether or not it requires a calculation.

Registering data

Before data can be consumed from the logic service, they must be registered. At system start, all dictionary concepts that are tests or questions are registered as sources of observational data. Higher level "derived" data can be registered (e.g., from modules) as logic rules. Rules are classes that implement the org.openmrs.logic.Rule interface.

Data are registered under a unique string token. The token is a system-wide unique string that can be used later to retrieve the data.

Retrieving data

Retrieve data from the logic service through the eval(patient, token) method:

// Assume 
  LogicService ls = Context.getLogicSerive();
  ls.eval(myPatient, "WEIGHT (KG)");

Creating your own rule

A rule must implement the org.openmrs.logic.Rule interface and be registered through the logic service's addRule() method before they can be consumed. The string token used when registering the rule must be unique system-wide and is passed to the logic service's eval() method to evaluate the rule for a patient or group of patients.

The critical method for a rule to implement is:

public Result eval(LogicContext context, Patient patient,
    Map<String, Object> parameters) throws LogicException;

Within this method, the rule should fetch data for the given patient from the given logic context and execute any business logic, returning the result as an org.openmrs.logic.result.Result object. The rule should use the provided LogicContext for all data requests -- i.e., rules should NOT go directly to the logic service, since that would bypass caching of the logic context and could result in infinite loops.