Virtuoso Open-Source Wiki
Virtuoso Open-Source, OpenLink Data Spaces, and OpenLink Ajax Toolkit
Advanced Search
Help?
Location: / Dashboard / Main / VirtTipsAndTricksGuide / VirtTipsAndTricksSPARQL11FeaturesExamplesCollection / VirtTipsAndTricksSPARQL11PropertyPaths

Virtuoso SPARQL 1.1. Property Paths Usage Examples

What?

This guide contains Virtuoso SPARQL 1.1. Property Paths Usage example queries which you can run against any SPARQL endpoint that supports SPARQL 1.1 and the ability to allow a verified user perform INSERT operations.

Why?

Property Paths give a more flexible way to write parts of basic graph patterns and also extend matching of triple pattern to arbitrary length paths.

How?

Here are some examples showcasing Virtuoso's support for this functionality:

Basic usage

## Basic
SELECT DISTINCT ?x ?name 
WHERE 
  {
    ?x foaf:mbox <mailto:kidehen@openlinksw.com>.
    ?x foaf:knows/foaf:name ?name .
 }

Sequence usage

SELECT DISTINCT ?x ?name 
WHERE 
  {
    ?x foaf:mbox <mailto:kidehen@openlinksw.com>.
    ?x foaf:knows/foaf:knows/foaf:name ?name .
  }

Inverse usage

## Mutual foaf:knows relationships: ?x knows someone who knows ?x
   
SELECT DISTINCT ?x ?name 
WHERE 
  {    
    ?x foaf:mbox <mailto:kidehen@openlinksw.com>.    
    ?x foaf:knows/^foaf:knows ?y . 
    ?y foaf:name ?name      
    FILTER ( ?x != ?y )     
  }

Negation an unary operator usage

SELECT DISTINCT * 
WHERE 
  {    
    ?x !rdf:type foaf:Person . 
  }  
LIMIT 10 

