Conditional clauses are invaluable structures for programmers, however, their use within Prolog TEC Rules is poorly documented, despite recent updates to IBM Documentation. This tip explains the use of the if-then-else clause within TEC Rules through the use of examples.
The failure of a predicate within a TEC Rule action will cause the processing of the action which contains that predicate to cease. In many circumstances the logic requires the predicates called to be dependent on a condition, or that subsequent processing to continue irrespective of the success or failure of a condition. It is in these situations that the if-then-else structure can be utilised.
Syntax
The basic syntax is as follows:
The conditional statement can utilize a standard TEC prolog conditional operator, for example "==", ">", "\==", or a predicate that exits with a success/failure dependent on supplied argument, for example retrieving a FACT
"custom_fact(_key, _value)".
The <if_body> and <else_body> can be one or more predicates, seperated by commas in the normal fashion. The following examples build on these concepts.
For example, a basic conditional clause could be dependet on the value of a variable "_cpuusage" :
%if cpuusage > 90
|
Subsequent statements can be added after the conditional clause by terminating the clause with a comma. This is powerful way to continue processing within the action, irrepective of the success or failure of the condition.
For example:
reception_action: set_severity:
|
Similarly, multiple statements can be added into each clause, for example, to send an email when the CPU is over 90%:
reception_action: set_severity:
|
Inherently, if-then statements are not supported, however, the "true" statement can be used to indicate no operation in the "else" clause. This facilitates an additional action for specific conditions, without interrupting the subsequent
flow of the action.
reception_action: set_severity:
|
The rules trace for if-then-else conditions can be more difficult to read, especially where they contain multiple statements, or further, nested, conditional clauses. However, the additional functionality more than out-weighs this disadvantage.