use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class IDConditionTest method generateBodyTest5.
/**
* h(X,Y,X,Y) :- b(X,Y).
*
* generateBody([a,b,X,Y]) => [a, b].
*/
@Test
public void generateBodyTest5() {
Term[] body1 = { X, Y };
Term[] head1 = { X, Y, X, Y };
IDCondition cond = createCondition(body1, head1);
Term[] head = { A, B, X, Y };
Term[] expected = { A, B };
Pair<List<Term>, Substitution> ret = cond.generateBody(Arrays.asList(head));
List<Term> computed = ret.getLeft();
Substitution s = ret.getRight();
Assert.assertEquals(Arrays.asList(expected), computed);
Assert.assertEquals(A, s.createImageOf(X));
Assert.assertEquals(B, s.createImageOf(Y));
Assert.assertEquals(2, s.getTerms().size());
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class DefaultUCQHomomorphism method exist.
@Override
public boolean exist(UnionOfConjunctiveQueries q, AtomSet a) throws HomomorphismException {
try {
CloseableIterator<Substitution> execute = this.execute(q, a);
boolean res = execute.hasNext();
execute.close();
return res;
} catch (IteratorException e) {
throw new HomomorphismException(e);
}
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class FullyInstantiatedQueryHomomorphism method execute.
@Override
public CloseableIterator<Substitution> execute(ConjunctiveQuery q, AtomSet a, RulesCompilation rc) throws HomomorphismException {
try {
CloseableIteratorWithoutException<Atom> it = q.getAtomSet().iterator();
boolean contains = true;
while (contains && it.hasNext()) {
Atom atom = it.next();
boolean containsAtom = false;
for (Pair<Atom, Substitution> im : rc.getRewritingOf(atom)) {
containsAtom = a.contains(im.getLeft());
if (containsAtom) {
break;
}
}
contains = containsAtom;
}
if (contains) {
return Iterators.singletonIterator(Substitutions.emptySubstitution());
} else {
return Iterators.emptyIterator();
}
} catch (AtomSetException e) {
throw new HomomorphismException(e);
}
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class PureHomomorphismImpl method backtrack.
protected boolean backtrack() {
int level = 0;
boolean foundImage;
Substitution nextCandidate;
// can backtrack and all the atom have not been associate
while (level >= 0 && level < this.source.size()) {
nextCandidate = this.getNextCandidate(level);
if (nextCandidate != null) {
// try next candidate
foundImage = this.checkCurrentCandidate(level, nextCandidate);
if (foundImage) {
// need go to the next atom
level++;
}
} else {
// need backtrack
level--;
}
}
return !(level < 0);
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class RecursiveBacktrackHomomorphism method existHomomorphism.
private static boolean existHomomorphism(AtomSet atomSet1, Collection<Atom>[] queryAtomRanked, AtomSet atomSet2, Substitution substitution, List<Variable> orderedVars, int rank) throws Exception {
if (orderedVars.size() == 0) {
return true;
} else {
Term var;
Set<Term> domaine = atomSet2.getTerms();
var = orderedVars.remove(0);
if (var.isVariable()) {
for (Term substitut : domaine) {
Substitution tmpSubstitution = new HashMapSubstitution(substitution);
tmpSubstitution.put((Variable) var, substitut);
// Test partial homomorphism
if (isHomomorphism(queryAtomRanked[rank], atomSet2, tmpSubstitution))
if (existHomomorphism(atomSet1, queryAtomRanked, atomSet2, tmpSubstitution, new LinkedList<Variable>(orderedVars), rank + 1)) {
return true;
}
}
}
}
return false;
}
Aggregations