RDF data stored in Virtuoso can be exposed as an OData service using the Virtuoso ADO.NET Provider. This document describes first how to create the service, and then how to consume the service with a simple browser that allows you to access and explore the RDF data by clicking on dereferenceable IRIs.
demo_dav.vad
) cartridges_dav.vad
) This example assumes that your accessible Virtuoso server is on the same machine as all other components (i.e., localhost), but it may be on any network-accessible host. If the Northwind demo database has not already been installed on your Virtuoso server, then download the and install it. The VAD package will create a new database in Virtuoso called demo containing the familiar Northwind tables. It will also creates RDF views of the Northwind tables.
In this example, we assume the database is accessible on a hostname of "demo.openlinksw.com
" on the default port 80, where a live instance of the Virtuoso Demo database is actually hosted.
Users should use the appropriate hostname and port number of their Virtuoso installation to create the sample application, which would be localhost:8890
for a default installation or whatever the URIQA DefaultHost
Virtuoso configuration parameter (in the virtuoso.ini
file) is set to when the demo database VAD package is installed.
Virtuoso supports an extension to SQL called SPASQL that allows SPARQL queries to be executed as if they were SQL queries. The SPARQL query is indicated using the SPARQL keyword. The simplest way to expose the results of a SPARQL query as OData is to create a view that is the result of a SPARQL query then expose the view as an OData service.
To create a view of the customers in the Northwind first open the Virtuoso Conductor and log in as dba. Then open iSQL from the menu on the left and execute the following statement.
CREATE VIEW Demo.demo.sparqlview as SPARQL PREFIX nwind: <http://demo.openlinksw.com/schemas/northwind#> SELECT DISTINCT ?s FROM <http://demo.openlinksw.com/Northwind> WHERE {?s a nwind:Customer}
Note:
grant execute on DB.DBA.RDF_MAKE_LONG_OF_SQLVAL to "demo"
public class WcfDataService1 : DataService<DemoEntities>
config.SetEntitySetAccessRule("*", EntitySetRights.All); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
public static void InitializeService(DataServiceConfiguration config) { // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc. // Examples: // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead); // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All); config.SetEntitySetAccessRule("*", EntitySetRights.All); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; }
<?xml version="1.0" encoding="utf-8" standalone="yes" ?> - <service xml:base="http://localhost:2344/WcfDataService1.svc/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2007/app"> - <workspace> <atom:title>Default</atom:title> - <collection href="sparqlviews"> <atom:title>sparqlviews</atom:title> </collection> </workspace> </service>
The service is now running.
Note the address on which the service is made available. You will need to know this when creating the app to consume the service. Look in the Address Bar of the browser. It will be something like:http://localhost:2344/WcfDataService1.svc/
.
datasvcutil.exe /uri:http://localhost:2344/WcfDataService1.svc /out:DemoEntities.cs
To display the RDF data on the web page we create a table with a row for each item in sparqlview. We then use each IRI from sparqlview to create a hyperlink. The hyperlinks are displayed in the table cells. To do this, first add a new table, iriTable to Defalt.aspx.
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RDFWebApp2._Default" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <h2> RDFWebApp </h2> <p> <asp:Table ID="iriTable" runat="server"></asp:Table> </p> </asp:Content>
Then add the following block of code to the page_load method in Default.aspx.cs.
DemoModel.DemoEntities svc = new DemoModel.DemoEntities(new Uri("http://localhost:2344/WcfDataService1.svc")); var query = svc.sparqlviews; foreach (DemoModel.sparqlview sv in query) { TableRow tRow = new TableRow(); iriTable.Rows.Add(tRow); TableCell tCell = new TableCell(); tRow.Cells.Add(tCell); HyperLink h = new HyperLink(); h.Text = sv.s; h.NavigateUrl = sv.s; tCell.Controls.Add(h); }Note the address of the service in the first line - you may need to change the port number to match the one seen in the address at the end of Step 4 in Creating the Web Service.
Compile and run RDFWebApp (ensuring that the service created above is still running). This will launch a browser and display the IRIs from sparqlview as a list of hyperlinks.
With the Cartridges VAD package installed in Virtuoso, clicking on these links will take you to a description page of the referenced resource. The description page is created using description.vsp.
To create and test this simple Web Service we have used the Visual Studio Development Server. This section describes how to deploy the service using IIS.
To deploy the service using IIS:
The start page that you see when you test the service will look the same as before but the address in the browser bar will be something like http://localhost/RDFWebDemo/WcfDataService1.svc/
.
You can now access your service remotely using the hostname or IP address of your server.
If at this point you get an Access is denied error, 401.3, then you will need to add the Internet Guest Account (IUSR_XXX where XXX is your computer name) to the users allowed to access the folder containing the RDFWebDemo project.
You will now need to modify RDFWebApp to access the service at the new address. At the same time we will also change RDFWebApp so that it too is deployed using IIS
DemoModel.DemoEntities svc = new DemoModel.DemoEntities(new Uri("http://localhost:2344/WcfDataService1.svc"));
DemoModel.DemoEntities svc = new DemoModel.DemoEntities(new Uri("http://localhost/RDFWebDemo/WcfDataService1.svc"));
ctrl-F5
).The web application is accessible on http://localhost/RDFWebApp/Default.aspx
and can also be accessed using the hostname or IP address of your server, e.g., http://192.168.7.129/RDFWebApp/Default.aspx
.