use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class Utils method rewriteWithMark.
/**
* Rewrite the marked fact q according to the unifier u between
*
* @param q
* the fact to rewrite must be a marked fact
* @param u
* the unifier between q and r
* @return the rewrite of q according to the unifier u.
*/
public static MarkedQuery rewriteWithMark(ConjunctiveQuery q, QueryUnifier u) {
InMemoryAtomSet ajout = u.getImageOf(u.getRule().getBody());
InMemoryAtomSet restant = u.getImageOf(AtomSetUtils.minus(q.getAtomSet(), u.getPiece()));
MarkedQuery rew = null;
InMemoryAtomSet res = AtomSetUtils.union(ajout, restant);
List<Term> ansVar = new LinkedList<Term>();
ansVar.addAll(q.getAnswerVariables());
rew = new MarkedQuery(res, ansVar);
ArrayList<Atom> markedAtoms = new ArrayList<Atom>();
CloseableIteratorWithoutException<Atom> it = ajout.iterator();
while (it.hasNext()) {
Atom a = it.next();
markedAtoms.add(a);
}
rew.setMarkedAtom(markedAtoms);
return rew;
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class IDCompilation method getUnification.
@Override
public LinkedList<Partition<Term>> getUnification(Atom father, Atom son) {
LinkedList<Partition<Term>> res = new LinkedList<Partition<Term>>();
Predicate predB = son.getPredicate();
Predicate predH = father.getPredicate();
List<IDCondition> conds = getConditions(predB, predH);
for (IDCondition cond : conds) {
Partition<Term> unif = cond.generateUnification(son.getTerms(), father.getTerms());
if (unif != null) {
res.add(unif);
}
}
return res;
}
use of fr.lirmm.graphik.graal.api.core.Term 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.Term in project graal by graphik-team.
the class IDConditionImpl method generateUnification.
/**
* Return the partition that unify the term of head with the term of body
* according to this. This method returns null the unification is not
* possible (two constants in the same partition class)
*/
@Override
public Partition<Term> generateUnification(List<Term> body, List<Term> head) {
Partition<Term> res = new Partition<Term>();
Term[] map = new Term[body.size()];
// put together term of body that must be unify according to this
for (int i = 0; i < condBody.length; ++i) {
Term t = body.get(i);
if (map[condBody[i]] == null) {
map[condBody[i]] = t;
} else {
res.add(map[condBody[i]], t);
}
}
// according this
for (int i = 0; i < condHead.length; i++) {
Term t = head.get(i);
res.add(map[condHead[i]], t);
}
// check validity (does not contains two different constants)
for (ArrayList<Term> classs : res) {
Term cst = null;
for (Term t : classs) {
if (t.isConstant()) {
if (cst == null)
cst = t;
else if (!cst.equals(t))
return null;
}
}
}
return res;
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class FreshVarSubstitution method createImageOf.
// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public Term createImageOf(Term term) {
Term substitut = term;
if (!term.isConstant()) {
substitut = this.getMap().get(term);
if (substitut == null) {
substitut = gen.getFreshSymbol();
this.put((Variable) term, substitut);
}
}
return substitut;
}
Aggregations