Chapter 4. Hello World Rule Example

This example demonstrates basic features of Red Hat JBoss BRMS by setting up a Hello World project with a simple business rule.
In this project, you will:
  1. Create a repository in the Artifact repository.
  2. Create a project.
  3. Create a rule.
  4. Create a knowledgebase.
  5. Build and deploy the project.
  6. Create a Maven project to fire the rule.

4.1. Create Your First Rule Using Business Central

Prerequisite

Ensure that you have successfully installed JBoss BRMS and Maven before you run this simple rule example using Business Central interface.

Procedure 4.1. Create and Execute your First Rule using Business Central

  1. Log into Business Central

    1. On the command line, change into the $SERVER_HOME/bin/ directory and execute the following command:
      • for Unix environment:
        ./standalone.sh
      • for Windows environment:
        ./standalone.bat
    2. Once your server is up and running, open the following address in a web browser:
      http://localhost:8080/business-central
      This opens the Business Central login page.
    3. Log into the Business Central with the user credentials created during installation.
  2. Create a repository structure and create a project under it

    1. On the main menu of Business Central, go to AuthoringAdministration.
    2. Click Organizational UnitsManage Organizational Units, then click Add.
    3. In the displayed Add New Organizational Unit dialog box, define the unit properties. For example:
      • Name: EmployeeWage
      • Owner: Employee
      Click OK.
    4. On the perspective menu, click RepositoriesNew repository.
    5. In the displayed Create Repository dialog box, define the repository properties. For example:
      • Repository Name: EmployeeRepo
      • Organizational Unit: EmployeeWage
      Click Finish.
    6. Go to AuthoringProject Authoring.
    7. In the Project Explorer, under the organizational unit drop-down box, select EmployeeWage, and in the repository drop-down box select EmployeeRepo.
    8. On the perspective menu, go to New ItemProject.
    9. In the displayed Create new Project dialog box, provide a name (for example, MyProject) for your project properties and click Ok.
    10. In the New Project dialog box, define the maven properties of the project. For example:
      • Group ID: org.brms
      • Artifact ID: MyProject
      • Version ID: 1.0.0
      Click Finish.
  3. Create a fact model

    1. On the perspective menu, go to New ItemData Object.
    2. In the displayed Create new Data Object dialog box, provide the values for object name and package. For example:
      • Data Object: Person
      • Package: org.brms.myproject
      Click Ok.
    3. In the displayed Person window of the newly created Person data object, click add field to open the New field dialogue. Add a variable name in the Id field, select data type for the variable in the Type field, and click Create and continue until you have defined all the necessary variables. For example:
      • Id: firstName
        Type: String
      • Id: lastName
        Type: String
      • Id: hourlyRate
        Type: Integer
      • Id: wage
        Type: Integer
      Click Create for the last variable and then Save.
  4. Create a rule

    1. On the perspective menu, click New ItemDRL File.
    2. In the Create new dialog box, provide the name and package name of your rule file. For example:
      • DRL file name: MyRule
      • Package: org.brms.myproject
      Click Ok.
    3. In the displayed DRL editor with the MyRule.drl file, write your rule as shown below:
      package org.brms.myproject;
      rule "MyRule"
      when
      
      	Person(hourlyRate*wage > 100)
      	Person(name : firstName, surname : lastName)
      
      then
      	System.out.println( "Hello" + " " + name + " " + surname + "!" );
      	System.out.println( "You are rich!" );
      
      end
    4. Click Save.
    Rules are simple when-then statements stored in DRL files. This example has two conditions:
    • Person(hourlyRate*wage > 100)
      
    • Person(name : firstName, surname : lastName)
      
    The first line evaluates a logical condition. The second line searches for an instance of the data object Person and saves its attributes, that is, the firstName and lastName into variables used in the second part of the rule.
    If the conditions are met, the then part of the rule executes. In this example, two lines will be printed out in the JBoss BRMS command line.
  5. Create a Knowledge Base

    Following steps demonstrate how to create your own knowledge base. For more information about KieSession, see Chapter 16.2.3 from the Red Hat JBoss BPM Suite Development Guide.
    1. Click Open Project Editor.
    2. Navigate to the drop-down menu and click Project Settings: Project General SettingsKnowledge Base Settings: Knowledge bases and sessions.
    3. Click Add and enter the name of your knowledge base. For this example, enter: myBase.
    4. Click Make Default
    5. Click Add under Packages.
    6. In this step, you are specifying which packages should your knowledge base include. In this example, we want all the packages. Therefore, enter * (an asterisk).
    7. Click Add under Knowledge Sessions.
    8. Enter the name of your session. For example, mySession. Check Default and select the stateless state.
    9. Click Save in the top right corner.
  6. Build and deploy your rule

    1. Click Open Project Editor and then click Build & Deploy.
      A green notification appears in the upper part of the screen informing you that the project has been built and deployed successfully to the Execution Server.

    Note

    In case a red notification appears, informing you that the build has failed, you will be presented with information about the build failure in the JBoss BRMS console. This is crucial information in case of troubleshooting your application. Make sure you have saved all assets before attempting to build your project.