XaLib  SPARQL Query Engine

Syntax of a Select  Query

Representation of  RDF Resources and literal values.

Resources

To enable resources to be identified in the query literal  they are represented with a distinct starting and ending delimiter. This same representation is also used to produce result values (but see the RDFResults clause);

Example:
    http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq
    is represented as:
    "<http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq>"

The default delimiters are "<" and ">". These may be changed for a given query using the startDelimiter/endDelimiter clauses.

Writing resources in full, can quickly become tedious, so SPARQL  has a mechanism for defining namespace prefixes.
Prefixes can be declared using the Prefix clause.

Example:
    Prefix: {rdf: "http://www.w3.org/1999/02/22-rdf-syntax-ns#" }
    enables the above resource to be represented as:
    "<rdf:Seq>"

Strings

Strings are represented as Javascript quoted strings (single, or double).

Integers

Integers are represented as Javascript integers.  Unfortunately the Mozilla RDF implementation does not include a Numeric datatype. Decimal numbers must be stored as strings.

Date

Dates are stored as Javascript Date objects.

Variable Declarations

The declaration of each variable referenced in the query: conforms to the following format:

var _variable = new Var("variableName");

A variable may also be given an initial value:

var _variable = new Var("variableName", "initialValue");

By convention each variable name begins with the underscore character "_". This prevents collision with Javascript reserved words.
When used within an element in the Where clause a variable is said to be bound or unbound.

Query Literal

The query object literal conforms to the following format:
q = {
? Prefix:{ PrefixList },
? startDelimiter:string,
? endDelimiter:string.
 ! Select:[ VariableList ],
? From: [ DataSourceList],
? With: [
DataSourceList ],
 ! Where: [ WhereClause ],
? Limit: integer,
? Distinct:true,
? RDFResults:true
};

Clauses may appear in any order. A leading "!" indicates a required clause and a leading "?" indicates an optional clause.

Prefix

The prefix clause contains a comma separated  list of prefix:namespace pairs. The prefix should be a valid Javascript name, and  must not be a Javascript reserved word. The namespace should be a valid  URI.
Examples
Prefix: {ext:"http://www.mozilla.org/2004/em-rdf#", dc:"http://purl.org/dc/elements/1.1/", moz:"urn:mozilla:package"}

startDelimiter / endDelimiter

The indicated string is used for encoding of resources into RDF format. This is not a SPARQL standard feature.

Select

The Select clause contains a comma separated list of output variables to be included in each result row.  Each variable referenced in the query must be declared as above.

From

The From clause contains a comma separated list of  RDF DataSource identifiers.  An RDF DataSource identifier can be
The combined datasources are used to provide data against which the base query is processed.
This clause may be omitted if a With clause is provided and the top level Where item is a Graph element.
This clause may also be specified as well as a With clause

With

The With clause contains a comma separated list of  RDF DataSource identifiers.  An RDF DataSource identifier can be
The With clause  provides a list of DataSources for use with the Graph element

Limit

Specify a limit on the maximum number of results rows to produce.

Distinct

Specify true to ensure that only unique rows are returned. The current implementation computes a hash value for each row and eliminates rows having duplicate hash values. The hash value has 15 digits so the chance of a false removal is small (assuming the hash is random).

RDFResults

