SemWeb.Sparql : SemWeb.Query Namespace
SparqlEngine Class

A SPARQL query engine.

public class SparqlEngine : Query

Remarks

This SPARQL engine parses and evaluates a SPARQL language query against any SemWeb.SelectableSource or SemWeb.QueryableSource. If the queried data source is a SemWeb.QueryableSource, the Query method is used where possible. Otherwise, query execution falls back to calling SemWeb.SelectableSource's Contains and Select methods.

This class is a fork of the SPARQL engine library at http://sparql.sourceforge.net/ by Ryan Levering, version 0.8. The upstream library has been heavily modified, however.

This example runs a SELECT query on a data source and writes the results as XML to standard output.

C# Example
MemoryStore datamodel = new MemoryStore(new RdfXmlReader("rdfdata.rdf"));

string sparqlQuery = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
                           + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"
                           + "SELECT * WHERE { ?person rdf:type foaf:Agent . \n"
                           + "?person foaf:name ?name . }";

SparqlEngine query = new SparqlEngine(sparqlQuery);
query.Run(datamodel, Console.Out).  

Although this example sends the results to a stream, the results can be processed programmatically by passing a SemWeb.Query.QueryResultSink instead of a TextWriter.

Something must be said about the handling of FROM, FROM NAMED, and GRAPH clauses in queries. The FROM clause is used to specify the set of graphs that combined make up the default dataset, which is what triples in the query are normally (that is, outside of a GRAPH clause) matched against. FROM clauses are essentially ignored by the SPARQL engine. It is up to the caller to check if any FROM clauses are specified using SparqlEngine.GetDefaultDatasets and passing the right data source to the SPARQL engine as appropriate. Outside of a GRAPH clause, the triples in a query are compared against the data source with the SemWeb.Statement.Meta or SemWeb.SelectFilter.Meta field set to SemWeb.Statement.DefaultMeta, the default value when no meta or provenance information is associated with a statement.

Triples inside of a GRAPH clause with an explicit URI, such as "GRAPH <....uri...> { ...graph pattern... }", are matched against the data source by passing the URI specified in the graph clause as the value of SemWeb.Statement.Meta or as a one-element array in SemWeb.SelectFilter.Meta.

In GRAPH clauses with graph variables, such as "GRAPH ?g { ...graph pattern... }", the potential values of the graph variable can sometimes be inferred from previous statements referencing the graph variable that were already evaluated during the processing of the query. Failing that, the potential values of graph variables are drawn from the set of FROM NAMED graphs, if any were specified. If any potential values for the graph variable are known, they are passed together in the SemWeb.SelectFilter.Meta field in calls to Contains, Select, or (if there is just one such value) Query on the data source, or if there is more than one value then in a call to Query the SemWeb.Statement.Meta field is set to a new SemWeb.Variable and QueryOptions.VariableKnownValues is filled in with the potential values for the variable

In GRAPH clauses with graph variables whose values are not known at all, null is passed as the value of SemWeb.SelectFilter.Meta, or in a call to SemWeb.QueryableSource.Query(SemWeb.Statement[], QueryOptions, QueryResultSink) the SemWeb.Statement.Meta field is set to a new SemWeb.Variable.

Since, for instance, it is impossible to set the Meta field of statements in RDF/XML, it is recommended in order to process GRAPH clauses to implement your own SemWeb.SelectableSource to pass as the data source for queries that will inspect the Meta field and pass off calls to Contains and Select to the correct source graph.

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0

Members

See Also: Inherited members from Query.

Public Constructors

Initialized the SPARQL query from a TextReader.
Initializes the SPARQL query from a string.

Public Fields

AllowPersistBNodes bool . Documentation for this section has not yet been entered.

Public Properties

MimeType string . Gets or sets the preferred MIME type for the output of the query. (Inherited from Query.)
QueryMeta SemWeb.Entity . A filter on the Meta value of statements in the target graph considered by the query. (Inherited from Query.)
ReturnLimit int . The number of bindings to return. (Inherited from Query.)
ReturnStart int . The index of the first binding to return. (Inherited from Query.)
[read-only]
Type SparqlEngine.QueryType . Gets the type of the query.

Public Methods

AddExternalFunction (RdfFunction)
Registers an external function that can be used in FILTER clauses.
Ask (SemWeb.SelectableSource) : bool
Executes an ASK query on the given data source and returns the result of the query as a boolean.
Ask (SemWeb.SelectableSource, System.IO.TextWriter)
Executes an ASK query on the given data source and outputs the result of the query in the SPARQL results format to a TextWriter.
Construct (SemWeb.SelectableSource, SemWeb.StatementSink)
Executes a CONSTRUCT query on the given data source and sends the constructed statements to a StatementSink.
Construct (SemWeb.SelectableSource, System.IO.TextWriter)
Executes a CONSTRUCT query on the given data source and outputs the result of the query in the SPARQL results format to a TextWriter.
Describe (SemWeb.SelectableSource, SemWeb.StatementSink)
Executes a DESCRIBE query on the given data source and sends the constructed statements to a StatementSink.
Describe (SemWeb.SelectableSource, System.IO.TextWriter)
Executes a DESCRIBE query on the given data source and outputs the result of the query in the SPARQL results format to a TextWriter.
GetDefaultDatasets () : string[]
Documentation for this section has not yet been entered.
abstract
GetExplanation () : string
Returns a textual explanation of the query. (Inherited from Query.)
GetQueryPrefixes () : SemWeb.NamespaceManager
Gets a NamespaceManager containing all of the PREFIX definitions used in the query.
abstract
Run (SemWeb.SelectableSource, QueryResultSink)
Runs the query on a data source, outputting to a QueryResultSink. (Inherited from Query.)
Run (SemWeb.SelectableSource, System.IO.TextWriter)
Runs the query on a data source, outputting to a TextWriter. (Inherited from Query.)
Select (SemWeb.SelectableSource, QueryResultSink)
Executes a SELECT query on the given data source and sends the resulting variable bindings to a QueryResultSink.
Select (SemWeb.SelectableSource, System.IO.TextWriter)
Executes a SELECT query on the given data source and outputs the result of the query in the SPARQL results format to a TextWriter.

