SPARQL Extensions Reference
Note that you can disable all extensions in the SPARQL parser by setting ParseMode = Compatability on construction.

Table of Contents


Update Commands


Syntax

INSERT INTO <graph> {graph pattern} [FROM <graph>] [WHERE {...}]
DELETE FROM <graph> {graph pattern} [FROM <graph>] [WHERE {...}]

Description

The INSERT and DELETE commands provide a flexible way to add and remove triples to and from a graph while continuing to provide full functionality of the SPARQL standard.

Examples

INSERT INTO <http://www.intellidimension.com/people#> {?s ?p ?o}
FROM <http://www.intellidimension.com/jbarron>
WHERE {?s ?p ?o}
 
DELETE FROM <http://www.intellidimension.com/people#> {?s ?p ?o}
FROM <http://www.intellidimension.com/people#>
WHERE {?s ?p ?o}

Back to Top


Functions Outside of Filters


Syntax

SELECT ... :functionName(variable) variables ... WHERE {...}
SELECT ... WHERE {variable :functionName(variable) ...}

Description

Custom functions may be used as variables within a select list or triple pattern.

Examples

SELECT :myFunction(?s) ?s WHERE {?s ?p ?o}
 
SELECT * WHERE {?s ?p ?o ; ?q :myFunction(?o)}

Back to Top


Aggregation & Grouping


Syntax

SELECT ... :aggregationFunctionName(variable) ... WHERE {...} GROUP BY variable

Description

An aggregate function will return a single value based on a collection of other values. Available aggregate functions are: count(), min(), max(), and avg(). Note that these functions are only available client-side (with Semantics.SDK).

The GROUP BY clause, a well-known component of the SQL standard, can also be used in conjunction with an aggregate function in a SPARQL query, as seen in the example below.

Examples

SELECT :count(?o) WHERE {?s ?p ?o} GROUP BY ?p

Back to Top


Negation


Syntax

SELECT ... WHERE {... triplePattern NOT([variables]){triplePattern} ...}

Description

The SPARQL standard has been extended to support negation in a WHERE clause.

Examples

SELECT * WHERE {?s ?p ?o NOT(?s){?s rdf:type rdfs:Class}}

Back to Top


Quantification


Syntax

SELECT ... WHERE {triplePattern FORALL(variable){triplePattern}}

Description

The SPARQL standard has been extended to support quantification in a WHERE clause.

Examples

SELECT * WHERE {?s ?p ?o FORALL(?s){?s rdf:type rdfs:Class}}

Back to Top


Rules


Syntax

WITH (
  [CONSTRUCT {...} WHERE {...}]
  [SELECT ... WHERE{...}] )
SELECT ... WHERE {...}

Description

The WITH clause provides support for LP-style inference rules within a SPARQL query.

Examples

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns>
PREFIX foaf: <http://www.xmlns.com/foaf/0.1/>
WITH (
  ## triple rules
  CONSTRUCT {?s rdf:type foaf:Person}
  WHERE {?s foaf:knows ?o}

  ##relations
  SELECT ?personId :count(?s) AS ?ct
  WHERE {?s foaf:knows ?personId.}
  GROUP BY ?personId AS :friends
)

SELECT * FROM <http://www.intellidimension.com/people>
WHERE {?s rdf:type foaf:Person. :friends(?s, ?ct)
FILTER(?ct >= 5)}

Back to Top


Relations


Syntax

WITH
(
  SELECT ... WHERE {...} [GROUP BY ...] AS relationName
)
SELECT ... FROM <graph> where {... :relationName(...) [FILTER(...)]}

Description

Custom relations can be defined as functional-style inference rules and used within a WHERE clause of a SPARQL query.

Examples

WITH
(
  ## define a friends relation
  SELECT ?personId :count(?s) AS ?ct
  WHERE {?s foaf:knows ?personId.}
  GROUP BY ?personId
  AS :friends
)
##use the friends relation in a query (relations do not go inside filters)
SELECT *
FROM <http://www.intellidimension.com/people>
WHERE
{
  ?s rdf:type foaf:Person .
  :friends(?s, ?ct)
  FILTER(?ct >= 5)
}

Back to Top


Extension Functions

String Functions

Name Description
:strlen(s) Returns the length of a string value.
:ucase(s) Converts a string value to upper case.
:lcase(s) Converts a string value to lower case.
:trim(s) Removes white space from the beginning and end of a string value.
:ltrim(s) Removes white space from the beginning of a string value.
:rtrim(s) Removes white space from the end of a string value.
:substr(s, start[, length]) Extracts a portion of a string value based on a starting offset and a length.
:instr(s, frag) Finds the starting position of the first occurrence of fragment in a string value.
:concat(s1[, s2[, ...]]) Concatenates multiple string values.

SQL Functions

Name Description
:sql(sqlcmd) Allows execution of SQL statements within SPARQL queries.
Semantics.Server Only
:contains(frag) Exposes SQL server full-text functionality to SPARQL queries.
Semantics.Server Only
:between(v1, v2) Value-based range expression.
Semantics.Server Only

Aggregation Functions

Name Description
:min(v) Finds the minimum value of an expression.
Semantics.SDK Only
:max(v) Finds the maximum value of an expression.
Semantics.SDK Only
:avg(v) Finds the average value of an expression.
Semantics.SDK Only
:count(v) Counts all unique occurances of an expression.
Semantics.SDK Only

Misc Functions

Name Description
:makeuri(v1[,v2[,...]]) Converts key values into a URI or a URI into key values.
Semantics.Server Only
:newid([v1[,v2[,...]]]) Generates a new unique URI value.


Back to Top