Semantics.Server Developer Guide

Semantics.Server 1.0 is a powerful new component included in RDF Gateway 3.0. Semantics.Server provides scalable RDF storage, query and inference capabilities to Microsoft SQL Server 2005® and Microsoft SQL Server 2008®. Semantics.Server is used by the RDF Gateway Server to handle all of the storage, query and inference requirements for RDF Gateway 3.0. However Semantics.Server can be used without RDF Gateway Server by accessing its capabilities directly through T-SQL. Semantics.Server is a set of assemblies developed in the .NET Framework that provide a suite of stored procedures, functions and data types for integrating semantics into applications based on Microsoft SQL Server®. This developer guide will focus on how to access and utilize these capabilities.

Installation

Semantics.Server 1.0 requires that a database on SQL Server® has been properly initialized to contain the tables, stored procedures, functions and custom data type Semantics.Server requires for the storing, querying and inferencing on RDF data. The RDF Gateway Configuration Utility program will perform this action but requires a database connection with administrative privileges. Please refer to the RDF Gateway installation guide for more information on initializing a database for use with Semantics.Server.

Executing Commands

Semantics.Server 1.0 runs within Microsoft SQL Server® therefore it's functionality can be accessed using any tool capable of issuing commands to SQL Server®. For the scope of this document it will be assumed that all commands are issued using Microsoft SQL Server Management Studio.

Creating A Named Graph

A collection of RDF statements is refered to a graph. A graph that is referenced via a unique URI is known as a "named graph". In Sematics.Server the stored procedure sw_CreateGraph is used to create a named graph. This procedure has a single required string parameter for the URI of the graph.

EXEC sw_CreateGraph 'http://www.intellidimension.com/resources/graph1';

Adding Statements To A Named Graph

Statements are added to a named graph by contructing a simple relational view onto the graph using the stored procedure sw_CreateGraphView. This stored procedure takes two parameters: the URI of the named graph and the name for the relational view.

EXEC sw_CreateGraphView 'http://www.intellidimension.com/resources/graph1', 'graph1_statements';

In the example above a relational view graph1_statements is created. Once the relational view is created then statements can be inserted using a SQL INSERT statement. The relational view has a column for the subject (s), predicate (p) and object (o) values, respectively, for each statement. All values are inserted as SQL strings using ntriples notation. The example below shows how to insert statements using T-SQL.

INSERT INTO graph1_statements VALUES
('<http://www.intellidimension.com/resources/semantics-server>', 
'<http://www.w3.org/2000/01/rdf-schema#label>',
'"Semantics.Server"');
INSERT INTO graph1_statements VALUES
('<http://www.intellidimension.com/resources/semantics-server>', 
'<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>',
'<http://www.intellidimension.com/resources/SoftwareProduct>');
INSERT INTO graph1_statements VALUES
('<http://www.intellidimension.com/resources/semantics-server>', 
'<http://www.intellidimension.com/resources/SoftwareVersion>',
'"1.0"');
INSERT INTO graph1_statements VALUES
('<http://www.intellidimension.com/resources/semantics-server>', 
'<http://www.intellidimension.com/resources/ReleaseDate>',
'"2008-06-15"^^<http://www.w3.org/2001/XMLSchema#date>');

RDF Values via User-Defined Type

Pay close attention to the use of the ntriples syntax within the SQL strings for specifying RDF URI and literal values. Semantics.Server uses a user-defined type (RdfValue) implemented using the .NET Framework to represent RDF values in SQL Server®. This user-defined type handles the conversion of SQL strings to RDF values (URIs, blank nodes, literals and typed literals) by parsing the ntriples notation into an internal binary representation. User-defined types can be returned in query results and converted back into the appropiate .NET object when using a data access framework such as ADO.NET. SQL Server® can also automatically convert between a string value and user-defined type when executing queries or returning results. This allows a string representation of an RDF value, using ntriples notation, to be used in almost any SQL command that requires an RDF value.

For more information about ADO.NET and using RdfValue in a client application refer to the Semantics.SDK.

Querying A Graph Using SPARQL

