July 30, 2023

tRules – Docs for ESB 7.x

tRules

Uses business rules defined in a Drools file of .xls or .drl format in order to
filter data.

tRules allows you to apply one or
more business rules on a data flow in order to output only relevant
data.

tRules Standard properties

These properties are used to configure tRules running in the Standard Job framework.

The Standard
tRules component belongs to the Processing
family.

The component in this framework is available in all subscription-based Talend products.

Basic settings

Property type

Either Built-In or Repository.

Built-In: No property data stored centrally.

Repository: Select the repository file where the
properties are stored.

The fields that come after are pre-filled in using the fetched data.

For more information, see the Business rules usage.

DRL/XLS FILE

Available when Built-in
is selected.

Browse to the Drools or Excel file that holds the business
rules you want to use in your Job.

You can use business rules stored in external files or create
business rules of DRL format from within
Talend Studio
. The example scenarios of this
component represent the two cases.

Outputs

Set the business rules to use on the output data.

Schema: Create/select the output
schema.

Rule: Create/select the business
rule to use on the corresponding output schema.

Note:

The import field in the code of a rule file needs to
correspond to the current project name.

For more information on creating business rules, see the Drools
documentation in the following site:
http://www.jboss.org/drools/documentation.html

Advanced settings

tStatCatcher Statistics

Select this check box to gather the Job processing metadata at the Job level
as well as at each component level.

Global Variables

Global Variables

ERROR_MESSAGE: the error message generated by the
component when an error occurs. This is an After variable and it returns a string. This
variable functions only if the Die on error check box is
cleared, if the component has this check box.

NB_LINE: the number of rows read by an input component or
transferred to an output component. This is an After variable and it returns an
integer.

A Flow variable functions during the execution of a component while an After variable
functions after the execution of the component.

To fill up a field or expression with a variable, press Ctrl +
Space
to access the variable list and choose the variable to use from it.

For further information about variables, see
Talend Studio

User Guide.

Usage

Usage rule

This component is usually used as an intermediate component, and it requires an
input component and an output component.

Extracting client data according to business rules stored in an external
file

This scenario applies only to subscription-based Talend products.

This scenario is a four-component Job that aims at reading client data and retrieving
only the clients that match business rules stored in an external Drools file.

Prerequisites

For this example, you must have a Drools file of .xls or .drl format that
holds the business rules you will use in the Job.

In this example, the business rules are defined in an Excel file as the following:

tRules_1.png
  • The Import field (cell C:2) must
    respect the following format <projectname>.<lowercase
    jobname>_0_1.<jobname>.*
    . For example,
    dq_project.business_rule_0_1.Business_Rule.* means that the
    name of the project in the studio is dq_project and the name of
    the Job is Business_Rule.

    Make sure to define in this field the exact project and Job names you have
    in your studio.

  • the RuleAGE rule retrieves all
    clients whose age is between 30 and 39 and
    writes them to the first output flow.

  • the RuleREGION rule retrieves all clients
    who live in the EMEA region and writes them to the
    second output flow.

In the output schema of the tRules component, make sure to use the
exact names of the rules defined in the Excel Drools file.

Designing the Job and configuring the input data

  1. Drop the following components from the Palette to the design workspace: tFileInputExcel, tRules
    and two tLogRow.

    tRules_2.png

  2. Double-click tFileInputExcel to display
    its Basic settings view and define the
    component properties.

    tRules_3.png

  3. In the Property type list, select
    Built-in and fill in the fields that
    follow.
  4. Click the three-dot button next to the File
    Name/Stream
    field and browse to the source file to set its path
    and name. The source file used in this example is called client and it holds client data.

    Note:
    As tRules does not support the .xlsx format, do
    not select Read excel2007 file format(xlsx).

  5. If needed, right-click tFileInputExcel and select
    Data viewer to have a preview of the input
    data.
  6. Select the All sheets check box to
    retrieve the data from all sheets of the excel file.
  7. From the Schema list, select Built-in and click Edit schema to open a dialog box where you can define the
    schema of the input file.

    tRules_4.png

    In this example, the source file holds four columns: id,
    name, age and region.

  8. Click OK to validate your changes and
    close the dialog box.

