Search in sources :

Example 1 with ASTVar

use of org.eclipse.rdf4j.query.parser.serql.ast.ASTVar in project rdf4j by eclipse.

the class ProjectionProcessor method visit.

@Override
public Object visit(ASTSelect selectNode, Object data) throws VisitorException {
    // Collect all variables used in the body of the select query
    Set<String> bodyVars = VariableCollector.process(selectNode.jjtGetParent());
    if (selectNode.isWildcard()) {
        // Make wildcard projection explicit
        for (String varName : bodyVars) {
            ASTProjectionElem projElemNode = new ASTProjectionElem(SyntaxTreeBuilderTreeConstants.JJTPROJECTIONELEM);
            selectNode.jjtAppendChild(projElemNode);
            projElemNode.jjtSetParent(selectNode);
            ASTVar varNode = new ASTVar(SyntaxTreeBuilderTreeConstants.JJTVAR);
            varNode.setName(varName);
            projElemNode.jjtAppendChild(varNode);
            varNode.jjtSetParent(projElemNode);
        }
        selectNode.setWildcard(false);
    } else {
        // Verify that all projection vars are bound
        Set<String> projVars = new LinkedHashSet<String>();
        for (ASTProjectionElem projElem : selectNode.getProjectionElemList()) {
            projVars.addAll(VariableCollector.process(projElem.getValueExpr()));
        }
        projVars.removeAll(bodyVars);
        if (!projVars.isEmpty()) {
            StringBuilder errMsg = new StringBuilder(64);
            errMsg.append("Unbound variable(s) in projection: ");
            Iterator<String> iter = projVars.iterator();
            while (iter.hasNext()) {
                errMsg.append(iter.next());
                if (iter.hasNext()) {
                    errMsg.append(", ");
                }
            }
            throw new VisitorException(errMsg.toString());
        }
    }
    return data;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ASTVar(org.eclipse.rdf4j.query.parser.serql.ast.ASTVar) ASTProjectionElem(org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem) VisitorException(org.eclipse.rdf4j.query.parser.serql.ast.VisitorException)

Example 2 with ASTVar

use of org.eclipse.rdf4j.query.parser.serql.ast.ASTVar in project rdf4j by eclipse.

the class AnonymousVarGenerator method createNodeElem.

private ASTNodeElem createNodeElem() {
    ASTNodeElem nodeElem = new ASTNodeElem(SyntaxTreeBuilderTreeConstants.JJTNODEELEM);
    ASTVar var = createAnonymousVar();
    var.jjtSetParent(nodeElem);
    nodeElem.jjtAppendChild(var);
    return nodeElem;
}
Also used : ASTVar(org.eclipse.rdf4j.query.parser.serql.ast.ASTVar) ASTNodeElem(org.eclipse.rdf4j.query.parser.serql.ast.ASTNodeElem)

Example 3 with ASTVar

use of org.eclipse.rdf4j.query.parser.serql.ast.ASTVar in project rdf4j by eclipse.

the class ProjectionAliasProcessor method visit.

@Override
public Object visit(ASTSelect node, Object data) throws VisitorException {
    // Iterate over all projection elements to retrieve the defined aliases
    Set<String> aliases = new HashSet<String>();
    List<Node> unaliasedNodes = new ArrayList<Node>();
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        ASTProjectionElem projElem = (ASTProjectionElem) node.jjtGetChild(i);
        String alias = projElem.getAlias();
        if (alias == null && projElem.getValueExpr() instanceof ASTVar) {
            alias = ((ASTVar) projElem.getValueExpr()).getName();
        }
        if (alias != null) {
            boolean isUnique = aliases.add(alias);
            if (!isUnique) {
                throw new VisitorException("Duplicate projection element names: '" + alias + "'");
            }
        } else {
            unaliasedNodes.add(projElem);
        }
    }
    // Iterate over the unaliased nodes and generate aliases for them
    int aliasNo = 1;
    for (Node projElem : unaliasedNodes) {
        // Generate unique alias for projection element
        String alias;
        while (aliases.contains(alias = "_" + aliasNo++)) {
        // try again
        }
        aliases.add(alias);
        ASTString aliasNode = new ASTString(SyntaxTreeBuilderTreeConstants.JJTSTRING);
        aliasNode.setValue(alias);
        aliasNode.jjtSetParent(projElem);
        projElem.jjtAppendChild(aliasNode);
    }
    return data;
}
Also used : ASTVar(org.eclipse.rdf4j.query.parser.serql.ast.ASTVar) ASTString(org.eclipse.rdf4j.query.parser.serql.ast.ASTString) Node(org.eclipse.rdf4j.query.parser.serql.ast.Node) ArrayList(java.util.ArrayList) ASTString(org.eclipse.rdf4j.query.parser.serql.ast.ASTString) ASTProjectionElem(org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem) VisitorException(org.eclipse.rdf4j.query.parser.serql.ast.VisitorException) HashSet(java.util.HashSet)

Example 4 with ASTVar

use of org.eclipse.rdf4j.query.parser.serql.ast.ASTVar in project rdf4j by eclipse.

the class QueryModelBuilder method visit.

@Override
public Var visit(ASTVar node, Object data) throws VisitorException {
    Var var = new Var(node.getName());
    var.setAnonymous(node.isAnonymous());
    return var;
}
Also used : ASTVar(org.eclipse.rdf4j.query.parser.serql.ast.ASTVar) Var(org.eclipse.rdf4j.query.algebra.Var)

Example 5 with ASTVar

use of org.eclipse.rdf4j.query.parser.serql.ast.ASTVar in project rdf4j by eclipse.

the class AnonymousVarGenerator method createAnonymousVar.

private ASTVar createAnonymousVar() {
    ASTVar var = new ASTVar(SyntaxTreeBuilderTreeConstants.JJTVAR);
    var.setName("-anon-" + anonymousVarNo++);
    var.setAnonymous(true);
    return var;
}
Also used : ASTVar(org.eclipse.rdf4j.query.parser.serql.ast.ASTVar)

Aggregations

ASTVar (org.eclipse.rdf4j.query.parser.serql.ast.ASTVar)5 ASTProjectionElem (org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem)2 VisitorException (org.eclipse.rdf4j.query.parser.serql.ast.VisitorException)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Var (org.eclipse.rdf4j.query.algebra.Var)1 ASTNodeElem (org.eclipse.rdf4j.query.parser.serql.ast.ASTNodeElem)1 ASTString (org.eclipse.rdf4j.query.parser.serql.ast.ASTString)1 Node (org.eclipse.rdf4j.query.parser.serql.ast.Node)1