SPARQLParser implements QueryParser { @Override public ParsedQuery parseQuery(String queryStr, String baseURI) throws MalformedQueryException { try { ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryStr); StringEscapesProcessor.process(qc); BaseDeclProcessor.process(qc, baseURI); Map<String, String> prefixes = PrefixDeclProcessor.process(qc); WildcardProjectionProcessor.process(qc); BlankNodeVarProcessor.process(qc); if (qc.containsQuery()) { TupleExpr tupleExpr = buildQueryModel(qc); ParsedQuery query; ASTQuery queryNode = qc.getQuery(); if (queryNode instanceof ASTSelectQuery) { query = new ParsedTupleQuery(queryStr, tupleExpr); } else if (queryNode instanceof ASTConstructQuery) { query = new ParsedGraphQuery(queryStr, tupleExpr, prefixes); } else if (queryNode instanceof ASTAskQuery) { query = new ParsedBooleanQuery(queryStr, tupleExpr); } else if (queryNode instanceof ASTDescribeQuery) { query = new ParsedDescribeQuery(queryStr, tupleExpr, prefixes); } else { throw new RuntimeException("Unexpected query type: " + queryNode.getClass()); } Dataset dataset = DatasetDeclProcessor.process(qc); if (dataset != null) { query.setDataset(dataset); } return query; } else { throw new IncompatibleOperationException("supplied string is not a query operation"); } } catch (ParseException | TokenMgrError e) { throw new MalformedQueryException(e.getMessage(), e); } }  @Override ParsedUpdate parseUpdate(String updateStr, String baseURI); @Override ParsedQuery parseQuery(String queryStr, String baseURI); static void main(String[] args);  }
@Test public void testSourceStringAssignment() throws Exception { String simpleSparqlQuery = "SELECT * WHERE {?X ?P ?Y }"; ParsedQuery q = parser.parseQuery(simpleSparqlQuery, null); assertNotNull(q); assertEquals(simpleSparqlQuery, q.getSourceString()); }
@Test public void testSES1922PathSequenceWithValueConstant() throws Exception { StringBuilder qb = new StringBuilder(); qb.append("ASK {?A (<foo:bar>)/<foo:foo> <foo:objValue>} "); ParsedQuery q = parser.parseQuery(qb.toString(), null); TupleExpr te = q.getTupleExpr(); assertNotNull(te); assertTrue(te instanceof Slice); Slice s = (Slice) te; assertTrue(s.getArg() instanceof Join); Join j = (Join) s.getArg(); assertTrue(j.getLeftArg() instanceof StatementPattern); assertTrue(j.getRightArg() instanceof StatementPattern); StatementPattern leftArg = (StatementPattern) j.getLeftArg(); StatementPattern rightArg = (StatementPattern) j.getRightArg(); assertTrue(leftArg.getObjectVar().equals(rightArg.getSubjectVar())); assertEquals(leftArg.getObjectVar().getName(), rightArg.getSubjectVar().getName()); }
@Test public void testParsedBooleanQueryRootNode() throws Exception { StringBuilder qb = new StringBuilder(); qb.append("ASK {?a <foo:bar> \"test\"}"); ParsedBooleanQuery q = (ParsedBooleanQuery) parser.parseQuery(qb.toString(), null); TupleExpr te = q.getTupleExpr(); assertNotNull(te); assertTrue(te instanceof Slice); assertNull(te.getParentNode()); }
@Test public void testParseIntegerObjectValue() throws Exception { String query = "select ?Concept where { ?Concept a 1. ?Concept2 a 1. } "; ParsedTupleQuery q = (ParsedTupleQuery) parser.parseQuery(query, null); assertNotNull(q); }
@Test public void testParsedTupleQueryRootNode() throws Exception { StringBuilder qb = new StringBuilder(); qb.append("SELECT * {?a <foo:bar> \"test\"}"); ParsedTupleQuery q = (ParsedTupleQuery) parser.parseQuery(qb.toString(), null); TupleExpr te = q.getTupleExpr(); assertNotNull(te); assertTrue(te instanceof Projection); assertNull(te.getParentNode()); }
@Test public void testParsedGraphQueryRootNode() throws Exception { StringBuilder qb = new StringBuilder(); qb.append("CONSTRUCT WHERE {?a <foo:bar> \"test\"}"); ParsedGraphQuery q = (ParsedGraphQuery) parser.parseQuery(qb.toString(), null); TupleExpr te = q.getTupleExpr(); assertNotNull(te); assertTrue(te instanceof Projection); assertNull(te.getParentNode()); }
@Test public void testOrderByWithAliases1() throws Exception { String queryString = " SELECT ?x (SUM(?v1)/COUNT(?v1) as ?r) WHERE { ?x <urn:foo> ?v1 } GROUP BY ?x ORDER BY ?r"; ParsedQuery query = parser.parseQuery(queryString, null); assertNotNull(query); TupleExpr te = query.getTupleExpr(); assertTrue(te instanceof Projection); te = ((Projection) te).getArg(); assertTrue(te instanceof Order); te = ((Order) te).getArg(); assertTrue(te instanceof Extension); }
@Test public void testSES1927UnequalLiteralValueConstants1() throws Exception { StringBuilder qb = new StringBuilder(); qb.append("ASK {?a <foo:bar> \"test\". ?a <foo:foo> \"test\"@en .} "); ParsedQuery q = parser.parseQuery(qb.toString(), null); TupleExpr te = q.getTupleExpr(); assertNotNull(te); assertTrue(te instanceof Slice); Slice s = (Slice) te; assertTrue(s.getArg() instanceof Join); Join j = (Join) s.getArg(); assertTrue(j.getLeftArg() instanceof StatementPattern); assertTrue(j.getRightArg() instanceof StatementPattern); StatementPattern leftArg = (StatementPattern) j.getLeftArg(); StatementPattern rightArg = (StatementPattern) j.getRightArg(); assertFalse(leftArg.getObjectVar().equals(rightArg.getObjectVar())); assertNotEquals(leftArg.getObjectVar().getName(), rightArg.getObjectVar().getName()); }
@Test public void testSES1927UnequalLiteralValueConstants2() throws Exception { StringBuilder qb = new StringBuilder(); qb.append("ASK {?a <foo:bar> \"test\". ?a <foo:foo> \"test\"^^<foo:bar> .} "); ParsedQuery q = parser.parseQuery(qb.toString(), null); TupleExpr te = q.getTupleExpr(); assertNotNull(te); assertTrue(te instanceof Slice); Slice s = (Slice) te; assertTrue(s.getArg() instanceof Join); Join j = (Join) s.getArg(); assertTrue(j.getLeftArg() instanceof StatementPattern); assertTrue(j.getRightArg() instanceof StatementPattern); StatementPattern leftArg = (StatementPattern) j.getLeftArg(); StatementPattern rightArg = (StatementPattern) j.getRightArg(); assertFalse(leftArg.getObjectVar().equals(rightArg.getObjectVar())); assertNotEquals(leftArg.getObjectVar().getName(), rightArg.getObjectVar().getName()); }
@Test public void testWildCardPathFixedEnd() { String query = "PREFIX : <http: ParsedQuery parsedQuery = parser.parseQuery(query, null); TupleExpr tupleExpr = parsedQuery.getTupleExpr(); }
@Test public void testWildCardPathPushNegation() { String query = "PREFIX : <http: ParsedQuery parsedQuery = parser.parseQuery(query, null); TupleExpr tupleExpr = parsedQuery.getTupleExpr(); Slice slice = (Slice) tupleExpr; Union union = (Union) slice.getArg(); Var leftSubjectVar = ((StatementPattern) union.getLeftArg()).getSubjectVar(); Var rightSubjectVar = ((StatementPattern) ((Filter) union.getRightArg()).getArg()).getSubjectVar(); assertEquals(leftSubjectVar, rightSubjectVar); }
@Test public void testWildCardPathPushNegation2() { String query = "PREFIX : <http: ParsedQuery parsedQuery = parser.parseQuery(query, null); TupleExpr tupleExpr = parsedQuery.getTupleExpr(); Slice slice = (Slice) tupleExpr; Union union = (Union) slice.getArg(); Var leftSubjectVar = ((StatementPattern) union.getLeftArg()).getSubjectVar(); Var rightSubjectVar = ((StatementPattern) ((Filter) union.getRightArg()).getArg()).getSubjectVar(); assertEquals(leftSubjectVar, rightSubjectVar); }
@Test public void testWildCardPathComplexSubjectHandling() { String query = "PREFIX : <http: ParsedQuery parsedQuery = parser.parseQuery(query, null); TupleExpr tupleExpr = parsedQuery.getTupleExpr(); Slice slice = (Slice) tupleExpr; ArbitraryLengthPath path = (ArbitraryLengthPath) slice.getArg(); Var pathStart = path.getSubjectVar(); Var pathEnd = path.getObjectVar(); assertThat(pathStart.getName()).isEqualTo("a"); assertThat(pathEnd.getName()).isEqualTo("b"); Join pathSequence = (Join) path.getPathExpression(); Join innerJoin = (Join) pathSequence.getLeftArg(); Var commentObjectVar = ((StatementPattern) innerJoin.getLeftArg()).getObjectVar(); Union union = (Union) innerJoin.getRightArg(); Var subClassOfSubjectVar = ((StatementPattern) union.getLeftArg()).getSubjectVar(); assertThat(subClassOfSubjectVar).isNotEqualTo(commentObjectVar); Var subClassOfObjectVar = ((StatementPattern) union.getLeftArg()).getObjectVar(); assertThat(subClassOfObjectVar).isEqualTo(commentObjectVar); }