This documentation is for WSO2 Stream Processor 4.1.0. View documentation for the latest release.

All docs This doc
Skip to end of metadata
Go to start of metadata


In the previous tutorials, you covered various Siddhi concepts and created many Siddhi applications to try them out.  In many scenarios, the basic outline of the Siddhi application has been the same. For example, the sources defined for SweetBots are same across multiple Siddhi applications. Furthermore, Siddhi application functionality can be templated so that conditions can be changed over time. Business Rules Management dashboard is a user friendly interface that can be used to build business rules (Siddhi applications customized as per business requirements).

Previously (in tutorial 7), you created a Siddhi application to observe KPIs. The Factory Foreman of the Sweet Factory needs to set different KPIs for the different categories of sweets. For this purpose, you need to template the Siddhi application for KPI analysis that you created in tutorial 7. Using this template, you can create different business rules based on sweet names, surplusMinThresholdInput, and the start and end of work hours.

This tutorial covers Business Rules Management.

Before you begin:

Before creating business rules, the required templates must be defined. The following is a sample template you can use in this tutorial.

 Click here to view the sample template


 "templateGroup" : {

   "name" : "Sweet Production Factory",


   "description" : "Analyzes productions from sweet factory",

   "ruleTemplates" : [


       "name" : "KPI Analysis" ,

       "uuid" : "sweet-production-kpi-analysis",

       "type" : "template",

       "instanceCount" : "many",

       "description" : "Key Performance Indicator analysis for Sweet Production",

       "script" :

       "// Validates given surplus minimum threshold

       function validateAndGetSurplusMinThreshold(amount) {

         if (isNaN(amount) || amount < 1) {

           throw 'Minimum value for surplus should be a positive number';


         return amount;



       // Gets working hour in 24 hours format

       function getWorkingHour(hour, amOrPm) {

         // Validate given hour

         if (isNaN(hour)) {

           throw 'A valid number is expected for hour instead of ' + hour;


         if (hour % 1 != 0) {

           throw 'A full working hour expected instead of ' + hour;


         if ((hour < 1) || (hour > 12)) {

           throw 'Working hour should be in between 1 and 12';


         // Convert to 24 hours format

         if (amOrPm == 'AM') {

           return Number(hour);

         } else {

           return Number(hour) + 12;



       var workHoursStart = getWorkingHour('${workHoursStartInput}', '${workHoursStartAM_PM}');

       var workHoursEnd = getWorkingHour('${workHoursEndInput}', '${workHoursEndAM_PM}');

       var surplusMinThreshold = validateAndGetSurplusMinThreshold('${surplusMinThresholdInput}');",

       "templates" : [

         { "type" : "siddhiApp",

           "content" :


           @source(type='http', receiver.url='http://localhost:5005/SweetProductionEP', @map(type = 'json', @attributes(name = '$.sweet', amount = '$.batch.count')))

           define stream SweetProductionStream (name string, amount long);

           @sink(type='log', prefix='${lowProductionAlertPrefix}')

           define stream LowProductionStream (name string, hourlyTotal long, currentHour int);

           @sink(type='log', prefix='${sweetProductionStatusPrefix}')

           define stream ProductionStatusStream (name string, hourlyTotal long, currentHour int, status string);

           from SweetProductionStream#window.time(${timeInterval} hour)

           select name, sum(amount) as hourlyTotal, time:extract(currentTimeMillis(), 'HOUR') as currentHour, ifThenElse(sum(amount) > ${surplusMinThreshold}, \"Possible surplus\", \"OK\") as status

           group by name

           having currentHour > ${workHoursStart} and currentHour < ${workHoursEnd}

           insert into ProductionStatusStream;"



       "properties" : {

         "lowProductionAlertPrefix": {"fieldName": "Low production alert prefix", "description": "Displayed when a low production alert is logged", "defaultValue": "Low production alert:"},

         "sweetProductionStatusPrefix": {"fieldName": "Sweet production status prefix", "description": "Displayed when a sweet production status is logged", "defaultValue": "Sweet production status:"},

         "timeInterval": {"fieldName": "Surplus analysis time interval (hours)", "description": "Possible surplus is analyzed per time interval", "defaultValue": "1"},

         "surplusMinThresholdInput": {"fieldName": "Minimum value of a surplus", "description": "Amount above this value will be considered as surplus", "defaultValue": "10000"},

         "workHoursStartInput": {"fieldName": "Working hours start at", "description": "Provide a full hour between 1-12", "defaultValue": "9"},

         "workHoursStartAM_PM": {"fieldName": "Working hours start at (AM/PM)", "description": "Select one of the options", "defaultValue": "AM", "options": ["AM", "PM"]},

         "workHoursEndInput": {"fieldName": "Working hours end at", "description": "Provide a full hour between 1-12.", "defaultValue": "5"},

         "workHoursEndAM_PM": {"fieldName": "Working hours end at (AM/PM)", "description": "Select one of the options", "defaultValue": "PM", "options": ["AM", "PM"]}






This template must be placed in the <SP_HOME>/wso2/dashboard/resources/businessRules/templates directory as Sweet_Production_Factory.json.

The group UUID (sweet-production-factory) which is in the ruleTemplates must be added in the <SP_HOME>/conf/dashboard/deployment.yaml file under the namespace as shown below.
 datasource: BUSINESS_RULES_DB
 # rule template wise configuration for deploying business rules
    #ip:port of the node
      # uuids of rule templates which are needed to be deployed on the node given above
      - sweet-production-factory
 # credentials for worker nodes
 username: admin
 password: admin

For more information, see Business Rules Templates.

Tutorial steps

Let's get started!

In this tutorial, the dashboard environment is started in the same computer as the worker environment for simplicity.

  1. To start the dashboard profile of WSO2 SP, issue one of the following commands from the <SP_HOME>/bin directory.
    • For Windows: dashboard.bat

    • For Linux: sh

  2. Now start the worker profile of WSO2 SP, issue one of the following commands from the <SP_HOME>/bin directory. This is needed when saving and deploying a business rule.

    • For Windows: worker.bat

    • For Linux: sh

  3. You can access the Business Rules dashboard via the following URL.

    Then login by specifying admin as both the username and the password. 

    This step uses the default URL and credentials. If the dashboard user store is changed, use a valid credential and ensure that the user has valid permisions for adding and deleting workers. For more information, see User Management.

  4. To create a new rule that can be used for KPI analysis, let's click CREATE.

    If previously defined business rules exist, they are displayed as shown in the example below. You can click + to add a new business rule.

  5. The following view opens when you click CREATE. You can observe that a business rule can be created from a business template or from scratch. You have already created and saved a business rules template. Therefore, let's click From Template to create a rule from it.
  6. When you click From Template the Sweet Production Factory template that you previously created and saved is displayed as follows.

    To create a rule from this template, let's click on this template. It opens as follows.
  7. In the Rule Template field, you can select the KPI Analysis rule template.
  8. You can enter values in the rule template that opens as shown below.

    Click Save and Deploy to proceed. As a result, the following log is displayed in the worker console.
  • No labels