第4章 Creating guided rule templates
You can use guided rule templates to define rule structures with placeholder values (template keys) that correspond to actual values defined in a data table. Guided rule templates are an efficient alternative to defining sets of many guided rules individually that use the same structure.
Procedure
-
In Business Central, go to Menu
Design Projects and click the project name. -
Click Add Asset
Guided Rule Template. - Enter an informative Guided Rule Template name and select the appropriate Package. The package that you specify must be the same package where the required data objects have been assigned or will be assigned.
Click Ok to create the rule template.
The new guided rule template is now listed in the Guided Rule Templates panel of the Project Explorer.
- Click the Data Objects tab and confirm that all data objects required for your rules are listed. If not, click New item to import data objects from other packages, or create data objects within your package.
After all data objects are in place, return to the Model tab and use the buttons on the right side of the window to add and define the WHEN (condition) and THEN (action) sections of the rule template, based on the available data objects. For the field values that vary per rule, use template keys in the format
$key
in the rule designer or in the format@{key}
in free form DRL (if used).図4.1 Sample guided rule template
Note on template keysTemplate keys are fundamental in guided rule templates. Template keys are what enable field values in the templates to be interchanged with actual values that you define in the corresponding data table to generate different rules from the same template. You can use other value types, such as Literal or Formula, for values that are part of the rule structure of all rules based on that template. However, for any values that differ among the rules, use the Template key field type with a specified key. Without template keys in a guided rule template, the corresponding data table is not generated in the template designer and the template essentially functions as an individual guided rule.
The WHEN part of the rule template is the condition that must be met to execute an action. For example, if a telecommunications company charges customers based on the services they subscribe to (Internet, phone, and TV), then one of the WHEN conditions would be
internetService | equal to | $hasInternetService
. The template key$hasInternetService
is interchanged with an actual Boolean value (true
orfalse
) defined in the data table for the template.The THEN part of the rule template is the action to be performed when the conditional part of the rule has been met. For example, if a customer subscribes to only Internet service, a THEN action for
RecurringPayment
with a template key$amount
would set the actual monthly amount to the integer value defined for Internet service charges in the data table.- After you define all components of the rule, click Save in the guided rule templates designer to save your work.
4.1. Adding WHEN conditions in guided rule templates
The WHEN part of the rule contains the conditions that must be met to execute an action. For example, if a telecommunications company charges customers based on the services they subscribe to (Internet, phone, and TV), then one of the WHEN conditions would be internetService | equal to | $hasInternetService
. The template key $hasInternetService
is interchanged with an actual Boolean value (true
or false
) defined in the data table for the template.
Prerequisites
- All data objects required for your rules have been created or imported and are listed in the Data Objects tab of the guided rule templates designer.
Procedure
In the guided rule templates designer, click the plus icon (
) on the right side of the
WHEN
section.The Add a condition to the rule window with the available condition elements opens.
図4.2 Add a condition to the rule
The list includes the data objects from the Data Objects tab of the guided rule templates designer, any DSL objects defined for the package, and the following standard options:
- The following does not exist: Use this to specify facts and constraints that must not exist.
- The following exists: Use this to specify facts and constraints that must exist. This option is triggered on only the first match, not subsequent matches.
- Any of the following are true: Use this to list any facts or constraints that must be true.
-
From: Use this to define a
From
conditional element for the rule. -
From Accumulate: Use this to define an
Accumulate
conditional element for the rule. -
From Collect: Use this to define a
Collect
conditional element for the rule. -
From Entry Point: Use this to define an
Entry Point
for the pattern. -
Free form DRL: Use this to insert a free-form DRL field where you can define condition elements freely, without the guided rules designer. For template keys in free form DRL, use the format
@{key}
.
- Choose a condition element (for example, Customer) and click Ok.
Click the condition element in the guided rule templates designer and use the Modify constraints for Customer window to add a restriction on a field, apply multiple field constraints, add a new formula style expression, apply an expression editor, or set a variable name.
図4.3 Modify a condition
注記A variable name enables you to identify a fact or field in other constructs within the guided rule. For example, you could set the variable of
Customer
toc
and then referencec
in a separateApplicant
constraint that specifies that theCustomer
is theApplicant
.c : Customer() Applicant( this == c )
c : Customer() Applicant( this == c )
Copy to Clipboard Copied! After you select a constraint, the window closes automatically.
-
Choose an operator for the restriction (for example,
equal to
) from the drop-down menu next to the added restriction. -
Click the edit icon (
) to define the field value.
-
Select Template key and add a template key in the format
$key
if this value varies among the rules that are based on this template. This allows the field value to be interchanged with actual values that you define in the corresponding data table to generate different rules from the same template. For field values that do not vary among the rules and are part of the rule template, you can use any other value type. To apply multiple field constraints, click the condition and in the Modify constraints for Customer window, select All of(And) or Any of(Or) from the Multiple field constraint drop-down menu.
図4.4 Add multiple field constraints
- Click the constraint in the guided rule templates designer and further define the field values.
- After you define all condition elements, click Save in the guided rule templates designer to save your work.