# Querying the data

• Querying data Loaded into DB. The tool can request data stored according to two storage schemas (see RDFDB tool):
• Triples table.
• Table per Role and Concept.

## Query syntax: Conjunctive Queries (CQs)

A CQ notation is used to express queries:

$Latex formula$
```Q01<\$X, \$Y> :-
triple(\$X, <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, <http://contentcheck.fr/schemas/Creator>),
triple(\$X, <http://contentcheck.fr/schemas/name>, \$Y);```

### Prerequisites:

• To run QuerySession, you will need the Java Runtime Environment (JRE). The tool was tested with JRE 1.8.
• The tool is configured by default to be used with PostgreSQL (see default configuration file)

• The QuerySession JAR file named as QuerySession-{version}.jar.
• The configuration directory which contains the default configuration file.
• query directory which contains queries of the example.

## QuerySession Home

```| + conf
|     - QuerySession.prop
| +  query
|          - *.queries file
|  QuerySession-{version}.jar
```

## How to use

```java -jar QuerySession-{version}.jar -type ucq -qr queries_file
```

For each query, a result set file will be created and by default the name for each file will be “RS_QueryName”, however, it is possible to change the pattern name for each result set using:

`-rs pattern_name`
1. The conf folder contains the default property file nammed QuerySession.prop. However, it is possible to specify another property file using the parameter:
```-pf the_path_of_a_new_property_file.prop

```

#### Translating approaches

##### Translating on the T-Layout (Triples table)
###### 1.  translating a CQ using a basic approach without any reformulation on a saturated graph into SQL.

```//create the TranslatedCQBuilder object TranslatedCQBuilder translateCQ = new TranslatedBasicQuery.Builder ().setConfiguration (config).build (); ConjunctiveQuery conjQuery = new ConjunctiveQuery(head,triples); translateCQ.setConjQuery (conjQuery);// set the encoded Query. String sqlQuery = translateCQ.getSQLQuery (); // get the translated Query```

2.  it is also possible to translate the CQ using this layout through a UCQ reformulation on a original (unsaturated) graph.

```//create the TranslatedCQBuilder object TranslatedCQBuilder translateCQ = new TranslatedUCQQuery.Builder ().setConfiguration (config).build (); ConjunctiveQuery conjQuery = new ConjunctiveQuery(head,triples); translateCQ.setConjQuery (conjQuery);// set the encoded Query. translateCQ.setTripleTranslated (true); // the translation of UCQ will be done on a T-layout. String sqlQuery = translateCQ.getSQLQuery (); // get the translated Query```

In the both cases, you need to set the database.storage_layout to TRIPLES_TABLE in the configuration file.

##### Translating on the  CP Layout (Table per Role and Concept)

Using this layout (database.storage_layout set to TABLE_PER_ROLE_AND_CONCEPT), two approaches can be used to translate CQ to SQL on a saturated graph.

1. Basic approach

```//create the TranslatedCQBuilder object TranslatedCQBuilder translateCQ = new TranslatedBasicQuery.Builder ().setConfiguration (config).build (); ConjunctiveQuery conjQuery = new ConjunctiveQuery(head,triples); translateCQ.setConjQuery (conjQuery);// set the encoded Query. String sqlQuery = translateCQ.getSQLQuery (); // get the translated Query. ```

2. Direct approach (with…as..), in this approach unspecified type triples (triples with rdf:type in property and object is a variable)  and  generic triples (triples where property is a variable) will be materialized using a union of conjunctive queries.

```//create the TranslatedCQBuilder object TranslatedCQBuilder translateCQ = new TranslatedBasicQuery.Builder ().setConfiguration (config).build (); ConjunctiveQuery conjQuery = new ConjunctiveQuery(head,triples); translateCQ.setConjQuery (conjQuery);// set the encoded Query. translateCQ.setDirectTranslation (true); // a direct translation will be performed. String sqlQuery = translateCQ.getSQLQuery (); // get the translated Query. ```

Using reformulations, three algorithms are available for this layout

• Semi-conjunctive queries (SCQ)

```//create the TranslatedCQBuilder object TranslatedCQBuilder translateCQ = new TranslatedSCQQuery.Builder ().setConfiguration (config).build (); ConjunctiveQuery conjQuery = new ConjunctiveQuery(head,triples); translateCQ.setConjQuery (conjQuery);// set the encoded Query. String sqlQuery = translateCQ.getSQLQuery (); // get the translated. Query. ```

• Union of conjunctive queries (UCQ)

```//create the TranslatedCQBuilder object TranslatedCQBuilder translateCQ = new TranslatedUCQQuery.Builder ().setConfiguration (config).build (); ConjunctiveQuery conjQuery = new ConjunctiveQuery(head,triples); translateCQ.setConjQuery (conjQuery);// set the encoded Query. String sqlQuery = translateCQ.getSQLQuery (); // get the translated Query. ```

• Unions of conjunctive queries (JUCQ) and a cost model.

```//create the TranslatedCQBuilder object TranslatedCQBuilder translateCQ = new TranslatedJUCQQuery.Builder ().setConfiguration (config).build (); ConjunctiveQuery conjQuery = new ConjunctiveQuery(head,triples); translateCQ.setConjQuery (conjQuery);// set the encoded Query. String sqlQuery = translateCQ.getSQLQuery (); // get the translated Query. ```

##### Translating on the TCP Layouts

```//create the TranslatedCQBuilder object TranslatedCQBuilder translateCQ = new TranslatedBasicQuery.Builder ().setConfiguration (config).build (); ConjunctiveQuery conjQuery = new ConjunctiveQuery(head,triples); translateCQ.setConjQuery (conjQuery);// set the encoded Query. translateCQ.setDirectTranslation (true); // a direct translation will be performed. translateCQ.setTCPTranslation (true); // set TCP layout. String sqlQuery = translateCQ.getSQLQuery (); // get the translated Query. ```

### Generating the JAR file using Maven

1. Import the project into your workspace.
2. Use mvn package command line to generate the JAR File.
3. The JAR file will be generated under target folder named: QuerySession-{version}.jar

More details to how build project using maven can be found here: https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

### Working with Eclipse

1. Import the project into you Eclipse workspace.
3. Find the application under the Java application
4. Put the arguments as in above section.
5. Run the application.

## Configuration

##### A set of parameters need to be set in teh configuration file, as follows:
• Database properties
```# Database engine

database.engine = POSTGRESQL

# Database host
database.host = localhost

# Database port
database.port = 5432

# Database name
database.name = cedardb

# Database user
database.user = postgres

# If this parameter is true and if there already exists a database of the given name,
# then the database is dropped before being rewritten.
database.drop_existing_db = true
```
• triple table properties
```# The RDBMS (input) triples table name
database.triples_table_name = triples

# The encoded triples table name
database.encoded_triples_table_name = encoded_triples```
• dictionary table properties
```# Database dictionary table name.
# Use to encode the input query and ontology and decode the results.
database.dictionary_table_name = dictionary```
• database storage layout
```# Valid options: TRIPLES_TABLE, TABLE_PER_ROLE_AND_CONCEPT
# Database (physical) storage layout.
# Valid options: TRIPLES_TABLE, TABLE_PER_ROLE_AND_CONCEPT
database.storage_layout = TRIPLES_TABLE
#database.storage_layout = TABLE_PER_ROLE_AND_CONCEPT```
• cost model options: these options are related to the cost model  used to compare the best DAG plan generated when “JUCQ”  is chosen as the query answering strategy.
```# Valid options: TRIPLES_TABLE_COST_MODEL, TABLE_PER_ROLE_AND_CONCEPT_COST_MODEL, POSTGRESQL
cover_selection_algorithm.cost_function = TABLE_PER_ROLE_AND_CONCEPT_COST_MODEL

#+++++++++++++++++++++++++++++++ TRIPLES TABLE COST MODEL PROPERTIES ++++++++++++++++++++++++++++++#
# Cost model cost model constants name.
cost_model.constants_name = POSTGRESQL_CONTSTANTS
# Cost model algorithm IO constant.
cost_model.io_constant = 0.00046167385
# Cost model CPU constant.
cost_model.cpu_constant = 0.00018806125
# Cost model materialization constant.
cost_model.materialization_constant = 0.00039235885
# Cost model index 1 constant.
cost_model.index1_constant = 0.001285472726
# Cost model index 2 constant.
cost_model.index2_constant = 0.00053436378
# Cost model external disk unique flag
cost_model.external_disk_unique_flag = false
# Cost model larger fragment sub-query intermediate result pipelining flag.
cost_model.pipeline_larger_subquery_flag = true
```
• Properties characterizing the translation to SQL
```# SQL main query distinct flag
translator.main_query_distinct_flag = true

# Attributes prefix for attributes renaming in SQL.
translator.attribute_prefix = att_

# Relation prefix for relations renaming in SQL.
# Used when the storage schema is TRIPLES_TABLE (see UNDERLYING RDBMS ENGINE PROPERTIES).
translator.relation_prefix = p

# Database dictionary-encoded (large) triples (s,p,o) table name.
# Used when the storage layout is TRIPLES_TABLE (see UNDERLYING RDBMS ENGINE PROPERTIES).
translator.encoded_triples_table_name = encoded_triples

# Database prefix for concept/role table names.
# Used when the storage layout is TABLE_PER_ROLE_AND_CONCEPT (see UNDERLYING RDBMS ENGINE PROPERTIES).
translator.table_name_prefix = t_

# CQ atom (triple pattern) prefix for relations renaming.
# Used when the storage layout is TABLE_PER_ROLE_AND_CONCEPT (see UNDERLYING RDBMS ENGINE PROPERTIES).
translator.alias_prefix = tt_
```
• Whether to use bag or set semantics
```# #Valid options: Set, Bag
query.semantics = Set```
`reasoner.type = ASSERTION_REF`

#### Translating approaches Using the command Line

##### Translating on the T-Layout (Triples table)
```#Set the storage layout to TRIPLES_TABLE
database.storage_layout = TRIPLES_TABLE
java -jar QuerySession.jar -pf confFile -qr queryFile -type SAT```
##### Translating on the CP-Layout (Table for each Concept and Role)
```#Set the storage layout to TABLE_PER_ROLE_AND_CONCEPT
database.storage_layout = TABLE_PER_ROLE_AND_CONCEPT
java -jar QuerySession.jar -pf confFile -qr queryFile -type SAT```
##### Translating on the TCP-Layout
`java -jar QuerySession.jar -pf confFile -qr queryFile -type SAT -isTCP `