Defining the business rules

Setting the rule schemas
  1. In the design workspace, double-click tRules to display its Basic
    settings
    view and define the component properties.

    tRules_5.png

  2. In the Property type
    list, select Repository if you have
    stored the file that holds the business rules in the Metadata > Rules
    management
    node of the Repository tree view. If not, select Built-in and browse to the Drools file.

    For more information on how to create and store business rules, see
    Talend Studio User Guide
    .

  3. In the Outputs table, click the plus
    button to add two rows that represent two different output flows, each
    using one of the two business rules defined in the Drools file.
  4. Click in the first row of the Schema
    column to display a three-dot button. Click the button to open a dialog
    box and set a name for the output schema.
  5. Enter the exact name of the first rule as it is written in the Drools
    file, RuleAGE in this example, and then click
    OK.
  6. Define your output schema in the dialog box that opens. In this
    example, we want to recuperate the input schema. Click OK to close the dialog box.
  7. Do the same in the second line of the Schema column.

    Enter the exact name of the second rule as it is written in the Drools file
    RuleREGION to have it as the name of the second
    output schema, and then recuperate the input schema in the open dialog
    box.

    You will have an error message when trying to execute the Job if the name of
    the output schemas in your Job do not match the exact names of the rules in
    the Drools file.

Selecting the rules
  1. Click in the first line of the Rule
    column to display a three-dot button. Click the button to open the
    Rule dialog box.

    tRules_6.png

  2. Select the option check box that corresponds to your needs:

    Option

    Description

    View Rules Open the business rule file in read-only mode.
    Select a rule from repository Select the relevant predefined rule from the business rules file
    stored in the Repository tree view.
  3. In the Rule list, select
    the rule you want to apply to the first output schema, RuleAGE, and then click OK.

    The selected rule displays in the Rules column.

    The selected rule displays in the Rules column.

    In this example, we want to apply RuleAGE to the first
    output schema and RuleREGION to the second output
    schema.

  4. Do the same to select the rule for the second output schema,
    RuleREGION and then click OK.
  5. In the design workspace, double-click each of the tLogRow components to define its properties.
    For more information, see tLogRow.
  6. Save your Job and press F6 to execute
    it.
tRules_7.png

The Run console displays the two output flows: the first
output flow lists all clients whose age is between 30 and 39, and
the second output flow lists all clients who live in the EMEA region.

Extracting zip codes using DRL rules you create from the Studio

This scenario applies only to subscription-based Talend products.

This scenario is a three-component Job that aims at creating business rules of DRL
format from the studio. You can then use these rules to retrieve zip codes for two
specific cities you define in the rules.

Creating the DRL rule template

  1. In the Repository tree view, expand
    Metadata > Rules management.
  2. Right-click Embedded Rules and select
    Create Rule.

    tRules_8.png

  3. In the open wizard, enter a name for the rule template, fill in its
    settings as needed and click Next.

    tRules_9.png

  4. Select the Create option and from the
    Type of rule resource list, select
    New DRL.
  5. Click Finish.

    A rule template is created and opened in a rule editor in the workspace.
    tRules_10.png

This rule template is embedded in a tRules
component. You can define one or several DRL rules in the template from inside the
tRules component.

Designing the Job and configuring the input data

  1. Drop a tFixedFlowInput and two tLogRow components from the Palette to the design workspace.
  2. From the Embedded Rules node in the
    Repository tree view, drop the rule
    template you created.

    tRules_11.png

    A tRules component with the embedded rule
    template is displayed on the workspace.
  3. Link tFixedFlowInput to tRule using a Row >
    Main
    link.
  4. Double-click tFileInputDelimited to
    display its Basic settings view and define
    the component properties.

    tRules_12.png

  5. Click the […] button next to Edit schema to open the schema editor.

    tRules_13.png

  6. Add two rows using the [+] button, name
    the rows as zipCode and CityName
    and click OK.

    When you define the DRL rules, you will use the
    zipCode column to match the city zip codes and the
    CityName column to output the name of the city that
    match the zip code.
    Note:

    Make sure to start the column name you will use to match the zip code
    with lower case, otherwise you will get an error when trying to run the
    Job.

  7. In the Mode area, select the Use Inline Content (delimited file).
  8. Set the row and field separators, and in the Content table, type in the delimited data on which to apply
    the DRL rules.