By default the query results will contain resources encoded as string values (e.g. "<rdf:Seq>" and strings, integers and Dates represented by the corresponding Javascript types. If this clause is specified, then the results will remain as sub-interfaces of nsIRDFNode (nsIRDFResource, nsIRDFLiteral, nsIRDFInt, nsIRDFDate). In some cases, where the results are to be further processed against a data store, this format may be more efficient. This is not a SPARQL standard feature.

Where

The Where clause specifies the relationships which the results are to satisfy. The following elements may be nested to arbitrary degree:

Group( elementList )
Optional( elementList )
Union( elementList )
Graph( graphSelectorelementList )
Triple( subject, predicate, object)
Resources( subject )
ArcsOut( subject, predicate)
ArcsIn( subject, predicate)
In( literalList, variable )
Filter( operand1 operationName, operand2)
Fun( operand1, functionName, operand2, variable)
IsBound( variable )

Where elementList is a comma separated list of the above elements. The other terms are explained in the descriptions of the
specific elements.

Triple

Triple( subject, predicate, object)
A Triple matches one or more  facts (triples) from the current datastore. Any unbound variable acts as a wild card so that successive
values are returned for  each fact which satisfies the criteria.

Examples:
Using variables, Triples can be chained together in a manner similar to joins in SQL

Group

Group( elementList )

Each element in elementList  is processed, in turn. If all the elements succeed then the group succeeds. If any element fails to be satisfied. the previous element is then asked to produce another result (backtrack). If this succeeds then the following element is tried again with the new value.
Ultimately the group either succeeds or fails. A group can succeed multiple times with the variables bound to different values.

Optional

A group which succeeds at least once.

Optional( elementList )

Each element in elementList  is processed as for a Group element..
Ultimately, the Optional element either succeeds or fails. If it does not succeed at least once,  then for a single time it pretends to have succeeded., and the query continues. Any variables within the Optional element which were not bound on entry,  will remain unbound.

Union

Union( elementList )

Each element in elementList  is processed in turn. If any element fails processing continues with the following element.

Graph

The graph element allows for the query to access other DataStores while the query is in progress. Access may be from a fixed list (With clause) or from data produced by preceding parts of the query.

Graph( graphSelectorelementList )

Resources

Access to all the resources in the currently active DataStore. This is not a SPARQL standard feature.

Resources( subject )


If the subject is bound  this serves as an existence check. Warning: this can be a slow operation for large DataStores.

ArcsOut

Access all predicates which are connected from the subject to some (unknown) object. This is not a SPARQL standard feature.

ArcsOut( subject, predicate)

ArcIn


Access all predicates which are connected from the object to some (unknown) object. This is not a SPARQL standard feature.

ArcsIn( object, predicate)

In

The In function ca be used to check whether a Variable has one of the values in a list of  literals, or  can be used to
successively bind the variable to each value in turn.This is not a SPARQL standard feature.

In(literalList, variable )
Example 1:
    ... Where [ In("<NS:listbox>", "<NS:tree>",_X), Triple(_X, _Predicate,  _Object) ]
     List all facts for resources <NS:listbox> and <NS:tree> 

Example 2:
    ... Where [ Triple(_X, _Predicate,  _Object) , In("<NS:listbox>", "<NS:tree>",_X)]
     List all facts for resources <NS:listbox> and <NS:tree>. in this case much less efficient as it acts as a Filter for a potentially
    large set of facts.
 

Filter

 Test whether one operand  bears the specified relationship to another operand. If the Filter succeeds then further elements are evaluated, eventually leading to a result. If the Filter fails then processing backtracks to look for another solution. This implements a SPARQL feature in a way more consistent with use in a Javascript object literal.

Filter( operand1 operationName, operand2)

Operation
Description
==
Equality.  Numbers  are not  comparable with  strings.
<
Less than. 
<=
Less than or equal.
>
Greater than
>=
Greater than or equal
!=
Not equal
=~
Matches operand2 which must be a Javascript Regular Expression literal.
!~
Does not Match operand2 which must be a Javascript Regular Expression literal.

Fun

Compute a value. This implements a SPARQL feature in a way more consistent with use in a Javascript object literal.

Fun( operand1 functionName, operand2, variable)

Function
Description
+
Operand1 is added to operand2. Both operands must be integers or strings containing integers, otherwise NaN  is returned.
-
Operand2 is subtracted from operand1. Both operands must be integers or strings containing integers, otherwise NaN  is returned.
*
Operand1 is multiplied by operand2. Both operands must be integers or strings containing integers, otherwise NaN  is returned.
/
Operand1 is divided by operand2 Both operands must be integers or strings containing integers, otherwise NaN  is returned. The result is rounded to the nearest integer. If operand2 is zero then Inf is returned.
%
The remainder (modulus) of operand1 divided by operand2. Both operands must be integers or strings containing integers, otherwise NaN  is returned. If operand2 is zero then Inf is returned.
match
Matches operand2 which must be a Javascript Regular Expression literal. Parentheses within the RegExp determine
the value of  result.
concat
Operand1 is concatenated with operand2


IsBound

Check whether a variable has a bound value. Elements following  an Optional or Union element  may contain variables whose
bound status is not know until the query is run.

IsBound( variable )