Using Decision Variables in String Expressions

OpenRules provides multiple ways to refer to decision variables inside decision tables and use their values in text expressions. In this post I will use simple examples to explain how to use different expressions inside decision tables.

Let’s consider the basic sample project “Hello” included in the standard installation workspace “openrules.samples”. Here is its glossary:

This model uses decision tables to define decision variables Greeting and Salutation (sub-goals):

As you can see, you may type text like “Good Morning” or “Mrs.” in the columns of the types “Then” and “Conclusion” to assign it to the decision variable defined in the column titles (Greeting or Salutation). But what if your text should include the values of decision variables already calculated by the decision model?

When a decision table cell contains only the name of a decision variable, OpenRules will assume that you mean the value of the proper decision variable. For instance, the following table of the type “Decision” finds the Maximum Effective Date among 3 variables of the type Date:

In many cases you need to use expressions that contain several decision variables. For example, the decision variable “Hello Statement” (the goal of the decision model “Hello”) can be defined using the calculated values of “Greeting” and “Salutation”. It can be done by one of the following decision tables:

The first table uses concatenation (with operator “+“), and the decision variables Greeting, Salutation, and Name will be replaced with their actual values (calculated or given as an input).

The second expression {{Greeting}}, {{Salutation}} {{Name}}! uses string interpolation when names of decision variables are taken into double curly braces to instruct OpenRules that they should be replaced with their values.

String interpolation can be very useful when you want to produce explanations. For instance, if we add one more column “Message” to the above table

our decision model will show the explanation such as below:

If we replace the column type “Message” to “Action” and add “Explanation” as another decision variable of the type String in the glossary, the same table will assign the printed above value to the variable “Explanation”.

Our customers frequently include in their decision models the variable-array “Explanations” of the type String[]. Then every time when they want to add a new explanation to the array “Explanations, they use the columns like this one:

Alternatively to concatenation and string interpolation, one also may define Hello Statement using this following Java snippet with macros like ${name}:

Inside the snippet that starts with “:=” we may use any Java construct, e.g. instead of ${Name} we may write $O{Customer}.getName().

P.S. Please note that the decision model “Hello” relies on the automatic build of the execution path by defining the model.goal as Hello Statement in the Environment table. It means OpenRules will automatically defines inter-rules relationships such as the variable “Hello Statement” is being dependent on the variables “Greeting” and “Salutation”.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.