fr.boreal.backward_chaining.cover.QueryCover Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of integraal-backward-chaining Show documentation
Show all versions of integraal-backward-chaining Show documentation
InteGraal has been designed in a modular way, in order to facilitate
software reuse and extension.
It should make it easy to test new scenarios and techniques, in
particular by combining algorithms.
The main features of Graal are currently the following:
(1) internal storage to store data by using a SQL or RDF representation
(Postgres, MySQL, HSQL, SQLite, Remote SPARQL endpoints, Local in-memory
triplestores) as well as a native in-memory representation
(2) data-integration capabilities for exploiting federated heterogeneous
data-sources through mappings able to target systems such as SQL, RDF,
and black-box (e.g. Web-APIs)
(3) algorithms for query-answering over heterogeneous and federated data
based on query rewriting and/or forward chaining (or chase)
The newest version!
package fr.boreal.backward_chaining.cover;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import fr.boreal.backward_chaining.homomorphism.QueryHomomorphism;
import fr.boreal.model.formula.api.FOFormula;
import fr.boreal.model.query.api.FOQuery;
import fr.boreal.model.ruleCompilation.NoRuleCompilation;
import fr.boreal.model.ruleCompilation.api.RuleCompilation;
/**
* Computes the cover of a set of queries using homomorphism to detect if a query is more general than another
*/
public class QueryCover implements CoverFunction {
private final QueryHomomorphism queryHomomorphism;
/**
* Default constructor.
* Uses no compilation
*/
public QueryCover() {
this(NoRuleCompilation.instance());
}
/**
* Constructor with compilation
* @param compilation the compilation to use
*/
public QueryCover(RuleCompilation compilation) {
this.queryHomomorphism = new QueryHomomorphism(compilation);
}
@Override
public Set> cover(Set> queries) {
Set> cover = new HashSet<>();
for(FOQuery extends FOFormula> q1 : queries) {
boolean toAdd = true;
Iterator> coverIt = cover.iterator();
while (coverIt.hasNext()) {
FOQuery extends FOFormula> q2 = coverIt.next();
if(this.isMoreGeneralThan(q2, q1)) {
toAdd = false;
break;
}
else if(this.isMoreGeneralThan(q1, q2)) {
coverIt.remove();
}
}
if(toAdd) {
cover.add(q1);
}
}
return cover;
}
/**
* @return true iff q1 subsumes q2
*/
private boolean isMoreGeneralThan(FOQuery extends FOFormula> q1, FOQuery extends FOFormula> q2) {
return this.queryHomomorphism.exists(q1, q2);
}
}