You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Andreas Kahl <ka...@bsb-muenchen.de> on 2018/09/19 06:59:34 UTC

***UNCHECKED*** Antw: Re: How to use ORDER BY in a query built by the ARQ API?

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 <rv...@dotnetrdf.org> 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 <ka...@bsb-muenchen.de>
Reply-To: <us...@jena.apache.org>
Date: Tuesday, 18 September 2018 at 07:55
To: <us...@jena.apache.org>
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 






***UNCHECKED*** Re: How to use ORDER BY in a query built by the ARQ API?

Posted by Andreas Kahl <ka...@bsb-muenchen.de>.
Lorenz, 

thanks for your effort. I already tried WhereBuilder and as SelectBuilder and it adds a WHERE clause inside the OPTIONAL which is syntactically incorrect. So I would need a different Builder which I cannot find in org.apache.jena.arq.querybuilder.* . My question is, whether there is a different approach to inserting "ElementGroups" inside e.g. an OPTIONAL using Querybuilder. I could also imagine that this is too complex for the simple API, but I would like to have that verified, because the Querybuilder is indeed a much cleaner API than ARQ. 


Best Regards
Andreas



>>> "Lorenz B." <bu...@informatik.uni-leipzig.de> 19.09.18 11.31 Uhr >>>
Hello Andreas,

it's not a simple TriplesPath that you want to add inside the OPTIONAL,
because you have a FILTER, thus it's a group pattern. The method you
should use is


addOptional(AbstractQueryBuilder<?> t)


with a new WhereBuilder object as argument that builds the TriplesPath +
Filter probably calling addWhere + addFilter

(untested as you didn't provide a complete executable example)

Cheers,
Lorenz

> Sorry, I forgot the attachment. Here it is: 
> ARQ Example: https://pastebin.com/kvZjiAT0
> Jena Querybuilder Example: https://pastebin.com/J0T3DRW5
>
> Andreas
>
>
>
>>>> "Andreas Kahl" <ka...@bsb-muenchen.de> 19.09.18 9.00 Uhr >>>
> 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 <rv...@dotnetrdf.org> 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 <ka...@bsb-muenchen.de>
> Reply-To: <us...@jena.apache.org>
> Date: Tuesday, 18 September 2018 at 07:55
> To: <us...@jena.apache.org>
> 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 
>
>
>
>
>
>
>
-- 
Lorenz Bühmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic

Re: ***UNCHECKED*** Re: How to use ORDER BY in a query built by the ARQ API?

Posted by "Lorenz B." <bu...@informatik.uni-leipzig.de>.
Hello Andreas,

it's not a simple TriplesPath that you want to add inside the OPTIONAL,
because you have a FILTER, thus it's a group pattern. The method you
should use is


addOptional(AbstractQueryBuilder<?> t)


with a new WhereBuilder object as argument that builds the TriplesPath +
Filter probably calling addWhere + addFilter

(untested as you didn't provide a complete executable example)

Cheers,
Lorenz

> Sorry, I forgot the attachment. Here it is: 
> ARQ Example: https://pastebin.com/kvZjiAT0
> Jena Querybuilder Example: https://pastebin.com/J0T3DRW5
>
> Andreas
>
>
>
>>>> "Andreas Kahl" <ka...@bsb-muenchen.de> 19.09.18 9.00 Uhr >>>
> 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 <rv...@dotnetrdf.org> 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 <ka...@bsb-muenchen.de>
> Reply-To: <us...@jena.apache.org>
> Date: Tuesday, 18 September 2018 at 07:55
> To: <us...@jena.apache.org>
> 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 
>
>
>
>
>
>
>
-- 
Lorenz Bühmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center


***UNCHECKED*** Re: How to use ORDER BY in a query built by the ARQ API?

Posted by Andreas Kahl <ka...@bsb-muenchen.de>.
Sorry, I forgot the attachment. Here it is: 
ARQ Example: https://pastebin.com/kvZjiAT0
Jena Querybuilder Example: https://pastebin.com/J0T3DRW5

Andreas



>>> "Andreas Kahl" <ka...@bsb-muenchen.de> 19.09.18 9.00 Uhr >>>
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 <rv...@dotnetrdf.org> 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 <ka...@bsb-muenchen.de>
Reply-To: <us...@jena.apache.org>
Date: Tuesday, 18 September 2018 at 07:55
To: <us...@jena.apache.org>
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