# ASP-Based Declarative Reasoning in Data-Intensive Enterprise and IoT Applications
## 2 The DLV-EE Framework
### Reasoning over Hive
[D1,X]
```  
Rules \(r_{1}\) and \(r_{2}\) compute the transitive closure of the friendship relation of [PERSON], restricting the computation to close friends only. Rule \(r_{3}\) suggests a person \(Y\) if there is a possible friend and is older than 18. Rule \(r_{4}\) guesses if a suggested friend should be invited or not. Assuming that the number of invited people should be limited, rule \(r_{5}\) ensures that the number of invited friends is not greater than the desired maximum one. Rule \(r_{6}\) imposes that the average age of the invited friends is not smaller than a given value. Rule \(r_{7}\) expresses that preferred solutions are those in which the total degree of dislike among [PERSON] and invited people is minimized. Intuitively, computing the transitive closure of the friendship relation in a social network could be very expensive when performed on a huge database. Thus, traditional main memory ASP systems would struggle in handling it; even the naive import of the friendship relation is not feasible in practice. Hence, given that this subprogram entirely falls into Datalog, its computation can be delegated to Hive, letting the DLV2 be in charge of evaluating the remaining subsequent disjunctive part of the program. From a practical point of view, the delegation mentioned above is possible thanks to an external atom, namely &bigasp, having the form (2):  
```
fbigasp(rules,input,output,db,host:port,user,password;term[,term]). (2)
```  
where:  
```
rules a string specifying some Datalog rules whose evaluation is delegated externally on the DB; input a string (possibly empty) featuring a set of ASP facts to be exported to the DB; \begin{tabular}{l l} \(output\) & a string specifying the name and the arity of the predicate corresponding to the resulting output relation; \\ \(db\) & a string specifying the name of the ODBC DSN (Data Source Name); \\ \(host\):\(port\) & a string reporting the address and the port of the Hive server; \\ \(user\) & a string specifying the username willing to connect to the server; \\ \(password\) & a string specifying the user's password; \\ \(term\) & one or more (optional) output terms. \\ \end{tabular} 

The Python function defining the behavior of such external atom connects to the specified Hive server via the given username and password, exports the given facts (if any), converts the given rules into one or more HiveQL queries, executes such queries on the DB and returns as output a set of tuples that will populate the specified output relation. The output terms correspond to selected fields of the resulting HiveQL queries. Let us consider again our running example and illustrate how the external atom is intended to be used to externally delegate the computation of suggested friends. \(r_{8}\):=suggested_friend(Y,A):-&kbigasp( \\  &possible_friend(Y):- close_friend(giovanni,Y). \\  & possible_friend(Y):- possible_friend(Y), close_friend(Y,L). \\  &suggested_friend(Y,A):- possible_friend(Y), person(Y,A), A?18. ', \"\". \\  &suggested_friend(Z/2,\"my_db\", \"192.168.1.1:10000\", \"my_username\", \\  & \"my_password\"; Y,A,B,C). \\ \end{tabular}

Apart from connection parameters and the name of the output relation, the external atom, presented in rule \(r_{8}\), takes as input the extracted rules, invokes the machinery for enabling their evaluation on the external my_db and returns the results as a sequence of tuples representing the suggested friends with their age. Such tuples populate the extension of the suggested_friend relation so that the traditional ASP evaluation can continue. The external atom &bigasp translates the extracted rules to the following HiveQL queries:

1. possible_friend(Y) :- close_friend(giovanni,Y). INSERT INTO possible_friend  SELECT DISTINCT t0.name1 as name,  '2023-01-11 01:21:09' AS currentStartDate,  '0' AS recursionLevel  FROM close_friend AS t0  WHERE t0.name0='giovanni'

2. possible_friend(X) :- possible_friend(Y), close_friend(Y, X).