OpenClinica Rules support the following arithmetic operators:
- + (Addition)
- – (Subtraction)
- * (Multiplication)
- / (Division)
All four of the operators above will work with INT and REAL DataTypes. For DATEs, an INT can be added (+) or subtracted (-) from the DATE to increase or decrease it by a certain number of days. Subtraction (-) can also be used to get the difference between two DATEs, with the result being an INT representing the number of days between the two DATEs. The difference between dates is always computed as an absolute number. For example, 2011-11-19 – 2011-11-20 evaluates to 1, not -1. ST fields can not be used with any of the arithmetic operators.
Example 1
<RuleDef OID=”RULE1″ Name=”This is an example”>
<Description>The average temperature is greater than 98.6</Description>
<Expression>((ITEM_OID_1 + ITEM_OID_2) / 2) gt 98.6</Expression>
</RuleDef>
The above example is taking 2 fields that are REAL DataTypes, ITEM_OID_1 and ITEM_OID_2, adding them together to get a total, and then dividing by 2. If the average is greater than 98.6, this Rule will fire.
Example 2
Addition and subtraction with dates can be very powerful, but there are a few things to keep in mind. When working with dates and these arithmetic operators, the values derived will be in days. For example, you can not have a rule that checks for a visit that is greater than 1 year. You would have to write this rule saying the difference between the dates is greater than 365 days.
<RuleDef OID=”RULE2″ Name=”This is an example”>
<Description>The surgery date is greater than 1 year from this visit date</Description>
<Expression>ITEM_OID_1 – ITEM_OID_2 gte 365</Expression>
</RuleDef>
In the above RuleExpression, we are subtracting one date from another, and if the value is 365 or greater, the Rule should evaluate to true.
Example 3
OpenClinica computes addition/subtraction expressions from left to right. When combining dates and integers in an expression this has some interesting consequences. A date that has an integer added to or subtracted from it is still treated as a date. When two dates are compared, it uses the absolute value of the difference (ie, the result is always a positive number or 0). Thus the two expressions below are not always equivalent:
A) 0 – 1 + DATE2 – DATE1 ne 0
B) 0 + DATE2 – DATE1 – 1 ne 0
Assume DATE2 = 31-Dec-2000 and DATE1 = 01-Jan-2001
In case A, -1 is added to DATE2 and the result is a date that is one day before DATE2. OpenClinica then takes that date and subtracts DATE1 from it, and since for date math we use absolute values, computes the absolute value of the difference between the two dates.
A) 0 – 1 + DATE2 – DATE1 ne 0
(-1) + 31-Dec-2000 – DATE1 ne 0
30-Dec-2000 – 01-Jan-2001 ne 0
2 ne 0
In case B, OpenClinica first computes the absolute value of the difference between DATE2 and DATE1, then subtracts 1.
B) 0 + DATE2 – DATE1 – 1 ne 0
31-Dec-2000 – 01-Jan-2001 – 1 ne 0
1 – 1 ne 0
0 ne 0