Search in sources :

Example 71 with Term

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;
}
Also used : InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) ArrayList(java.util.ArrayList) Term(fr.lirmm.graphik.graal.api.core.Term) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 72 with Term

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;
}
Also used : Partition(fr.lirmm.graphik.util.Partition) Term(fr.lirmm.graphik.graal.api.core.Term) LinkedList(java.util.LinkedList) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Example 73 with Term

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;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) MethodNotImplementedError(fr.lirmm.graphik.util.MethodNotImplementedError) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Term(fr.lirmm.graphik.graal.api.core.Term)

Example 74 with Term

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;
}
Also used : Partition(fr.lirmm.graphik.util.Partition) Term(fr.lirmm.graphik.graal.api.core.Term)

Example 75 with Term

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;
}
Also used : Term(fr.lirmm.graphik.graal.api.core.Term)

Aggregations

Term (fr.lirmm.graphik.graal.api.core.Term)173 Atom (fr.lirmm.graphik.graal.api.core.Atom)86 LinkedList (java.util.LinkedList)41 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)36 Test (org.junit.Test)35 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)34 Variable (fr.lirmm.graphik.graal.api.core.Variable)32 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)27 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)26 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)23 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)18 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)15 Rule (fr.lirmm.graphik.graal.api.core.Rule)15 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)14 TreeSet (java.util.TreeSet)12 Constant (fr.lirmm.graphik.graal.api.core.Constant)9 DBTable (fr.lirmm.graphik.graal.store.rdbms.util.DBTable)9 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)9 TreeMap (java.util.TreeMap)8