SPARQL is the W3C standard for querying RDF data. Semantics.Server allows a SPARQL query to executed from within T-SQL. This is accomplished through the use of the stored procedure sw_Sparql. The procedure takes two parameters: (1) the SPARQL query as a SQL string and a integer flag to indicate how to the return the result values. If the second parameter is non-zero the values are return as string data type otherwise they are return as a user-defined type RdfValue.

The example below uses SPARQL to query for all the statements in the named graph and returns the values strings.

EXEC sw_Sparql 'SELECT * FROM <http://www.intellidimension.com/resources/graph1> WHERE {?s ?p ?o}', 1;

The results of the SPARQL query are shown below. Note that the string values are encoded using the ntriples syntax.

s                                                               p                                                               o
--------------------------------------------------------------- --------------------------------------------------------------- ----------------------------------------------------------------
<http://www.intellidimension.com/resources/semantics-server>    <http://www.w3.org/2000/01/rdf-schema#label>                    "Semantics.Server"
<http://www.intellidimension.com/resources/semantics-server>    <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>               <http://www.intellidimension.com/resources/SoftwareProduct>
<http://www.intellidimension.com/resources/semantics-server>    <http://www.intellidimension.com/resources/SoftwareVersion>     "1.0"
<http://www.intellidimension.com/resources/semantics-server>    <http://www.intellidimension.com/resources/ReleaseDate>         "2008-06-15"^^<http://www.w3.org/2001/XMLSchema#date>

Removing Statements From A Named Graph

One method for removing statements from a graph is via a simple SQL DELETE command executed on the SQL view that was created using the procedure sw_CreateView. This method works well for relatively simple queries such as deleting one or more statements on a specific resource like in the example show below.

DELETE FROM graph1_statements WHERE 
s = '<http://www.intellidimension.com/resources/semantics-server>' AND 
p = '<http://www.intellidimension.com/resources/ReleaseDate>';

Using SPARQL Extensions: INSERT And DELETE

Semantics.Server supports several extensions to the SPARQL specification such as the INSERT and DELETE commands. This makes it possible to perform more complex graph matching operations when inserting and deleting statements from a named graph. The example below demonstrates their use.

EXEC sw_Sparql '
PREFIX itd: <http://www.intellidimension.com/resources/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
INSERT INTO <http://www.intellidimension.com/resources/graph1> 
{itd:semantics-server rdfs:comment "Cool semantics for SQL"} WHERE {}';

EXEC sw_Sparql '
PREFIX itd: <http://www.intellidimension.com/resources/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
DELETE FROM <http://www.intellidimension.com/resources/graph1> 
{itd:semantics-server rdfs:comment "Cool semantics for SQL"} WHERE {}';

Adding Inference Rules to a SPARQL query

Semantics.Server implements inference rules in SPARQL via an extension to the CONSTRUCT command. In the following example a simple inference rule ("All software products are products") is added to a query.

EXEC sw_Sparql '
PREFIX itd: <http://www.intellidimension.com/resources/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
WITH (
CONSTRUCT {?s rdf:type itd:Product} WHERE {?s rdf:type itd:SoftwareProduct}
)
SELECT ?s FROM <http://www.intellidimension.com/resources/graph1> 
WHERE {?s rdf:type itd:Product}', 1;

Incremental Inferencing Via A Derived Graph

Semantics.Server can create a derived graph that maintains a set of inferences over a set of facts via a very efficient process called incremental inferencing. Semantics.Server exposes this feature through a stored procedure sw_CreateDerivedGraph that takes a URI for the new derived graph and SPARQL query that defines the rules and facts. The example below creates a derived graph and performs a query based on an infered statement.

EXEC sw_CreateDerivedGraph 
'http://www.intellidimension.com/resources/derived-graph1',
'
PREFIX itd: <http://www.intellidimension.com/resources/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
WITH (
CONSTRUCT {?s rdf:type itd:Product} WHERE {?s rdf:type itd:SoftwareProduct}
)
CONSTRUCT {?s ?p ?o} FROM <http://www.intellidimension.com/resources/graph1> 
WHERE {?s ?p ?o}';

EXEC sw_Sparql '
PREFIX itd: <http://www.intellidimension.com/resources/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?s FROM <http://www.intellidimension.com/resources/derived-graph1> 
WHERE {?s rdf:type itd:Product}', 1;