SPARQL Examples

Stefano Costa edited this page Nov 23, 2015 · 9 revisions
Clone this wiki locally

Prefixes

PREFIX lawd: <http://lawd.info/ontology/>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX foaf:  <http://xmlns.com/foaf/0.1/>
PREFIX snap:  <http://onto.snapdrgn.net/snap#>
PREFIX bibo:  <http://purl.org/ontology/bibo/>
PREFIX rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs:  <http://www.w3.org/2000/01/rdf-schema#>
PREFIX cnt:   <http://www.w3.org/2011/content#>
PREFIX dct: <http://purl.org/dc/terms/>

Queries

Direct Query to Endpoint outputing in JSON

https://snap.dighum.kcl.ac.uk/sparql/?query=PREFIX%20lawd:%20%3Chttp://lawd.info/ontology/%3E%20SELECT%20?id%20WHERE%20{GRAPH%20%3Chttp://PIR%3E%20{?id%20a%20lawd:Person}}&output=json

Get list of Person IDs for given graph

PREFIX lawd: <http://lawd.info/ontology/>

SELECT ?id WHERE {GRAPH <graph> {
 ?id a lawd:Person }
}

Get original project ID given derived SNAP ID

(NB. Need a version that gets the most recent Snap ID, not just the first one - although if the merged records are stacking this may be less of an issue)

PREFIX prov: <http://www.w3.org/ns/prov#>

SELECT DISTINCT ?id {
 < snap_id > prov:wasDerivedFrom ?id
}

Get SNAP ID from original project ID

PREFIX prov: <http://www.w3.org/ns/prov#>

SELECT DISTINCT ?id {
 ?id prov:wasDerivedFrom < original_uri >
}

Get replaced SNAP ID

PREFIX dc: <http://purl.org/dc/terms/>

SELECT DISTINCT ?id {
<snap_id> dc:replaces ?id
}

Find canonical SNAP uri (direct or replaced) from original URI

PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX dc: <http://purl.org/dc/terms/>
SELECT DISTINCT ?snap2
WHERE {
{?snap2 dc:replaces ?snap1 .
?snap1 prov:DerivedFrom <http://www.lgpn.ox.ac.uk/id/V4-18886>}
UNION
{ ?snap2 prov:DerivedFrom <http://www.lgpn.ox.ac.uk/id/V4-18886>
MINUS { ?s dc:replaces ?snap2 } }
}

Get information related to original URI

SELECT DISTINCT ?s ?p ?o
WHERE {
{< original_uri > ?p ?o}
UNION
{?s ?p < original_uri >}
}

General additonal information

outgoing relationship:

    PREFIX snap:  <http://onto.snapdrgn.net/snap#>
    PREFIX cnt:   <http://www.w3.org/2011/content#>
    PREFIX rdfs:  <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX lawd: <http://lawd.info/ontology/> 

    SELECT DISTINCT ?type, ?string, ?rel
    {
    < original_uri > <relationship> ?x .
    ?x a ?type .
    OPTIONAL { ?x cnt:chars ?string } .
    OPTIONAL { ?x rdfs:label ?string } .
    OPTIONAL { ?x lawd:primaryForm ?string } .
    OPTIONAL { ?x snap:bond-with ?rel } .
    FILTER(STRSTARTS(STR(?type), "http://onto.snapdrgn.net/snap#") || STRSTARTS(STR(?type), "http://lawd.info/ontology/"))
    } 

incoming relationship:

    PREFIX snap:  <http://onto.snapdrgn.net/snap#>
    PREFIX cnt:   <http://www.w3.org/2011/content#>
    PREFIX rdfs:  <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX lawd: <http://lawd.info/ontology/> 

    SELECT DISTINCT ?type, ?string, ?rel {
    ?x <relationship> < original_uri > .
    ?x a ?type .
    OPTIONAL { ?x cnt:chars ?string } .
    OPTIONAL { ?x rdfs:label ?string } .
    OPTIONAL { ?x lawd:primaryForm ?string } .
    OPTIONAL { ?x snap:bond-with ?rel } .
    FILTER(STRSTARTS(STR(?type), "http://onto.snapdrgn.net/snap#") || STRSTARTS(STR(?type), "http://lawd.info/ontology/"))
    }

Get Bond information

(NB. should be able to get rid of cnt:chars option after next ingest as that is being depricated in favour of rdfs:label)

has-bond:

    PREFIX snap:  <http://onto.snapdrgn.net/snap#>
    PREFIX cnt:   <http://www.w3.org/2011/content#>
    PREFIX rdfs:  <http://www.w3.org/2000/01/rdf-schema#>

    SELECT DISTINCT ?bond_type, ?string, ?rel {
    < original_uri > snap:has-bond ?bond .
    ?bond a ?bond_type .
    OPTIONAL { ?bond cnt:chars ?string } .
    OPTIONAL { ?bond rdfs:label ?string } .
    OPTIONAL { ?bond snap:bond-with ?rel } .
    FILTER(STRSTARTS(STR(?bond_type), "http://onto.snapdrgn.net/snap#"))
    }

 bond-with:

    PREFIX snap:  <http://onto.snapdrgn.net/snap#>
    PREFIX cnt:   <http://www.w3.org/2011/content#>
    PREFIX rdfs:  <http://www.w3.org/2000/01/rdf-schema#>

    SELECT DISTINCT ?bond_type, ?string, ?rel {
    ?bond snap:bond-with < original_uri > .
    ?bond a ?bond_type .
    OPTIONAL { ?bond cnt:chars ?string } .
    OPTIONAL { ?bond rdfs:label ?string } .
    OPTIONAL { ?rel snap:has-bond ?bond } .
    FILTER(STRSTARTS(STR(?bond_type), "http://onto.snapdrgn.net/snap#"))
    }

e.g.

    PREFIX snap:  <http://onto.snapdrgn.net/snap#>
    PREFIX cnt:   <http://www.w3.org/2011/content#>
    PREFIX rdfs:  <http://www.w3.org/2000/01/rdf-schema#>

    SELECT DISTINCT ?bond_type, ?string, ?rel {
    <http://www.lgpn.ox.ac.uk/id/V1-23070> snap:has-bond ?bond .
    ?bond a ?bond_type .
    OPTIONAL { ?bond cnt:chars ?string } .
    OPTIONAL { ?bond rdfs:label ?string } .
    OPTIONAL { ?bond snap:bond-with ?rel } .
    FILTER(STRSTARTS(STR(?bond_type), "http://onto.snapdrgn.net/snap#"))
    }


    PREFIX snap:  <http://onto.snapdrgn.net/snap#>
    PREFIX cnt:   <http://www.w3.org/2011/content#>
    PREFIX rdfs:  <http://www.w3.org/2000/01/rdf-schema#>

    SELECT DISTINCT ?bond_type, ?string, ?rel {
    ?bond snap:bond-with <http://www.trismegistos.org/person/79146#this> .
    ?bond a ?bond_type .
    OPTIONAL { ?bond cnt:chars ?string } .
    OPTIONAL { ?bond rdfs:label ?string } .
    OPTIONAL { ?rel snap:has-bond ?bond } .
    FILTER(STRSTARTS(STR(?bond_type), "http://onto.snapdrgn.net/snap#"))
    }

Get Place information

Get Name information

PREFIX lawd: <http://lawd.info/ontology/>

SELECT DISTINCT ?name {
 < original_uri > lawd:hasName [ lawd:primaryForm ?name ].
}

e.g.

PREFIX lawd: <http://lawd.info/ontology/>

SELECT DISTINCT ?name {
 <http://www.lgpn.ox.ac.uk/id/V1-23070> lawd:hasName [ lawd:primaryForm ?name ].
}

Get Attestation information

Filters

All persons with citation X
All persons associated with place X
PREFIX snap:  <http://onto.snapdrgn.net/snap#>

SELECT * WHERE {
 ?associatedPlace snap:associatedPlace <http://pleiades.stoa.org/places/589796>
} LIMIT 10
All persons with name X
date?

(don't think sparql can deal with the date ranges)

All persons with occupation X