Logic Expression Examples

We are in the process of defining the grammar for logic expressions. Statements on this page are subject to change.

Background

The OpenMRS ?logic service provides an engine for retrieving data from the system in a normalized manner -- i.e., data can be retrieved from the logic service in the same manner, regardless whether the data come directly from a table, come from an external data source, or involve complex calculations.  Logic results are retrieved using tokens (a unique string linked to the rule providing the results).  In order to improve the usability and efficiency of getting to desired results, the logic service allow for criteria to be applied when requesting results for a token -- e.g., asking for the latest result or asking for all results over a specific value.

Places logic expressions may be used:

  • Within the cohort builder (when defining groups of patients)

  • Within the configuration of some widgets (e.g., configuring a graph to show "all WEIGHT (KG) results within the last 6 months")

  • For form field defaults

  • By developers to add data to a web page (e.g., <?= logic.eval(patient, "LAST({WEIGHT (KG)})") ?>)

Grammar Design

We are actively designing the grammar for logic expressions.

Grammar Candidates

If you are proposing a grammar, please:

  1. Give your grammar a short, descriptive name

  2. Enter your grammar's name and a brief description of its approach in the list below

  3. Provide examples following your grammar's rules for each of the scenaros

  • Simplified -- aiming to be brief, intuitive; makes some inferences based on operator or ordering.  Assumes that results are coerced into logic result format (only available attributes are logic result attributes).

Grammar Scenarios

Each scenario describes a (hopefully) unambiguous description of desired result(s).  In each case, the proposed expression is defined in each of the candidate grammars.

Scenario 1: Simple Condition

Grammar Candidate

Expression

Comments

Grammar Candidate

Expression

Comments

Simplified

{CD4 COUNT} < 350

 

 

 

 

Scenario 2: Multiple Conditions

Grammar Candidate

Expression

Comments

Grammar Candidate

Expression

Comments

Simplified

Not Supported

The simplified grammar works against logic result attributes only.  If the encounter data source exposed an ENCOUNTER key that returned encounter results with the encounter location ID as the numeric value and the encounter type as the string value, then you could ask for {ENCOUNTER} = 4 to get encounters with a specific location or {ENCOUNTER} = 'IMMUNIZATION' to get encounters with the immunization encounter type.  The simplified grammar does not allow for multiple conditions against the same subject.

 

 

 

Scenario 3: Condition and Aggregate

Grammar Candidate

Expression

Comments

Grammar Candidate

Expression

Comments

Simplified

last({CD4 COUNT} < 350)

Note that parentheses apply the last aggregation after the less than condition.

 

 

 

Scenario 4: Aggregate and Condition

Grammar Candidate

Expression

Comments

Grammar Candidate

Expression

Comments

Simplified

last({CD4 COUNT}) < 350

Note that parentheses apply the last aggregation before the less than condition.

 

 

 

Scenario 5: Relative Query

Grammar Candidate

Expression

Comments

Grammar Candidate

Expression

Comments

Simplified

last({SPUTUM CULTURE} before last({TB PROGRAM COMPLETION}))

 

 

 

 

Scenario 6: Encounter-Based Criteria

Grammar Candidate

Expression

Comments

Grammar Candidate

Expression

Comments

Simplified

 

 

 

 

 

Scenario 7: Age-Based Criteria

Grammar Candidate

Expression

Comments

Grammar Candidate

Expression

Comments

Simplified

last({CD4 COUNT} before ({AGE} = 15))

This one is tricky because we need to make sure that the ({AGE} = 15) return datetime instead of true or false.

 

 

 

Scenario 8: Parameters

Grammar Candidate

Expression

Comments

Grammar Candidate

Expression

Comments

Simplified

 

 

 

 

 

Related Links