Scopri come scegliere il giusto elettricista per la tua casa domotica, ottimizzando ogni aspetto della progettazione e delle ristrutturazioni. Con Ernesto.it, puoi contare su esperti in giardinaggio e progettazione edilizia per creare spazi unici e funzionali. Affidati a noi!
Trova professionisti vicino a te
Trova professionisti
/*
* Copyright (C) 2009-2013, Free University of Bozen Bolzano
* This source code is available under the terms of the Affero General Public
* License v3.
*
* Please see LICENSE.txt for full license terms, including the availability of
* proprietary exceptions.
*/
package it.unibz.krdb.obda.owlrefplatform.core.queryevaluation;
import it.unibz.krdb.obda.model.OBDADataFactory;
import it.unibz.krdb.obda.model.OBDAQuery;
import it.unibz.krdb.obda.model.Predicate;
import it.unibz.krdb.obda.model.impl.OBDADataFactoryImpl;
import it.unibz.krdb.obda.ontology.Assertion;
import it.unibz.krdb.obda.ontology.ObjectPropertyExpression;
import it.unibz.krdb.obda.ontology.Ontology;
import it.unibz.krdb.obda.ontology.impl.OntologyImpl;
import it.unibz.krdb.obda.owlrefplatform.core.basicoperations.Substitution;
import it.unibz.krdb.obda.owlrefplatform.core.dagjgrapht.TBoxReasoner;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class QueryRewriter {
private Ontology ontology;
private TBoxReasoner reasoner;
private int rewritings = 0;
private Map> predicatesInclusionMap;
private OBDADataFactory fac = OBDADataFactoryImpl.getInstance();
private static final Logger log = LoggerFactory.getLogger(QueryRewriter.class);
public QueryRewriter(Ontology ontology) {
this.ontology = new OntologyImpl(ontology);
this.reasoner = new TBoxReasoner(ontology);
this.predicatesInclusionMap = reasoner.getPredicateInclusionMap();
}
public OBDAQuery rewrite(OBDAQuery query) {
OBDAQuery newQuery = query.clone();
List assertions = ontology.getAssertions();
for (int i = 0; i < assertions.size(); i++) {
Assertion assertion = assertions.get(i);
if (assertion instanceof ObjectPropertyExpression) {
Set subProperties = predicatesInclusionMap.get(assertion.getPredicate());
if (subProperties != null && subProperties.size() > 0) {
rewriteQuery(newQuery, subProperties);
}
}
}
if (rewritings > 0) {
log.debug("Query {} rewritten in {} subqueries", query, rewritings);
}
return newQuery;
}
/**
* Replaces all the predicates in the query that are a subproperty of the
* given one
*
* @param query
* @param subProperties
*/
private void rewriteQuery(OBDAQuery query, Set subProperties) {
List substitutions = new LinkedList();
for (Predicate subProperty : subProperties) {
substitutions.add(new Substitution(subProperty, null));
}
rewriteQuery(query, substitutions);
}
/**
* Replaces all the predicates in the query that occur in the substitution
* map.
*
* @param query
* @param substitutions
* A map of predicates and the object to be substituted
*/
private void rewriteQuery(OBDAQuery query, List substitutions) {
for (Substitution s : substitutions) {
Predicate predicateToSubstitute = s.getPredicate();
for (int i = 0; i < query.getBody().size(); i++) {
if (query.getBody().get(i).getPredicate().equals(predicateToSubstitute)) {
query.getBody().remove(i);
OBDAQuery subquery = buildSubQuery(query, s);
query.getBody().addAll(i, subquery.getBody());
i += subquery.getBody().size() - 1;
rewritings++;
}
}
}
}
private OBDAQuery buildSubQuery(OBDAQuery query, Substitution s) {
OBDADataFactory ofac = OBDADataFactoryImpl.getInstance();
Predicate subProperty = s.getPredicate();
Predicate superProperty = (Predicate) s.getTerm();
OBDAQuery subquery = ofac.getOBDAQuery();
subquery.setName(query.getName());
Map varMap = new HashMap();
for (int i = 0; i < query.getBody().size(); i++) {
if (query.getBody().get(i).getPredicate().equals(subProperty)) {
List varPos = query.getBody().get(i).getVariables();
List newVarPos = getNewVariablePositions(query, varPos, varMap);
subquery.getBody().add(ofac.getAtom(superProperty, newVarPos));
updateVarMap(varMap, varPos, newVarPos);
}
}
return subquery;
}
private List getNewVariablePositions(OBDAQuery query, List varPos, Map varMap) {
List newVarPos = new ArrayList();
for (Integer var : varPos) {
Integer newVar;
if (varMap.containsKey(var)) {
newVar = varMap.get(var);
} else {
newVar = query.getVariables().size();
query.getVariables().add(query.getVariables().get(var));
}
newVarPos.add(newVar);
}
return newVarPos;
}
private void updateVarMap(Map varMap, List varPos, List newVarPos) {
Iterator iterVarPos = varPos.iterator();
Iterator iterNewVarPos = newVarPos.iterator();
while (iterVarPos.hasNext()) {
varMap.put(iterVarPos.next(), iterNewVarPos.next());
}
}
}