use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class IDConditionImpl method homomorphism.
@Override
public Substitution homomorphism(List<Term> head, List<Term> to) {
if (!checkBody(to)) {
return null;
}
Pair<List<Term>, Substitution> ret = this.generateBody(head);
if (ret == null) {
return null;
}
Substitution s = ret.getRight();
Substitution homo = DefaultSubstitutionFactory.instance().createSubstitution();
List<Term> generatedBody = ret.getLeft();
// check for a simple homomorphism from generated body into 'to'
Iterator<Term> itFrom = generatedBody.iterator();
Iterator<Term> itTo = to.iterator();
while (itFrom.hasNext() && itTo.hasNext()) {
Term termFrom = itFrom.next();
Term termTo = itTo.next();
if (termFrom.isConstant()) {
if (!termFrom.equals(termTo)) {
return null;
}
} else {
if (!homo.put((Variable) termFrom, termTo)) {
return null;
}
}
}
if (itFrom.hasNext() || itTo.hasNext()) {
throw new Error("Wrong term number");
}
// homo
for (Variable t : s.getTerms()) {
homo.put(t, homo.createImageOf(s.createImageOf(t)));
}
return homo;
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class DefaultRule method computeFrontierAndExistentials.
// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private void computeFrontierAndExistentials() {
this.frontier = new TreeSet<Variable>();
this.existentials = new TreeSet<Variable>();
Collection<Variable> body = this.getBody().getVariables();
for (Variable termHead : this.getHead().getVariables()) {
boolean isExistential = true;
for (Variable termBody : body) {
if (termBody.equals(termHead)) {
this.frontier.add(termHead);
isExistential = false;
}
}
if (isExistential) {
this.existentials.add(termHead);
}
}
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class BacktrackIteratorData method computeAtomOrder.
/**
* The index 0 contains the fully instantiated atoms.
*
* @param atomset
* @param vars
*/
private static void computeAtomOrder(CloseableIterableWithoutException<Atom> atomset, VarSharedData[] vars, Map<Variable, Integer> index) {
int tmp, rank;
// initialisation preAtoms and postAtoms Collections
for (int i = 0; i < vars.length; ++i) {
vars[i].preAtoms = new HashSet<Atom>();
vars[i].postAtoms = new HashSet<Atom>();
vars[i].postVars = new HashSet<VarSharedData>();
vars[i].preVars = new TreeSet<VarSharedData>();
}
//
CloseableIteratorWithoutException<Atom> it = atomset.iterator();
while (it.hasNext()) {
Atom a = it.next();
rank = 0;
for (Variable t : a.getVariables()) {
Integer idx = index.get(t);
if (idx != null) {
tmp = vars[idx].level;
vars[tmp].postAtoms.add(a);
if (rank < tmp)
rank = tmp;
}
}
vars[rank].postAtoms.remove(a);
vars[rank].preAtoms.add(a);
}
for (int i = 0; i < vars.length; ++i) {
for (Atom a : vars[i].postAtoms) {
for (Variable t : a.getVariables()) {
Integer idx = index.get(t);
if (idx != null) {
if (vars[idx].level > i) {
vars[i].postVars.add(vars[idx]);
vars[idx].preVars.add(vars[i]);
}
}
}
}
}
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class RecursiveBacktrackHomomorphism method homomorphism.
// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private Collection<Substitution> homomorphism(ConjunctiveQuery query, Collection<Atom>[] queryAtomRanked, AtomSet facts, Substitution substitution, List<Variable> orderedVars, int rank) throws Exception {
Collection<Substitution> substitutionList = new LinkedList<Substitution>();
if (orderedVars.size() == 0) {
Substitution filteredSub = new HashMapSubstitution();
for (Term var : query.getAnswerVariables()) {
if (var.isVariable()) {
filteredSub.put((Variable) var, substitution.createImageOf(var));
}
}
substitutionList.add(filteredSub);
} else {
Term var = orderedVars.remove(0);
if (var.isVariable()) {
for (Term substitut : domain) {
Substitution tmpSubstitution = new HashMapSubstitution(substitution);
tmpSubstitution.put((Variable) var, substitut);
// Test partial homomorphism
if (isHomomorphism(queryAtomRanked[rank], facts, tmpSubstitution))
substitutionList.addAll(homomorphism(query, queryAtomRanked, facts, tmpSubstitution, new LinkedList<Variable>(orderedVars), rank + 1));
}
}
}
return substitutionList;
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class RecursiveBacktrackHomomorphism method execute.
// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Substitution> execute(ConjunctiveQuery query, AtomSet facts) throws HomomorphismException {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(query.toString());
}
if (profiler != null) {
profiler.start("preprocessing time");
}
Pair<ConjunctiveQuery, Substitution> pair = EqualityUtils.processEquality(query);
List<Variable> orderedVars = order(pair.getLeft().getAtomSet().getVariables());
Collection<Atom>[] queryAtomRanked = getAtomRank(pair.getLeft().getAtomSet(), orderedVars);
if (profiler != null) {
profiler.stop("preprocessing time");
}
try {
this.domain = facts.getTerms();
if (profiler != null) {
profiler.start("backtracking time");
}
CloseableIterator<Substitution> results;
if (isHomomorphism(queryAtomRanked[0], facts, new HashMapSubstitution())) {
results = new CloseableIteratorAdapter<Substitution>(homomorphism(pair.getLeft(), queryAtomRanked, facts, new HashMapSubstitution(), orderedVars, 1).iterator());
} else {
// return false
results = new CloseableIteratorAdapter<Substitution>(Collections.<Substitution>emptyList().iterator());
}
if (profiler != null) {
profiler.stop("backtracking time");
}
if (!pair.getRight().getTerms().isEmpty()) {
results = new ConverterCloseableIterator<Substitution, Substitution>(results, new EqualityHandlerConverter(pair.getRight()));
}
return results;
} catch (Exception e) {
throw new HomomorphismException(e.getMessage(), e);
}
}
Aggregations