Usage

There are two ways to use RDQL : from the command line and in Java programs.

Command Line

The main class is jena.rdfquery:

java -cp ... jena.rdfquery [--data modelFile] [--query File | queryString]

With no arguments, it prints a usage message.

Java

Example code fragment 1 (basic template)

    Query query = new Query(queryString) ;
    // Need to set the source if the query does not.
    // This provided this override any query named source.
    query.setSource(model);
    QueryExecution qe = new QueryEngine(query) ;
    QueryResults results = qe.exec() ;
    for ( Iterator iter = results ; iter.hasNext() ; )
    {
        ResultBinding rbind = (ResultBinding)iter.next() ;
        // Return from get is null if not found
        Object obj = rbind.get("x") ;
        // obj will be a Jena object: resource, property or RDFNode.
    }
    results.close() ;

The ResultBinding class allows an alternative form which can be used to get quoted strings:

        Value v = rbind.getValue("x") ;
        String s = (v == null) ? "<null>" : v.asQuotedString() ;

Example code fragment 2 (using the results formatter)

    Query query = new Query(queryString) ;
    query.setSource(model);
    QueryExecution qe = new QueryEngine(query) ;
    QueryResults results = qe.exec() ;
    QueryResultsFormatter fmt = new QueryResultsFormatter(results) ;
    PrintWriter pw = new PrintWriter(System.out) ;
    fmt.printAll(pw, " | ") ;
    pw.flush() ;
    fmt.close() ;	
    results.close() ;

The command line usage is a wrapper around the Java interface.

Query Templates

Query templates are the ability to have a single query as a string and reuse it by setting the initial values for some variables.

Example

    // This query would select everything
    Query query = new Query("SELECT * WHERE (?x, ?y, ?z)") ;
    query.setSource(model);
    QueryExecution qe = new QueryEngine(query) ;

    ResultBinding initialBinding = new ResultBinding() ;
    initialBinding.add("x", model.createResource("http://never/r-1")) ;
    initialBinding.add("y", model.createResource("http://never/p-1")) ;
    QueryResults results = qe.exec(initialBinding ) ;
    // Now the query is like "SELECT * WHERE (<http://never/r-1>, <http://never/p-1>, ?z)"
    // except ?x and ?y are bound in the result set.

    for ( Iterator iter = results ; iter.hasNext() ; )
    {
        ResultBinding rbind = (ResultBinding)iter.next() ;
        Object obj_x = rbind.get("x") ;
        Object obj_z = rbind.get("z") ;
    }
    results.close() ;