Queries support in the Rules Engine


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

1 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

1 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.

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

Ejemplo regla definida en DRL

Let's analyze the following example:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package com.minsait.onesait.platform.rulesengine; import com.minsait.onesait.platform.rulesengine.model.OntologyJsonWrapper; import com.minsait.onesait.platform.rulesengine.model.QueryWrapper; global com.minsait.onesait.platform.rulesengine.model.OntologyJsonWrapper output; dialect "mvel" function Boolean existEmployee(Object id){ QueryWrapper queryWrapper = new QueryWrapper("employee", "select * from employee as c where c.employee.id = " + Integer.parseInt(id.toString()), "client", "917069adcdf94d96832e711acc9c6631", "http://localhost:19000"); queryWrapper.run(); return !queryWrapper.getQueryResult().isEmpty(); } rule "Create new employee" when input: OntologyJsonWrapper() eval( !existEmployee(input.getProperty("id"))) then output.setRootNode("employee") output.setProperty("id", input.getProperty("id")); output.setProperty("rol", "Consultant"); end
  • 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: