Search in sources :

Example 6 with ExtensionElem

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

the class QueryModelBuilder method visit.

@Override
public TupleExpr visit(ASTSelect node, Object data) throws VisitorException {
    TupleExpr result = (TupleExpr) data;
    Extension extension = new Extension();
    ProjectionElemList projElemList = new ProjectionElemList();
    for (ASTProjectionElem projElemNode : node.getProjectionElemList()) {
        ValueExpr valueExpr = (ValueExpr) projElemNode.getValueExpr().jjtAccept(this, null);
        String alias = projElemNode.getAlias();
        if (alias != null) {
            // aliased projection element
            extension.addElement(new ExtensionElem(valueExpr, alias));
            projElemList.addElement(new ProjectionElem(alias));
        } else if (valueExpr instanceof Var) {
            // unaliased variable
            Var projVar = (Var) valueExpr;
            projElemList.addElement(new ProjectionElem(projVar.getName()));
        } else {
            throw new IllegalStateException("required alias for non-Var projection elements not found");
        }
    }
    if (!extension.getElements().isEmpty()) {
        extension.setArg(result);
        result = extension;
    }
    result = new Projection(result, projElemList);
    if (node.isDistinct()) {
        result = new Distinct(result);
    } else if (node.isReduced()) {
        result = new Reduced(result);
    }
    return result;
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) ASTValueExpr(org.eclipse.rdf4j.query.parser.serql.ast.ASTValueExpr) ASTVar(org.eclipse.rdf4j.query.parser.serql.ast.ASTVar) Var(org.eclipse.rdf4j.query.algebra.Var) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) Projection(org.eclipse.rdf4j.query.algebra.Projection) ASTString(org.eclipse.rdf4j.query.parser.serql.ast.ASTString) ASTProjectionElem(org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Reduced(org.eclipse.rdf4j.query.algebra.Reduced) Extension(org.eclipse.rdf4j.query.algebra.Extension) Distinct(org.eclipse.rdf4j.query.algebra.Distinct) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem) ASTProjectionElem(org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem)

Example 7 with ExtensionElem

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

the class ConstructorBuilder method buildConstructor.

private TupleExpr buildConstructor(TupleExpr bodyExpr, TupleExpr constructExpr, boolean explicitConstructor, boolean distinct, boolean reduced) {
    TupleExpr result = bodyExpr;
    // Retrieve all StatementPattern's from the construct expression
    List<StatementPattern> statementPatterns = StatementPatternCollector.process(constructExpr);
    Set<Var> constructVars = getConstructVars(statementPatterns);
    // Finally, the spo-bindings are again filtered for duplicates.
    if (distinct || reduced) {
        // Create projection that removes all bindings that are not used in the
        // constructor
        ProjectionElemList projElemList = new ProjectionElemList();
        for (Var var : constructVars) {
            // the distinct
            if (!var.isAnonymous() && !var.hasValue()) {
                projElemList.addElement(new ProjectionElem(var.getName()));
            }
        }
        result = new Projection(result, projElemList);
        // Filter the duplicates from these projected bindings
        if (distinct) {
            result = new Distinct(result);
        } else {
            result = new Reduced(result);
        }
    }
    // Create BNodeGenerator's for all anonymous variables
    Map<Var, ExtensionElem> extElemMap = new HashMap<Var, ExtensionElem>();
    for (Var var : constructVars) {
        if (var.isAnonymous() && !extElemMap.containsKey(var)) {
            ValueExpr valueExpr = null;
            if (var.hasValue()) {
                valueExpr = new ValueConstant(var.getValue());
            } else if (explicitConstructor) {
                // only generate bnodes in case of an explicit constructor
                valueExpr = new BNodeGenerator();
            }
            if (valueExpr != null) {
                extElemMap.put(var, new ExtensionElem(valueExpr, var.getName()));
            }
        }
    }
    if (!extElemMap.isEmpty()) {
        result = new Extension(result, extElemMap.values());
    }
    // Create a Projection for each StatementPattern in the constructor
    List<ProjectionElemList> projections = new ArrayList<ProjectionElemList>();
    for (StatementPattern sp : statementPatterns) {
        ProjectionElemList projElemList = new ProjectionElemList();
        projElemList.addElement(new ProjectionElem(sp.getSubjectVar().getName(), "subject"));
        projElemList.addElement(new ProjectionElem(sp.getPredicateVar().getName(), "predicate"));
        projElemList.addElement(new ProjectionElem(sp.getObjectVar().getName(), "object"));
        projections.add(projElemList);
    }
    if (projections.size() == 1) {
        result = new Projection(result, projections.get(0));
    // Note: no need to apply the second duplicate elimination step if
    // there's just one projection
    } else if (projections.size() > 1) {
        result = new MultiProjection(result, projections);
        if (distinct) {
            // Add another distinct to filter duplicate statements
            result = new Distinct(result);
        } else if (reduced) {
            result = new Reduced(result);
        }
    } else {
        // Empty constructor
        result = new EmptySet();
    }
    return result;
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) HashMap(java.util.HashMap) Var(org.eclipse.rdf4j.query.algebra.Var) EmptySet(org.eclipse.rdf4j.query.algebra.EmptySet) ArrayList(java.util.ArrayList) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) Projection(org.eclipse.rdf4j.query.algebra.Projection) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Reduced(org.eclipse.rdf4j.query.algebra.Reduced) Extension(org.eclipse.rdf4j.query.algebra.Extension) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) Distinct(org.eclipse.rdf4j.query.algebra.Distinct) BNodeGenerator(org.eclipse.rdf4j.query.algebra.BNodeGenerator) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem)

