Search in sources :

Example 31 with Term

use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.

the class DefaultScheduler method execute.

@Override
public VarSharedData[] execute(InMemoryAtomSet h, List<Term> ans, AtomSet data, RulesCompilation rc) {
    Set<Variable> terms = h.getVariables();
    VarSharedData[] vars = new VarSharedData[terms.size() + 2];
    int level = 0;
    vars[level] = new VarSharedData(level);
    Set<Term> alreadyAffected = new TreeSet<Term>();
    for (Term t : ans) {
        if (t instanceof Variable && !alreadyAffected.contains(t)) {
            ++level;
            vars[level] = new VarSharedData(level);
            vars[level].value = (Variable) t;
            alreadyAffected.add(t);
        }
    }
    int lastAnswerVariable = level;
    for (Term t : terms) {
        if (!alreadyAffected.contains(t)) {
            ++level;
            vars[level] = new VarSharedData(level);
            vars[level].value = (Variable) t;
        }
    }
    ++level;
    vars[level] = new VarSharedData(level);
    vars[level].previousLevel = lastAnswerVariable;
    return vars;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) TreeSet(java.util.TreeSet) Term(fr.lirmm.graphik.graal.api.core.Term) VarSharedData(fr.lirmm.graphik.graal.homomorphism.VarSharedData)

Example 32 with Term

use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.

the class BackwardChainingTest method getBody2.

/**
 * c(X) :- b(X,X).
 *
 * getBody([X]) => b(X, X).
 *
 * @throws ParseException
 */
@Theory
public void getBody2(RulesCompilation compilation, RewritingOperator operator) throws ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("p(X) :- q(X,X)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- p(X).");
    compilation.compile(rules.iterator());
    PureRewriter bc = new PureRewriter(operator, true);
    CloseableIteratorWithoutException<? extends ConjunctiveQuery> rewIt = bc.execute(query, rules, compilation);
    boolean isFound = false;
    while (rewIt.hasNext()) {
        ConjunctiveQuery rew = rewIt.next();
        CloseableIteratorWithoutException<Atom> it = rew.getAtomSet().iterator();
        if (it.hasNext()) {
            Atom a = it.next();
            if (a.getPredicate().equals(new Predicate("q", 2))) {
                isFound = true;
                List<Term> terms = a.getTerms();
                Assert.assertEquals(terms.get(0), terms.get(1));
            }
        }
    }
    Assert.assertTrue("Rewrite not found", isFound);
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Term(fr.lirmm.graphik.graal.api.core.Term) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Theory(org.junit.experimental.theories.Theory)

Example 33 with Term

use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.

the class Utils method imply.

/**
 * This methods test if the first rule logically imply the second. This
 * methods works with linear rules (atomic head and body).
 *
 * @param r1
 * @param r2
 * @return true, if r1 logically imply r2.
 */
public static boolean imply(Rule r1, Rule r2) {
    Atom b1 = r1.getBody().iterator().next();
    Atom b2 = r2.getBody().iterator().next();
    Atom h1 = r1.getHead().iterator().next();
    Atom h2 = r2.getHead().iterator().next();
    if (b1.getPredicate().equals(b2.getPredicate()) && h1.getPredicate().equals(h2.getPredicate())) {
        Map<Term, Term> s = new TreeMap<Term, Term>();
        Term term;
        for (int i = 0; i < b1.getPredicate().getArity(); i++) {
            term = s.get(b1.getTerm(i));
            if (term == null)
                s.put(b1.getTerm(i), b2.getTerm(i));
            else if (!term.equals(b2.getTerm(i)))
                return false;
        }
        for (int i = 0; i < h1.getPredicate().getArity(); i++) {
            term = s.get(h1.getTerm(i));
            if (term == null)
                s.put(h1.getTerm(i), h2.getTerm(i));
            else if (!term.equals(h2.getTerm(i)))
                return false;
        }
        return true;
    }
    return false;
}
Also used : Term(fr.lirmm.graphik.graal.api.core.Term) TreeMap(java.util.TreeMap) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 34 with Term

use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.

the class Utils method rewrite.

/**
 * Rewrite the fact q according to the unifier u.
 *
 * @param q
 *            the fact to rewrite
 * @param u
 *            the unifier between q and r
 * @return the rewrite of q according to the unifier u.
 */
public static ConjunctiveQuery rewrite(ConjunctiveQuery q, QueryUnifier u) {
    InMemoryAtomSet ajout = u.getImageOf(u.getRule().getBody());
    InMemoryAtomSet restant = u.getImageOf(AtomSetUtils.minus(q.getAtomSet(), u.getPiece()));
    ConjunctiveQuery rew = null;
    if (ajout != null && restant != null) {
        // FIXME
        InMemoryAtomSet res = AtomSetUtils.union(ajout, restant);
        List<Term> ansVar = new LinkedList<Term>();
        ansVar.addAll(q.getAnswerVariables());
        rew = DefaultConjunctiveQueryFactory.instance().create(res, ansVar);
    }
    return rew;
}
Also used : InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) LinkedList(java.util.LinkedList)

Example 35 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)

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