Mirror of the Rel4tion website/wiki source, view at <http://rel4tion.org>
Clone
HTTPS:
git clone https://vervis.peers.community/repos/yEzqv
SSH:
git clone USERNAME@vervis.peers.community:yEzqv
Branches
Tags
examples.mdwn
I’m going to try improving Naya and detecting problems by taking the [[sparql-examples]] list and translating the examples one by one to Naya.
- Query structure: q = (P, A, V, K, T, H, l, N)
- Default/no value marker: -
- Empty set marker: /
- uid placeholders: <&x>, <&y>, etc.
- Parameter prefix: $
- Optional parameter prefix: %
- Variable prefix: ?
2.1
[[!format n3 """ SELECT ?title WHERE { http://example.org/book/book1 http://purl.org/dc/elements/1.1/title ?title . } """]]
- P = $title
- A = /
- V = ?i
- K = <&book> <&title>
- T = (?i <&book> <&title> $title)
- H = /
- l = -
- N = /
2.2
[[!format n3 """ PREFIX foaf: http://xmlns.com/foaf/0.1/ SELECT ?name ?mbox WHERE { ?x foaf:name ?name . ?x foaf:mbox ?mbox } """]]
- P = $name $mbox
- A = /
- V = ?i ?j ?x
- K = /
- T = AND{(?i ?x foaf:name $name), (?j ?x foaf:mbox $mbox)}
- H = /
- l = en
- N = foaf:<&foaf>
2.3.1
[[!format n3 """ SELECT ?v WHERE { ?v ?p "cat"@en } """]]
- P = $v
- A = /
- V = ?i ?p
- K = /
- T = (?i $v ?p “cat”)
- H = /
- l = -
- N = /
Or:
- P = $v
- A = /
- V = ?i ?j ?k ?p ?text
- K = <&lang> <&content> <&en>
- T = AND{(?i $v ?p ?text), (?j ?text <&lang> <&en>), (?k ?text <&content> “cat”)}
- H = /
- l = -
- N = /
2.3.2
[[!format n3 """ SELECT ?v WHERE { ?v ?p 42 } """]]
- P = $v
- A = /
- V = ?i ?p
- K = /
- T = (?i $v ?p 42)
- H = /
- l = -
- N = /
2.3.3
[[!format n3 """ SELECT ?v WHERE { ?v ?p “abc”^^http://example.org/datatype#specialDatatype } """]]
- P = $v
- A = /
- V = ?i ?p
- K = /
- T = (?i $v ?p “abc”@<&dt>)
- H = /
- l = -
- N = /
2.4
?
2.5
These are two queries, but they mean exactly the same thing.
[[!format n3 """ PREFIX foaf: http://xmlns.com/foaf/0.1/ SELECT ( CONCAT(?G, " ", ?S) AS ?name ) WHERE { ?P foaf:givenName ?G ; foaf:surname ?S }
PREFIX foaf: http://xmlns.com/foaf/0.1/ SELECT ?name WHERE { ?P foaf:givenName ?G ; foaf:surname ?S BIND(CONCAT(?G, " “, ?S) AS ?name) }”""]]
- P = $name
- A = /
- V = ?i ?j ?P ?G ?S
- K = /
- T = AND{(?i ?P foaf:givenName ?G), (?j ?P foaf:surname ?S), ($name = concat(?G, " ", ?S))}
- H = /
- l = en
- N = foaf:<&foaf>
2.6
[[!format n3 """ PREFIX foaf: http://xmlns.com/foaf/0.1/ PREFIX org: http://example.com/ns#
CONSTRUCT { ?x foaf:name ?name } WHERE { ?x org:employeeName ?name } """]]
- P = ($x foaf:name $name)
- A = /
- V = ?i
- K = /
- T = (?i $x world:personName $name)
- H = /
- l = en
- N = foaf:<&foaf> world:<&world>
3.1
Doing just the first one. The second is the same, but with a case-insensitive regex.
[[!format n3 """ PREFIX dc: http://purl.org/dc/elements/1.1/ SELECT ?title WHERE { ?x dc:title ?title FILTER regex(?title, “^SPARQL”) }
PREFIX dc: http://purl.org/dc/elements/1.1/ SELECT ?title WHERE { ?x dc:title ?title FILTER regex(?title, “web”, “i” ) } """]]
- P = ($title)
- A = /
- V = ?i ?x
- K = <&matches_regex>
- T = AND{(?i ?x dc:title title), (title <&matches_regex> “^SPARQL”)}
- H = /
- l = en
- N = dc:<&dc>
3.2
[[!format n3 """ PREFIX dc: http://purl.org/dc/elements/1.1/ PREFIX ns: http://example.org/ns# SELECT ?title ?price WHERE { ?x ns:price ?price . FILTER (?price < 30.5) ?x dc:title ?title . } """]]
- P = ($title, $size)
- A = /
- V = ?i ?j ?x
- K = /
- T = AND{(?i ?x ns:size size), (size < 30.5), (?j ?x dc:title $title)}
- H = /
- l = en
- N = dc:<&dc> ns:<&ns>
4.2.3
[[!format n3 """ (1 ?x 3 4) :p “w” .
(1 [:p :q] ( 2 ) ) . """]]
This is just syntactic sugar for RDF collections, but these are simply encoded in triples so no new model features are introduced by them.
6.1
[[!format n3 """ PREFIX foaf: http://xmlns.com/foaf/0.1/ SELECT ?name ?mbox WHERE { ?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox } } """]]
- P = $name
- A = %mbox
- R = ($name %mbox)
- V = ?i ?j ?x
- K = /
- T = AND{(?i ?x foaf:name $name), (?j ?x foaf:mbox %mbox)}
- H = /
- l = en
- N = foaf:<&foaf>
6.2
[[!format n3 """ PREFIX dc: http://purl.org/dc/elements/1.1/ PREFIX ns: http://example.org/ns# SELECT ?title ?price WHERE { ?x dc:title ?title . OPTIONAL { ?x ns:price ?price . FILTER (?price < 30) } } """]]
- P = $title
- A = %size
- R = ($title %size)
- V = ?i ?j ?x
- K = /
- T = AND{(?i ?x dc:title $title), (?j ?x ns:size %size), (%size
<
30)} - H = /
- l = en
- N = dc:<&dc> ns:<&ns>
6.3
[[!format n3 """ PREFIX foaf: http://xmlns.com/foaf/0.1/ SELECT ?name ?mbox ?hpage WHERE { ?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox } . OPTIONAL { ?x foaf:homepage ?hpage } } """]]
- P = $name
- A = %mbox %hpage
- R = ($name %mbox %hpage)
- V = ?i ?j ?k ?x
- K = /
- T = AND{(?i ?x foaf:name $name), (?j ?x foaf:mbox %mbox), (?k ?x foaf:homepage %hpage)}
- H = /
- l = en
- N = foaf:<&foaf>
7
[[!format n3 """ PREFIX dc10: http://purl.org/dc/elements/1.0/ PREFIX dc11: http://purl.org/dc/elements/1.1/ SELECT ?title WHERE { { ?book dc10:title ?title } UNION { ?book dc11:title ?title } }
PREFIX dc10: http://purl.org/dc/elements/1.0/ PREFIX dc11: http://purl.org/dc/elements/1.1/ SELECT ?x ?y WHERE { { ?book dc10:title ?x } UNION { ?book dc11:title ?y } }
PREFIX dc10: http://purl.org/dc/elements/1.0/ PREFIX dc11: http://purl.org/dc/elements/1.1/ SELECT ?title ?author WHERE { { ?book dc10:title ?title . ?book dc10:creator ?author } UNION { ?book dc11:title ?title . ?book dc11:creator ?author } } """]]
- P = $title
- A = /
- R = ($title)
- V = ?i ?book
- K = /
- T = OR{(?i ?book dc10:title $title), (?i ?book dc11:title $title)}
- H = /
- l = en
N = dc10:<&dc10> dc11:<&dc11>
- P = /
- A = %x %y
- R = (%x %y)
- V = ?i ?book
- K = /
- T = AND{(?i ?book dc10:title %x), (?i ?book dc11:title %y)}
- H = /
- l = en
N = dc10:<&dc10> dc11:<&dc11>
- P = $title $author
- A = /
- R = ($title $author)
- V = ?i ?j ?book
- K = /
- T = OR{ AND{(?i ?book dc10:title $title), (?j ?book dc10:creator $author)}, AND{(?i ?book dc11:title $title), (?j ?book dc11:creator $author)} }
- H = /
- l = en
N = dc10:<&dc10> dc11:<&dc11>
8.1.1
[[!format n3 """ PREFIX rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns# PREFIX foaf: http://xmlns.com/foaf/0.1/
SELECT ?person WHERE { ?person rdf:type foaf:Person . FILTER NOT EXISTS { ?person foaf:name ?name } } """]]
Discussed in [[design3]].
8.1.2
[[!format n3 """ PREFIX rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns# PREFIX foaf: http://xmlns.com/foaf/0.1/
SELECT ?person WHERE { ?person rdf:type foaf:Person . FILTER EXISTS { ?person foaf:name ?name } } """]]
Discussed in [[design3]].
8.2
[[!format n3 """ PREFIX : http://example/ PREFIX foaf: http://xmlns.com/foaf/0.1/
SELECT DISTINCT ?s WHERE { ?s ?p ?o . MINUS { ?s foaf:givenName “Bob” . } } """]]
“Distinct” is trivial to specify. “Minus” can be modeled using other things in this case, but let’s check the SPARQL spec and see how it works. Hmmm… yes, it looks like I can do the same with other features I already have. I’m adding a distinct flag.
- P = $s
- A = /
- R = ($s)
- V = ?i ?p ?o
- K = /
- T = AND{(?i $s ?p ?o), OR{(?p != foaf:givenName), (?o != “Bob”)}}
- H = /
- l = en
- N = foaf:<&foaf>
- d = true
8.3.1
[[!format n3 """ SELECT * { ?s ?p ?o FILTER NOT EXISTS { ?x ?y ?z } }
SELECT * { ?s ?p ?o MINUS { ?x ?y ?z } } """]]
- P = $s $p $o
- A = /
- R = ($s $p $o)
- V = ?i
- W = ^j ^x ^y ^z
- K = /
- T = AND{(?i $s $p $o), (^j ^x ^y ^z)}
- H = /
- l = -
- N = /
- d = false
8.3.2
TODO continue here after design 4
[[TODO|TODO/OPEN]] continue here to next example