Query { public static Sparql11Parser getParser(String query) { Sparql11Lexer lexer = new Sparql11Lexer(new CaseInsensitiveInputStream(query)); CommonTokenStream tokens = new CommonTokenStream(lexer); Sparql11Parser parser = new Sparql11Parser(tokens); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException ex) { throw new MalformedQueryException("Failed to parse at line " + line + " due to " + msg, ex); } }); return parser; }  static Sparql11Parser getParser(String query); static String getQueryType(String queryString);  }
@Test public void parseSimpleQuery() throws Exception { InputStream query = getClass().getResourceAsStream("/example1.rq"); Sparql11Parser parser = Query.getParser(streamToString(query)); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e); } }); parser.query(); }
@Test public void replaceDatasetClause() throws Exception { InputStream query = getClass().getResourceAsStream("/example2.rq"); Sparql11Parser parser = Query.getParser(streamToString(query)); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e); } }); Sparql11Parser.QueryContext queryContext = parser.query(); TokenStreamRewriter rewriter = new TokenStreamRewriter(parser.getTokenStream()); ParseTreeWalker walker = new ParseTreeWalker(); DatasetListener listener = new DatasetListener(rewriter); walker.walk(listener, queryContext); String newText = rewriter.getText(); parser = Query.getParser(newText); String datasetText = parser.query().selectQuery().datasetClause().get(0).getText(); assertEquals(DATASET_REPLACEMENT, datasetText); }
@Test public void insensitiveToCase() throws Exception { String queryString = "select * WHERE { ?x ?Y ?z }"; String queryNoSpaces = "select*WHERE{?x?Y?z}"; Sparql11Parser parser = Query.getParser(queryString); TokenStream tokens = parser.getTokenStream(); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e); } }); assertEquals(queryNoSpaces, parser.query().selectQuery().getText()); assertEquals(queryString, tokens.getText()); }
@Test public void multipleSelectVars() throws Exception { String queryString = "select ?s ?p where { ?s ?p ?o }"; Sparql11Parser parser = Query.getParser(queryString); TokenStream tokens = parser.getTokenStream(); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e); } }); parser.query(); assertEquals(queryString, tokens.getText()); }
@Test public void insensitiveToCaseBuiltInCall() throws Exception { String queryString = "select * WHERE { ?s ?P ?o FILTeR (sameTeRm(?s, ?o))}"; Sparql11Parser parser = Query.getParser(queryString); TokenStream tokens = parser.getTokenStream(); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e); } }); parser.query(); assertEquals(queryString, tokens.getText()); }
@Test public void simpleCommentsWork() throws Exception { String queryString = "select * where { ?s ?p ?o }#?s ?p ?o }"; String expectedQuery = "select * where { ?s ?p ?o }"; Sparql11Parser parser = Query.getParser(queryString); TokenStream tokens = parser.getTokenStream(); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e); } }); parser.query(); assertEquals(expectedQuery, tokens.getText()); }
@Test public void complexCommentsWork() throws Exception { String queryString = "########################\n" + "### Find all the things\n" + "########################\n" + "\n" + "prefix uhtc: <http: "\n" + "select\n" + "\t?formula\n" + "\t?density # This is a comment\n" + "\t?meltingPointCelsius\n" + "\t(group_concat(distinct ?elementName;separator=',') as ?elements)\n" + "where { # This is a comment\n" + " ?material a uhtc:Material ; # This is a comment\n" + " uhtc:chemicalFormula ?formula ; # This is a comment\n" + " uhtc:density ?density ;\n" + " uhtc:meltingPoint ?meltingPointCelsius ;\n" + " uhtc:element ?element . # This is a comment\n" + " \n" + " ?element a uhtc:Element ; # This is a comment\n" + " uhtc:elementName ?elementName ;\n" + " uhtc:symbol ?symbol .\n" + "} # This is a comment\n" + "GROUP BY ?formula ?density ?meltingPointCelsius" + "# This is a comment"; String expectedQuery = "\nprefix uhtc: <http: "\n" + "select\n" + "\t?formula\n" + "\t?density " + "\t?meltingPointCelsius\n" + "\t(group_concat(distinct ?elementName;separator=',') as ?elements)\n" + "where { " + " ?material a uhtc:Material ; " + " uhtc:chemicalFormula ?formula ; " + " uhtc:density ?density ;\n" + " uhtc:meltingPoint ?meltingPointCelsius ;\n" + " uhtc:element ?element . " + " \n" + " ?element a uhtc:Element ; " + " uhtc:elementName ?elementName ;\n" + " uhtc:symbol ?symbol .\n" + "} " + "GROUP BY ?formula ?density ?meltingPointCelsius"; Sparql11Parser parser = Query.getParser(queryString); TokenStream tokens = parser.getTokenStream(); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e); } }); parser.query(); assertEquals(expectedQuery, tokens.getText()); }