# ASP-Based Declarative Reasoning in Data-Intensive Enterprise and IoT Applications
## 2 The DLV-EE Framework
### Reasoning over Hive  
_Hive_[[https://hive.apache.org](https://hive.apache.org)]([https://hive.apache.org](https://hive.apache.org)) is a system allowing to build of distributed, large-scale data warehouses featuring a metadata repository for advanced analyses. Hive is built on top of Apache Hadoop [[https://hadoop.apache.org](https://hadoop.apache.org)]([https://hadoop.apache.org](https://hadoop.apache.org)), which is a robust and very popular open-source framework conceived to facilitate the development of distributed computing over large amounts of data. Hadoop relies on HDFS (Hadoop Distributed File System) [[https://hadoop.apache.org/docs/r1.2.1/hdfs_design.html](https://hadoop.apache.org/docs/r1.2.1/hdfs_design.html)]([https://hadoop.apache.org/docs/r1.2.1/hdfs_design.html](https://hadoop.apache.org/docs/r1.2.1/hdfs_design.html)) for data storage and makes use of _MapReduce_[26] or _YARN_[[https://hadoop.apache.org/docs/stable/hadoop-yarn/hadoop-yarn-site/YARN.html](https://hadoop.apache.org/docs/stable/hadoop-yarn/hadoop-yarn-site/YARN.html)]([https://hadoop.apache.org/docs/stable/hadoop-yarn/hadoop-yarn-site/YARN.html](https://hadoop.apache.org/docs/stable/hadoop-yarn/hadoop-yarn-site/YARN.html)) algorithms to allow the distributed execution across clusters of machines. Hadoop is widely used in industry, as it is highly reliable (e.g., it facilitates the replacement of cluster nodes in case of failure) and scalable (e.g., the computational capacity can be increased or decreased by just adding or removing nodes). Queries to Hive are given via _HiveQL_[[https://cwiki.apache.org/confluence/display/Hive/LanguageManual](https://cwiki.apache.org/confluence/display/Hive/LanguageManual)]([https://cwiki.apache.org/confluence/display/Hive/LanguageManual](https://cwiki.apache.org/confluence/display/Hive/LanguageManual)), a language based on SQL. Roughly, when an HiveQL query is issued, Hadoop first analyzes the query and generates an execution plan, then a proper MapReduce or YARN algorithm is used to determine the query answer(s). A properly designed integration with such a system can allow us not only to retrieve data but also to delegate to the external large-scale database some of the most data-intensive parts of the evaluation. In particular, specific sub-programs of the input ASP program (i.e., those falling in a fragment of ASP corresponding to Datalog) can be evaluated by Hive, without the need of loading the whole input data in the main memory. This can be performed by identifying an evaluation order for the Datalog rules, translating such rules into SQL instructions, and asking Hive for evaluating the so-defined SQL queries. DLV2 then takes the results and evaluates the remaining part of the program, i.e., the part falling in fragments more expressive than Datalog and thus, impossible to be translated into standard SQL. The following example simulates a potential use case. Suppose that a social network would like to offer its users suggestions about people to invite for a dinner. Besides direct friends, suggestions could also include friends of friends. It is reasonable to assume that close friends could be preferred in suggestions and that the social network can estimate the degree of likeness/unlikeness among subscribed people on the basis of their interests, interactions, and so on. The following ASP program can be adapted to explore (directly and indirectly) the friendship network of a user, namely [PERSON], and then to provide him with suitable suggestions on people that he could invite. ```
f1:=rriends(10). f2:averageAge(25). f3:possible_friend(Y) - close_friend(giovanni,Y). r1:=possible_friend(X) - possible_friend(Y), close_friend(Y,X). r2:suggested_friend(Y,A) - possible_friend(Y), person(Y,A), A18. r3:=invite(X) - invite(X) - suggested_friend(X). r5:=#count(X:invite(X)) > N, nfrindas(N). r6:=#sum(a,X:suggested_friend(X,A),invite(X)) < AV6=#, nfrindas(N), averageAge(AV6). r7:=invite(X),suggested_friend(X,_), unlike(giovanni,X,D).