6.5. Rule Templates

6.5.1. The Guided Rule Template

Rule Templates allow the user to define a rule structure. They provide a place-holder for values and data, and they populate templates to generate many rules. From the user's perspective, Guided Rule Templates are a parametrized guided rule with a data table which provides parameter values. This can allow for more flexible decision tables and it can enhance the flexibility of rules in existing databases. For more information about Rule Templates, see the Red Hat JBoss BRMS Development Guide.

Procedure 6.5. Creating a new Guided Rule Template

  1. In the Project Explorer view, do one of the following:
    • If you are in the Project view, select the organizational unit, repository, and the project where you want to create the template.
    • If you are in the Repository view, navigate to src/main/resources/ and the SUBFOLDER/PACKAGE where you want to create the project folder for the rule template.
  2. In the perspective menu, go to New ItemGuided Rule Template.
  3. In the Create new Guided Rule Template dialog window, specify the rule template name:
    1. In the Resource Name text box, enter the Guided Rule Template name and click OK.
  4. The new Guided Rule Template is now created and under the selected project.
Red Hat JBoss BRMS Guided Template Editor screenshot example for BRMS USer Guide 6.0.2.

Figure 6.25. Guided Template Editor

Note

Rule templates created within Business Central are supported but only when created and used through Business Central. Explicit use of drools-templates api and jar is not.

6.5.2. WHEN conditions in the Guided Rule Template

The Guided Template Editor within Business Central allows users to set rule templates where the data is completely separate from the rules.
The editor contains Edit, Source, Data, Config, and Metadata tabs.
Within this section, we will explore how to alter the WHEN constrains within the Edit tab.

Note

In the Guided Rule Template example procedures below, a Nurse Rostering data model was created for a fictitious hospital, Sister Veronica's .

Procedure 6.6. Using the Guided Template Editor with WHEN constraints

  1. Assuming you have already set up a Data Model for your project (as described in Section 5.3, “Creating a Data Object (Not Persistable)”), select the plus icon to the right of the WHEN section of the Guided Template Editor.
  2. A dialog window will appear with available condition templates to choose from. In the example below, we select the Nurse... condition from the list.
    Guided Template Editor dialog window for BRMS User Guide 6.0.2

    Figure 6.26. Nurse Roster WHEN Dialog Window

  3. Click OK and the Guided Template Editor will display your WHEN condition.
  4. Click on the newly added WHEN condition. In the example below, it is the "There is a Nurse" condition. A "Modify constraints for Nurse" dialog appears.
    The Modify Constrain dialog window for BRMS User Guide 6.0.2

    Figure 6.27. Modify Constraints Dialog

  5. From here you can 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.
  6. In the example below, we will add a restriction of servicelength to the condition.
    Constraint option for Guided Template Editor in BRMS User Guide 6.0.2

    Figure 6.28. Title

  7. Once selected, the dialog window closes automatically.
  8. Next to the newly selected restriction will be a drop down box to choose an operator. In the example below, we have chosen an operator of "less than."
    WHEN constraint for BRMS User Guide 6.0.2 drop down list.

    Figure 6.29. Restriction drop-down menu

  9. By selecting the Edit Icon within the restrictions field, you will be able to define the field value with a literal value, template key, a formula, or expression editor.
    Field Value options for the Guided Template Editor in BRMS User Guide 6.0.1

    Figure 6.30. Field Value Options

  10. By clicking on the WHEN condition again, we can supply a variable name to help define the restriction. In the example below, we name it "nurse" and click the Set button.
    Setting the Variable Name for a condition in the Guided Template Editor in BRMS User Guide 6.0.2

    Figure 6.31. Setting a variable name

  11. Continue to add WHEN conditions as appropriate for the project. The example below demonstrates "servicelength" and "rating" constraints.
    Multiple WHEN constraints for Guided Template Editor in BRMS User Guide 6.0.2

    Figure 6.32. WHEN Constraints

6.5.3. THEN actions in the Guided Rule Template

The THEN section of a rule holds the actions to be executed when it matches the WHEN section.This section explores how to alter the THEN actions within the Edit tab of the Guided Template Editor.

Procedure 6.7. Using the Guided Template Editor with THEN actions

  1. Select the plus icon to the right of the THEN section of the Guided Template Editor to input THEN actions.
  2. A dialog window will appear with available action templates to choose from. In the example below, we select the Modify nurse... action from the list.
    Guided Template Editor dialog window for BRMS User Guide 6.0.2

    Figure 6.33. Nurse Roster THEN Dialog Window

  3. Click OK and the Guided Template Editor will display your THEN action.
  4. Click on the newly added THEN action. In the example below, it is the "Modify value of Nurse [nurse]" action. An "Add a field" dialog appears.
    An Add a field dialog for the Guided Template Editor in BRMS User Guide 6.0.2

    Figure 6.34. Add a field Dialog

  5. Within this dialog, you can choose a field from the Add field drop-down menu.
  6. Once selected, the dialog window closes automatically.
  7. By selecting the Edit Icon within the item field, you will be able to define the field value with a literal value, template key, or a formula.
    Field Value options for the Guided Template Editor in BRMS User Guide 6.0.1

    Figure 6.35. Field Value Options

6.5.4. Data Tables in the Guided Rule Template

Data tables can be altered within the Guided Template Editor directly by clicking on the Data tab. The procedure below illustrates how to alter the data created within Guided Template Editor itself.

Procedure 6.8. Using the Guided Template Editor with Data Tables

  1. Click on the Data tab at the bottom of the Guided Template Editor in order to access the newly created data table.
  2. Click the Add row... button to add more table rows.
  3. Input additional data into the table. In the example below, we see the ServiceLessThan, ServiceGreaterThan, EmployeeRating, and VacationTime column options and supply data to each field.
    An Example Guided Template Editor Data table for the BRMS User Guide 6.0.2

    Figure 6.36. Data Table for Guided Template Editor

  4. To view the code source, click the Source tab at the bottom of the Guided Template Editor. Illustrated below is the source code for the Nurse example in the Veronica Template.
    Code Source information for the Guided Template Editor in BRMS User Guide 6.0.2

    Figure 6.37. Source Code for Nurse Example

  5. Save the template when you are finished working in the Guided Template Editor.