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:
Give your grammar a short, descriptive name
Enter your grammar's name and a brief description of its approach in the list below
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.
Use the Tools --> View Wiki Markup (or the Wiki Markup editing mode) to see how others have successfully formatted their expressions.
Scenario 1: Simple Condition
Return all CD4 COUNT results with value less than 350.
Grammar Candidate | Expression | Comments |
---|---|---|
Simplified | {CD4 COUNT} < 350 |
|
|
|
|
Scenario 2: Multiple Conditions
Return all encounters at location CLINIC A with encounter type IMMUNIZATION.
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
Return the most recent CD4 COUNT amongst all available CD4 COUNT results with value less than 350.
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
Return the most recent CD4 COUNT count if it has a value less than 350; otherwise, return an empty result.
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
Return the most recent SPUTUM CULTURE that occurred before the most recent TB PROGRAM COMPLETION.
Grammar Candidate | Expression | Comments |
---|---|---|
Simplified | last({SPUTUM CULTURE} before last({TB PROGRAM COMPLETION})) |
|
|
|
|
Scenario 6: Encounter-Based Criteria
Return PULSE result(s) from the most recent OUTPATIENT CONSULT encounter.
Grammar Candidate | Expression | Comments |
---|---|---|
Simplified |
|
|
|
|
|
Scenario 7: Age-Based Criteria
Return the most recent CD4 COUNT obtained before the patient turned 15 years old.
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
Return the result of PEDIATRIC given an age threshold of 17.
Grammar Candidate | Expression | Comments |
---|---|---|
Simplified |
|
|
|
|
|