Member Details

SparqlEngine Constructor

Initialized the SPARQL query from a TextReader.

public SparqlEngine (System.IO.TextReader query)

Parameters

query
A TextReader containing the SPARQL query.

Remarks

None.

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0

SparqlEngine Constructor

Initializes the SPARQL query from a string.

public SparqlEngine (string query)

Parameters

query
The SPARQL query.

Remarks

None.

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0

AddExternalFunction Method

Registers an external function that can be used in FILTER clauses.

public void AddExternalFunction (RdfFunction function)

Parameters

function
An object implementing an external function.

Remarks

None.

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0

AllowPersistBNodes Field

Documentation for this section has not yet been entered.

public bool AllowPersistBNodes

Remarks

Documentation for this section has not yet been entered.

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0

Ask Method

Executes an ASK query on the given data source and returns the result of the query as a boolean.

public bool Ask (SemWeb.SelectableSource source)

Parameters

source
The data source to query.

Returns

A boolean value giving the result of the ASK query.

Remarks

None.

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0

Ask Method

Executes an ASK query on the given data source and outputs the result of the query in the SPARQL results format to a TextWriter.

Parameters

source
The data source to query.
output
The System.IO.TextWriter to output the results to in the SPARQL Results XML output format.

Remarks

This method can also be called through Query.Run(SemWeb.SelectableSource, System.IO.TextWriter).

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0

Construct Method

Executes a CONSTRUCT query on the given data source and sends the constructed statements to a StatementSink.

public void Construct (SemWeb.SelectableSource source, SemWeb.StatementSink sink)

Parameters

source
The data source to query.
sink
The SemWeb.StatementSink to send the constructed statements to.

Remarks

None.

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0

Construct Method

Executes a CONSTRUCT query on the given data source and outputs the result of the query in the SPARQL results format to a TextWriter.

public void Construct (SemWeb.SelectableSource source, System.IO.TextWriter output)

Parameters

source
The data source to query.
output
The System.IO.TextWriter to output the results to in the SPARQL Results XML output format.

Remarks

This method can also be called through Query.Run(SemWeb.SelectableSource, System.IO.TextWriter).

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0

Describe Method

Executes a DESCRIBE query on the given data source and sends the constructed statements to a StatementSink.

public void Describe (SemWeb.SelectableSource source, SemWeb.StatementSink sink)

Parameters

source
The data source to query.
sink
The SemWeb.StatementSink to send the constructed statements to.

Remarks

None.

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0

Describe Method

Executes a DESCRIBE query on the given data source and outputs the result of the query in the SPARQL results format to a TextWriter.

public void Describe (SemWeb.SelectableSource source, System.IO.TextWriter output)

Parameters

source
The data source to query.
output
The System.IO.TextWriter to output the results to in the SPARQL Results XML output format.

Remarks

This method can also be called through Query.Run(SemWeb.SelectableSource, System.IO.TextWriter).

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0

GetDefaultDatasets Method

Documentation for this section has not yet been entered.

public string[] GetDefaultDatasets ()

Returns

Documentation for this section has not yet been entered.

Remarks

Documentation for this section has not yet been entered.

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0

GetQueryPrefixes Method

Gets a NamespaceManager containing all of the PREFIX definitions used in the query.

public SemWeb.NamespaceManager GetQueryPrefixes ()

Returns

A NamespaceManager.

Remarks

None.

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0

Select Method

Executes a SELECT query on the given data source and sends the resulting variable bindings to a QueryResultSink.

public void Select (SemWeb.SelectableSource source, QueryResultSink sink)

Parameters

source
The data source to query.
sink
The SemWeb.Query.QueryResultSink to send the resulting variable bindings to.

Remarks

None.

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0

Select Method

Executes a SELECT query on the given data source and outputs the result of the query in the SPARQL results format to a TextWriter.

public void Select (SemWeb.SelectableSource source, System.IO.TextWriter output)

Parameters

source
The data source to query.
output
The System.IO.TextWriter to output the results to in the SPARQL Results XML output format.

Remarks

This method can also be called through Query.Run(SemWeb.SelectableSource, System.IO.TextWriter).

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0

Type Property

Gets the type of the query.

public SparqlEngine.QueryType Type { get; }

Value

The type of the query: SELECT, DESCRIBE, CONSTRUCT, or ASK.

Remarks

None.

Requirements

Namespace: SemWeb.Query
Assembly: SemWeb.Sparql (in SemWeb.Sparql.dll)
Assembly Versions: 0.0.0.0