use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class ReflexivePropertyVisitorTest method testReflexiveProperty.
@Test
public void testReflexiveProperty() throws Exception {
// Define a reflexive property
final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
when(inferenceEngine.isReflexiveProperty(HAS_FAMILY)).thenReturn(true);
// Construct a query, then visit it
final StatementPattern sp = new StatementPattern(new Var("s", ALICE), new Var("p", HAS_FAMILY), new Var("o"));
final Projection query = new Projection(sp, new ProjectionElemList(new ProjectionElem("o", "member")));
query.visit(new ReflexivePropertyVisitor(conf, inferenceEngine));
// Expected structure after rewriting SP(:Alice :hasFamilyMember ?member):
//
// Union(
// originalSP(:Alice :hasFamilyMember ?member),
// ZeroLengthPath(:Alice, ?member)
// )
Assert.assertTrue(query.getArg() instanceof Union);
final TupleExpr left = ((Union) query.getArg()).getLeftArg();
final TupleExpr right = ((Union) query.getArg()).getRightArg();
Assert.assertEquals(sp, left);
Assert.assertTrue(right instanceof ZeroLengthPath);
Assert.assertEquals(sp.getSubjectVar(), ((ZeroLengthPath) right).getSubjectVar());
Assert.assertEquals(sp.getObjectVar(), ((ZeroLengthPath) right).getObjectVar());
}
use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class SomeValuesFromVisitorTest method testSomeValuesFrom.
@Test
public void testSomeValuesFrom() throws Exception {
// Configure a mock instance engine with an ontology:
final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
Map<Resource, Set<URI>> personSVF = new HashMap<>();
personSVF.put(gradCourse, Sets.newHashSet(takesCourse));
personSVF.put(course, Sets.newHashSet(takesCourse));
personSVF.put(department, Sets.newHashSet(headOf));
personSVF.put(organization, Sets.newHashSet(worksFor, headOf));
when(inferenceEngine.getSomeValuesFromByRestrictionType(person)).thenReturn(personSVF);
// 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 SomeValuesFromVisitor(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/value type combinations
// with another join querying for any nodes who are the subject of a triple with that
// predicate and with an object of that type.
//
// Union(
// SP(?node a :impliedType),
// Join(
// FSP(<?property someValuesFrom ?valueType> {
// takesCourse/Course;
// takesCourse/GraduateCourse;
// headOf/Department;
// headOf/Organization;
// worksFor/Organization;
// }),
// Join(
// SP(_:object a ?valueType),
// SP(?node ?property _:object)
// )
// )
Assert.assertTrue(query.getArg() instanceof Union);
TupleExpr left = ((Union) query.getArg()).getLeftArg();
TupleExpr right = ((Union) query.getArg()).getRightArg();
Assert.assertEquals(originalSP, left);
Assert.assertTrue(right instanceof Join);
final Join join = (Join) right;
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/type pairs
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(takesCourse, OWL.SOMEVALUESFROM, course)));
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(takesCourse, OWL.SOMEVALUESFROM, gradCourse)));
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(headOf, OWL.SOMEVALUESFROM, department)));
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(headOf, OWL.SOMEVALUESFROM, organization)));
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(worksFor, OWL.SOMEVALUESFROM, organization)));
Assert.assertEquals(5, fsp.statements.size());
// Verify pattern for matching instances of each pair: a Join of <_:x rdf:type ?t> and
// <?s ?p _:x> where p and t are the predicate/type pair and s is the original subject
// variable.
StatementPattern leftSP = (StatementPattern) left;
StatementPattern rightSP = (StatementPattern) right;
Assert.assertEquals(rightSP.getObjectVar(), leftSP.getSubjectVar());
Assert.assertEquals(RDF.TYPE, leftSP.getPredicateVar().getValue());
Assert.assertEquals(fsp.getObjectVar(), leftSP.getObjectVar());
Assert.assertEquals(originalSP.getSubjectVar(), rightSP.getSubjectVar());
Assert.assertEquals(fsp.getSubjectVar(), rightSP.getPredicateVar());
}
use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class ConstructConsequentVisitorTest method testConcreteSP.
@Test
public void testConcreteSP() {
Extension extension = new Extension(new SingletonSet(), new ExtensionElem(new ValueConstant(FOAF.PERSON), "x"), new ExtensionElem(new ValueConstant(RDF.TYPE), "y"), new ExtensionElem(new ValueConstant(OWL.CLASS), "z"));
Projection projection = new Projection(extension, new ProjectionElemList(new ProjectionElem("x", "subject"), new ProjectionElem("y", "predicate"), new ProjectionElem("z", "object")));
ConstructConsequentVisitor visitor = new ConstructConsequentVisitor();
projection.visit(visitor);
Set<StatementPattern> expected = Sets.newHashSet(new StatementPattern(s(FOAF.PERSON), p(RDF.TYPE), o(OWL.CLASS)));
Assert.assertEquals(expected, visitor.getConsequents());
}
use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class ConstructConsequentVisitorTest method testMissingVariables.
@Test
public void testMissingVariables() {
Extension extension = new Extension(new SingletonSet(), new ExtensionElem(new ValueConstant(FOAF.PERSON), "x"), new ExtensionElem(new ValueConstant(RDF.TYPE), "y"));
Projection projection = new Projection(extension, new ProjectionElemList(new ProjectionElem("x", "s"), new ProjectionElem("y", "predicate"), new ProjectionElem("z", "object")));
ConstructConsequentVisitor visitor = new ConstructConsequentVisitor();
projection.visit(visitor);
Set<StatementPattern> expected = Sets.newHashSet(new StatementPattern(s(null), p(RDF.TYPE), o(null)));
Assert.assertEquals(expected, visitor.getConsequents());
}
use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class ProjectionEvaluatorTest method changesNothing.
/**
* This Projection enumerates all of the variables that were in the query, none of them are anonymous, and
* none of them insert constants.
*/
@Test
public void changesNothing() throws Exception {
// Read the projection object from a SPARQL query.
final Projection projection = getProjection("SELECT ?person ?employee ?business " + "WHERE { " + "?person <urn:talksTo> ?employee . " + "?employee <urn:worksAt> ?business . " + "}");
// Create a Binding Set that contains the result of the WHERE clause.
final ValueFactory vf = new ValueFactoryImpl();
final MapBindingSet bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Alice"));
bs.addBinding("employee", vf.createURI("urn:Bob"));
bs.addBinding("business", vf.createURI("urn:TacoJoint"));
final VisibilityBindingSet original = new VisibilityBindingSet(bs, "a|b");
// Execute the projection.
final VisibilityBindingSet result = ProjectionEvaluator.make(projection).project(original);
assertEquals(original, result);
}
Aggregations