Search in sources :

Example 1 with TupleExprWrapper

use of openrdffork.TupleExprWrapper in project QueryAnalysis by Wikidata.

the class Main method getExampleQueries.

/**
 * Reads the example queries from https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples.
 */
private static void getExampleQueries() {
    Document doc;
    Connection connection = Jsoup.connect("http://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples").header("Accept-Encoding", "gzip, deflate").userAgent("github.com/Wikidata/QueryAnalysis").maxBodySize(0);
    try {
        doc = connection.get();
    } catch (IOException e) {
        try {
            logger.warn("While trying to download the example queries could not connect directloy to wikidata.org, trying via a proxy now.");
            doc = connection.proxy("webproxy.eqiad.wmnet", 8080).get();
        } catch (IOException e2) {
            logger.error("Could not even connect to wikidata.org via the proxy.", e2);
            return;
        }
    }
    doc.select("span.lineno").remove();
    Elements links = doc.select("pre");
    for (Element link : links) {
        Element previous = link.parent();
        String name = null;
        while (name == null) {
            if (previous.nodeName().matches("h[1-6]")) {
                name = previous.child(0).text();
                break;
            }
            if (previous.previousElementSibling() != null) {
                previous = previous.previousElementSibling();
            } else if (previous.parent() != null) {
                previous = previous.parent();
            } else {
                break;
            }
        }
        if (name != null) {
            String query = link.text();
            exampleQueriesString.put(query, name);
            OpenRDFQueryHandler queryHandler = new OpenRDFQueryHandler(QueryHandler.Validity.DEFAULT, -1L, -1, query, "exampleQueries", "", -1);
            if (queryHandler.getValidityStatus() != QueryHandler.Validity.VALID) {
                logger.warn("The example query " + name + " is no valid SPARQL.");
            } else {
                exampleQueriesTupleExpr.put(new TupleExprWrapper(queryHandler.getParsedQuery().getTupleExpr()), name);
            }
        } else {
            logger.error("Could not find header to: " + link.text());
        }
    }
}
Also used : OpenRDFQueryHandler(query.OpenRDFQueryHandler) TupleExprWrapper(openrdffork.TupleExprWrapper) Element(org.jsoup.nodes.Element) Connection(org.jsoup.Connection) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Aggregations

TupleExprWrapper (openrdffork.TupleExprWrapper)1 Connection (org.jsoup.Connection)1 Document (org.jsoup.nodes.Document)1 Element (org.jsoup.nodes.Element)1 Elements (org.jsoup.select.Elements)1 OpenRDFQueryHandler (query.OpenRDFQueryHandler)1