Search in sources :

Example 51 with TupleExpr

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

the class TupleExprBuilder method visit.

@Override
public Object visit(ASTPathAlternative pathAltNode, Object data) throws VisitorException {
    int altCount = pathAltNode.jjtGetNumChildren();
    if (altCount > 1) {
        GraphPattern parentGP = graphPattern;
        Union union = new Union();
        Union currentUnion = union;
        for (int i = 0; i < altCount - 1; i++) {
            graphPattern = new GraphPattern(parentGP);
            pathAltNode.jjtGetChild(i).jjtAccept(this, data);
            TupleExpr arg = graphPattern.buildTupleExpr();
            currentUnion.setLeftArg(arg);
            if (i == altCount - 2) {
                // second-to-last item
                graphPattern = new GraphPattern(parentGP);
                pathAltNode.jjtGetChild(i + 1).jjtAccept(this, data);
                arg = graphPattern.buildTupleExpr();
                currentUnion.setRightArg(arg);
            } else {
                Union newUnion = new Union();
                currentUnion.setRightArg(newUnion);
                currentUnion = newUnion;
            }
        }
        parentGP.addRequiredTE(union);
        graphPattern = parentGP;
    } else {
        pathAltNode.jjtGetChild(0).jjtAccept(this, data);
    }
    return null;
}
Also used : Union(org.eclipse.rdf4j.query.algebra.Union) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 52 with TupleExpr

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

the class TupleExprBuilder method visit.

@Override
public TupleExpr visit(ASTDescribe node, Object data) throws VisitorException {
    TupleExpr tupleExpr = (TupleExpr) data;
    if (tupleExpr == null) {
        tupleExpr = new SingletonSet();
    }
    Extension e = new Extension();
    ProjectionElemList projectionElements = new ProjectionElemList();
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        ValueExpr resource = (ValueExpr) node.jjtGetChild(i).jjtAccept(this, null);
        if (resource instanceof Var) {
            projectionElements.addElement(new ProjectionElem(((Var) resource).getName()));
        } else {
            String alias = "_describe_" + UUID.randomUUID().toString().replaceAll("-", "_");
            ExtensionElem elem = new ExtensionElem(resource, alias);
            e.addElement(elem);
            projectionElements.addElement(new ProjectionElem(alias));
        }
    }
    if (!e.getElements().isEmpty()) {
        e.setArg(tupleExpr);
        tupleExpr = e;
    }
    Projection p = new Projection(tupleExpr, projectionElements);
    return new DescribeOperator(p);
}
Also used : Extension(org.eclipse.rdf4j.query.algebra.Extension) ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) SingletonSet(org.eclipse.rdf4j.query.algebra.SingletonSet) Var(org.eclipse.rdf4j.query.algebra.Var) DescribeOperator(org.eclipse.rdf4j.query.algebra.DescribeOperator) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) Projection(org.eclipse.rdf4j.query.algebra.Projection) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem)

Example 53 with TupleExpr

use of org.eclipse.rdf4j.query.algebra.TupleExpr 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)

Example 54 with TupleExpr

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

the class TupleExprBuilder method visit.

@Override
public Object visit(ASTGraphPatternGroup node, Object data) throws VisitorException {
    GraphPattern parentGP = graphPattern;
    graphPattern = new GraphPattern(parentGP);
    boolean optionalPatternInGroup = false;
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        if (optionalPatternInGroup) {
            // building the LeftJoin and resetting the graphPattern.
            TupleExpr te = graphPattern.buildTupleExpr();
            graphPattern = new GraphPattern(parentGP);
            graphPattern.addRequiredTE(te);
            optionalPatternInGroup = false;
        }
        Node childNode = node.jjtGetChild(i);
        data = childNode.jjtAccept(this, data);
        if (childNode instanceof ASTOptionalGraphPattern) {
            optionalPatternInGroup = true;
        }
    }
    // Filters are scoped to the graph pattern group and do not affect
    // bindings external to the group
    TupleExpr te = graphPattern.buildTupleExpr();
    parentGP.addRequiredTE(te);
    graphPattern = parentGP;
    return te;
}
Also used : QueryModelNode(org.eclipse.rdf4j.query.algebra.QueryModelNode) IsBNode(org.eclipse.rdf4j.query.algebra.IsBNode) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 55 with TupleExpr

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

the class TupleExprBuilder method visit.

@Override
public TupleExpr visit(ASTAskQuery node, Object data) throws VisitorException {
    graphPattern = new GraphPattern();
    super.visit(node, null);
    TupleExpr tupleExpr = graphPattern.buildTupleExpr();
    tupleExpr = new Slice(tupleExpr, 0, 1);
    // Apply grouping
    Group group = null;
    ASTGroupClause groupNode = node.getGroupClause();
    if (groupNode != null) {
        tupleExpr = (TupleExpr) groupNode.jjtAccept(this, tupleExpr);
        group = (Group) tupleExpr;
    }
    final ASTHavingClause havingClause = node.getHavingClause();
    if (havingClause != null) {
        if (group == null) {
            // create implicit group
            group = new Group(tupleExpr);
        }
        // Apply HAVING group filter condition
        tupleExpr = processHavingClause(havingClause, tupleExpr, group);
    }
    // process bindings clause
    final ASTBindingsClause bindingsClause = node.getBindingsClause();
    if (bindingsClause != null) {
        tupleExpr = new Join((BindingSetAssignment) bindingsClause.jjtAccept(this, null), tupleExpr);
    }
    final ASTOrderClause orderClause = node.getOrderClause();
    if (orderClause != null) {
        if (group == null) {
            // create implicit group
            group = new Group(tupleExpr);
        }
        // Apply result ordering
        tupleExpr = processOrderClause(node.getOrderClause(), tupleExpr, group);
    }
    return tupleExpr;
}
Also used : Group(org.eclipse.rdf4j.query.algebra.Group) Slice(org.eclipse.rdf4j.query.algebra.Slice) BindingSetAssignment(org.eclipse.rdf4j.query.algebra.BindingSetAssignment) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Aggregations

TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)61 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)19 Var (org.eclipse.rdf4j.query.algebra.Var)14 Projection (org.eclipse.rdf4j.query.algebra.Projection)13 Join (org.eclipse.rdf4j.query.algebra.Join)12 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)11 Slice (org.eclipse.rdf4j.query.algebra.Slice)11 ArrayList (java.util.ArrayList)9 Extension (org.eclipse.rdf4j.query.algebra.Extension)9 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)9 Test (org.junit.Test)9 Distinct (org.eclipse.rdf4j.query.algebra.Distinct)8 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)8 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)8 Filter (org.eclipse.rdf4j.query.algebra.Filter)7 Union (org.eclipse.rdf4j.query.algebra.Union)7 Group (org.eclipse.rdf4j.query.algebra.Group)6 MultiProjection (org.eclipse.rdf4j.query.algebra.MultiProjection)6 Order (org.eclipse.rdf4j.query.algebra.Order)6 Reduced (org.eclipse.rdf4j.query.algebra.Reduced)6