Rob, Thanks a lot for your help. Now I can generate my complete query with the ARQ API (I simplified the initial example in my first question):
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT DISTINCT * WHERE { ?id skos:broader <URI> OPTIONAL { ?id skos:prefLabel|skos:altLabel ?label FILTER langMatches("en", lang(?label)) } OPTIONAL { ?id skos:prefLabel ?label} } ORDER BY (?id) The SelectBuilder is really great and I would like to use it. Only at the moment I struggle with creating the first OPTIONAL block, which contains a FILTER applied only inside this block. How can I create such a block? I tried addSubQuery with another SelectBuilder or WhereBuilder, but both add an unnecessary WHERE inside the OPTIONAL - rendering the query syntactically incorrect. Is the querybuilder API too limited for that, or am I missing something? Please find both versions of the code (ARQ API and Querybuilder) as Java code snippets in the attachment. (The ARQ version is working correctly) Best Regards Andreas >>> Rob Vesse <[email protected]> 18.09.18 17.30 Uhr >>> Use one of the .addOrderBy() methods on the Query object - https://jena.apache.org/documentation/javadoc/arq/org/apache/jena/query/Query.html#addOrderBy-org.apache.jena.sparql.expr.Expr-int- If you are building lots of queries programmatically the higher level Query Builder API may be more user friendly - https://jena.apache.org/documentation/extras/querybuilder/index.html Rob From: Andreas Kahl <[email protected]> Reply-To: <[email protected]> Date: Tuesday, 18 September 2018 at 07:55 To: <[email protected]> Subject: How to use ORDER BY in a query built by the ARQ API? Hello everyone, how is it possible to build a query like: SELECT DISTINCT * WHERE { ?s skos:broader ?o } ORDER BY ?s with the Jena ARQ API (org.apache.jena.sparql.syntax.*) The first part would be achieved by: final ElementTriplesBlock baseCriteria = new ElementTriplesBlock(); baseCriteria.addTriple( new Triple( Var.alloc("s"), NodeFactory.createURI("skos:broader"), Var.alloc("o") ) ); I can add this to an ElementGroup, but which Java Type shall be used to gernerate ORDER BY ?s ? Thanks & Best Regards Andreas
