Skip to content

Commit c42f877

Browse files
thomasdegroot18RunDevelopment
authored andcommitted
Added support for SPARQL language (#2033)
This adds support for the SPARQL language. https://www.w3.org/TR/sparql11-query/
1 parent 1aabcd1 commit c42f877

18 files changed

+1086
-4
lines changed

components.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
"owner": "Golmote"
119119
},
120120
"asciidoc": {
121-
"alias": "adoc",
121+
"alias": "adoc",
122122
"title": "AsciiDoc",
123123
"owner": "Golmote"
124124
},
@@ -883,6 +883,12 @@
883883
"require": "markup-templating",
884884
"owner": "Golmote"
885885
},
886+
"sparql": {
887+
"title": "SPARQL",
888+
"require": "turtle",
889+
"owner": "Triply-Dev",
890+
"alias": "rq"
891+
},
886892
"splunk-spl": {
887893
"title": "Splunk SPL",
888894
"owner": "RunDevelopment"

components/prism-sparql.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Prism.languages.sparql = Prism.languages.extend('turtle', {
2+
'variable': {
3+
pattern: /[?$]\w+/,
4+
greedy: true
5+
},
6+
'boolean': /\b(?:true|false)\b/i,
7+
}
8+
);
9+
10+
Prism.languages.insertBefore('sparql', 'punctuation', {
11+
'keyword': [
12+
/\b(?:A|ADD|ALL|AS|ASC|ASK|BNODE|BY|CLEAR|CONSTRUCT|COPY|CREATE|DATA|DEFAULT|DELETE|DESC|DESCRIBE|DISTINCT|DROP|EXISTS|FILTER|FROM|GROUP|HAVING|INSERT|INTO|LIMIT|LOAD|MINUS|MOVE|NAMED|NOT|NOW|OFFSET|OPTIONAL|ORDER|RAND|REDUCED|SELECT|SEPARATOR|SERVICE|SILENT|STRUUID|UNION|USING|UUID|VALUES|WHERE)\b/i,
13+
/\b(?:ABS|AVG|BIND|BOUND|CEIL|COALESCE|CONCAT|CONTAINS|COUNT|DATATYPE|DAY|ENCODE_FOR_URI|FLOOR|GROUP_CONCAT|HOURS|IF|IRI|isBLANK|isIRI|isLITERAL|isNUMERIC|isURI|LANG|LANGMATCHES|LCASE|MAX|MD5|MIN|MINUTES|MONTH|ROUND|REGEX|REPLACE|sameTerm|SAMPLE|SECONDS|SHA1|SHA256|SHA384|SHA512|STR|STRAFTER|STRBEFORE|STRDT|STRENDS|STRLANG|STRLEN|STRSTARTS|SUBSTR|SUM|TIMEZONE|TZ|UCASE|URI|YEAR)\b(?=\s*\()/i,
14+
/\b(?:GRAPH|BASE|PREFIX)\b/i
15+
]
16+
});
17+
18+
Prism.languages.rq = Prism.languages.sparql;

components/prism-sparql.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-sparql.html

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
<h2>Introduction</h2>
2+
The queries shown here can be found in the SPARQL specifications:
3+
<a href="https://www.w3.org/TR/sparql11-query/">https://www.w3.org/TR/sparql11-query/</a>
4+
5+
<h2>query 2.1.6 Examples of Query Syntax</h2>
6+
7+
<pre><code>PREFIX dc: &lt;http://purl.org/dc/elements/1.1/&gt;
8+
SELECT ?title
9+
WHERE { &lt;http://example.org/book/book1&gt; dc:title ?title }
10+
</code></pre>
11+
12+
<h2>query 2.1.6-q1 Examples of Query Syntax</h2>
13+
14+
<pre><code>PREFIX dc: &lt;http://purl.org/dc/elements/1.1/&gt;
15+
PREFIX : &lt;http://example.org/book/&gt;
16+
SELECT $title
17+
WHERE { :book1 dc:title $title }
18+
</code></pre>
19+
20+
<h2>query 2.1.6-q2 Examples of Query Syntax</h2>
21+
22+
<pre><code>BASE &lt;http://example.org/book/&gt;
23+
PREFIX dc: &lt;http://purl.org/dc/elements/1.1/&gt;
24+
SELECT $title
25+
WHERE { &lt;book1&gt; dc:title ?title }
26+
</code></pre>
27+
28+
<h2>query 2.5.3 Example of Basic Graph Pattern Matching</h2>
29+
30+
<pre><code>PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
31+
SELECT ?mbox
32+
WHERE
33+
{ ?x foaf:name "Johnny Lee Outlaw" .
34+
?x foaf:mbox ?mbox }
35+
</code></pre>
36+
37+
<h2>query 3.1.1 Matching Integers</h2>
38+
39+
<pre><code>SELECT ?v WHERE { ?v ?p 42 }</code></pre>
40+
41+
<h2>query 3.1.2 Matching Arbitrary Datatypes</h2>
42+
43+
<pre><code>SELECT ?v WHERE { ?v ?p "abc"^^&lt;http://example.org/datatype#specialDatatype&gt; }</code></pre>
44+
45+
<h2>query 3.1.3-q1 Matching Language Tags</h2>
46+
47+
<pre><code>SELECT ?x WHERE { ?x ?p "cat"@en }</code></pre>
48+
49+
<h2>query 3.2 Value Constraints</h2>
50+
51+
<pre><code>PREFIX dc: &lt;http://purl.org/dc/elements/1.1/&gt;
52+
PREFIX ns: &lt;http://example.org/ns#&gt;
53+
SELECT ?title ?price
54+
WHERE { ?x ns:price ?price .
55+
FILTER (?price &lt; 30) .
56+
?x dc:title ?title . }
57+
</code></pre>
58+
59+
<h2>query 5.5 Nested Optional Graph Patterns</h2>
60+
61+
<pre><code>PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
62+
PREFIX vcard: &lt;http://www.w3.org/2001/vcard-rdf/3.0#&gt;
63+
SELECT ?foafName ?mbox ?gname ?fname
64+
WHERE
65+
{ ?x foaf:name ?foafName .
66+
OPTIONAL { ?x foaf:mbox ?mbox } .
67+
OPTIONAL { ?x vcard:N ?vc .
68+
?vc vcard:Given ?gname .
69+
OPTIONAL { ?vc vcard:Family ?fname }
70+
}
71+
}
72+
</code></pre>
73+
74+
<h2>query 6.1-q2 Joining Patterns with UNION</h2>
75+
76+
<pre><code>PREFIX dc10: &lt;http://purl.org/dc/elements/1.1/&gt;
77+
PREFIX dc11: &lt;http://purl.org/dc/elements/1.0/&gt;
78+
79+
SELECT ?title ?author
80+
WHERE { { ?book dc10:title ?title . ?book dc10:creator ?author }
81+
UNION
82+
{ ?book dc11:title ?title . ?book dc11:creator ?author }
83+
}
84+
85+
</code></pre>
86+
87+
<h2>query 8.3 Restricting by Bound Variables</h2>
88+
89+
<pre><code>PREFIX data: &lt;http://example.org/foaf/&gt;
90+
PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
91+
PREFIX rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
92+
93+
SELECT ?mbox ?nick ?ppd
94+
WHERE
95+
{
96+
GRAPH data:aliceFoaf
97+
{
98+
?alice foaf:mbox &lt;mailto:alice@work.example&gt; ;
99+
foaf:knows ?whom .
100+
?whom foaf:mbox ?mbox ;
101+
rdfs:seeAlso ?ppd .
102+
?ppd a foaf:PersonalProfileDocument .
103+
} .
104+
GRAPH ?ppd
105+
{
106+
?w foaf:mbox ?mbox ;
107+
foaf:nick ?nick
108+
}
109+
}
110+
</code></pre>
111+
112+
<h2>query 9.3 Combining FROM and FROM NAMED</h2>
113+
114+
<pre><code>PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
115+
PREFIX dc: &lt;http://purl.org/dc/elements/1.1/&gt;
116+
117+
SELECT ?who ?g ?mbox
118+
FROM &lt;http://example.org/dft.ttl&gt;
119+
FROM NAMED &lt;http://example.org/alice&gt;
120+
FROM NAMED &lt;http://example.org/bob&gt;
121+
WHERE
122+
{
123+
?g dc:publisher ?who .
124+
GRAPH ?g { ?x foaf:mbox ?mbox }
125+
}
126+
</code></pre>
127+
128+
<h2>query 10.1.2 DISTINCT</h2>
129+
130+
<pre><code>
131+
PREFIX foaf: &lt;http://xmlns.com/foaf/0.1//&gt;
132+
SELECT DISTINCT ?name WHERE { ?x foaf:name ?name }
133+
</code></pre>
134+
135+
<h2>query 10.1.3-q2 ORDER BY</h2>
136+
137+
<pre><code>PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
138+
139+
SELECT ?name
140+
WHERE { ?x foaf:name ?name ; :empId ?emp }
141+
ORDER BY ?name DESC(?emp)
142+
</code></pre>
143+
144+
<h2>query 10.1.5 OFFSET</h2>
145+
146+
<pre><code>PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
147+
148+
SELECT ?name
149+
WHERE { ?x foaf:name ?name }
150+
ORDER BY ?name
151+
LIMIT 5
152+
OFFSET 10
153+
</code></pre>
154+
155+
<h2>query 10.3.1 Templates with Blank Nodes</h2>
156+
157+
<pre><code>PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
158+
PREFIX vcard: &lt;http://www.w3.org/2001/vcard-rdf/3.0#&gt;
159+
160+
CONSTRUCT { ?x vcard:N _:v .
161+
_:v vcard:givenName ?gname .
162+
_:v vcard:familyName ?fname }
163+
WHERE
164+
{
165+
{ ?x foaf:firstname ?gname } UNION { ?x foaf:givenname ?gname } .
166+
{ ?x foaf:surname ?fname } UNION { ?x foaf:family_name ?fname } .
167+
}
168+
</code></pre>
169+
170+
<h2>query 10.3.3 Solution Modifiers and CONSTRUCT</h2>
171+
172+
<pre><code>PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
173+
PREFIX site: &lt;http://example.org/stats#&gt;
174+
175+
CONSTRUCT { [] foaf:name ?name }
176+
WHERE
177+
{ [] foaf:name ?name ;
178+
site:hits ?hits .
179+
}
180+
ORDER BY desc(?hits)
181+
LIMIT 2</code></pre>
182+
183+
<h2>query 10.4.3 Descriptions of Resources</h2>
184+
185+
<pre><code>PREFIX ent: &lt;http://org.example.com/employees#&gt;
186+
DESCRIBE ?x WHERE { ?x ent:employeeId "1234" }
187+
</code></pre>
188+
189+
<h2>query 10.5-q1 Asking "yes or no" questions</h2>
190+
191+
<pre><code>PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
192+
ASK { ?x foaf:name "Alice" ;
193+
foaf:mbox &lt;mailto:alice@work.example&gt; }
194+
</code></pre>
195+
196+
<h2>query 11.4.1 bound</h2>
197+
198+
<pre><code>PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
199+
PREFIX dc: &lt;http://purl.org/dc/elements/1.1/&gt;
200+
PREFIX xsd: &lt;http://www.w3.org/2001/XMLSchema#&gt;
201+
SELECT ?name
202+
WHERE { ?x foaf:givenName ?givenName .
203+
OPTIONAL { ?x dc:date ?date } .
204+
FILTER ( bound(?date) ) }</code></pre>
205+
206+
<h2>query 11.4.3 isBlank</h2>
207+
208+
<pre><code>
209+
PREFIX a: &lt;http://www.w3.org/2000/10/annotation-ns#&gt;
210+
PREFIX dc: &lt;http://purl.org/dc/elements/1.1/&gt;
211+
PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
212+
213+
SELECT ?given ?family
214+
WHERE { ?annot a:annotates &lt;http://www.w3.org/TR/rdf-sparql-query/&gt; .
215+
?annot dc:creator ?c .
216+
OPTIONAL { ?c foaf:given ?given ; foaf:family ?family } .
217+
FILTER isBlank(?c)
218+
}</code></pre>
219+
220+
<h2>query 11.4.5 str</h2>
221+
222+
<pre><code>
223+
PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
224+
SELECT ?name ?mbox
225+
WHERE { ?x foaf:name ?name ;
226+
foaf:mbox ?mbox .
227+
FILTER regex(str(?mbox), "@work.example") }
228+
</code></pre>
229+
230+
<h2>query 11.4.7 datatype</h2>
231+
232+
<pre><code>
233+
PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
234+
PREFIX xsd: &lt;http://www.w3.org/2001/XMLSchema#&gt;
235+
PREFIX eg: &lt;http://biometrics.example/ns#&gt;
236+
SELECT ?name ?shoeSize
237+
WHERE { ?x foaf:name ?name ; eg:shoeSize ?shoeSize .
238+
FILTER ( datatype(?shoeSize) = xsd:integer ) }
239+
</code></pre>
240+
241+
<h2>query 11.4.10-q1 RDFterm-equal</h2>
242+
243+
<pre><code>
244+
PREFIX a: &lt;http://www.w3.org/2000/10/annotation-ns#&gt;
245+
PREFIX dc: &lt;http://purl.org/dc/elements/1.1/&gt;
246+
PREFIX xsd: &lt;http://www.w3.org/2001/XMLSchema#&gt;
247+
248+
SELECT ?annotates
249+
WHERE { ?annot a:annotates ?annotates .
250+
?annot dc:date ?date .
251+
FILTER ( ?date = xsd:dateTime("2004-01-01T00:00:00Z") || ?date = xsd:dateTime("2005-01-01T00:00:00Z") ) }
252+
</code></pre>
253+
254+
<h2>query 11.6-q1 Extensible Value Testing</h2>
255+
256+
<pre><code>
257+
PREFIX aGeo: &lt;http://example.org/geo#&gt;
258+
259+
SELECT ?neighbor
260+
WHERE { ?a aGeo:placeName "Grenoble" .
261+
?a aGeo:location ?axLoc .
262+
?a aGeo:location ?ayLoc .
263+
264+
?b aGeo:placeName ?neighbor .
265+
?b aGeo:location ?bxLoc .
266+
?b aGeo:location ?byLoc .
267+
268+
FILTER ( aGeo:distance(?axLoc, ?ayLoc, ?bxLoc, ?byLoc) &lt; 10 ) .
269+
}
270+
</code></pre>
271+
272+
The final example query is not based on the SPARQL 1.1 queries.
273+
274+
<h2>Full Example query</h2>
275+
276+
<pre><code>
277+
base &lt;http://example.org/geo#&gt;
278+
prefix geo: &lt;http://www.opengis.net/ont/geosparql#&gt;
279+
prefix rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
280+
select ?shape ?shapeColor ?shapeHeight ?shapeName (sample(?shapeLabel) as ?shapeLabel) {
281+
{
282+
select * {
283+
values (?placeName ?streetName) {
284+
"Grenoble" "Paul Mistral"
285+
}
286+
?place geo:NamePlace ?placeName.
287+
?pand geo:hasGeometry/geo:asWKT ?shape;
288+
}
289+
290+
}
291+
?pand geo:measuredHeight ?shapeHeight.
292+
# Only retrieve buildings larger then 10 meters.
293+
FILTER ( ?shapeHeight &lt; 10 ) .
294+
BIND(IF(!bound(?EindGeldigheid5), "#22b14c", "#ed1c24" ) AS?tColor)
295+
# tekst label
296+
bind(concat(str(?streetName),' ',str(?houseNumber),', ',str(?PlaceName)) as ?shapeName)
297+
bind("""Multi-line
298+
String Element
299+
""" as ?shapeLabel)
300+
}
301+
group by ?shape ?shapeColor ?shapeHeight ?shapeName
302+
limit 10
303+
</code></pre>

plugins/autoloader/prism-autoloader.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"smarty": "markup-templating",
101101
"solidity": "clike",
102102
"soy": "markup-templating",
103+
"sparql": "turtle",
103104
"swift": "clike",
104105
"tap": "yaml",
105106
"textile": "markup",
@@ -155,6 +156,7 @@
155156
"py": "python",
156157
"robot": "robot-framework",
157158
"rb": "ruby",
159+
"rq": "sparql",
158160
"trig": "turtle",
159161
"ts": "typescript",
160162
"t4": "t4-cs",

plugins/autoloader/prism-autoloader.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/show-language/prism-show-language.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@
127127
"shell-session": "Shell session",
128128
"solidity": "Solidity (Ethereum)",
129129
"soy": "Soy (Closure Template)",
130+
"sparql": "SPARQL",
131+
"rq": "SPARQL",
130132
"splunk-spl": "Splunk SPL",
131133
"sql": "SQL",
132134
"tap": "TAP",

0 commit comments

Comments
 (0)