use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class ProjectionEvaluatorTest method addsBlankNodeBinding.
/**
* This projection creates a Binding Set that represents a Statement that has a blank node added to it.
*/
@Test
public void addsBlankNodeBinding() throws Exception {
// Read the projection object from a SPARQL query.
final Projection projection = getProjection("CONSTRUCT { ?person <urn:hasChild> _:b } " + "WHERE {" + "?person <urn:hasGrandchild> ?grandchild ." + "}");
// Create a Binding Set that contains the result of the WHERE clause.
final ValueFactory vf = new ValueFactoryImpl();
MapBindingSet bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Alice"));
bs.addBinding("hasGrandchild", vf.createURI("urn:Bob"));
final VisibilityBindingSet original = new VisibilityBindingSet(bs, "a|b");
// Execute the projection.
final VisibilityBindingSet result = ProjectionEvaluator.make(projection).project(original);
// The expected binding set represents a statement. We need to get the blank node's id from the
// result since that is different every time.
bs = new MapBindingSet();
bs.addBinding("subject", vf.createURI("urn:Alice"));
bs.addBinding("predicate", vf.createURI("urn:hasChild"));
bs.addBinding("object", result.getValue("object"));
final VisibilityBindingSet expected = new VisibilityBindingSet(bs, "a|b");
assertEquals(expected, result);
}
use of org.openrdf.query.algebra.Projection 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();
}
use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class PipelineQueryIT method testNoVariableSP.
@Test
public void testNoVariableSP() throws Exception {
// Insert data
insert(OWL.THING, RDF.TYPE, OWL.CLASS);
insert(FOAF.PERSON, RDF.TYPE, OWL.CLASS, 1);
insert(FOAF.PERSON, RDFS.SUBCLASSOF, OWL.THING);
insert(VF.createURI("urn:Alice"), RDF.TYPE, FOAF.PERSON);
dao.flush();
// Define query and expected results
final String query = "SELECT * WHERE {\n" + " owl:Thing a owl:Class .\n" + "}";
Multiset<BindingSet> expectedSolutions = HashMultiset.create();
expectedSolutions.add(new EmptyBindingSet());
// Execute pipeline and verify results
QueryRoot queryTree = new QueryRoot(PARSER.parseQuery(query, null).getTupleExpr());
SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(getRyaCollection());
queryTree.visit(visitor);
Assert.assertTrue(queryTree.getArg() instanceof Projection);
Projection projection = (Projection) queryTree.getArg();
Assert.assertTrue(projection.getArg() instanceof AggregationPipelineQueryNode);
AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) projection.getArg();
Multiset<BindingSet> solutions = HashMultiset.create();
CloseableIteration<BindingSet, QueryEvaluationException> iter = pipelineNode.evaluate(new QueryBindingSet());
while (iter.hasNext()) {
solutions.add(iter.next());
}
Assert.assertEquals(expectedSolutions, solutions);
}
use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class SparqlToPipelineTransformVisitorTest method testProjection.
@Test
public void testProjection() throws Exception {
StatementPattern isUndergrad = new StatementPattern(new Var("x"), constant(RDF.TYPE), constant(UNDERGRAD));
StatementPattern isCourse = new StatementPattern(new Var("course"), constant(RDF.TYPE), constant(COURSE));
StatementPattern hasEdge = new StatementPattern(new Var("x"), new Var("p"), new Var("course"));
ProjectionElemList projectionElements = new ProjectionElemList(new ProjectionElem("p", "relation"), new ProjectionElem("course"));
QueryRoot queryTree = new QueryRoot(new Projection(new Join(new Join(isCourse, hasEdge), isUndergrad), projectionElements));
SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
queryTree.visit(visitor);
Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
Assert.assertEquals(Sets.newHashSet("relation", "course"), pipelineNode.getAssuredBindingNames());
}
use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class AllValuesFromVisitorTest method testRewriteTypePattern.
@Test
public void testRewriteTypePattern() throws Exception {
// Configure a mock instance engine with an ontology:
final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
Map<Resource, Set<URI>> personAVF = new HashMap<>();
personAVF.put(parentsAreTallPeople, new HashSet<>());
personAVF.put(parentsArePeople, new HashSet<>());
personAVF.put(relativesArePeople, new HashSet<>());
personAVF.get(parentsAreTallPeople).add(parent);
personAVF.get(parentsArePeople).add(parent);
personAVF.get(relativesArePeople).add(relative);
personAVF.get(relativesArePeople).add(parent);
Map<Resource, Set<URI>> dogAVF = new HashMap<>();
dogAVF.put(parentsAreDogs, new HashSet<>());
dogAVF.get(parentsAreDogs).add(parent);
when(inferenceEngine.getAllValuesFromByValueType(person)).thenReturn(personAVF);
when(inferenceEngine.getAllValuesFromByValueType(dog)).thenReturn(dogAVF);
// Query for a specific type and rewrite using the visitor:
StatementPattern originalSP = new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", person));
final Projection query = new Projection(originalSP, new ProjectionElemList(new ProjectionElem("s", "subject")));
query.visit(new AllValuesFromVisitor(conf, inferenceEngine));
// Expected structure: a union of two elements: one is equal to the original statement
// pattern, and the other one joins a list of predicate/restriction type combinations
// with another join querying for values of that predicate for members of that type.
Assert.assertTrue(query.getArg() instanceof Union);
TupleExpr left = ((Union) query.getArg()).getLeftArg();
TupleExpr right = ((Union) query.getArg()).getRightArg();
final Join join;
if (left instanceof StatementPattern) {
Assert.assertEquals(originalSP, left);
Assert.assertTrue(right instanceof Join);
join = (Join) right;
} else {
Assert.assertEquals(originalSP, right);
Assert.assertTrue(left instanceof Join);
join = (Join) left;
}
Assert.assertTrue(join.getLeftArg() instanceof FixedStatementPattern);
Assert.assertTrue(join.getRightArg() instanceof Join);
FixedStatementPattern fsp = (FixedStatementPattern) join.getLeftArg();
left = ((Join) join.getRightArg()).getLeftArg();
right = ((Join) join.getRightArg()).getRightArg();
Assert.assertTrue(left instanceof StatementPattern);
Assert.assertTrue(right instanceof StatementPattern);
// Verify expected predicate/restriction pairs
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(parentsArePeople, OWL.ONPROPERTY, parent)));
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(relativesArePeople, OWL.ONPROPERTY, relative)));
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(relativesArePeople, OWL.ONPROPERTY, parent)));
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(parentsAreTallPeople, OWL.ONPROPERTY, parent)));
Assert.assertEquals(4, fsp.statements.size());
// Verify general pattern for matching instances of each pair: Join on unknown subject; left
// triple states it belongs to the restriction while right triple relates it to the original
// subject variable by the relevant property. Restriction and property variables are given
// by the FixedStatementPattern.
StatementPattern leftSP = (StatementPattern) left;
StatementPattern rightSP = (StatementPattern) right;
Assert.assertEquals(rightSP.getSubjectVar(), leftSP.getSubjectVar());
Assert.assertEquals(RDF.TYPE, leftSP.getPredicateVar().getValue());
Assert.assertEquals(fsp.getSubjectVar(), leftSP.getObjectVar());
Assert.assertEquals(fsp.getObjectVar(), rightSP.getPredicateVar());
Assert.assertEquals(originalSP.getSubjectVar(), rightSP.getObjectVar());
}
Aggregations