use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class AccumuloStatementMetadataNodeTest method simpleQueryWithConstantContext.
/**
* Tests if StatementMetadataNode joins BindingSet values correctly for
* variables appearing as the object in one of the StatementPattern
* statements (in the case ?x appears as the Object in the statement
* _:blankNode rdf:object ?x). StatementPattern statements have either
* rdf:subject, rdf:predicate, or rdf:object as the predicate. Additionally,
* this test also determines whether node uses specified context as a query
* constraint.
*
* @throws MalformedQueryException
* @throws QueryEvaluationException
* @throws RyaDAOException
*/
@Test
public void simpleQueryWithConstantContext() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
// query used to create StatementPatternMetadataNode
String contextQuery = "prefix owl: <http://www.w3.org/2002/07/owl#> prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> select ?x ?y where { graph <http://context_1> {_:blankNode rdf:type owl:Annotation; owl:annotatedSource <http://Joe>; " + "owl:annotatedProperty <http://worksAt>; owl:annotatedTarget ?x; <http://createdBy> ?y; <http://createdOn> \'2017-01-04\'^^xsd:date }}";
StatementMetadata metadata = new StatementMetadata();
metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe"));
metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context_1"), "", metadata);
RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context_2"), "", metadata);
dao.add(statement1);
dao.add(statement2);
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(contextQuery, null);
List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
StatementMetadataNode<AccumuloRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);
List<BindingSet> bsCollection = new ArrayList<>();
QueryBindingSet bsConstraint1 = new QueryBindingSet();
bsConstraint1.addBinding("x", new LiteralImpl("CoffeeShop"));
bsConstraint1.addBinding("z", new LiteralImpl("Virginia"));
QueryBindingSet bsConstraint2 = new QueryBindingSet();
bsConstraint2.addBinding("x", new LiteralImpl("HardwareStore"));
bsConstraint2.addBinding("z", new LiteralImpl("Maryland"));
QueryBindingSet bsConstraint3 = new QueryBindingSet();
bsConstraint3.addBinding("x", new LiteralImpl("BurgerShack"));
bsConstraint3.addBinding("z", new LiteralImpl("Delaware"));
bsCollection.add(bsConstraint1);
bsCollection.add(bsConstraint2);
bsCollection.add(bsConstraint3);
CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsCollection);
QueryBindingSet expected1 = new QueryBindingSet();
expected1.addBinding("x", new LiteralImpl("CoffeeShop"));
expected1.addBinding("y", new LiteralImpl("Joe"));
expected1.addBinding("z", new LiteralImpl("Virginia"));
List<BindingSet> bsList = new ArrayList<>();
while (iteration.hasNext()) {
bsList.add(iteration.next());
}
Assert.assertEquals(1, bsList.size());
Assert.assertEquals(expected1, bsList.get(0));
dao.delete(statement1, conf);
dao.delete(statement2, conf);
}
use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class AccumuloStatementMetadataNodeTest method simpleQueryWithoutBindingSet.
@Test
public void simpleQueryWithoutBindingSet() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
StatementMetadata metadata = new StatementMetadata();
metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe"));
metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
RyaStatement statement = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata);
dao.add(statement);
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
// RyaQueryEngine<RdfCloudTripleStoreConfiguration> engine =
// (RyaQueryEngine<>) dao.getQueryEngine();
StatementMetadataNode<?> node = new StatementMetadataNode<>(spList, conf);
CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(new QueryBindingSet());
QueryBindingSet bs = new QueryBindingSet();
bs.addBinding("x", new LiteralImpl("CoffeeShop"));
bs.addBinding("y", new LiteralImpl("Joe"));
List<BindingSet> bsList = new ArrayList<>();
while (iteration.hasNext()) {
bsList.add(iteration.next());
}
Assert.assertEquals(1, bsList.size());
Assert.assertEquals(bs, bsList.get(0));
dao.delete(statement, conf);
}
use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class StatementMetadataExternalSetProviderTest method createSingleAccumuloMetadataNode.
@Test
public void createSingleAccumuloMetadataNode() throws MalformedQueryException {
AccumuloRdfConfiguration conf = (AccumuloRdfConfiguration) getConf(false);
Set<RyaURI> propertySet = new HashSet<>();
propertySet.add(new RyaURI("http://createdBy"));
conf.setStatementMetadataProperties(propertySet);
StatementMetadataExternalSetProvider metaProvider = new StatementMetadataExternalSetProvider(conf);
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
List<QueryModelNode> patterns = new ArrayList<>();
List<StatementMetadataNode<?>> expected = new ArrayList<>();
Set<StatementPattern> sp = StatementMetadataTestUtils.getMetadataStatementPatterns(pq.getTupleExpr(), propertySet);
patterns.addAll(StatementPatternCollector.process(pq.getTupleExpr()));
JoinSegment<StatementMetadataNode<?>> segment = new JoinSegment<>(new HashSet<>(patterns), patterns, new HashMap<ValueExpr, Filter>());
List<StatementMetadataNode<?>> extSets = metaProvider.getExternalSets(segment);
expected.add(new StatementMetadataNode<>(sp, conf));
Assert.assertEquals(expected, extSets);
}
use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class ListFluoQueries method getPrettyPrintSparql.
private static String getPrettyPrintSparql(String sparql, int indent) throws Exception {
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(sparql, null);
SPARQLQueryRenderer render = new SPARQLQueryRenderer();
String renderedQuery = render.render(pq);
// remove extra quotes generated by query renderer
String[] splitRender = renderedQuery.split("\"\"\"");
StringBuilder builder = new StringBuilder();
for (String s : splitRender) {
builder.append(s).append("\"");
}
builder.replace(builder.length() - 1, builder.length(), "");
// add indent to all lines following newline char
String[] newLineRender = builder.toString().split("\n");
builder = new StringBuilder();
String prefix = getVariableIndent(indent);
for (int i = 0; i < newLineRender.length; i++) {
if (i != 0) {
builder.append(prefix);
}
builder.append(newLineRender[i]).append("\n");
}
return builder.toString();
}
use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class ProjectionEvaluatorTest method getProjection.
/**
* Get the first {@link Projection} node from a SPARQL query.
*
* @param sparql - The query that contains a single Projection node.
* @return The first {@link Projection} that is encountered.
* @throws Exception The query could not be parsed.
*/
@Nullable
public static Projection getProjection(final String sparql) throws Exception {
requireNonNull(sparql);
final AtomicReference<Projection> projection = new AtomicReference<>();
final ParsedQuery parsed = new SPARQLParser().parseQuery(sparql, null);
parsed.getTupleExpr().visit(new QueryModelVisitorBase<Exception>() {
@Override
public void meet(final Projection node) throws Exception {
projection.set(node);
}
});
return projection.get();
}
Aggregations