Search in sources :

Example 1 with ZeroLengthPath

use of org.eclipse.rdf4j.query.algebra.ZeroLengthPath in project rdf4j by eclipse.

the class TupleExprBuilder method createPath.

private TupleExpr createPath(Scope scope, Var subjVar, TupleExpr pathExpression, Var endVar, Var contextVar, long length) throws VisitorException {
    if (pathExpression instanceof StatementPattern) {
        Var predVar = ((StatementPattern) pathExpression).getPredicateVar();
        if (length == 0L) {
            return new ZeroLengthPath(scope, subjVar, endVar, contextVar);
        } else {
            GraphPattern gp = new GraphPattern();
            gp.setContextVar(contextVar);
            gp.setStatementPatternScope(scope);
            Var nextVar = null;
            for (long i = 0L; i < length; i++) {
                if (i < length - 1) {
                    nextVar = createAnonVar();
                } else {
                    nextVar = endVar;
                }
                gp.addRequiredSP(subjVar, predVar, nextVar);
                subjVar = nextVar;
            }
            return gp.buildTupleExpr();
        }
    } else {
        if (length == 0L) {
            return new ZeroLengthPath(scope, subjVar, endVar, contextVar);
        } else {
            GraphPattern gp = new GraphPattern();
            gp.setContextVar(contextVar);
            gp.setStatementPatternScope(scope);
            Var nextVar = null;
            for (long i = 0L; i < length; i++) {
                if (i < length - 1L) {
                    nextVar = createAnonVar();
                } else {
                    nextVar = endVar;
                }
                // create a clone of the path expression.
                TupleExpr clone = pathExpression.clone();
                VarReplacer replacer = new VarReplacer(endVar, nextVar);
                clone.visit(replacer);
                gp.addRequiredTE(clone);
                subjVar = nextVar;
            }
            return gp.buildTupleExpr();
        }
    }
}
Also used : StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) ZeroLengthPath(org.eclipse.rdf4j.query.algebra.ZeroLengthPath) Var(org.eclipse.rdf4j.query.algebra.Var) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Aggregations

StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)1 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)1 Var (org.eclipse.rdf4j.query.algebra.Var)1 ZeroLengthPath (org.eclipse.rdf4j.query.algebra.ZeroLengthPath)1