Example 8 with ExtensionElem

use of org.eclipse.rdf4j.query.algebra.ExtensionElem 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 9 with ExtensionElem

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

the class TupleExprBuilder method processOrderClause.

private TupleExpr processOrderClause(ASTOrderClause orderNode, TupleExpr tupleExpr, Group group) throws VisitorException {
    if (orderNode != null) {
        @SuppressWarnings("unchecked") List<OrderElem> orderElements = (List<OrderElem>) orderNode.jjtAccept(this, null);
        for (OrderElem orderElem : orderElements) {
            // retrieve any aggregate operators from the order element.
            AggregateCollector collector = new AggregateCollector();
            collector.meet(orderElem);
            Extension extension = new Extension();
            for (AggregateOperator operator : collector.getOperators()) {
                Var var = createAnonVar();
                // replace occurrence of the operator in the order condition
                // with the variable.
                AggregateOperatorReplacer replacer = new AggregateOperatorReplacer(operator, var);
                replacer.meet(orderElem);
                // create an extension linking the operator to the variable
                // name.
                String alias = var.getName();
                ExtensionElem pe = new ExtensionElem(operator, alias);
                extension.addElement(pe);
                // add the aggregate operator to the group.
                GroupElem ge = new GroupElem(alias, operator);
                group.addGroupElement(ge);
            }
            if (!extension.getElements().isEmpty()) {
                extension.setArg(tupleExpr);
                tupleExpr = extension;
            }
        }
        tupleExpr = new Order(tupleExpr, orderElements);
    }
    return tupleExpr;
}
Also used : Order(org.eclipse.rdf4j.query.algebra.Order) Var(org.eclipse.rdf4j.query.algebra.Var) GroupElem(org.eclipse.rdf4j.query.algebra.GroupElem) OrderElem(org.eclipse.rdf4j.query.algebra.OrderElem) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) Extension(org.eclipse.rdf4j.query.algebra.Extension) AggregateOperator(org.eclipse.rdf4j.query.algebra.AggregateOperator) List(java.util.List) ArrayList(java.util.ArrayList) ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList)

Example 10 with ExtensionElem

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

the class TupleExprBuilder method processHavingClause.

private TupleExpr processHavingClause(ASTHavingClause havingNode, TupleExpr tupleExpr, Group group) throws VisitorException {
    if (havingNode != null) {
        // create an implicit group
        if (group == null) {
            group = new Group(tupleExpr);
        }
        ValueExpr expr = (ValueExpr) havingNode.jjtGetChild(0).jjtAccept(this, tupleExpr);
        // retrieve any aggregate operators from the expression.
        AggregateCollector collector = new AggregateCollector();
        collector.meetOther(expr);
        // replace operator occurrences with an anonymous var, and alias it
        // to the group
        Extension extension = new Extension();
        for (AggregateOperator operator : collector.getOperators()) {
            Var var = createAnonVar();
            // replace occurrence of the operator in the filter expression
            // with the variable.
            AggregateOperatorReplacer replacer = new AggregateOperatorReplacer(operator, var);
            replacer.meetOther(expr);
            String alias = var.getName();
            // create an extension linking the operator to the variable
            // name.
            ExtensionElem pe = new ExtensionElem(operator, alias);
            extension.addElement(pe);
            // add the aggregate operator to the group.
            GroupElem ge = new GroupElem(alias, operator);
            // FIXME quite often the aggregate in the HAVING clause will be
            // a duplicate of an aggregate in the projection. We could
            // perhaps
            // optimize for that, to avoid having to evaluate twice.
            group.addGroupElement(ge);
        }
        extension.setArg(group);
        tupleExpr = new Filter(extension, expr);
    }
    return tupleExpr;
}
Also used : Extension(org.eclipse.rdf4j.query.algebra.Extension) Group(org.eclipse.rdf4j.query.algebra.Group) ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) Filter(org.eclipse.rdf4j.query.algebra.Filter) Var(org.eclipse.rdf4j.query.algebra.Var) AggregateOperator(org.eclipse.rdf4j.query.algebra.AggregateOperator) GroupElem(org.eclipse.rdf4j.query.algebra.GroupElem) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem)

Aggregations

Extension (org.eclipse.rdf4j.query.algebra.Extension)11 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)11 Var (org.eclipse.rdf4j.query.algebra.Var)10 ValueExpr (org.eclipse.rdf4j.query.algebra.ValueExpr)9 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)8 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)8 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)7 MultiProjection (org.eclipse.rdf4j.query.algebra.MultiProjection)6 Projection (org.eclipse.rdf4j.query.algebra.Projection)6 ArrayList (java.util.ArrayList)5 Reduced (org.eclipse.rdf4j.query.algebra.Reduced)5 Distinct (org.eclipse.rdf4j.query.algebra.Distinct)4 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)4 ValueConstant (org.eclipse.rdf4j.query.algebra.ValueConstant)4 AggregateOperator (org.eclipse.rdf4j.query.algebra.AggregateOperator)3 BNodeGenerator (org.eclipse.rdf4j.query.algebra.BNodeGenerator)3 EmptySet (org.eclipse.rdf4j.query.algebra.EmptySet)3 Filter (org.eclipse.rdf4j.query.algebra.Filter)3 Group (org.eclipse.rdf4j.query.algebra.Group)3 GroupElem (org.eclipse.rdf4j.query.algebra.GroupElem)3