How to create Business Rules on Platform?
You can create business rules on the platform thanks to the Rules Engine module defined in DRL (Drools Rule Language).
There are two types of platform rules:
Rules on ontologies: these rules are associated with two ontologies: 'trigger' ontology and target ontology, so that, whenever an insert is made on the trigger ontology on the platform, the rule is evaluated, and the results of the rule are stored in the target ontology.
REST rules: these rules are not associated with any ontology, and are executed only on demand, making a REST request.
Both types of rules work with JSON both in and out. This JSON is embedded in a Wrapper class called OntologyJsonWrapper; we will explain later how to use it.
Rule Domain
To create the rules, we must access our rule engine domain through this menu:
Here, you will see your associated domain on which you can create rules. By default it will be on. If you stop it, your rules defined on ontologies will not be automatically executed.
Access our rules directory through the eye icon. In this view, you will see all your rules, and you can edit their code, test them, enable and unenable them, edit associated ontologies, and even delete them.
To edit the DRL, simply modify the code and press 'Edit'. If there is an error in the syntax when updating the rule, an error like the following one will be displayed, and the rule will not be updated:
To perform a test on the rule, press the 'Test' button and a pop-up will open where you will have to enter an input JSON, then press the 'Test' button again:
This utility allows us to test the rules by simulating ontology data entry in the platform.
If you want to enable or unenable, edit or delete a rule, you must right-click on the rule in the navigation menu:
The rules that are disabled will appear in gray in the menu:
Rule Creation
To create a rule, click on the 'Create' button. There you can indicate the type of rule you want, associate the ontologies if appropriate, and implement the logic of the rule itself.
This link is provided for the logic of the rule, which explains the syntax of the DRL.
Rule's input and output variables
As we mentioned before, to input and output data in the rule, you use a Wrapper class (OntologyJsonWrapper). This class provides a simple API to work with its JSON attributes. The different available methods are detailed in green (comment) in the example rule that appears in the editor at the time of creation:
Input variable -> input
Output variable -> output
Bearibn mind: If the target ontology contains a root JSON Node, you must specify it like this:
Otherwise, the output data of the rule will fail validation when trying to insert.
DRL flexibility
The DRL language allows you to work with Java code in the "then" segment, so you can inject Java code to perform actions, for example, send a REST request (don't forget the imports , otherwise the rule will not pass the compilation).
REST API
For any rule, and in particular for those of REST type , there is an endpoint to execute a rule on demand:
URL: https://{URL_ENTORNO}/rules-engine/execute/rule/:identification -> identification is the rule's name such as: RULE_SENSOR_ALERT
HTTP Method: POST
Headers: Authentication -> Bearer token Oauth2 of the plataform.
Request body: JSON