You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Maria Jackson <ma...@gmail.com> on 2013/10/31 14:47:38 UTC

How to write queries using named graphs?

I think jena employs named graphs as a way to achieve reification. As named
graphs can achieve reification by attaching only one statement with named
graphs.
Is it possible to express information of the following form using named
graphs, if yes then can someone please help me write this information using
named graphs, so that I may import the same into Jena:

     _:a rdf:subject "Mr X".
     _:a rdf:predicate "isPresident".
     _:a rdf:object "countryZ".
     _:a saidBy "Tia".
     _:a saidOn "29 October 2013".
     _:a saidAt "New York".
     _:a confidence 0.9.
     _:a comment "This is just a comment by Tia".

Is it possible to write the corresponding query Jena using named graph
query on the above given data?

Re: How to write queries using named graphs?

Posted by Andy Seaborne <an...@apache.org>.
Try the online query validator:

http://www.sparql.org/query-validator.html

	Andy

On 31/10/13 15:17, Maria Jackson wrote:
> select ?x ?a ?b ?c ?d ?e ?f where {
>    graph ?r {
>      ?x ?y <CountryZ>
>    }
>   graph ?r1{
>      ?x ?y <CountryZ1>
> }
>    ?r <saidBy> Tia ;
>
>       <saidOn> ?b ;
>       <saidAt> ?c ;
>       <confidence> ?d ;
>       <comment> ?e .
> ?r1 <saidBy> Astrid.
> }
> Is the above query to retrieve a person who is linked to two different
> countries correct?
>
> I am just curious as to whether it is syntactically accepted by Jena
>
>
> On Thu, Oct 31, 2013 at 8:45 PM, Joshua TAYLOR <jo...@gmail.com>wrote:
>
>> On Thu, Oct 31, 2013 at 10:47 AM, Eric Scott <er...@att.net> wrote:
>>> You could try something like this:
>>>
>>>   select ?a?b?c?d?e
>>>   where
>>> {
>>>         ?g a :MyKindaGraph.
>>>         graph ?g
>>>
>>>         {
>>>           ?r rdf:subject <Mr_X>.
>>>           ?r rdf:predicate <isPresident>.
>>>           ?r rdf:object <CountryZ>.
>>>           ?r saidBy ?a.
>>>           ?r saidOn ?b.
>>>           ?r saidAt ?c.
>>>           ?r confidence ?d.
>>>           ?r comment ?e
>>>         }
>>> }
>>
>> I think, though I could be wrong, that the point wasn't just to put
>> the triples _into_ a named graph, but to use a named graph to serve
>> _as_ the reified triple (or triples).  E.g.,
>>
>> graph ?g { <Mr_X> <isPresident> <CountryZ> }
>> ?g saidBy ?a ; ... ; comment ?e .
>>
>> This avoids needing three triples for each reified statement, and
>> makes the queries easier to write. It also makes it easier to provide
>> meta information about a collection of triples, rather than just a
>> single reified triple.
>>
>> //JT
>> --
>> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>>
>


Re: How to write queries using named graphs?

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Thu, Oct 31, 2013 at 11:17 AM, Maria Jackson
<ma...@gmail.com> wrote:
> select ?x ?a ?b ?c ?d ?e ?f where {
>   graph ?r {
>     ?x ?y <CountryZ>
>   }
>  graph ?r1{
>     ?x ?y <CountryZ1>
> }
>   ?r <saidBy> Tia ;
>
>      <saidOn> ?b ;
>      <saidAt> ?c ;
>      <confidence> ?d ;
>      <comment> ?e .
> ?r1 <saidBy> Astrid.
> }
> Is the above query to retrieve a person who is linked to two different
> countries correct?
>
> I am just curious as to whether it is syntactically accepted by Jena

