Queries support in the Rules Engine

ES | EN

Available from version 3.0.0

It has been possible for a long time to create business rules on the platform thanks to the Rules Engine module.

In this Q1 we have made several significant improvements in this module, such as the support of Decision Tables, a new wrapper to be able to execute queries on ontologies in a very simple way within the rule and the possibility of loading a JSON from a file in the part rules testing.

Queries support

For the execution of queries on Ontologies in the rule, a Wrapper class (QueryWrapper) is used, this Wrapper has to be imported in the rule in order to be used.

In order to operate with this new class, it must be taken into account that the connection will be made through a Digital Client, so that only the ontologies associated with the Digital Client used can be consulted. To create a QueryWrapper object we need to indicate:

  • Ontology that we want to consult

  • Query to execute

  • Digital Client in which we have the associated Ontology

  • Digital Client Token

  • Connection URL to specify the environment in which you are working

QueryWrapper queryWrapper = new QueryWrapper(<ontologyName>, <query>, <digitalClientName>, <token>, <URL_conection>);

In the event that the Digital Client does not have permissions on the queried Ontology or the error Query, the rule will not be executed

Once the queryWrapper object has been created, we proceed to execute the query, for this it is enough to call the run() method

queryWrapper.run();

Once the query is executed, in the queryResult parameter of the queryWrapper object we will have the result of the Query, which will be a list of OntologyJsonWrapper.

List<OntologyJsonWrapper> result = queryWrapper.getQueryResult();

Ejemplo regla definida en DRL

Let's analyze the following example:

  • Line 2 and 3: Wrappers are imported

  • Line 4: the global variable output is defined

  • Line 8: The existEmployee function is defined, where a query is launched on the employee ontology to see if there is an employee with a specific identifier

  • Line 18: The result of the existEmployee function is evaluated for the id parameter of the input variable. In the event that the selected employee does not exist, the rule is executed and the employee is created in the target ontology.

Example rule defined in DDT

You can also use this new Wrapper in Decision Tables. Next, we see the same previous example but this time defined with a Decision Table: