# ASP-Based Declarative Reasoning in Data-Intensive Enterprise and IoT Applications
## 2 The DLV-EE Framework
### Reasoning over Hive
\begin{tabular}{l l} _host_ & is the address of the MongoDB server. \\ _port_ & is the port of the MongoDB server. \\ _database_ & is the name of the database over which the query has to be performed. \\ _collection_ & is the name of the collection from which data is to be retrieved. \\ _query_ & is the query to be executed on MongoDB. \\ _key_ & is needed to select fields within the document; this parameter determines the arity of the predicate. If an empty string (\"\") is specified, all fields are returned. \\ _aggr_ & this parameter informs the Python module about the presence of aggregate functions, hence providing instructions about the search method to be used: \"yes\" indicates to use db.aggregate(), while \"no\" indicates to use db.find(). \\ _term_ & one or more (optional) output terms determining the values of the field(s) retrieved via the query. \\ \end{tabular} 

The following example illustrates how an ASP program can make use of data retrieved via MongoDB. Considering the ASP rule \(r_{1}\) below: \begin{tabular}{l l} \(r_{1}\): b(X,Y) :- & Among(\"localhost\", 27017, \"admin\", \"football\", \"(age: (Sgt: 30))\", \"-id: 0, name: 1, surname: 1\",\"yes\"; X, Y ). \\ \end{tabular} 

\(r_{1}\) can be used to fill in the extension of the head predicate \(b\) having arity 2 with the names and the surnames of the football players appearing in the collection \"football\" who are over 30 years old. During the evaluation of the logic program, DLV2 invokes the Python module corresponding to the external atom mongo where the input terms contain the information needed for establishing the connection with the MongoDB server and performing the query. DLV2 waits for the results, that return as JSON documents, for instance, \(json_{1}\) and \(json_{2}\):\(j_{\text{nom1}}\) : { \"player\" : { \"name\": \"[PERSON]\", \"surname\": \"[PERSON]\" } } \(j_{\text{nom2}}\) : { \"player\": { \"name\": \"[PERSON]\", \"surname\": \"[PERSON]\" } }

Such documents are then transformed into tuples that bind the output parameters X and Y, used by DLV2 for evaluating the rule. Elasticsearch [[https://www.elastic.co/elasticsearch](https://www.elastic.co/elasticsearch)]([https://www.elastic.co/elasticsearch](https://www.elastic.co/elasticsearch)) is a full-text search engine based on Lucene [[https://lucene.apache.org](https://lucene.apache.org)]([https://lucene.apache.org](https://lucene.apache.org)), a free open-source API for information extraction, widely used in the development of search engines. It is also a document-oriented DBMS that stores data over a distributed database. Documents are collections of fields represented in JSON format; each document belongs to an index and is identified by a unique key. Load balancing over the database distributed architecture is guaranteed by the division into Shards (partitions). The interoperability between DLV2 and Elasticsearch is obtained by defining a specific external atom. In this case, the external atom has the form (4):

&elastic(host, port, query,index; term [.term]) (4)

where:

_host_ is the address of the Elasticsearch server. _port_ is the listening port of the Elasticsearch server (9200 is the standard one). _query_ is the query to be executed on MongoDB. _index_ is a string representing the index where the query should be performed. _term_ one or more (optional) output terms determining the values of the field(s) retrieved via the query. Similarly to the MongoDB case, the external atom of Elasticsearch can be exploited to perform a query, e.g. for obtaining all the football players as in the rule \(r_{2}\) below:

\(r_{2}\): b(X,Y) : -&elasticsearch(\"localhost\", 9200,  *{'_source':['teams','player'],'query':{'match_all':{}}}),\"anElasticIndex\";X,Y). Also in this case the Python module invokes Elasticsearch to pass the query and obtain the results. ## 3 DLV-IoT

The Internet of Things (IoT) is a rapidly growing technology trend that is changing the way people live, work, and interact with the world.