The Intellidimension Semantics.SDK supports the SPARQL syntax for querying RDF data. This article will focus on using SPARQL to query a single in-memory graph and accessing the results.
The DataSource.Query Method
As mentioned in earlier articles, the abstract base class DataSource provides the interface to all sources of RDF data. All DataSource objects support a Query method with several overloads. The simplest overload takes a single string parameter that is the SPARQL query string to be executed on the DataSource object. The Query method returns a Table object that contains any results for the query.
SELECT Command
One of the most commonly used SPARQL commands is the SELECT command. When executing a SELECT command against a single DataSource object the name of the graph does not need to be included in the FROM clause of the command since it is implied, as shown below.
GraphDataSource g = new GraphDataSource();
Table results = g.Query(@"
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?title WHERE
{<http://example.org/book1> dc:title ?title}");
Per the SPARQL specification, the graph pattern is matched against the statements in the DataSource object. The variables in the select list are bound as specified in the graph pattern and returned as results.
Query Results
SPARQL commands such as the SELECT command return query results in a Table object. The Table object has a row for each distinct set of column values. The column values are defined in the select list of the SELECT command. Below is an example of how to iterate over the results of a SELECT command.
for (int i = 0; i < results.RowCount; i++)
RdfLiteral title = results[i][0] as RdfLiteral;
The Semantics.SDK provides support for the XML serialization of query results using the SPARQL result syntax, as shown below.
SparqlXmlFormatter fmt = new SparqlXmlFormatter(
results, typeof(CommandSelect));
fmt.Write(stream);
Parameterized Queries
SPARQL queries can be parameterized using the Semantics.SDK via the QueryParameters object. The QueryParameter object is used to bind a RdfValue object to a named parameter that is specified in the query using the @ character followed by any valid variable name. A query parameter can be used anywhere that a query variable can be specified. Below shows an example of the use of a query parameter in a SPARQL SELECT command.
QueryParameters qp = new QueryParameters();
qp.Add("id", new RdfUri("http://example.org/"));
g.Query(@"SELECT ?s ?p ?o WHERE
{?s ?p ?o. filter(?s=@id)}", qp);
Conclusion
This article provided an introduction to the objects and methods used to execute a SPARQL query and access the results using the Semantics.SDK. However this article did not provide much insight into the Semantics.SDK support for all the SPARQL commands and extensions. The Semantics.SDK supports a variety of SPARQL commands such as: SELECT, DESCRIBE, CONSTRUCT, ASK, INSERT, and DELETE. In addition, it provides support for inference rules and a variety of extension functions. These will all be discussed in future articles.
0 comments:
Post a Comment