Business Rules Template Editor for WSO2 Stream Processor

Senthuran Ambalavanar
6 min readJan 12, 2018

--

As an enhancement for the WSO2 Business Rules Manager — which was my first internship project at WSO2, I had the opportunity to work on the Business Rules Template Editor for WSO2 Stream Processor.

The Business Rules Manager relies on templates, for creating deployable SiddhiApps. To get an in-depth idea of this process, you are welcome to refer my previous blog post.

Developing business rule templates for use cases with the concern of re-usability, is not going to be a very easy task when your SiddhiApps grow bigger.

As an example, when working with a script like the one below in a rule template, it is normal to make mistakes, varying from escaping regex patterns in a script — to referring script variables’ provider elements in properties.

A carefully escaped script definition (above) and the actual working script (below)

When a lot of elements are templated, and perhaps more processing is done via scripts, there is going to be some amount of struggle in creating a template.

The Business Rules Template Editor provides the ability to create templates without much struggle like this, by adhering to the conventions used in a business rule template.

Note: A user of the Business Rules Template Editor, is actually a developer of business rule templates.

The overall idea

The basic idea is to create a visual representation of the templateGroup. The user should actually see how the template group is structured and used, and should be able to interact with it. The user will be creating a template group, at a time.

The Design View

In the Design View, the user can interact with the templateGroup, and its granular components — Rule Templates, and Templates.

Updating information from the DesignView

The Code View

In the Code View, the user can type in values for creating the template group definition. Updating the code view is also going to visualize the templateGroup simultaneously in the design view, which is useful such that the user will become more used to the constraints and conventions of the template definition himself/herself.

This is going to be similar to working with any JSON compatible editor. But, this will be adhering to the conventions of a template group definition.

Updating information from the Code View

A valid template group definition: A template group has a minimal skeleton, that is, a JSON structure having all the mandatory keys. That structure, is known as a valid template group definition.

The content entered in the code view is converted to a template group, only when it is a valid template group definition. After that, the template group definition is updated as you type, until it becomes invalid again.

In an occurrence where the content is a valid JSON, but not a valid template group (eg: No name has been given for a template group), the relevant error is shown. The process of updating the template group definition is halted until a valid definition is captured.

An error, shown for an invalid template group definition

With the error message shown during a messed up state, there is a Recover button.

This will revert the current state of the code view, to the last ever updated valid template group definition. Note that, at any given time, the design view will show the latest ever updated template group definition.

Reverting to a valid template group definition

Creating a Template Group

Creating from the Code View

When you start from the empty skeleton, errors are updated & shown until you reach a valid definition. The template group definition will be updated for the first time, at that point.

Template group definition updates for the first time

Creating from the Design View

The first ever element added to a template group, will be a Siddhi app template (template).

Adding a templated Siddhi app

Adding templated elements from SiddhiApp template, to the script

The script is optionally used when the value for a templated element is going to be processed in some manner after acquired from the user, before replacing the SiddhiApp.

A scenario without script
A scenario where script is used

When Add Variables button is clicked, a prompt is given for selecting templated elements from SiddhiApp template(s), to add to the script.

After selecting and adding the elements to script, each of them will be added as a variable along with a sample function in the script. The user can then add some logic to the script by either modifying the generated function or adding new function(s).

The following figure illustrates how this process is done:

The script is obviously going to have some templated elements in it, otherwise it’s not possible to get values for the script variables, from the business user, when creating business rules.

The following GIF shows, how it is specified in the script — to generate a HTTP URL with the port given by the business user.

Adding a script & modifying auto-generated function

Generating Properties

Each templated element from the script; and each templated element from the SiddhiApp template — that was not added to script, is going to be generated as a property. These properties will be the input fields when a business rule is being derived with the Business Rules Manager.

After clicking the Generate button, properties will be produced in order to be filled.

This process is illustrated in the following figure:

The following GIF shows that, how properties for templated elements from the script are generated, and filled.

Generating properties & filling them
Few configurations for the rule template

Filling the fields for each properties, and completing the necessary values related to the rule template (e.g: type, instanceCount), will mark the end of a rule template’s creation.

Then, you can add more rule templates and configure them, if you need.

Finally, configure the template group’s uuid, name and description. We have the template group ready now.

Overall, this is going to be a step-by-step process of creating templates for the business rules manager. A created template can be downloaded to the resources folder of WSO2_SP, and used from the business rules manager.

The following image shows the high level architecture of the Business Rules Template Editor.

The Business Rules Template Editor can be opened up by going to <WSO2SP_HOME>/bin and launching editor.sh, and following the URL of template-editor.

Have a look at the WSO2 Documentation on using the Business Rules Template Editor for more details.

A template group, loaded in the Business Rules Template Editor

--

--