Re: List of statements

On  16 Aug 2009, at 10:12 PM, On Lee wrote:

> Many FOAF files leverage other vocabulary such as http://purl.org/dc/elements/1.1/,http://xmlns.com/wot/0.1/ 
> , etc.
>
> Given a FOAF file, how to write SPARQL that can do the following?
>
> 1.      List all statements that use foaf vocabulary such as  
> foaf:name, foaf:mbox, etc?
> 2.      List all statements that do *not* use foaf vocabulary?
>
> Thanks in advance for your help.
>
> Best,
> -- On Lee


If you have the ontology loaded, in an appropriate named graph, with  
sufficient reasoning, you can write a query such as

CONSTRUCT {
   ?s ?p ?o
}

FROM <your.data>
FROM NAMED <http://xmlns.com/foaf/0.1/>

WHERE {
   ?s ?p ?o .
   GRAPH <http://xmlns.com/foaf/0.1/> {
     ?p a rdf:Property .
   }
}

to construct the subgraph of triples where the property is defined in  
the FOAF graph. Note that reasoning will need to be tightly  
constrained, here, to avoid undesirable entailments arising from that  
graph (such as rdf:type a rdf:Property, and thus all type statements  
appearing in your output).


The other way to do this in SPARQL is to write a query like

CONSTRUCT {
   ?s ?p ?o
}
WHERE {
   ?s ?p ?o .
   FILTER (regex(str(?p), "^http://xmlns\.com/foaf/0.1/"))
}

... i.e., matching the namespace string. You'll have to check the  
regex escaping, or -- if your implementation supports it -- use  
fn:starts-with instead.

Extending this to also match classes is an exercise for the reader. :)

This is likely to be quite expensive; implementations are typically  
not designed to do this kind of thing. URIs are meant to be opaque.


I'd be tempted to do this a completely different way, not using  
SPARQL: dump the FOAF file as N-Triples, and simply use grep. This is  
much simpler and more general than the SPARQL solutions above.

E.g.,

wheeljack:~ rnewman$ rapper -q -i rdfxml -o ntriples http://heddley.com/edd/foaf.rdf 
  | fgrep "http://xmlns.com/foaf/0.1/"
<http://heddley.com/edd/foaf.rdf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type 
 > <http://xmlns.com/foaf/0.1/PersonalProfileDocument> .
<http://heddley.com/edd/foaf.rdf> <http://xmlns.com/foaf/0.1/maker> <http://heddley.com/edd/foaf.rdf#edd 
 > .
<http://heddley.com/edd/foaf.rdf> <http://xmlns.com/foaf/0.1/primaryTopic 
 > <http://heddley.com/edd/foaf.rdf#edd> .
<http://heddley.com/edd/foaf.rdf#edd> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type 
 > <http://xmlns.com/foaf/0.1/Person> .
<http://heddley.com/edd/foaf.rdf#edd> <http://xmlns.com/foaf/0.1/myersBriggs 
 > "ENFJ" .
<http://heddley.com/edd/foaf.rdf#edd> <http://xmlns.com/foaf/0.1/aimChatID 
 > "EddDumbill" .


Finding the non-FOAF properties is trivial; just add "-v" to the fgrep  
incantation.

-R

Received on Monday, 17 August 2009 05:44:33 UTC