Using SPARQL 1.1 Property Paths in Virtuoso
What are Property Paths?
A property path is a possible route through a graph between two graph nodes.
Why use SPARQL 1.1 Property Paths?
SPARQL 1.1 Property Paths are syntactic sugar, allowing for more concise expression of some SPARQL basic graph patterns, and adding the ability to match paths of arbitrary length. They are not necessary nor required, but they can make queries shorter and easier to both write and read.
How can I use SPARQL 1.1 Property Paths with Virtuoso?
Here are some examples demonstrating Virtuoso's support of SPARQL 1.1 Property Paths. These example queries should work against any SPARQL endpoint that supports SPARQL 1.1 and has the ability to allow a verified user to perform INSERT operations.
Basic Usage
## Basic SELECT DISTINCT ?x ?name WHERE { ?x foaf:mbox <mailto:kidehen@openlinksw.com> ; foaf:knows/foaf:name ?name }
- View the SPARQL Query Definition via SPARQL Protocol URL
- View the SPARQL Query Results via SPARQL Protocol URL
Usage of Sequence
SELECT DISTINCT ?x ?name WHERE { ?x foaf:mbox <mailto:kidehen@openlinksw.com> ; foaf:knows/foaf:knows/foaf:name ?name }
- View the SPARQL Query Definition via SPARQL Protocol URL
- View the SPARQL Query Results via SPARQL Protocol URL
Usage of Inverse
## Mutual foaf:knows relationships: ?x knows someone who knows ?x SELECT DISTINCT ?x ?name WHERE { ?x foaf:mbox <mailto:kidehen@openlinksw.com> ; foaf:knows/^foaf:knows ?y . ?y foaf:name ?name FILTER ( ?x != ?y ) }
- View the SPARQL Query Definition via SPARQL Protocol URL
- View the SPARQL Query Results via SPARQL Protocol URL
Usage of Negation of a Unary Operator
SELECT DISTINCT * WHERE { ?x !rdf:type foaf:Person } LIMIT 10
- View the SPARQL Query Definition via SPARQL Protocol URL
- View the SPARQL Query Results via SPARQL Protocol URL
Constructing an Object using Terms from vCard, FOAF, and OWL
- 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" ; <http://www.w3.org/2006/vcard/ns#family-name> "Mutt" ; <http://www.w3.org/2006/vcard/ns#email> <mailto:rick@selavy.org> . <#vcardAl> <http://www.w3.org/2006/vcard/ns#given-name> "Alan" ; <http://www.w3.org/2006/vcard/ns#family-name> "Smithee" ; <http://www.w3.org/2006/vcard/ns#email> <mailto:alan@paramount.com> .
- 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" ; <http://www.w3.org/2006/vcard/ns#family-name> "Mutt" ; <http://www.w3.org/2006/vcard/ns#email> <mailto:rick@selavy.org> . <#vcardAl> <http://www.w3.org/2006/vcard/ns#given-name> "Alan" ; <http://www.w3.org/2006/vcard/ns#family-name> "Smithee" ; <http://www.w3.org/2006/vcard/ns#email> <mailto:alan@paramount.com> } }
- 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" ; <http://xmlns.com/foaf/0.1/familyName> "Shears" ; <http://xmlns.com/foaf/0.1/mbox> <mailto:bill@northernsongs.org> . <#foafNate> <http://xmlns.com/foaf/0.1/givenName> "Nanker" ; <http://xmlns.com/foaf/0.1/familyName> "Phelge" ; <http://xmlns.com/foaf/0.1/mbox> <mailto:nate@abkco.com> .
- 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" ; <http://xmlns.com/foaf/0.1/familyName> "Shears" ; <http://xmlns.com/foaf/0.1/mbox> <mailto:bill@northernsongs.org> . <#foafNate> <http://xmlns.com/foaf/0.1/givenName> "Nanker" ; <http://xmlns.com/foaf/0.1/familyName> "Phelge" ; <http://xmlns.com/foaf/0.1/mbox> <mailto:nate@abkco.com> } }
- 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 statements (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> .
- 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> } }
- 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
Related
- W3 documentation of SPARQL 1.1. Property Paths
- Virtuoso Tips and Tricks Collection
- Virtuoso SPARQL 1.1 Syntax Tutorial
- Virtuoso Documentation