Search in sources :

Example 21 with ConjunctiveQuery

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

the class BasicAggregAllRulesOperator 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> currentRewrites = new LinkedList<ConjunctiveQuery>();
    LinkedList<QueryUnifier> srUnifiers = new LinkedList<QueryUnifier>();
    LinkedList<QueryUnifier> unifiers = new LinkedList<QueryUnifier>();
    for (Rule r : getUnifiableRules(q.getAtomSet().predicatesIterator(), ruleSet, compilation)) {
        /**
         * compute the single rule unifiers *
         */
        srUnifiers.addAll(getSRUnifier(q, r, compilation));
    }
    /**
     * compute the aggregated unifier *
     */
    unifiers = getAggregatedUnifiers(srUnifiers);
    /**
     * compute the rewrite from the unifier *
     */
    for (QueryUnifier u : unifiers) {
        ConjunctiveQuery a = Utils.rewrite(q, u);
        currentRewrites.add(a);
    }
    return currentRewrites;
}
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)

Example 22 with ConjunctiveQuery

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

the class ChaseTest method test2.

// @Theory
// public void coreChaseTest(AtomSet atomSet) throws AtomSetException,
// HomomorphismFactoryException,
// HomomorphismException, ChaseException {
// atomSet.addAll(DlgpParser.parseAtomSet("e(X,Y), e(Y,X)."));
// 
// LinkedList<Rule> ruleSet = new LinkedList<Rule>();
// ruleSet.add(DlgpParser.parseRule("e(Z,Z) :- e(X,Y), e(Y,X)."));
// ruleSet.add(DlgpParser.parseRule("e(X,Z), e(Z,Y) :- e(X,Y)."));
// 
// Chase chase = new DefaultChase(ruleSet, atomSet,
// RecursiveBacktrackHomomorphism.instance(),
// new CoreChaseStopCondition());
// chase.execute();
// 
// int size = 0;
// for (Iterator<Atom> it = atomSet.iterator(); it.hasNext(); it.next()) {
// if (++size > 3)
// Assert.assertFalse(true);
// }
// 
// Assert.assertTrue(true);
// }
// 
// @Theory
// public void coreChaseTest2(AtomSet atomSet) throws AtomSetException,
// HomomorphismFactoryException,
// HomomorphismException, ChaseException {
// atomSet.addAll(DlgpParser.parseAtomSet("e(X,Y), e(Y,Z)."));
// 
// LinkedList<Rule> ruleSet = new LinkedList<Rule>();
// ruleSet.add(DlgpParser.parseRule("e(X,Z) :- e(X,Y), e(Y,Z)."));
// 
// Chase chase = new DefaultChase(ruleSet, atomSet,
// RecursiveBacktrackHomomorphism.instance(),
// new CoreChaseStopCondition());
// chase.execute();
// 
// ConjunctiveQuery query = DlgpParser.parseQuery("? :-
// e(X,Y),e(Y,Z),e(X,Z).");
// Assert.assertTrue(StaticHomomorphism.instance().execute(query,
// atomSet).hasNext());
// }
@Theory
public void test2(InMemoryAtomSet atomSet) throws ChaseException, HomomorphismFactoryException, HomomorphismException, IteratorException, ParseException {
    // add assertions into this atom set
    atomSet.add(DlgpParser.parseAtom("<P>(a,a)."));
    atomSet.add(DlgpParser.parseAtom("<P>(c,c)."));
    atomSet.add(DlgpParser.parseAtom("<Q>(b,b)."));
    atomSet.add(DlgpParser.parseAtom("<Q>(c,c)."));
    atomSet.add(DlgpParser.parseAtom("<S>(z,z)."));
    // /////////////////////////////////////////////////////////////////////
    // create a rule set
    RuleSet ruleSet = new LinkedListRuleSet();
    // add a rule into this rule set
    ruleSet.add(DlgpParser.parseRule("<R>(X,X) :- <P>(X,X), <Q>(X,X)."));
    ruleSet.add(DlgpParser.parseRule("<S>(X, Y) :- <P>(X,X), <Q>(Y,Y)."));
    // /////////////////////////////////////////////////////////////////////
    // run saturation
    StaticChase.executeChase(atomSet, ruleSet);
    // /////////////////////////////////////////////////////////////////////
    // execute query
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y) :- <S>(X, Y), <P>(X,X), <Q>(Y,Y).");
    CloseableIterator<Substitution> subReader = SmartHomomorphism.instance().execute(query, atomSet);
    Assert.assertTrue(subReader.hasNext());
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

Example 23 with ConjunctiveQuery

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

the class ConjunctiveQueryFixedBugTest method HomomorphismAnsVarOverwritingBug.

/**
 * Overwriting of answer variable values before creating substitution to
 * return.
 */
@Theory
public void HomomorphismAnsVarOverwritingBug(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
    try {
        store.addAll(DlgpParser.parseAtomSet("<P>(a,b)."));
        ConjunctiveQuery query = DlgpParser.parseQuery("?(Y) :- <P>(X,Y).");
        CloseableIterator<Substitution> subReader;
        Substitution sub;
        subReader = h.execute(query, store);
        Assert.assertTrue(subReader.hasNext());
        sub = subReader.next();
        Assert.assertEquals(1, sub.getTerms().size());
        Assert.assertEquals(DefaultTermFactory.instance().createConstant("b"), sub.createImageOf(DefaultTermFactory.instance().createVariable("Y")));
        Assert.assertFalse(subReader.hasNext());
        subReader.close();
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage(), false);
    }
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Theory(org.junit.experimental.theories.Theory)

Example 24 with ConjunctiveQuery

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

the class ConjunctiveQueryFixedBugTest method issue52.

@Theory
public void issue52(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
    try {
        store.addAll(DlgpParser.parseAtomSet("<P0>(a,b),<P1>(b,c),<P2>(c,d),<P2>(a,a)."));
        ConjunctiveQuery query = DlgpParser.parseQuery("?(X0,X1,X2,X3) :- <P0>(X0,X2), <P1>(X2,X3), <P2>(X3,X1).");
        Assert.assertTrue(h.exist(query, store));
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage(), false);
    }
}
Also used : ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Theory(org.junit.experimental.theories.Theory)

Example 25 with ConjunctiveQuery

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

the class ConjunctiveQueryFixedBugTest method issue80.

@Theory
public void issue80(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) throws AtomSetException, HomomorphismException, IteratorException {
    store.addAll(DlgpParser.parseAtomSet("<P>(a,b), <P>(b,c), <P>(c,d), <P>(e,c), <P>(f,e)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X0,X1,X2,X3) :- <P>(X0,X1), <P>(X1,X2), <P>(X2,X3).");
    CloseableIterator<Substitution> results = h.execute(query, store);
    int nbResults = 0;
    results = Iterators.uniq(results);
    while (results.hasNext()) {
        results.next();
        ++nbResults;
    }
    Assert.assertEquals(2, nbResults);
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

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