Object constructed using terms from the vCard, FOAF vocabulary and OWL Ontology

  1. Assume the following Raw Data Representation in Turtle of a Address Book Data Object constructed using terms from the vCard vocabulary:

    <#vcardRick> <http://www.w3.org/2006/vcard/ns#given-name> "Richard" ; <#vcardRick> <http://www.w3.org/2006/vcard/ns#family-name> "Mutt" ; <#vcardRick> <http://www.w3.org/2006/vcard/ns#email> <mailto:rick@selavy.org> . <#vcardAl> <http://www.w3.org/2006/vcard/ns#given-name> "Alan" ; <#vcardAl> <http://www.w3.org/2006/vcard/ns#family-name> "Smithee" ; <#vcardAl> <http://www.w3.org/2006/vcard/ns#email> <mailto:alan@paramount.com> .

  2. Load the sample data:

    PREFIX dc: <http://purl.org/dc/elements/1.1/> INSERT DATA { GRAPH <urn:sparql:fed:vcard:addressbook:demo> { <#vcardRick> <http://www.w3.org/2006/vcard/ns#given-name> "Richard" . <#vcardRick> <http://www.w3.org/2006/vcard/ns#family-name> "Mutt" . <#vcardRick> <http://www.w3.org/2006/vcard/ns#email> <mailto:rick@selavy.org> . <#vcardAl> <http://www.w3.org/2006/vcard/ns#given-name> "Alan" . <#vcardAl> <http://www.w3.org/2006/vcard/ns#family-name> "Smithee" . <#vcardAl> <http://www.w3.org/2006/vcard/ns#email> <mailto:alan@paramount.com> . } }

  3. View the SPARQL Query Definition via SPARQL Protocol URL;
  4. View the SPARQL Query Results via SPARQL Protocol URL.
  5. Assume the following Raw Data Representation in Turtle of a Address Book Data Object constructed using terms from the FOAF vocabulary:

    <#foafBill> <http://xmlns.com/foaf/0.1/givenName> "Billy" . <#foafBill> <http://xmlns.com/foaf/0.1/familyName> "Shears" . <#foafBill> <http://xmlns.com/foaf/0.1/mbox> <mailto:bill@northernsongs.org> . <#foafNate> <http://xmlns.com/foaf/0.1/givenName> "Nanker" . <#foafNate> <http://xmlns.com/foaf/0.1/familyName> "Phelge" . <#foafNate> <http://xmlns.com/foaf/0.1/mbox> <mailto:nate@abkco.com> .

  6. Load the sample data:

    PREFIX dc: <http://purl.org/dc/elements/1.1/> INSERT DATA { GRAPH <urn:sparql:fed:foaf:addressbook:demo> { <#foafBill> <http://xmlns.com/foaf/0.1/givenName> "Billy" . <#foafBill> <http://xmlns.com/foaf/0.1/familyName> "Shears" . <#foafBill> <http://xmlns.com/foaf/0.1/mbox> <mailto:bill@northernsongs.org> . <#foafNate> <http://xmlns.com/foaf/0.1/givenName> "Nanker" . <#foafNate> <http://xmlns.com/foaf/0.1/familyName> "Phelge" . <#foafNate> <http://xmlns.com/foaf/0.1/mbox> <mailto:nate@abkco.com> . } }

  7. View the SPARQL Query Definition via SPARQL Protocol URL;
  8. View the SPARQL Query Results via SPARQL Protocol URL.
  9. Assume the following Raw Data Representation in Turtle of a mapping oriented Data Object constructed using terms from the OWL Ontology that describe entity relationship semantics:

    # This data object is comprised of statments (triples) that map certain properties across the vCard and FOAF vocabularies. # These mappings express machine readable entity relationship semantics usable by a Reasoner to produce (by inference) smart query results. <http://xmlns.com/foaf/0.1/givenName> <http://www.w3.org/2002/07/owl#equivalentProperty> <http://www.w3.org/2006/vcard/ns#given-name> . <http://xmlns.com/foaf/0.1/familyName> <http://www.w3.org/2002/07/owl#equivalentProperty> <http://www.w3.org/2006/vcard/ns#family-name> . <http://xmlns.com/foaf/0.1/mbox> <http://www.w3.org/2002/07/owl#equivalentProperty> <http://www.w3.org/2006/vcard/ns#email> .

  10. Load the sample data:

    PREFIX dc: <http://purl.org/dc/elements/1.1/> INSERT DATA { GRAPH <urn:sparql:fed:owl:addressbook:demo> { <http://xmlns.com/foaf/0.1/givenName> <http://www.w3.org/2002/07/owl#equivalentProperty> <http://www.w3.org/2006/vcard/ns#given-name> . <http://xmlns.com/foaf/0.1/familyName> <http://www.w3.org/2002/07/owl#equivalentProperty> <http://www.w3.org/2006/vcard/ns#family-name> . <http://xmlns.com/foaf/0.1/mbox> <http://www.w3.org/2002/07/owl#equivalentProperty> <http://www.w3.org/2006/vcard/ns#email> . } }

  11. View the SPARQL Query Definition via SPARQL Protocol URL;
  12. View the SPARQL Query Results via SPARQL Protocol URL.
  13. Query the graphs:

    PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT bif:sprintf ("<%s> = %s\t<%s> = %s", str(?givenNameProp),?fn,str(?lastNameProp),?ln) FROM <urn:sparql:fed:vcard:addressbook:demo> FROM <urn:sparql:fed:foaf:addressbook:demo> FROM <urn:sparql:fed:owl:addressbook:demo> WHERE { <http://xmlns.com/foaf/0.1/givenName> (owl:equivalentProperty|^owl:equivalentProperty)* ?givenNameProp . <http://xmlns.com/foaf/0.1/familyName> (owl:equivalentProperty|^owl:equivalentProperty)* ?lastNameProp . ?s ?givenNameProp ?fn ; ?lastNameProp ?ln . } LIMIT 10

  14. View the SPARQL Query Definition via SPARQL Protocol URL;
  15. View the SPARQL Query Results via SPARQL Protocol URL.

Related

Powered By Virtuoso