David
David

Reputation: 31

foaf and count in a sparql 1.1 query

I'm a beginner in SPARQL, so it would be great if you could help me. . I have a RDF doc like this:

  @prefix foaf: <http://xmlns.com/foaf/0.1/> .
  @prefix lb:   <http://example.org/lastfm/> .

  lb:bob     foaf:knows      lb:user2, lb:user3, lb:user4 ;
       foaf:age        25 ;
       lb:listenedTo   lb:track1, lb:track2 ;
       lb:topArtist    lb:artist1, lb:artist2 .        

  lb:user2   foaf:knows      lb:user5, lb:user6 ;
       foaf:age        40 ;
       lb:listenedTo   lb:track1, lb:track2, lb:track3 ;  
       lb:topArtist    lb:artist2, lb:artist4 .        

  lb:user3   foaf:knows      lb:user5, lb:bob, lb:user6 ;
       foaf:age        19 ;
       lb:listenedTo   lb:track2, lb:track3, lb:track4 ;
       lb:topArtist    lb:artist2, lb:artist3 .      

  lb:user4   lb:listenedTo   lb:track2, lb:track3, lb:track4 ;
       foaf:age        61 ;
       lb:topArtist    lb:artist3, lb:artist4, lb:artist5 .     

  lb:user5   foaf:knows      lb:user7 ;
       foaf:age        23 ;
       lb:topArtist    lb:artist1, lb:artist3 .

and I want to find all users that can be reached from lb:bob by following the foaf:knows at most 3 times and who have listened to at least two tracks. I wrote a query like this:

  PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
  prefix foaf: <http://xmlns.com/foaf/0.1/>
  prefix lb:   <http://example.org/lastfm/>

  select ?user
  where
  {
   lb:bob foaf:knows | foaf:knows/foaf:knows| foaf:knows/foaf:knows/foaf:knows ?user.
    ?user lb:listenedTo ?tracks
     minus{lb:bob lb:listenedTo ?track}
      filter (?tracks count(*)>=2)
  }

but this error:

Encountered " "count" "count "" at line 12, column 23. Was expecting one of:
 "not" ...
 "in" ...
 <INTEGER_POSITIVE> ...
 <DECIMAL_POSITIVE> ...
 <DOUBLE_POSITIVE> ...
 <INTEGER_NEGATIVE> ...
 <DECIMAL_NEGATIVE> ...
 <DOUBLE_NEGATIVE> ...
 ")" ...
 "=" ...
 "!=" ...
 ">" ...
 "<" ...
 "<=" ...
 ">=" ...
 "||" ...
 "&&" ...
 "+" ...
 "-" ...
 "*" ...
 "/" ...

I know that my query has a lot of problems, would you please rewrite it for me?

Upvotes: 3

Views: 349

Answers (1)

user205512
user205512

Reputation: 8888

How about:

prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix lb:   <http://example.org/lastfm/>

select ?user
where
{
  lb:bob foaf:knows | foaf:knows/foaf:knows| foaf:knows/foaf:knows/foaf:knows ?user.
  ?user lb:listenedTo ?tracks
}
group by ?user
having (count(?tracks) > 2)

You have minus in the original query that I'm not sure about. Are you trying to remove lb:bob as a binding for ?user, perhaps? filter (?user != lb:bob) would stop that.

Upvotes: 5

Related Questions