There's a query validator at sparql.org that's based on Jena [1].  If
you copy your query into it, it says there's a syntax error at "Tia",
which makes sense, because it's not a prefixed name or an absolute
IRI. (This is why I've been wrapping everything in angle brackets.)
We can clean it up a a bit and get the following valid (and slightly
better formatted) query:

select ?x ?a ?b ?c ?d ?e ?f where {
  graph ?r {
    ?x ?y <CountryZ>
  }
  graph ?r1{
    ?x ?y <CountryZ1>
  }
  ?r <saidBy> <Tia> ;
     <saidOn> ?b ;
     <saidAt> ?c ;
     <confidence> ?d ;
     <comment> ?e .
  ?r1 <saidBy> <Astrid>.
}


//JT

[1] http://sparql.org/query-validator.html
-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: How to write queries using named graphs?

Posted by Maria Jackson <ma...@gmail.com>.
select ?x ?a ?b ?c ?d ?e ?f where {
  graph ?r {
    ?x ?y <CountryZ>
  }
 graph ?r1{
    ?x ?y <CountryZ1>
}
  ?r <saidBy> Tia ;

     <saidOn> ?b ;
     <saidAt> ?c ;
     <confidence> ?d ;
     <comment> ?e .
?r1 <saidBy> Astrid.
}
Is the above query to retrieve a person who is linked to two different
countries correct?

I am just curious as to whether it is syntactically accepted by Jena


On Thu, Oct 31, 2013 at 8:45 PM, Joshua TAYLOR <jo...@gmail.com>wrote:

> On Thu, Oct 31, 2013 at 10:47 AM, Eric Scott <er...@att.net> wrote:
> > You could try something like this:
> >
> >  select ?a?b?c?d?e
> >  where
> > {
> >        ?g a :MyKindaGraph.
> >        graph ?g
> >
> >        {
> >          ?r rdf:subject <Mr_X>.
> >          ?r rdf:predicate <isPresident>.
> >          ?r rdf:object <CountryZ>.
> >          ?r saidBy ?a.
> >          ?r saidOn ?b.
> >          ?r saidAt ?c.
> >          ?r confidence ?d.
> >          ?r comment ?e
> >        }
> > }
>
> I think, though I could be wrong, that the point wasn't just to put
> the triples _into_ a named graph, but to use a named graph to serve
> _as_ the reified triple (or triples).  E.g.,
>
> graph ?g { <Mr_X> <isPresident> <CountryZ> }
> ?g saidBy ?a ; ... ; comment ?e .
>
> This avoids needing three triples for each reified statement, and
> makes the queries easier to write. It also makes it easier to provide
> meta information about a collection of triples, rather than just a
> single reified triple.
>
> //JT
> --
> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>

Re: How to write queries using named graphs?

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Thu, Oct 31, 2013 at 10:47 AM, Eric Scott <er...@att.net> wrote:
> You could try something like this:
>
>  select ?a?b?c?d?e
>  where
> {
>        ?g a :MyKindaGraph.
>        graph ?g
>
>        {
>          ?r rdf:subject <Mr_X>.
>          ?r rdf:predicate <isPresident>.
>          ?r rdf:object <CountryZ>.
>          ?r saidBy ?a.
>          ?r saidOn ?b.
>          ?r saidAt ?c.
>          ?r confidence ?d.
>          ?r comment ?e
>        }
> }

I think, though I could be wrong, that the point wasn't just to put
the triples _into_ a named graph, but to use a named graph to serve
_as_ the reified triple (or triples).  E.g.,

graph ?g { <Mr_X> <isPresident> <CountryZ> }
?g saidBy ?a ; ... ; comment ?e .

This avoids needing three triples for each reified statement, and
makes the queries easier to write. It also makes it easier to provide
meta information about a collection of triples, rather than just a
single reified triple.

//JT
-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: How to write queries using named graphs?

Posted by Eric Scott <er...@att.net>.
You could try something like this:

  select ?a?b?c?d?e
  where
{
        ?g a :MyKindaGraph.
        graph ?g
        {
          ?r rdf:subject <Mr_X>.
          ?r rdf:predicate <isPresident>.
          ?r rdf:object <CountryZ>.
          ?r saidBy ?a.
          ?r saidOn ?b.
          ?r saidAt ?c.
          ?r confidence ?d.
          ?r comment ?e
        }
}


On 10/31/2013 07:26 AM, Maria Jackson wrote:
> Yes I am talking about the same. Is it possible to query over Jena such
> that there is a join between two named graphs. I know I can write using
> reification in SPARQL using:
>
>   select ?a?b?c?d?e
>   where{
>         ?r rdf:subject <Mr_X>.
>         ?r rdf:predicate <isPresident>.
>         ?r rdf:object <CountryZ>.
>         ?r saidBy ?a.
>         ?r saidOn ?b.
>         ?r saidAt ?c.
>         ?r confidence ?d.
>         ?r comment ?e
>    }
>
>
>
> But I am not sure as to how can I write this query using named graphs
> without knowing the names of the graphs. As i am converting from
> reification to named graphs automatically. So the user does not know the
> names of the graphs
>
>
> On Thu, Oct 31, 2013 at 7:50 PM, Joshua TAYLOR <jo...@gmail.com>wrote:
>
>> On Thu, Oct 31, 2013 at 9:47 AM, Maria Jackson
>> <ma...@gmail.com> wrote:
>>> I think jena employs named graphs as a way to achieve reification. As
>> named
>>> graphs can achieve reification by attaching only one statement with named
>>> graphs.
>> Jena uses named graphs to provide names for RDF graphs as specified in
>> the SPARQL standards.  It's not about reification, it's about having
>> more than one graph.  It's not unlike the fact that Wikipedia has a
>> bunch of articles that can be identified by their URLs;  it's not
>> about refification, it's about providing different articles and not
>> having just one big article.
>>
>>> Is it possible to express information of the following form using named
>>> graphs, if yes then can someone please help me write this information
>> using
>>> named graphs, so that I may import the same into Jena:
>>>
>>>       _:a rdf:subject "Mr X".
>>>       _:a rdf:predicate "isPresident".
>>>       _:a rdf:object "countryZ".
>>>       _:a saidBy "Tia".
>>>       _:a saidOn "29 October 2013".
>>>       _:a saidAt "New York".
>>>       _:a confidence 0.9.
>>>       _:a comment "This is just a comment by Tia".
>>>
>>> Is it possible to write the corresponding query Jena using named graph
>>> query on the above given data?
>> You'll need to clarify what you're trying to do here. You can already
>> store this data in a named (or in an unnamed) RDF graph.  Maybe what
>> you're talking about doing is having a graph named (e.g.)
>>
>>      <http://example.org/comment_by_tia>
>>
>> with the triple
>>
>>      <MrX> <isPresidentOf> <CountryZ>
>>
>> and then having some triples around somewhere like
>>
>>      <http://example.org/comment_by_tia> <saidBy> <Tia>
>>      <http://example.org/comment_by_tia> <saidOn> "2013-10-29"^^...
>>      <http://example.org/comment_by_tia> <saidAt> <NewYork>
>>      <http://example.org/comment_by_tia> <confidence> 0.9
>>      <http://example.org/comment_by_tia> <comment> "This is just a
>> comment by Tia"
>>
>> This would be convenient way to provide a sort of reification,
>> especially because you could put more than one triple in the named
>> graph, so the "meta-information" could be describing a collection of
>> triples.  Is this the sort of thing that you're talking about doing?
>>
>> //JT
>> --
>> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>>


Re: How to write queries using named graphs?

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Thu, Oct 31, 2013 at 11:08 AM, Maria Jackson
<ma...@gmail.com> wrote:
> select ?x ?a ?b ?c ?d ?e ?f where {
>   graph ?r {
>     ?x ?y <CountryZ>
>   }
>  graph ?r1{
>     ?x ?y <CountryZ1>
> }
>   ?r <saidBy> Tia ;
>
>      <saidOn> ?b ;
>      <saidAt> ?c ;
>      <confidence> ?d ;
>      <comment> ?e .
> ?r1 <saidBy> Astrid.
> }
> Is the above query to retrieve a person who is linked to two different
> countries correct?

Yes, if you've represented information in the way that I've described,
this query would succeed if there's a graph ?r1 that Astrid said that
contains the triple `?x ?y <CountryZ1>` and there's a graph ?r that
Tia said that contains the triple `?x ?y <CountryZ>` _and_ which has
all the other metadata, too (i.e., saidOn, saidAt, confidence, and
comment).

//JT
-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: How to write queries using named graphs?

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Thu, Oct 31, 2013 at 10:26 AM, Maria Jackson
<ma...@gmail.com> wrote:
> Yes I am talking about the same. Is it possible to query over Jena such that
> there is a join between two named graphs. I know I can write using
> reification in SPARQL using:
>
>  select ?a?b?c?d?e
>  where{
>        ?r rdf:subject <Mr_X>.
>        ?r rdf:predicate <isPresident>.
>        ?r rdf:object <CountryZ>.
>        ?r saidBy ?a.
>        ?r saidOn ?b.
>        ?r saidAt ?c.
>        ?r confidence ?d.
>        ?r comment ?e
>   }
>
> But I am not sure as to how can I write this query using named graphs
> without knowing the names of the graphs. As i am converting from reification
> to named graphs automatically. So the user does not know the names of the
> graphs

If you use the approach I outlined in my first response, so that
you've got something like a graph named:

    <http://example.org/comment_by_tia>

with the triple

    <MrX> <isPresidentOf> <CountryZ>

and then having some triples around somewhere like

    <http://example.org/comment_by_tia> <saidBy> <Tia>
    <http://example.org/comment_by_tia> <saidOn> "2013-10-29"^^...
    <http://example.org/comment_by_tia> <saidAt> <NewYork>
    <http://example.org/comment_by_tia> <confidence> 0.9
    <http://example.org/comment_by_tia> <comment> "This is just a
comment by Tia"

you'd retrieve it with

select ?a ?b ?c ?d ?e ?f where {
  graph ?r {
    <Mr_X> <isPresidentOf> <CountryZ>
  }
  ?r <saidBy> ?a ;
     <saidOn> ?b ;
     <saidAt> ?c ;
     <confidence> ?d ;
     <comment> ?e .
}

//JT
-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: How to write queries using named graphs?

Posted by Maria Jackson <ma...@gmail.com>.
Yes I am talking about the same. Is it possible to query over Jena such
that there is a join between two named graphs. I know I can write using
reification in SPARQL using:

 select ?a?b?c?d?e
 where{
       ?r rdf:subject <Mr_X>.
       ?r rdf:predicate <isPresident>.
       ?r rdf:object <CountryZ>.
       ?r saidBy ?a.
       ?r saidOn ?b.
       ?r saidAt ?c.
       ?r confidence ?d.
       ?r comment ?e
  }



But I am not sure as to how can I write this query using named graphs
without knowing the names of the graphs. As i am converting from
reification to named graphs automatically. So the user does not know the
names of the graphs


On Thu, Oct 31, 2013 at 7:50 PM, Joshua TAYLOR <jo...@gmail.com>wrote:

> On Thu, Oct 31, 2013 at 9:47 AM, Maria Jackson
> <ma...@gmail.com> wrote:
> > I think jena employs named graphs as a way to achieve reification. As
> named
> > graphs can achieve reification by attaching only one statement with named
> > graphs.
>
> Jena uses named graphs to provide names for RDF graphs as specified in
> the SPARQL standards.  It's not about reification, it's about having
> more than one graph.  It's not unlike the fact that Wikipedia has a
> bunch of articles that can be identified by their URLs;  it's not
> about refification, it's about providing different articles and not
> having just one big article.
>
> > Is it possible to express information of the following form using named
> > graphs, if yes then can someone please help me write this information
> using
> > named graphs, so that I may import the same into Jena:
> >
> >      _:a rdf:subject "Mr X".
> >      _:a rdf:predicate "isPresident".
> >      _:a rdf:object "countryZ".
> >      _:a saidBy "Tia".
> >      _:a saidOn "29 October 2013".
> >      _:a saidAt "New York".
> >      _:a confidence 0.9.
> >      _:a comment "This is just a comment by Tia".
> >
> > Is it possible to write the corresponding query Jena using named graph
> > query on the above given data?
>
> You'll need to clarify what you're trying to do here. You can already
> store this data in a named (or in an unnamed) RDF graph.  Maybe what
> you're talking about doing is having a graph named (e.g.)
>
>     <http://example.org/comment_by_tia>
>
> with the triple
>
>     <MrX> <isPresidentOf> <CountryZ>
>
> and then having some triples around somewhere like
>
>     <http://example.org/comment_by_tia> <saidBy> <Tia>
>     <http://example.org/comment_by_tia> <saidOn> "2013-10-29"^^...
>     <http://example.org/comment_by_tia> <saidAt> <NewYork>
>     <http://example.org/comment_by_tia> <confidence> 0.9
>     <http://example.org/comment_by_tia> <comment> "This is just a
> comment by Tia"
>
> This would be convenient way to provide a sort of reification,
> especially because you could put more than one triple in the named
> graph, so the "meta-information" could be describing a collection of
> triples.  Is this the sort of thing that you're talking about doing?
>
> //JT
> --
> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>

Re: How to write queries using named graphs?

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Thu, Oct 31, 2013 at 9:47 AM, Maria Jackson
<ma...@gmail.com> wrote:
> I think jena employs named graphs as a way to achieve reification. As named
> graphs can achieve reification by attaching only one statement with named
> graphs.

Jena uses named graphs to provide names for RDF graphs as specified in
the SPARQL standards.  It's not about reification, it's about having
more than one graph.  It's not unlike the fact that Wikipedia has a
bunch of articles that can be identified by their URLs;  it's not
about refification, it's about providing different articles and not
having just one big article.

> Is it possible to express information of the following form using named
> graphs, if yes then can someone please help me write this information using
> named graphs, so that I may import the same into Jena:
>
>      _:a rdf:subject "Mr X".
>      _:a rdf:predicate "isPresident".
>      _:a rdf:object "countryZ".
>      _:a saidBy "Tia".
>      _:a saidOn "29 October 2013".
>      _:a saidAt "New York".
>      _:a confidence 0.9.
>      _:a comment "This is just a comment by Tia".
>
> Is it possible to write the corresponding query Jena using named graph
> query on the above given data?

You'll need to clarify what you're trying to do here. You can already
store this data in a named (or in an unnamed) RDF graph.  Maybe what
you're talking about doing is having a graph named (e.g.)

    <http://example.org/comment_by_tia>

with the triple

    <MrX> <isPresidentOf> <CountryZ>

and then having some triples around somewhere like

    <http://example.org/comment_by_tia> <saidBy> <Tia>
    <http://example.org/comment_by_tia> <saidOn> "2013-10-29"^^...
    <http://example.org/comment_by_tia> <saidAt> <NewYork>
    <http://example.org/comment_by_tia> <confidence> 0.9
    <http://example.org/comment_by_tia> <comment> "This is just a
comment by Tia"

This would be convenient way to provide a sort of reification,
especially because you could put more than one triple in the named
graph, so the "meta-information" could be describing a collection of
triples.  Is this the sort of thing that you're talking about doing?

//JT
-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: How to write queries using named graphs?

Posted by Maria Jackson <ma...@gmail.com>.
I can write a SPARQL query for the above graph as:

 select ?a?b?c?d?e
 where{
       ?r rdf:subject <Mr_X>.
       ?r rdf:predicate <isPresident>.
       ?r rdf:object <CountryZ>.
       ?r saidBy ?a.
       ?r saidOn ?b.
       ?r saidAt ?c.
       ?r confidence ?d.
       ?r comment ?e
  }

Can someone please help me write the above query using named graphs in
SPARQL so that I may query over Jena?


On Thu, Oct 31, 2013 at 7:17 PM, Maria Jackson
<ma...@gmail.com>wrote:

> I think jena employs named graphs as a way to achieve reification. As
> named graphs can achieve reification by attaching only one statement with
> named graphs.
> Is it possible to express information of the following form using named
> graphs, if yes then can someone please help me write this information using
> named graphs, so that I may import the same into Jena:
>
>      _:a rdf:subject "Mr X".
>      _:a rdf:predicate "isPresident".
>      _:a rdf:object "countryZ".
>      _:a saidBy "Tia".
>      _:a saidOn "29 October 2013".
>      _:a saidAt "New York".
>      _:a confidence 0.9.
>      _:a comment "This is just a comment by Tia".
>
> Is it possible to write the corresponding query Jena using named graph
> query on the above given data?
>
>
>