There are common situations where a business rule or expression can be used in different scenarios across a same project, to perform a specific capability.
In order to avoid the need of rewriting the same rule or expression in each of those scenarios, Bizagi features Library Rules.
Library Rules (also known as Global Rules), can be reused across all applications and processes contained in one same project, in each of those situations.
This way, users developing processes will only write the rule once and associate it wherever they might need it.
Applicability and concepts
Some examples of Library Rules are: obtaining today's date, obtaining the case creator's ID, validating if a given date is a holiday, etc.
Library rules work just as business rules, but they do not have a context associated to them since they can be invoked from anywhere on the project.
This means that from Library rules there is no access to the Data Model, thus Xpaths can't be used.
Keeping library rules organized
In some projects many library rules may be needed, potentially creating a difficulty to identify the specific rule someone needs and maintaining the rules updated. In order to keep all your library rules organized, it is encouraged to use Expression families, which allow to create groups of rules.
Therefore, before actually starting to create a Library rule, you should first create an Expression family.
To use this option, go to the Expert view, Business Rules options, Library Rules node and click on the New expression family option.
A window prompting for a name for your new Expression family will emerge.
Input the desired name and click OK.
Then on the main panel you will see your newly created Expression family.
For example, if the name you inputted was "Date", the result will be as follows.
Inside each family, any number of rules can be added.
Creating Library Rules
Library rules can be created independently or as part of an already existing Expression family. In order to create a standalone rule, click on the New library rule option, next to the New expression family option. If you wish to add a rule as part of a family, right click on the family and select the New library rule option that is displayed on the menu.
Either way, a similar window to the one shown below is displayed.
There you can build your rule, similarly to what you would do when building an expression.
You should set a Display name (and a name), a Description, select the family you wish to add the rule to (note that the one previously selected will be shown by default), the Return type, and Parameters.
Then, build your rule as you would normally do in an expression editor.
Recall that when building library rules there is no Context, which means that the Data Model cannot be browsed, thus Xpaths cannot be used.
Using library rules
When you have already set up a Library rule, you might want to use it during some point of a process.
Wherever expressions are available to be used in Bizagi, then the library rules can be used and leveraged.
To do so, click the Library rules option on the expression editor, as shown below.
A window with a tree view with all the Library rules set on the project is shown.
Notice how they are organized by Expression family.
Select the rule you wish to add, and click OK.
A pop-up window is shown, and in there you should specify where the value returned by the Library rule should be stored.
There are two different ways to choose how to store the return value of the Library rule (i.e., two different places to store the output):
•The first one (the button with a pencil Icon), which enables you to choose an existing variable or create a new one.
As hinted, this one enables you to store the output in a variable (for later use).
•And the second one (the button with a diagram Icon vertically flowing), enables you to choose an attribute from the Data Model to store the value.
This one enables you to directly store the output into an attribute of a given Entity of your Data Model.
Consider that the third button simply clears what you have set up previously, in case you wish to perform a change or adjust any previous configuration.
In either case, the resulting configuration is seen as an assignment instruction, just as programming languages commonly handle them.
An assignation instruction where on its left part, it displays the Data Model's attribute or variable storing the output.
And on the right part, it features the actual invocation of the Library rule, while having any inputs sent to it (inside of parenthesis).
The following image displays an example, having specifically the Me object sent as an input, and having its output stored in a variable called vDate.
Rely on the following two examples for additional guidance.
Example 1 - Today's date
Suppose you work in a bank.
Suppose whenever an applicant of a loan makes a request, you want to set the receive date.
This way you make sure that whenever a new case is created, the Request date is defaulted to "today".
Now in another process, Purchase Request, suppose you need to set an minimum valid date for the delivery: It has to be later than today, because a delivery date can only be from the next day onwards.
Since there are different scenarios that need to have a date be checked and compared to the current date (today), it makes sense to create a single business rule and reuse it throughout all points of processes needing such validation.
Note that this rule doesn't need any context.
There is no data model manipulation, it is a matter of just sending today's date and receiving concluding information.
Thus and to resolve the above, we create a Global rule that returns Today's Date using the function on the Duration-Date category.
In the Expert view go to Business Rules. Right-click Expressions under the Business Rules classification and select New Scripting Expression.
If you have Families of rules created you can allocate this rule to a family in the Rule Family drop-down list, or leave it blank.
We can use this rule in the Forms Modeler for any Process, to set a Default value, or a minimum/maximum date.
Open the Forms Modeler, select the attribute where you wish to relate the rule.
Select default value and choose the rule.
Make sure that this rule returns a value.
Notice that this example returns the value contained in vDate.
Example 2 - Using email templates
Lets suppose that you have many processes in place as part of a project, and many of the tasks automatically send emails to Managers or users as notifications.
Your boss pointed out that all emails sent as part of the automated processes must comply with the organizational standards:
They all must have a certain look and feel, and they all should include a disclaimer message saying that the information is highly restricted and that it may not be shared or reproduced.
As the processes' developer you have to change all emails sent automatically by Bizagi so that they comply with such standard, but you identify that doing that manually for each of the emails is not as efficient.
The best way to do that is to set up a library rule that returns the HTML for the email and that receives as a parameter the customized text that should be used.
For this example the final result will look as an email like this:
Note that the email is embedded within a corporate template and that it contains the personalized message and the disclaimer at the bottom.
In order to accomplish this, the first step is to set up a Library rule which returns the HTML template and receives a parameter to personalize the message. In this example, the personalized text is the name of the user.
As you can see, a rule called Email template has been set up.
The rule stores in the name variable the value received on the Name parameter.
And then stores and returns the HTML template in the msg variable.
The Name parameter was simply set up in the Parameter list option, as a String parameter like so (notice that the Me parameter is set up by default and should not be changed):
Using String concatenation, the parameter is used to inject the personalized text, as it can be seen in the following image.
Then, when setting up the email activity action, and on the body of the email select the Expression field option. There, set up an expression using and returning the Library rule that you have just set up.
Notice that when invoking the Emailtemplate library rule, an additional parameter is sent in. In this case you need to select from the Data Model the user's full name so that it can be injected into the HTML template and resolved on the email as a personalized message. The body of the email will end up looking as follows.
At this point you will have set up the library rule to reuse an HTML email template successfully!
You may now proceed to reuse this template in all of your email tasks without the need of copying and pasting the content on each of the tasks; and this way, simply call the Library rule and customize the message sent so that it matches your specific needs.