IBM Business Automation Workflow Implementing Decision Tasks

Adding decision task in a process or service flow and implementing the decision logic using an action rule or a decision table.

Serhii Radachynskyi © 123RF.com

Code Repository

The examples can be found in the TipsforBAW Toolkit Github Repository

Introduction

Processes have frequent need to make decisions and IBM Business Automation Workflow (BAW) has built in capabilities that can help to extract rules from code into decision tasks and decision services. This makes it easier to understand and maintain your decisions. For those familiar with IBM Operation Decision Manager (ODM) there is a lot of synergy in the approach, and the decision tables created in BAW can be exported to ODM. With this enhanced decision capability, it is recommended to adopt the decision tasks and services in our workflow to simplify and reduce the amount of decision code in a process.

Business rules such as action rules and decision tables are authoring components that use predefine business vocabulary that are easily readable by humans and can be interpreted by a computer. In this article, we will examine how to create and use decision tasks in a workflow by implementing the decision logic in action rules or decision tables.

Decision tasks are a convenient low-code way of representing decision logic. Some of the advantages of the decisions task include:

  • No Need to Code — you don’t have to write JavaScript code or even need a programmer/developer to implement business rules.
  • Easy to Read — it uses natural language technology to express business rules in a form that is easy to read and understood by all.
  • Easy to Modify — you can easily add, update and delete conditions/actions of the business rules when you use decision tables since it is presented in a table format.
  • Easy to Identify Problems — it helps you to easily see or catch errors/problems such as overlaps, gaps, etc. in Decision Table and missing words, mismatch type, etc. in Action Rule.
  • Reuse and Consistency — you can prevent a developer to create the same rules in JavaScript because the created decision task in a service flow can be called and reuse within your process application.

For the remainder of this article, we will help to demonstrate these advantages by stepping through sample scenarios that present a business process and provides examples of the following:

  1. Using Decision Task
  2. Implementing Decision Task in a Service Flow
  3. Implementing Decision Service in a Process

Using Decision Task

Decision task helps incorporate decision logic and business rules in your process application. During development, we should use Decision tasks in our service flows, to reduce coding and create reuse-able decision service flows.

Business rule/decision

As a developer, we need to know when a Decision Task is necessary to use. Let’s observe the example below, we have here a business rule/decision written as JavaScript code. The code snippet shows a script that determines the customer loyalty and rewards benefit based on the amount spent in the last 2 years.

if (tw.local.amountSpent > 350000) { 
tw.local.discount = 0.20;
tw.local.loyaltyType = “Diamond”;
} else if (tw.local.amountSpent > 200000 &&
tw.local.amountSpent < 350000) {
tw.local.discount = 0.12;
tw.local.loyaltyType = “Platinum”;

} else if (tw.local.amountSpent > 100000 &&
tw.local.amountSpent < 200000) {
tw.local.discount = 0.10;
tw.local.loyaltyType = “Gold”;
} else if (tw.local.amountSpent > 50000 &&
tw.local.amountSpent < 100000) {
tw.local.discount = 0.07;
tw.local.loyaltyType = “Silver”;

} else if (tw.local.amountSpent > 25000 &&
tw.local.amountSpent < 50000) {
tw.local.discount = 0.05;
tw.local.loyaltyType = “Bronze”;

} else if (tw.local.amountSpent < 25000) {
tw.local.discount = 0.025;
tw.local.loyaltyType = “General”;
}

One quickly notices the if-else statements, they have very similar and highly repetitive statements. They have the same number of conditions that need to be evaluated and same number of actions that must be performed. Each statement is checking for the amount spent in the last two years and will give the customer its discount percentage and type of loyalty. Decision tasks provide (Decision Tables and Action Rules) for us to simplify this coding and we should use a decision task to replace the code. Figure 1, implements the same business rules using a Decision Table. Notice that the decision table is easier to read and understand and more concise compare to the if-else statement JavaScript code.

Figure 1: Decision Table Example

Lets now look at the detailed steps for the creation of Decision tasks.

Implementing Decision Task in a Service Flow

Implementing Decision task allows you to create rule-based decision using action rule or decision table. In this section, we provide detailed steps on how you add Decision task to a service flow and then add decision tables and/or action rules to the decision task. The business scenario represented is business rules that will validate and display the customer loyalty and rewards benefit based on the amount spent in the last 2years. This example walks through the steps on how to use decision task in displaying the customer’s loyalty and rewards program as seen here in Figure 2.

Figure 2: Implementing Decision Task in a Service Flow Example

There are 3 steps to complete in adding decision task in a service flow:

  1. Create a Decision task in a Service Flow
  2. Define Input and Output Variables
  3. Add and Write the Rules in your Decision task (implemented as a Decision Table or Action Rule).

