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;
}
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());
}
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);
}
}
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);
}
}
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);
}
Aggregations