Search in sources :

Example 1 with ZeroLengthPath

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());
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) StatementPattern(org.openrdf.query.algebra.StatementPattern) ZeroLengthPath(org.openrdf.query.algebra.ZeroLengthPath) Var(org.openrdf.query.algebra.Var) Projection(org.openrdf.query.algebra.Projection) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) Union(org.openrdf.query.algebra.Union) TupleExpr(org.openrdf.query.algebra.TupleExpr) Test(org.junit.Test)

Example 2 with ZeroLengthPath

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));
    }
}
Also used : StatementPattern(org.openrdf.query.algebra.StatementPattern) ZeroLengthPath(org.openrdf.query.algebra.ZeroLengthPath) Var(org.openrdf.query.algebra.Var) URI(org.openrdf.model.URI)

Aggregations

StatementPattern (org.openrdf.query.algebra.StatementPattern)2 Var (org.openrdf.query.algebra.Var)2 ZeroLengthPath (org.openrdf.query.algebra.ZeroLengthPath)2 Test (org.junit.Test)1 URI (org.openrdf.model.URI)1 Projection (org.openrdf.query.algebra.Projection)1 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)1 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)1 TupleExpr (org.openrdf.query.algebra.TupleExpr)1 Union (org.openrdf.query.algebra.Union)1