Search in sources :

Example 1 with Query

use of org.apache.jena.query.Query in project jena by apache.

the class RDFConnectionExample3 method main.

public static void main(String... args) {
    Query query = QueryFactory.create("SELECT * { <http://example.org/book/book1> ?p ?o }");
    String queryService = "http://sparql.org/books/query";
    // Query service, no update, no graph store protocol.
    try (RDFConnection conn = RDFConnectionFactory.connect(queryService, null, null)) {
        conn.queryResultSet(query, ResultSetFormatter::out);
    }
}
Also used : ResultSetFormatter(org.apache.jena.query.ResultSetFormatter) Query(org.apache.jena.query.Query) RDFConnection(org.apache.jena.rdfconnection.RDFConnection)

Example 2 with Query

use of org.apache.jena.query.Query in project jena by apache.

the class TestSerialization method runTestForReal.

// A serialization test is:
//   Read query in.
//   Serialize to string.
//   Parse again.
//   Are they equal?
@Override
protected void runTestForReal() {
    Query query = null;
    if (queryString == null)
        query = queryFromTestItem(testItem);
    else
        query = queryFromString(queryString);
    // Whatever was read in.
    runTestWorker(query, query.getSyntax());
}
Also used : Query(org.apache.jena.query.Query)

Example 3 with Query

use of org.apache.jena.query.Query in project jena by apache.

the class QueryValidator method execute.

//static final String paramSyntaxExtended   = "extendedSyntax" ;
@Override
protected void execute(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        //            if ( log.isInfoEnabled() )
        //                log.info("validation request") ;
        String[] args = httpRequest.getParameterValues(paramQuery);
        if (args == null || args.length == 0) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "No query parameter to validator");
            return;
        }
        if (args.length > 1) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Too many query parameters");
            return;
        }
        final String queryString = httpRequest.getParameter(paramQuery).replaceAll("(\r|\n| )*$", "");
        //            queryString = queryString.replace("\r\n", "\n") ;
        //            queryString.replaceAll("(\r|\n| )*$", "") ;
        String querySyntax = httpRequest.getParameter(paramSyntax);
        if (querySyntax == null || querySyntax.equals(""))
            querySyntax = "SPARQL";
        Syntax language = Syntax.lookup(querySyntax);
        if (language == null) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown syntax: " + querySyntax);
            return;
        }
        String lineNumbersArg = httpRequest.getParameter(paramLineNumbers);
        String[] a = httpRequest.getParameterValues(paramFormat);
        boolean outputSPARQL = false;
        boolean outputPrefix = false;
        boolean outputAlgebra = false;
        boolean outputQuads = false;
        boolean outputOptimized = false;
        boolean outputOptimizedQuads = false;
        if (a != null) {
            for (String anA : a) {
                if (anA.equals("sparql")) {
                    outputSPARQL = true;
                }
                if (anA.equals("prefix")) {
                    outputPrefix = true;
                }
                if (anA.equals("algebra")) {
                    outputAlgebra = true;
                }
                if (anA.equals("quads")) {
                    outputQuads = true;
                }
                if (anA.equals("opt")) {
                    outputOptimized = true;
                }
                if (anA.equals("optquads")) {
                    outputOptimizedQuads = true;
                }
            }
        }
        //            if ( ! outputSPARQL && ! outputPrefix )
        //                outputSPARQL = true ;
        boolean lineNumbers = true;
        if (lineNumbersArg != null)
            lineNumbers = lineNumbersArg.equalsIgnoreCase("true") || lineNumbersArg.equalsIgnoreCase("yes");
        // Headers
        setHeaders(httpResponse);
        ServletOutputStream outStream = httpResponse.getOutputStream();
        outStream.println("<html>");
        printHead(outStream, "SPARQL Query Validation Report");
        outStream.println("<body>");
        outStream.println("<h1>SPARQL Query Validator</h1>");
        // Print query as received
        {
            outStream.println("<p>Input:</p>");
            // Not Java's finest hour.
            Content c = new Content() {

                @Override
                public void print(IndentedWriter out) {
                    out.print(queryString);
                }
            };
            output(outStream, c, lineNumbers);
        }
        // Attempt to parse it.
        Query query = null;
        try {
            query = QueryFactory.create(queryString, "http://example/base/", language);
        } catch (ARQException ex) {
            // Over generous exception (should be QueryException)
            // but this makes the code robust.
            outStream.println("<p>Syntax error:</p>");
            startFixed(outStream);
            outStream.println(ex.getMessage());
            finishFixed(outStream);
        } catch (RuntimeException ex) {
            outStream.println("<p>Internal error:</p>");
            startFixed(outStream);
            outStream.println(ex.getMessage());
            finishFixed(outStream);
        }
        if (query != null) {
            if (outputSPARQL)
                outputSyntax(outStream, query, lineNumbers);
            if (outputAlgebra)
                outputAlgebra(outStream, query, lineNumbers);
            if (outputQuads)
                outputAlgebraQuads(outStream, query, lineNumbers);
            if (outputOptimized)
                outputAlgebraOpt(outStream, query, lineNumbers);
            if (outputOptimizedQuads)
                outputAlgebraOptQuads(outStream, query, lineNumbers);
        }
        outStream.println("</body>");
        outStream.println("</html>");
    } catch (Exception ex) {
        serviceLog.warn("Exception in doGet", ex);
    }
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) Query(org.apache.jena.query.Query) ServletOutputStream(javax.servlet.ServletOutputStream) ARQException(org.apache.jena.sparql.ARQException) Syntax(org.apache.jena.query.Syntax) ARQException(org.apache.jena.sparql.ARQException) IOException(java.io.IOException)

