1
votes

I'm new to SPARQL and I'm trying to fetch some results from triples in a SQL join fashion.

e.g. I'm looking for the ID of the object a certain image belongs to, and the ID of the author of the object, and so on.

RDF relations are defined like this:

<image> <isResourceOf> <object>
<person> <isAuthorOf> <object>

I tried this:

SELECT ?o ?a from <#ri> 
WHERE {
  {<myns:myImageID> <myns:isResourceOf> ?o}
  UNION
  {?a <myns:isAuthorOf> ?o}
}

But this fetches all the authors, since the two ?o variables are not bound.

How do I get to match the authors ONLY for the first match set?

Thanks

gm

EDIT:

More complete example:

PREFIX aic: <http://mydomain.org/definitions/1.0/fedora/3#>

SELECT *
WHERE {
  <info:fedora/AICTEST:DOResImg-G38562> aic:isResourceOf ?o . 
  ?a aic:isAuthorOf ?o .
}

No results.

SELECT *
WHERE {
  <info:fedora/AICTEST:DOResImg-G38562> aic:isResourceOf ?o . 
}

Results (CSV):

"o"
AICTEST:DOArtObj-1946.479

With this:

SELECT *
WHERE {
  ?a aic:isAuthorOf ?o .
}

Results:

"a","o"
info:fedora/AICTEST:DOAgent-35729,info:fedora/AICTEST:DOArtObj-1946.479
info:fedora/AICTEST:DOAgent-35729,AICTEST:DOObjSet-1946.494
3
I forgot to mention that I'm using Mulgara. I'm not sure how exactly Mulgara implements SPARQL, so there might be a different behavior in this context.gattu marrudu

3 Answers

3
votes

What you would call a join in SQL is simply expressed as a list of basic graph patterns in SPARQL. No UNION is necessary:

 SELECT ?o ?a 
 WHERE { 
         <myns:myImageId> <myns:isResourceOf> ?o .
         ?a <myns:isAuthorOf> ?o .
 }

That's all there is to it. The way to think of SPARQL is as specifying a "graph template" on your data. Everything that exactly matches the template is a result of the query. So the above query will give you all things that <myns:myImageId> is a resource of ( = ?o), and for each of those, all authors (?a).

2
votes

This question duplicates http://answers.semanticweb.com//questions/23912/fetching-chained-relationships-with-sparql-mulgara

As I said there, I strongly suspect there's something wrong with the namespace prefix definition in your original query, and possibly in your simplified versions as well, given that you're getting errors if the cURIes are not angle-bracket-wrapped.

I suggest that you present complete information to us; else, we will be unable to usefully advise you.

1
votes

I managed to get the right relation chain to work:

PREFIX aic: <http://mydomain.org/definitions/1.0/fedora/3#>
PREFIX fedora-rels-ext: <info:fedora/fedora-system:def/relations-external#>

SELECT ?o ?a ?p ?s
FROM <#ri>
WHERE {
  <info:fedora/AICTEST:DOResImg-G38562> aic:isResourceOf ?o . 
  OPTIONAL { 
    ?a aic:isAuthorOf ?o .
    OPTIONAL { 
      ?p aic:isBirthplaceOf ?a 
    } . 
  } . 
  OPTIONAL { 
    ?o fedora-rels-ext:isPartOf ?s .
  }
}

Results (CSV):

"o","a","p","s"
info:fedora/AICTEST:DOArtObj-1946.479,info:fedora/AICTEST:DOAgent-35729,info:fedora/AICTEST:DOPlace-Meissen,AICTEST:DOObjSet-1946.494