WildcardProjectionProcessor extends AbstractASTVisitor { public static void process(ASTOperationContainer container) throws MalformedQueryException { ASTOperation operation = container.getOperation(); if (operation != null) { ASTWhereClause whereClause = operation.getWhereClause(); if (whereClause != null) { SelectClauseCollector collector = new SelectClauseCollector(); try { whereClause.jjtAccept(collector, null); Set<ASTSelect> selectClauses = collector.getSelectClauses(); for (ASTSelect selectClause : selectClauses) { if (selectClause.isWildcard()) { ASTSelectQuery q = (ASTSelectQuery) selectClause.jjtGetParent(); addQueryVars(q.getWhereClause(), selectClause); selectClause.setWildcard(false); } } } catch (VisitorException e) { throw new MalformedQueryException(e); } } } if (operation instanceof ASTSelectQuery) { ASTSelectQuery selectQuery = (ASTSelectQuery) operation; ASTSelect selectClause = selectQuery.getSelect(); if (selectClause.isWildcard()) { addQueryVars(selectQuery.getWhereClause(), selectClause); selectClause.setWildcard(false); } } else if (operation instanceof ASTDescribeQuery) { ASTDescribeQuery describeQuery = (ASTDescribeQuery) operation; ASTDescribe describeClause = describeQuery.getDescribe(); if (describeClause.isWildcard()) { addQueryVars(describeQuery.getWhereClause(), describeClause); describeClause.setWildcard(false); } } }  static void process(ASTOperationContainer container);  }
@Test public void testVarInFilter() throws Exception { String queryStr = "SELECT * {\n" + " FILTER (!bound(?a))\n" + "}"; ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryStr); WildcardProjectionProcessor.process(qc); List<ASTProjectionElem> projection = ((ASTSelectQuery) qc.getQuery()).getSelect().getProjectionElemList(); assertThat(projection).isEmpty(); }
@Test public void testVarInBGP() throws Exception { String queryStr = "SELECT * {\n" + " ?a <ex:p> <ex:o> . \n" + " FILTER (!bound(?a))\n" + "}"; ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryStr); WildcardProjectionProcessor.process(qc); List<ASTProjectionElem> projection = ((ASTSelectQuery) qc.getQuery()).getSelect().getProjectionElemList(); assertThat(projection.size()).isEqualTo(1); assertThat(((ASTVar) projection.get(0).jjtGetChild(0)).getName()).isEqualTo("a"); }
@Test public void testVarInBind() throws Exception { String queryStr = "SELECT * {\n" + " BIND (?a AS ?b)\n" + "}"; ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryStr); WildcardProjectionProcessor.process(qc); List<ASTProjectionElem> projection = ((ASTSelectQuery) qc.getQuery()).getSelect().getProjectionElemList(); assertThat(projection.size()).isEqualTo(1); assertThat(((ASTVar) projection.get(0).jjtGetChild(0)).getName()).isEqualTo("b"); }
@Test public void testVarInSubselect() throws Exception { String queryStr = "SELECT * {\n" + " { SELECT ?a { ?a ?p ?o } }\n" + "}"; ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryStr); WildcardProjectionProcessor.process(qc); List<ASTProjectionElem> projection = ((ASTSelectQuery) qc.getQuery()).getSelect().getProjectionElemList(); assertThat(projection.size()).isEqualTo(1); assertThat(((ASTVar) projection.get(0).jjtGetChild(0)).getName()).isEqualTo("a"); }