use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class RdbmsAtomIterator method hasNext.
// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean hasNext() throws IteratorException {
if (!this.hasNextCallDone) {
this.hasNextCallDone = true;
while (this.predicateIt.hasNext() && (this.atomIt == null || !this.atomIt.hasNext())) {
Predicate p = predicateIt.next();
List<Term> terms = new LinkedList<Term>();
VariableGenerator gen = new DefaultVariableGenerator("X");
for (int i = 0; i < p.getArity(); ++i) {
terms.add(gen.getFreshSymbol());
}
InMemoryAtomSet atomSet = new LinkedListAtomSet();
Atom atom = new DefaultAtom(p, terms);
atomSet.add(atom);
ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(atomSet);
SqlHomomorphism solver = SqlHomomorphism.instance();
try {
this.atomIt = new SubstitutionIterator2AtomIterator(atom, solver.execute(query, this.store));
} catch (HomomorphismException e) {
throw new IteratorException(e);
}
}
}
return this.atomIt != null && this.atomIt.hasNext();
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class SqlUCQHomomorphism method preprocessing.
private static SQLQuery preprocessing(UnionOfConjunctiveQueries queries, RdbmsStore store) throws HomomorphismException {
boolean emptyQuery = false;
CloseableIterator<ConjunctiveQuery> it = queries.iterator();
StringBuilder ucq = new StringBuilder();
RdbmsConjunctiveQueryTranslator translator = store.getConjunctiveQueryTranslator();
try {
if (!it.hasNext())
return SQLQuery.hasSchemaErrorInstance();
while (it.hasNext()) {
SQLQuery query = translator.translate(it.next());
if (!query.hasSchemaError()) {
if (ucq.length() > 0)
ucq.append("\nUNION\n");
ucq.append(query.toString());
} else if (query.isEmpty()) {
emptyQuery = true;
}
}
} catch (Exception e) {
throw new HomomorphismException("Error during query translation to SQL", e);
}
SQLQuery query = new SQLQuery(ucq.toString());
if (query.isEmpty() && !emptyQuery) {
return SQLQuery.hasSchemaErrorInstance();
}
return query;
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class RDF4jStore method match.
@Override
public CloseableIterator<Atom> match(Atom atom) throws AtomSetException {
ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(atom);
StringWriter s = new StringWriter();
SparqlConjunctiveQueryWriter w = new SparqlConjunctiveQueryWriter(s, this.utils.getURIzer());
try {
w.write(query);
w.close();
} catch (IOException e1) {
throw new AtomSetException("Error while converting to SPARQL " + atom, e1);
}
TupleQuery sparqlQuery = this.connection.prepareTupleQuery(s.toString());
TupleQueryResult result = sparqlQuery.evaluate();
return new TupleQueryResultAtomIterator(result, atom, utils);
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class ConjunctiveQuery2Test method nonexistingPredicateQuery.
@Theory
public void nonexistingPredicateQuery(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
try {
store.addAll(DlgpParser.parseAtomSet("<P>(a,b)."));
ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y) :- q(X,Y).");
CloseableIterator<Substitution> subReader;
subReader = h.execute(query, store);
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 ConjunctiveQuery2Test method wrongArityQuery2.
@Theory
public void wrongArityQuery2(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
Assume.assumeFalse(store instanceof TripleStore);
try {
store.add(DlgpParser.parseAtom("<P>(a,b)."));
ConjunctiveQuery query = DlgpParser.parseQuery("? :- <P>(X,Y,Z).");
CloseableIterator<Substitution> subReader;
subReader = h.execute(query, store);
Assert.assertFalse(subReader.hasNext());
subReader.close();
} catch (Exception e) {
Assert.assertTrue(e.getMessage(), false);
}
}
Aggregations