Example 4 with Query

use of org.apache.jena.query.Query in project jena by apache.

the class QueryCommandAssembler method open.

@Override
public Object open(Assembler a, Resource root, Mode mode) {
    // Query
    Resource queryDesc = getUniqueResource(root, AssemblerVocab.pQuery);
    Query query = (Query) a.open(a, queryDesc, mode);
    // Dataset
    Resource datasetDesc = getUniqueResource(root, AssemblerVocab.pDataset);
    Dataset dataset = (Dataset) a.open(a, datasetDesc, mode);
    // Output format
    String s = GraphUtils.getStringValue(root, AssemblerVocab.pOutputFormat);
    if (s == null)
        s = "text";
    ResultsFormat format = ResultsFormat.lookup(s);
    QueryExecution qExec = QueryExecutionFactory.create(query, dataset);
    return new QExec(query, qExec, format);
}
Also used : Query(org.apache.jena.query.Query) Dataset(org.apache.jena.query.Dataset) Resource(org.apache.jena.rdf.model.Resource) ResultsFormat(org.apache.jena.sparql.resultset.ResultsFormat) QueryExecution(org.apache.jena.query.QueryExecution)

Example 5 with Query

use of org.apache.jena.query.Query in project jena by apache.

the class sdbprint method execCmd.

@Override
protected void execCmd(List<String> positionalArgs) {
    Query query = modQuery.getQuery();
    compilePrint(getStore(), query);
}
Also used : Query(org.apache.jena.query.Query)

Aggregations

Query (org.apache.jena.query.Query)264 Test (org.junit.Test)78 ContractTest (org.xenei.junit.contract.ContractTest)65 QueryExecution (org.apache.jena.query.QueryExecution)63 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)49 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)42 Var (org.apache.jena.sparql.core.Var)42 Triple (org.apache.jena.graph.Triple)38 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)32 ResultSet (org.apache.jena.query.ResultSet)29 Node (org.apache.jena.graph.Node)28 TriplePath (org.apache.jena.sparql.core.TriplePath)28 QuerySolution (org.apache.jena.query.QuerySolution)26 ExprVar (org.apache.jena.sparql.expr.ExprVar)24 Binding (org.apache.jena.sparql.engine.binding.Binding)22 Model (org.apache.jena.rdf.model.Model)20 HashMap (java.util.HashMap)19 Dataset (org.apache.jena.query.Dataset)15 Op (org.apache.jena.sparql.algebra.Op)15 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)14