Search in sources :

Example 16 with ConjunctiveQuery

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

the class BackwardChainingTest method issue34_1.

@Theory
public void issue34_1(RulesCompilation compilation, RewritingOperator operator) throws IteratorException {
    try {
        RuleSet rules = new LinkedListRuleSet();
        rules.add(DlgpParser.parseRule("p(X,Y) :- q(Y,X)."));
        rules.add(DlgpParser.parseRule("r(X,Z) :- p(X,Y)."));
        rules.add(DlgpParser.parseRule("r(X,Z) :- q(X,Y)."));
        ConjunctiveQuery query = DlgpParser.parseQuery("? :- r(a,Y).");
        compilation.compile(rules.iterator());
        PureRewriter bc = new PureRewriter(operator, true);
        CloseableIterator<? extends ConjunctiveQuery> it = bc.execute(query, rules, compilation);
        int i = Iterators.count(it);
        Assert.assertEquals(4, i);
    } catch (Throwable t) {
        Assert.assertFalse("There is an error.", true);
    }
}
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) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

Example 17 with ConjunctiveQuery

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

the class RewritinCloseableIterator method next.

@Override
public ConjunctiveQuery next() {
    if (this.rewrites == null) {
        this.compute();
    }
    ConjunctiveQuery query = this.rewrites.next();
    PureQuery.removeAnswerPredicate(query);
    return query;
}
Also used : ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)

Example 18 with ConjunctiveQuery

use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery 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 19 with ConjunctiveQuery

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

the class Utils method computeCover.

/**
 * Remove the fact that are not the most general (taking account of compiled
 * rules) in the given facts
 *
 * @param comp
 */
public static void computeCover(Iterable<ConjunctiveQuery> set, RulesCompilation comp) {
    Iterator<ConjunctiveQuery> beg = set.iterator();
    Iterator<ConjunctiveQuery> end;
    InMemoryAtomSet q;
    InMemoryAtomSet o;
    boolean finished;
    while (beg.hasNext()) {
        q = beg.next().getAtomSet();
        finished = false;
        end = set.iterator();
        while (!finished && end.hasNext()) {
            o = end.next().getAtomSet();
            if (o != q && isMoreGeneralThan(o, q, comp)) {
                finished = true;
                beg.remove();
            }
        }
    }
}
Also used : InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)

Example 20 with ConjunctiveQuery

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

the class AggregSingleRuleOperator method getRewritesFrom.

// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
/**
 * Returns the rewrites compute from the given fact and the rule set of the
 * receiving object.
 *
 * @param q
 *            A fact
 * @return the ArrayList that contains the rewrites compute from the given
 *         fact and the rule set of the receiving object.
 */
@Override
public Collection<ConjunctiveQuery> getRewritesFrom(ConjunctiveQuery q, IndexedByHeadPredicatesRuleSet ruleSet, RulesCompilation compilation) {
    LinkedList<ConjunctiveQuery> rewriteSet = new LinkedList<ConjunctiveQuery>();
    Collection<QueryUnifier> unifiers = new LinkedList<QueryUnifier>();
    for (Rule r : getUnifiableRules(q.getAtomSet().predicatesIterator(), ruleSet, compilation)) {
        unifiers.addAll(getSRUnifier(q, r, compilation));
    }
    /**
     * compute the rewrite from the unifier *
     */
    ConjunctiveQuery a;
    for (QueryUnifier u : unifiers) {
        a = Utils.rewrite(q, u);
        if (a != null) {
            rewriteSet.add(a);
        }
    }
    return rewriteSet;
}
Also used : QueryUnifier(fr.lirmm.graphik.graal.core.unifier.QueryUnifier) Rule(fr.lirmm.graphik.graal.api.core.Rule) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) LinkedList(java.util.LinkedList)

Aggregations

ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)113 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)58 Theory (org.junit.experimental.theories.Theory)57 Atom (fr.lirmm.graphik.graal.api.core.Atom)34 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)29 Test (org.junit.Test)29 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)23 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)22 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)22 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)19 DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)18 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)14 Term (fr.lirmm.graphik.graal.api.core.Term)14 PureRewriter (fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter)14 LinkedList (java.util.LinkedList)13 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)12 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)11 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)10 TripleStore (fr.lirmm.graphik.graal.api.store.TripleStore)9 RulesCompilation (fr.lirmm.graphik.graal.api.core.RulesCompilation)7