use of org.eclipse.rdf4j.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class StatementPatternEvalTest method simpleQueryWithoutBindingSets.
@Test
public void simpleQueryWithoutBindingSets() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
// query is used to build statement that will be evaluated
String query = "select ?x ?c where{ graph ?c {?x <uri:talksTo> <uri:Bob>. }}";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
RyaStatement statement1 = new RyaStatement(new RyaIRI("uri:Joe"), new RyaIRI("uri:talksTo"), new RyaType("uri:Bob"), new RyaIRI("uri:context1"), "", new StatementMetadata());
dao.add(statement1);
RyaStatement statement2 = new RyaStatement(new RyaIRI("uri:Doug"), new RyaIRI("uri:talksTo"), new RyaType("uri:Bob"), new RyaIRI("uri:context2"), "", new StatementMetadata());
dao.add(statement2);
RyaStatement statement3 = new RyaStatement(new RyaIRI("uri:Eric"), new RyaIRI("uri:talksTo"), new RyaType("uri:Bob"), new RyaIRI("uri:context3"), "", new StatementMetadata());
dao.add(statement3);
QueryBindingSet bsConstraint1 = new QueryBindingSet();
CloseableIteration<BindingSet, QueryEvaluationException> iteration = eval.evaluate(spList.get(0), Arrays.asList(bsConstraint1));
List<BindingSet> bsList = new ArrayList<>();
while (iteration.hasNext()) {
bsList.add(iteration.next());
}
Assert.assertEquals(3, bsList.size());
QueryBindingSet expected1 = new QueryBindingSet();
expected1.addBinding("x", VF.createIRI("uri:Joe"));
expected1.addBinding("c", VF.createIRI("uri:context1"));
QueryBindingSet expected2 = new QueryBindingSet();
expected2.addBinding("x", VF.createIRI("uri:Doug"));
expected2.addBinding("c", VF.createIRI("uri:context2"));
QueryBindingSet expected3 = new QueryBindingSet();
expected3.addBinding("x", VF.createIRI("uri:Eric"));
expected3.addBinding("c", VF.createIRI("uri:context3"));
Set<BindingSet> expected = new HashSet<>(Arrays.asList(expected1, expected2, expected3));
Set<BindingSet> actual = new HashSet<>(bsList);
Assert.assertEquals(expected, actual);
dao.delete(Arrays.asList(statement1, statement2, statement3).iterator(), conf);
}
use of org.eclipse.rdf4j.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class StatementPatternEvalTest method simpleQueryWithBindingSetSameContext.
@Test
public void simpleQueryWithBindingSetSameContext() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
// query is used to build statement that will be evaluated
String query = "select ?x ?c where{ graph ?c {?x <uri:talksTo> <uri:Bob>. }}";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
RyaStatement statement1 = new RyaStatement(new RyaIRI("uri:Joe"), new RyaIRI("uri:talksTo"), new RyaType("uri:Bob"), new RyaIRI("uri:context1"), "", new StatementMetadata());
dao.add(statement1);
RyaStatement statement2 = new RyaStatement(new RyaIRI("uri:Doug"), new RyaIRI("uri:talksTo"), new RyaType("uri:Bob"), new RyaIRI("uri:context2"), "", new StatementMetadata());
dao.add(statement2);
RyaStatement statement3 = new RyaStatement(new RyaIRI("uri:Eric"), new RyaIRI("uri:talksTo"), new RyaType("uri:Bob"), new RyaIRI("uri:context3"), "", new StatementMetadata());
dao.add(statement3);
QueryBindingSet bsConstraint1 = new QueryBindingSet();
bsConstraint1.addBinding("c", VF.createIRI("uri:context1"));
QueryBindingSet bsConstraint2 = new QueryBindingSet();
bsConstraint2.addBinding("c", VF.createIRI("uri:context1"));
CloseableIteration<BindingSet, QueryEvaluationException> iteration = eval.evaluate(spList.get(0), Arrays.asList(bsConstraint1, bsConstraint2));
List<BindingSet> bsList = new ArrayList<>();
while (iteration.hasNext()) {
bsList.add(iteration.next());
}
Assert.assertEquals(1, bsList.size());
QueryBindingSet expected = new QueryBindingSet();
expected.addBinding("x", VF.createIRI("uri:Joe"));
expected.addBinding("c", VF.createIRI("uri:context1"));
Assert.assertEquals(expected, bsList.get(0));
dao.delete(Arrays.asList(statement1, statement2, statement3).iterator(), conf);
}
use of org.eclipse.rdf4j.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class RdfTestUtil method getMultiProjection.
/**
* Get the first {@link MultiProjection} node from a SPARQL query.
*
* @param sparql - The query that contains a single Projection node.
* @return The first {@link MultiProjection} that is encountered.
* @throws Exception The query could not be parsed.
*/
@Nullable
public static MultiProjection getMultiProjection(final String sparql) throws Exception {
requireNonNull(sparql);
final AtomicReference<MultiProjection> multiProjection = new AtomicReference<>();
final ParsedQuery parsed = new SPARQLParser().parseQuery(sparql, null);
parsed.getTupleExpr().visit(new AbstractQueryModelVisitor<Exception>() {
@Override
public void meet(final MultiProjection node) throws Exception {
multiProjection.set(node);
}
});
return multiProjection.get();
}
use of org.eclipse.rdf4j.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class RdfTestUtil method getFilter.
/**
* Get the first {@link Filter} node from a SPARQL query.
*
* @param sparql - The query that contains a single Projection node.
* @return The first {@link Filter} that is encountered.
* @throws Exception The query could not be parsed.
*/
@Nullable
public static Filter getFilter(final String sparql) throws Exception {
requireNonNull(sparql);
final AtomicReference<Filter> filter = new AtomicReference<>();
final ParsedQuery parsed = new SPARQLParser().parseQuery(sparql, null);
parsed.getTupleExpr().visit(new AbstractQueryModelVisitor<Exception>() {
@Override
public void meet(final Filter node) throws Exception {
filter.set(node);
}
});
return filter.get();
}
use of org.eclipse.rdf4j.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class MultiProjectionEvaluatorTest method getMultiProjection.
/**
* Get the first {@link MultiProjection} node from a SPARQL query.
*
* @param sparql - The query that contains a single Projection node.
* @return The first {@link MultiProjection} that is encountered.
* @throws Exception The query could not be parsed.
*/
@Nullable
public static MultiProjection getMultiProjection(final String sparql) throws Exception {
requireNonNull(sparql);
final AtomicReference<MultiProjection> multiProjection = new AtomicReference<>();
final ParsedQuery parsed = new SPARQLParser().parseQuery(sparql, null);
parsed.getTupleExpr().visit(new AbstractQueryModelVisitor<Exception>() {
@Override
public void meet(final MultiProjection node) throws Exception {
multiProjection.set(node);
}
});
return multiProjection.get();
}
Aggregations