use of org.openrdf.query.algebra.ZeroLengthPath 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.ZeroLengthPath in project incubator-rya by apache.
the class ReflexivePropertyVisitor method meetSP.
/**
* Check whether any solution for the {@link StatementPattern} could be derived from
* reflexive property inference, and if so, replace the pattern with a union of itself and the
* reflexive solution.
*/
@Override
protected void meetSP(StatementPattern node) throws Exception {
// Only applies when the predicate is defined and reflexive
final Var predVar = node.getPredicateVar();
if (predVar.getValue() != null && inferenceEngine.isReflexiveProperty((URI) predVar.getValue())) {
final StatementPattern originalSP = node.clone();
// The reflexive solution is a ZeroLengthPath between subject and
// object: they can be matched to one another, whether constants or
// variables.
final Var subjVar = node.getSubjectVar();
final Var objVar = node.getObjectVar();
final ZeroLengthPath reflexiveSolution = new ZeroLengthPath(subjVar, objVar);
node.replaceWith(new InferUnion(originalSP, reflexiveSolution));
}
}
Aggregations