The example is available in Decision Task in Process Example CSHS in the TipsForBAW Toolkit stored in the GitHub repository.

Step 1: Create a Decision task in a Service Flow

Create ‘ValidateLoyaltyRewards’ service flow and then in the diagram, click the down arrow of the Activity icon on the component palette and then drag the decision icon as shown in Figure 3.

Figure 3: ValidateLoyaltyRewards Service Flow

Another way is to drag the Activity icon in the service flow diagram (default is Server Script) and then change the Activity Type in Implementation tab as shown in Figure 4.

Figure 4: Selecting Decision Activity Type

Step 2: Define Input and Output Variables

Click the Variables tab and define the following input and output variables of the Decision task in Figure 5.

Figure 5: Decision task Input and Output Variables

Step 3: Add and Write the Rules in your Decision task

This step shows you how to add and write the rules in our Decision task. There are two ways to write your business rules, one is Decision Table and the other is Action Rule. Decision table represent decision logic as a table while Action Rules are sentence-like statements.

You use a Decision Table when you have rules that has the same rule statement but have different conditions and actions otherwise use an Action Rule when you have rules that defines a specific action to take when a certain condition is met.

First, we will demonstrate the following steps on how to add a Decision Table to your Decision task and define the rules for your decision table.

Using Decision Table

Decision Tables contains rows and columns where each row corresponds to a rule statement. To add a decision table, follow the steps below:

  • Open the decision editor by clicking the Decision tab or the ‘Open Decision’ in Implementation tab, see Figure 6.
Figure 6: Opening Decision Editor
  • When the decision editor is open, it shows a basic rule template with one condition (if) and one action (then), see Figure 7.
Figure 7: Basic Action Rule Template
  • Since we are creating Decision Table, we need remove the Action Rule by clicking the ‘X’ button as shown in Figure 8, to delete that action rule.
Figure 8: Delete Action Rule or Decision Table
  • To add Decision Table, you need to click the ‘Add decision table’ button on left pane, shown in Figure 8. This shows you the default decision table that contains a condition column (C0), an action column (A0) and 20 rows, see Figure 9.
Figure 9: Default Decision Table

When you’re done adding the Decision Table, you need to write the rules for your decision table. We will use the business rules define in the first section of this article (Using Decision Task). For our example, enter the given rule statement:

Rule Condition: amountSpent is at least <min> and less than <max>
Rule Action:
1. set discount to <variable value>
2. set loyaltyType to <variable value>

Now let’s start writing the rules in the decision table by following these steps:

  • Rename condition column (C0) to ‘Amount Spent in the last 2 years’ and then right-click the column and choose ‘Define Column…’. An editor window will open where you can write your rules.
Figure 10: Define Condition Column
  • From the editor window, press Ctrl+Spacebar or Ctrl+Shift+Spacebar (Hierarchical View) to show Content Assist menu, see Figure 11. Select the appropriate terms and phrases based on the given rule condition above.
Figure 11: Writing Rule Condition
  • When you’re done writing the rule condition, click the Define button. Wait until the table finishes loading, then check to see min and max labels below the Amount Spent column header, see Figure 12.
Figure 12: Rule Condition Column
  • Double-click the first row below the min and max labels and enter 350,000 for the min value. Let’s observe our first rule condition which is greater than 350,000, to do this you need to right-click the row, select Change operator and then select the greater than sign (>) as shown in Figure 13.
Figure 13: Change Operator in Decision Table Row

Notice: The rule condition column label and the first row becomes red, see Figure 13 and when you change the operator, you will find gaps (yellow sign) on the rows. These gaps are not errors but instead they are warnings which will not cause any issue during runtime. For more details on gaps and overlaps, see Decision Table Errors and Warnings section after the last step.

  • On the second row, enter 200,000 for the min value and 350,000 for the max value. Fill out the succeeding rows up to the sixth row using the min and max values shown in Figure 14.
Figure 14: Amount Min and Max Values
  • Rename action column (A0) to ‘Discount’ and then right-click the column and choose ‘Define Column…’. An editor window will open where you can write your rule action.
Figure 15: Define Action Column
  • From the editor window, press Ctrl+Spacebar or Ctrl+Shift+Spacebar (Hierarchical View) to show Content Assist menu, see Figure 16. Select the appropriate terms and phrases based on the given rule action above.
Figure 16: Writing Rule Action
  • When you’re done writing the rule action, click the Define button. From the decision table, enter the values for Discount column. Double-click the first row below the Discount label and enter 0.2, continue to enter values up to the last row using the values shown in Figure 17.
