---
title: "Queries support in the Rules Engine"
canonical: "https://onesaitplatform.refined.site/space/DOCT/2220808690/Queries%20support%20in%20the%20Rules%20Engine"
format: markdown
---
[ES](https://onesaitplatform.atlassian.net/wiki/pages/createpage.action?spaceKey=doct&title=Soporte%20de%20Queries%20en%20el%20Rules%20Engine&linkCreation=true&fromPageId=2220808690) | EN

> Macro (toc)

> ℹ️ 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](https://onesaitplatform.atlassian.net/wiki/spaces/DOCT/pages/2220808591)**, 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](https://onesaitplatform.atlassian.net/wiki/spaces/DOCT/pages/2220808736)**[.](https://onesaitplatform.atlassian.net/wiki/spaces/DOCT/pages/2220808736)

# 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

```sql
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

```java
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**.

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

### Ejemplo regla definida en DRL

Let's analyze the following example:

```java
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:

![image](media://f481903c-2ca6-437e-82b6-3992d7de5bbc)