# Acquiring Ontology Axioms through Mappings to Data Sources +
## 5 Query Answering
### Query Answering  
We now provide an algorithm for query answering over MKBs, which makes use of the query rewriting technique presented in the previous subsection. As already said, our idea is to first compute a \(\mathit{DL-Lite}_{\mathcal{R}}\) TBox by evaluating the mapping assertions involving the predicates \(\mathit{Isa}_{C}\), \(\mathit{Isa}_{R}\), \(\mathit{Disj}_{C}\), \(\mathit{Disj}_{R}\)over the database of the MKB; then, such a TBox is used to compute the perfect rewriting of the input IHUCQ. To complete query answering, we have to also consider the mapping of the predicates \(\mathit{Inst}_{C}\) and \(\mathit{Inst}_{R}\) and reformulate the query thus obtained by replacing the above predicates with the FOL queries occurring in the corresponding mapping assertions (step 3 of our technique). In this way we obtain an FOL query expressed over the database. This second rewriting step, usually called _unfolding_, can be performed by the algorithm UnfoldDB presented in [20] (Here, we assume that the algorithm UnfoldDB takes as input an EUCQ and an instance-mapping. This corresponds to actually considering a straightforward extension of the algorithm presented in [20] in order to deal with the presence of the ternary predicate \(\mathit{Inst}_{R}\).). In the following, given a mapping \(\mathcal{M}\) and a database \(DB\), we denote by \(\mathit{DB}_{\mathcal{M}_{T}}\) the database constituted by every relation \(R\) of \(DB\) such that \(R\) occurs in \(\mathcal{M}_{T}\). Furthermore, we define \(\mathit{DB}_{\mathcal{M}_{A}}\) as the database \(\mathit{DB}-\mathit{DB}_{\mathcal{M}_{T}}\) (i.e., \(\mathit{DB}_{\mathcal{M}_{A}}\) is the portion of \(\mathit{DB}\) which is not involved by the mapping \(\mathcal{M}_{T}\)). We are now ready to present our query answering algorithm. ALGORITHM \(\mathit{Answer}(Q,\mathcal{K})\)  
INPUT: IHUCQ \(Q\), \(\mathit{Hi}(\mathit{DL}\)-\(\mathit{Lite}_{\mathcal{R}})\) MKB \(\mathcal{K}=\langle\mathit{DB},\mathcal{M}\rangle\)  
OUTPUT: \(\mathit{cert}(Q,K)\)  
**begin**  
\(\mathcal{T}=\mathit{Retrieve}(\mathcal{M}_{T},\mathit{DB}_{\mathcal{M}_{T}})\);  
\(Q^{\prime}=\mathit{Rewrite}\mathit{IHUCQ}(Q,\mathcal{T},\mathcal{M}_{A})\);  
\(Q^{\prime\prime}=\mathsf{UnfoldDB}(Q^{\prime},\mathcal{M}_{A})\);  
**return**\(\mathit{IntEval}(Q^{\prime\prime},\mathit{DB}_{\mathcal{M}_{A}})\)  
**end**  
The algorithm starts by retrieving (function \(\mathit{Retrieve}(\mathcal{M}_{T},\mathit{DB}_{\mathcal{M}_{T}})\)) the TBox \(\mathcal{T}\) from \(\mathit{DB}_{\mathcal{M}_{T}}\) through the mapping \(\mathcal{M}_{T}\). Then, it computes (function \(\mathit{Rewrite}\mathit{IHUCQ}(Q,\mathcal{T},\mathcal{M}_{A})\)) the perfect rewriting of the query with respect to the retrieved TBox and next computes (function \(\mathsf{UnfoldDB}(Q^{\prime},\mathcal{M}_{A})\)) the unfolding of such a query with respect to the mapping \(\mathcal{M}_{A}\). Finally, it evaluates (function \(\mathit{IntEval}(Q^{\prime\prime},\mathit{DB}_{\mathcal{M}_{A}})\) the query over the database and returns the result of the evaluation. **Example 9**. : _Let us consider again the MKD system and the query of Example 6. We notice that \(\mathcal{M}_{T}\) and \(\mathcal{M}_{A}\) consists of the mapping assertions M1-M9 and M10-M-13 given in Example 4, respectively, and \(\mathit{DB}_{\mathcal{M}_{T}}=\mathit{DB}_{\mathcal{M}_{A}}\) coincides with the database \(\mathcal{D}\) described in Figure 3. Thus, the TBox \(\mathcal{T}\) returned by \(\mathit{Retrieve}(\mathcal{M}_{T},\mathit{DB}_{\mathcal{M}_{T}})\) coincides with the TBox described in Figure 6 (not considering InstanceOf arrows). \(Q^{\prime}\) is as discussed in Example 8. As said, amog all queries in \(Q^{\prime}\) we consider only query (4).