use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class AdHocRdbmsStore method atomsByPredicate.
@Override
public CloseableIterator<Atom> atomsByPredicate(Predicate p) throws AtomSetException {
List<Term> terms = new LinkedList<Term>();
for (int i = 0; i < p.getArity(); ++i) {
terms.add(DefaultTermFactory.instance().createVariable("X" + i));
}
Atom atom = DefaultAtomFactory.instance().create(p, terms);
ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(new LinkedListAtomSet(atom));
SqlHomomorphism solver = SqlHomomorphism.instance();
try {
return new SubstitutionIterator2AtomIterator(atom, solver.execute(query, this));
} catch (HomomorphismException e) {
throw new AtomSetException(e);
}
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class AbstractRdbmsConjunctiveQueryTranslator method translate.
@Override
public Iterator<SQLQuery> translate(Rule rangeRestrictedRule) throws AtomSetException {
Collection<SQLQuery> queries = new LinkedList<SQLQuery>();
InMemoryAtomSet body = rangeRestrictedRule.getBody();
CloseableIterator<Atom> it = rangeRestrictedRule.getHead().iterator();
try {
while (it.hasNext()) {
Atom headAtom = it.next();
DBTable table = this.store.createPredicateTableIfNotExist(headAtom.getPredicate());
List<Term> terms = headAtom.getTerms();
ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(body, terms);
SQLQuery selectQuery = this.translate(query);
if (!selectQuery.hasSchemaError()) {
queries.add(new SQLQuery(store.getDriver().getInsertOrIgnoreQuery(table, selectQuery.toString())));
}
}
} catch (IteratorException e) {
throw new AtomSetException(e);
} catch (SQLException e) {
throw new AtomSetException(e);
}
return queries.iterator();
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class ConjunctiveQueryWithCompilation method test2.
@Theory
public void test2(Homomorphism<ConjunctiveQuery, AtomSet> hh, RulesCompilationFactory factory, AtomSet store) throws Exception {
Assume.assumeTrue(hh instanceof HomomorphismWithCompilation);
HomomorphismWithCompilation<ConjunctiveQuery, AtomSet> h = (HomomorphismWithCompilation<ConjunctiveQuery, AtomSet>) hh;
store.add(DlgpParser.parseAtom("<P>(a,b)."));
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("<Q>(X,Y) :- <P>(Y,X)."));
RulesCompilation comp = factory.create();
comp.compile(rules.iterator());
StaticChase.executeChase(store, rules);
CloseableIterator<Substitution> results = h.execute(DlgpParser.parseQuery("?(X,Y) :- <Q>(X,Y)."), store, comp);
Assert.assertTrue(results.hasNext());
Substitution next = results.next();
Assert.assertEquals(DefaultTermFactory.instance().createConstant("a"), next.createImageOf(DefaultTermFactory.instance().createVariable("Y")));
Assert.assertEquals(DefaultTermFactory.instance().createConstant("b"), next.createImageOf(DefaultTermFactory.instance().createVariable("X")));
Assert.assertFalse(results.hasNext());
results.close();
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class ConjunctiveQueryWithCompilation method issue34.
@Theory
public void issue34(Homomorphism<ConjunctiveQuery, AtomSet> hh, RulesCompilationFactory factory, AtomSet store) throws Exception {
Assume.assumeTrue(hh instanceof HomomorphismWithCompilation);
HomomorphismWithCompilation<ConjunctiveQuery, AtomSet> h = (HomomorphismWithCompilation<ConjunctiveQuery, AtomSet>) hh;
store.add(DlgpParser.parseAtom("<Q>(a,b)."));
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("<P>(X,Y) :- <Q>(Y,X)."));
RulesCompilation comp = factory.create();
comp.compile(rules.iterator());
StaticChase.executeChase(store, rules);
InMemoryAtomSet query1 = new LinkedListAtomSet();
query1.add(DlgpParser.parseAtom("<P>(a,Y)."));
CloseableIterator<Substitution> results = h.execute(new DefaultConjunctiveQuery(query1), store, comp);
Assert.assertFalse(results.hasNext());
results.close();
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class NaturalRDBMSStore method atomsByPredicate.
@Override
public CloseableIterator<Atom> atomsByPredicate(Predicate p) throws AtomSetException {
if (!this.check(p)) {
return Iterators.<Atom>emptyIterator();
}
List<Term> terms = new LinkedList<Term>();
for (int i = 0; i < p.getArity(); ++i) {
terms.add(DefaultTermFactory.instance().createVariable("X" + i));
}
Atom atom = DefaultAtomFactory.instance().create(p, terms);
ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(new LinkedListAtomSet(atom));
SqlHomomorphism solver = SqlHomomorphism.instance();
try {
return new SubstitutionIterator2AtomIterator(atom, solver.execute(query, this));
} catch (HomomorphismException e) {
throw new AtomSetException(e);
}
}
Aggregations