Defining the DRL rules

Setting the rule schemas

  1. In the design workspace, double-click tRules to display its Basic
    settings
    view and define the component properties.

    tRules_14.png

    The Property Type is automatically set to
    Repository as you have already stored
    the rule template in the Studio.
  2. Click the […] button to open a dialog
    box that lists the DRL rules stored locally in the repository.

    tRules_15.png

  3. Select the rule template in which you want to define the rule schemas,
    ZipCodeRuleSet in this example, and then click
    OK.
  4. Use the [+] button to add two rows to the
    Outputs table, click in the Schema column and then click the […] button.

    tRules_16.png

  5. In the open dialog box, set a name for the first output schema, call it
    Paris, and click OK.
  6. In the open dialog box, define your output schema. Copy
    zipCode and CityName from the
    input flow to the output flow and click OK.

    tRules_17.png

  7. Do the same to create a second output schema, call it
    Suresnes and similarly copy the two input columns
    to the output flow.

    Each of the two output schemas will use one of the two DRL rules you will
    define in the rule template.
  8. Right-click tRules and select Row > Paris to link the component to the first
    tLogRow.
  9. Do the same and link tRules to the second
    tLogRow using the Row > Suresnes link.

Creating the DRL rules

  1. In the Outputs table, click in the
    Rule column and then click the
    […] button of the
    Paris schema.

    tRules_18.png

  2. In the open dialog box, select one of the options as the following:

    Select

    To…

    Edit Rules

    open the rule in the rule editor in the
    workspace.

    Create a rule with
    guide

    open a dialog box where you can define a rule in the
    rule template.

    select a rule from
    repository

    select a predefined rule from the rule template
    created and stored in the Repository tree view.

    In this example, select the Create a rule with
    guide
    option.
  3. In the open dialog box, use the Drools syntax to set the condition of the
    “Paris” rule as the following: zipCode
    matches "75\d{3}"
    , and then click OK.

    tRules_19.png

    The new “Paris” rule is generated and displayed in
    the Rule column. This rule retrieves from
    the Paris schema all zip codes that start with 75 and
    the three figures that follow.
  4. Click in the Rule column and then click
    the three-dot button of the “Paris” rule.

    The rule template is opened in the rule editor in the workspace.
    tRules_20.png

  5. In the “Paris” rule, add the code
    output.CityName = "Paris" to output
    Paris as the city name in the first output
    flow.
  6. Repeat the above steps to create a “Suresnes” rule
    and set its condition as the following zipCode ==
    "92150"
    .

    tRules_21.png

    The new rule is displayed in the Rule
    column. This rule retrieves from the Suresnes schema
    all zip codes that are equal to 92150.
  7. In the “Suresnes” rule, add the code
    output.CityName = "Suresnes" to output
    Suresnes as the city name in the second output
    flow.
  8. In the design workspace, double-click each of the tLogRow components to define its properties.

    For more information, see tLogRow.
  9. Save your Job and press F6 to execute
    it.

    tRules_22.png

    The Run console displays two output flows
    with zip codes and city names.
    In the first output flow, the “Paris” rule retrieves
    all zip codes that start with 75 and writes the city name as
    Paris.
    In the second output flow, the “Suresnes” rule retrieves all zip
    codes that are equal to 92150 and writes the city name as
    Suresnes.

Business rules usage in tRules

You can import business rules or create them from Talend Studio.

When you create business rules of DRL format from Talend Studio, the created file is a DRL rule
template that partially conforms to the Drools syntax. For more information on how to
create the business rules, see the Creating the DRL rule template procedure.

To use a DRL rule template in your Job, select Property Type > Repository. When the Job is running, a file that conforms to the Drools syntax is
generated from the template. This file is used in the Job.

In Built-in mode, the file you browse must
conform to the Drools syntax. You cannot browse the Studio repository
folders to select a DRL rule template.

If you switch the Property Type setting
from Repository to Built-in, adapt the DRL file to the Drools syntax.


Document get from Talend https://help.talend.com
Thank you for watching.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x