Figure 17: Discount Values
  • Notice the values in Discount action column, we need to present the values in percentage format to make it more precise and understandable. To format the values, right-click the Discount action column and choose ‘Format column…’. A formatting editor will open where you can define formatting options for the column, see Figure 18.
Figure 18: Action Column Formatting
  • In Action Column Formatting, select the percentage format and then click Update. The discount values should now be converted from a number to percentage format, see Figure 19.
Figure 19: Formatted Discount Values
  • Since we have 2 rule actions, we need to add another rule action column. To insert a column, right-click the Discount action column, choose ‘Insert column’ and then choose ‘Action after’. Check to see the action column (A2) was added after the Discount action column, see Figure 20.
Figure 20: Adding Another Rule Action Column
  • After adding another action column, rename the action column (A2) to ‘Loyalty Type’ and then repeat steps #5 to #7 to define the rule action and from the decision table, enter the values for Loyalty Type column as shown in Figure 21.
Figure 21: Define Loyalty Type Rule Action Column

Decision Table Errors and Warnings

As you enter values to the cells of your decision table, the Decision Editor automatically checks for column errors (red sign) and warnings (yellow sign), see Figure 22.

Figure 22: Decision Table Errors and Warnings

The Decision Editor checks for overlaps and gaps in decision table conditions you can hover/point your mouse through the red/yellow sign to verify the error you encountered, see sample in Figure 23.

Figure 23: Sample Errors/Warnings

The decision editor helps you to verify these warnings so that the values you entered in each condition throughout the different rows does not overlap or not identical and all possible cases are considered to make sure that there are no gaps in your table. Since these are warnings, it will not cause runtime errors but might affect the result of your rule.

If you are certain that the warning will not affect the result of your rule, you may want to turn off the checking for gaps and/or overlaps by unchecking the setting in Figure 24, so that the warning signs will disappear.

NOTE: If in case the amount spent does not fall to any rule condition (e.g. amount is less than 0) in the decision table, you need to define default using Action Rule so that you can still set an appropriate and acceptable value to your result. Check on the next section for more information on Action Rule.

Figure 24: Check gap and overlap setting

Another way of expressing business rules is by using Action Rule. The following steps describe how to add a simple action rule to your Decision Task.

Using Action Rule

Action Rule is written as a sentence-like in a natural language which uses an if-then statement to associate a condition (if) with an action (then). To add an action rule, follow the steps below:

  • Open the decision editor by clicking the Decision tab or the ‘Open Decision’ in Implementation tab, see Figure 6.
  • When the decision editor is open, it shows a basic rule template with one condition (if) and one action (then), see Figure 25.
  • In case you want to add another action rule, you may click on the ‘Add action rule’ button as shown in Figure 25.
Figure 25: Add Action Rule

Now, let’s define our action rule, for our example, enter the given rule statement:

if amountSpent is less than 0 then 
set loyaltyType to ""; set discount to 0;

If you are not familiar on writing rules in IBM Operational Decision Management, we provided following the steps on how you could write the rule from the basic template.

  • Click the <condition> placeholder to open the Content Assist menu in Figure 26, and then select ‘amountSpent’.
Figure 26: Content Assist Menu

NOTE: Content Assist Menu contains predefined terms and phrases that helps you build your rules. The menu lists terms and phrases that are valid in the current context of the rule. These terms and phrases make the action rule more human readable where creating rules does not require an expert programmer instead the business team can even learn to do this.

  • To complete your condition, press Ctrl+Spacebar or Ctrl+Shift+Spacebar (Hierarchical View) to show again Content Assist menu than select the ‘is less than’ phrase and type ‘0’ as the value.
Figure 27: Rule Condition Part

NOTE: The rule condition part can be made up of one or more condition statements.

  • Click the <action> placeholder and select the phrase ‘set <variable>’ in the menu list.
Figure 28: Replacing <action> placeholder
  • To complete your action, press Ctrl+Spacebar or Ctrl+Shift+Spacebar (Hierarchical View) to show again Content Assist menu then select the ‘loyaltyType’ term. Continue pressing Ctrl+Spacebar or Ctrl+Shift+Spacebar in order to show the valid term or phrase you can use to complete your action. Select the term ‘to’ and then type “”(empty string) as the value, see Figure 29.
Figure 29: Rule Action Part
  • To add the second rule action, after ‘;’ (semi-colon), repeat step #4 in order to complete the business rule statement as shown in Figure 30.
Figure 30: Completed Action Rule

Additional Information:

Rule Definition — This is an optional part of the rule which allows you to define a variable and set its value. You use the rule variable/s to make your business rules less ambiguous and easier to understand. The simplest use of rule variable is to declare a constant value, see Figure 31. The variable ‘minimum_discount’ makes the following rule easier to understand.

