Monday, April 29, 2013

QT010: Using the IfThen Activity in Cast Iron Studio

Quick Tip #010: Using the If..Then Activity in Cast Iron Studio

Most Cast Iron developers are probably already familiar with the If..Then Activity however there may be some things that you didn't know about it.  The If..Then activity allows you to incorporate conditional logic in your orchestration and only execute certain activities under certain conditions.  When you drag an If..Then activity into your orchestration you can place activities that should only be executed under certain conditions inside the If branch of the If..Then activity.  You may also place activities that should only be executed if the condition is false into the Else branch of the If..Then activity.

Manipulating the Branches

An If..Then activity can have multiple branches each with their own logical test for execution, you may add a branch to an If..Then activity by right clicking in the outer gray box and choosing the add if branch.  You can delete branches by right clicking the if (or else) icon and choosing delete.  (Note if you delete the else branch you may add it back by right clicking inside the gray box and choosing add else)  The logic of each branch behaves like an if(){} else if() {} C or Java statement, not like a switch or case statement.  That is to say that each condition is evaluated from the top down and the branch with the first condition to evaluate to true is executed and processing continues after the If..Then activity.  If none of the conditional expressions are true, then the Else branch is executed (if one exists). You may reorder branches by right clicking on the If icon for the branch and choosing Go Up or Go Down to move the chosen branch up or down in the order of precedence.

Add a branch to an If..Then by right clicking inside the outer gray box and selecting Add "If"

Defining the Logic for If..Then Branches

The logic for an If branch is essentially defined using XPath syntax.  You may use the builder to define your logic or you may click the Advanced button and define the logic as a free form string.  Note that switching back and forth may cause issues because you can create expressions in advanced mode that cannot be expressed with the builder.  Note: if you need to define the order of operations you can use advanced mode and use parens around your expressions to define how the expression should be evaluated.

An example of using advanced mode and parens to change the order of operations.

Using the Builder

The builder allows you to create a set of boolean expressions tied together with the AND and OR logical operators.  Each expression contains a left hand side an equality operator and a right hand side.  Both the left hand side and the right hand side allow you to click an elipses button to choose a field from a variable defined in your orchestration or you may set a static expression.  If you set a static expression, if the expression is a string type be sure to encapsulated it in single quotes.  If it is a boolean use the true() or false() XPath functions to ensure that it is properly evaluated.  You may also use other XPath functions and constructs in your expressions for example a common pattern to determine whether or not an element is null you may want to ensure that the tag is present and that the value is not the empty string.  To do this use a count function to count the number of instances whether the value is not equal to the empty string such as: 
count(bpws:getVariableData('var')/*:body/*:record[*:element!=''])>0 
The above example uses both an XPath function (count) and an XPath predicate to determine whether or not an element is null.  You can use other functions such as starts-with, substring, etc to build even more complex expressions.

There are several equality operators available via the drop down.
You can add multiple conditions tied together with AND/OR logical operators.


No comments:

Post a Comment