Figure 31: Simplest use of rule definition

‘else’ part of a rule — This is also an optional part of the action rule which allows you to specify the action when the condition is not met, see sample in Figure 32.

Figure 32: else part of a rule

Rule Error List — This section lists the error/s that you may encounter when writing your rules. It helps you to easily check and fix error/s in your rule.

Figure 33: Rule Error List

Run the Decision Task in Service Flow Example CSHS

Now that you have added your action rule and decision table to a Decision task in a service flow, let’s run our sample implementation by opening the Decision Task in Service Flow Example CSHS in TipsForBAW Toolkit and then run the human service.

  1. In Customer Information List, click the Approval icon button to open the Approval Request dialog window.
  2. Expand Customer Loyalty and Rewards Program section.
  3. Enter the Amount Spent in the last 2 years and click Validate button.
  4. It should display the corresponding Discount and Loyalty Type based on the rule definitions.
Figure 34: Decision Task in Service Flow Example

Implementing Decision Service in a Process

After you implement the Decision task in service flow, you can now invoke this service within your process application. This section details 2 ways on how you can implement the Decision service in a process. These are:

  1. Add Decision Task in a Process
  2. Attach Decision Service to an Exclusive Gateway in a Process

Add Decision Task in a Process

You can add a Decision task in your process flow by associating the Decision service for an activity implementation. You use Decision task when you want a decision or condition to determine which associated activity or action will be implemented or invoke.

Figure 35: Decision Task in Process Example Business Process Definition

Now, let’s look at our sample business process in Figure 35, this scenario shows a simple approval process that will review the additional rewards offer to the customer depending on their loyalty membership type. The customer loyalty and rewards information will be validated to determine which option is subjected for approval. This example will use ‘Determine Additional Rewards’ decision service, see sample rules in Figure 36.

Figure 36: Determine Additional Rewards Decision Service

The sample scenario presented in ‘Decision Task in Process Example’ business process definition is available in the TipsForBAW Toolkit stored in the GitHub repository.

To add Decision task and associate Decision service as the implementation for an activity in our process flow, follow these steps:

  • In the ‘Decision Task in Process Example’ process diagram, click the down arrow of the Activity icon on the component palette and then drag the Decision Task icon as shown in Figure 37.
  • Select Decision Task > Implementation tab, select ‘Determine Benefit Level’ as service implementation.
Figure 37: Adding Decision Task and Decision Service Implementation
  • In Data Mapping tab, map the loyaltyType as input and benefitLevel as output.
Figure 38: Decision Task Data Mapping
  • Once the setup is done, you can now run the process to validate the rules by launching the Decision Task in Process Example process, see Figure 39.
Figure 39: Running the Decision Task in Process Example Process

Attach Decision Service to an Exclusive Gateway in a Process

Another way to implement to your decision service in a process is by attaching Decision Service to an exclusive gateway. You use it in your process flow when you have several paths but only one path can be followed depending on the result of the conditions and actions in the rule component of your decision service.

Figure 40: Decision Service in Exclusive Gateway Example Business Process Definition

Let’s look at the business process shown in Figure 40, this scenario shows another approval process that uses decision gateway to determine the team assigned to review and approve the customer’s loyalty and rewards information depending on their loyalty membership type. This example will use ‘Determine Assign Approver’ decision service, see sample rules in Figure 41.

Figure 41: Determine Assign Approver Decision Service

The sample scenario presented in ‘Decision Service in Exclusive Gateway Example’ business process definition is available in the TipsForBAW Toolkit stored in the GitHub repository.

To attach a Decision service to an exclusive gateway in our process, follow these steps:

  • In the ‘Decision Service in Exclusive Gateway Example’ process diagram, click the down arrow of the Gateway icon on the component palette and then drag the Exclusive Gateway icon as shown in Figure 42.
  • Select Exclusive Gateway > Decision tab and then select ‘Determine Benefit Level’ as service implementation and enter loyaltyType as input mapping.
Figure 42: Attaching Decision Service in Exclusive Gateway
  • In Implementation tab, define the decision paths as shown in Figure 43.
Figure 43: Exclusive Gateway Decisions Implementation
  • Once the setup is done, you can now run the process to validate the rules by launching the ‘Decision Service in Exclusive Gateway Example’ process, see Figure 44.
Figure 44: Running the Decision Service in Exclusive Gateway Example Process

--

--

Diorella Mari Tom
IBM Digital Business Automation Tips and Assets

Diorella works for IBM Solutions Delivery Inc., Philippines, part of the IBM Expert Labs team, with more than 10 years of experience in application development.