Architecture - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Architecture arrow Understanding and Using Resource Description Framework (RDF) Files Part 2
Understanding and Using Resource Description Framework (RDF) Files Part 2
By John Mueller

Rate This Article: Add This Article To:

Understanding and Using Resource Description Framework (RDF) Files Part 2
( Page 1 of 4 )

Now that you have a Resource Description Framework (RDF) file to you, you’ll want to work with it in Visual Studio.

LINQ provides a common way to access many kinds of data. It shouldn’t be any surprise, then, that it also provides a method for accessing RDF files. You use the same common programming technique with RDF that you use with any other LINQ application. However, this scenario has some differences that you need to consider. The most important difference is that you don’t use a built-in provider to access RDF—you use a third party provider. It also pays to know something about the structure of RDF files so you understand how LINQ is accessing them. You can learn about the structure of the data files used for this example in Part 1 of this series at http://www.devsource.com/c/a/Architecture/Modeling-with-RDF-Part-1. Finally, the provider used in this example requires a supplementary technology named Simple Protocol and RDF Query Language (SPARQL).

Getting the RDF Provider

ADVERTISEMENT

You can find at least two RDF providers online (there may be more and I’d love to hear about them). The provider that works best right now relies on the N3 format, rather than XML format to create the RDF content. Download this provider from http://code.google.com/p/linqtordf/. Make sure you get the latest edition because the author has made some important changes in the current releases that fix bugs and add functionality. You can discuss the LINQ to RDF provider at http://groups.google.com/group/linqtordf-discuss/. It’s even possible to have a digest of the current discussions delivered to your e-mail.

Installation of this provider is easy because the author provides a full installation program for it. Just double click the file you download and follow the instructions. I used all of the default installation options, so the files for this provider appear in the \Program Files\LinqToRdf folder of my hard drive. The location is important because you need to add reference to your application during the setup process.

A second RDF provider exists and this one relies on the XML format. Read about this version at http://blogs.msdn.com/hartmutm/archive/2006/07/10/661512.aspx and http://blogs.msdn.com/hartmutm/archive/2006/07/24/677200.aspx. Don't download the provider source code found on the blog because it's based on Visual Studio 2005 and the LINQ preview. Hopefully, the author will update this provider because it works with the XML RDF format.

A Quick Description of SPARQL

As previously mentioned, this example relies on SPARQL. Because the author has done such a good job of putting the provider together, I didn’t have to spend hours learning about SPARQL, I could simply follow the author’s examples on how to write the required code. Yes, it really is just that easy—I was amazed. The SemWeb.Sparql.DLL file that comes with the LINQ to RDF provider (\Program Files\LinqToRdf) encapsulates the functionality you need to implement SPARQL within a .NET application.

You create a query by posting information to the host site. In SPARQL terminology, this Web site acts as an ontology host. Essentially, it's your means of communicating with the RDF data—the ontology host is a kind of listener. Consequently, your client application calls the ontology host, which then provides access to the RDF data. In many respects, SPARQL is a kind of Web service that provides access without relying on a single product definition. You can discover more about how SPARQL works at:

  • W3C Opens Data on the Web with SPARQL: http://www.w3.org/2007/12/sparql-pressrelease
  • SPARQL Query Language for RDF: http://www.w3.org/TR/rdf-sparql-query/
  • SPARQL Tutorial: http://jena.sourceforge.net/ARQ/Tutorial/
  • SPARQLer: http://sparql.org/
  • GovTrack.us (see queries through a Web page): http://www.govtrack.us/sparql.xpd
  • Introducing SPARQL: Querying the Semantic Web: http://www.xml.com/pub/a/2005/11/16/introducing-sparql-querying-semantic-web-tutorial.html
  • SPARQL Protocol and Query Language: SPARQL Frequently Asked Questions: http://thefigtrees.net/lee/sw/sparql-faq
  • Search RDF data with SPARQL: http://www-128.ibm.com/developerworks/library/j-sparql/

Performing the Project Setup

This example isn't designed to show you how to work with RDF or create complex SPARQL syntax—that's a topic for another article. This example begins with the Contacts.N3 and ContactData.N3 files discussed in Part 1 of this article. If you haven’t reviewed those files yet, you need to do so before you can understand this example fully.

The Contacts.N3 and ContactData.N3 files must appear in a folder on IIS. You can create a directory for them anywhere and create virtual directory entry in IIS. Even simpler, you can create a folder in the \inetpub\wwwroot folder as I’ve done for this example. The example uses RDFData as the name for this data folder. The application you write won't access this folder directly. It instead relies on some ASP.NET connectivity.

If you can't see the N3 files on your system, you need to add a Multipurpose Internet Mail Extensions (MIME) type for the extension to your IIS configuration. Use the text/plain type for the N3 file so you can view it directly in the browser. In IIS 7 you can add this MIME type directly to the folder that holds the RDF data so that you don't have to worry about people using the N3 extension in other folders. It also helps to enable directory browsing for text scenarios (but it isn't absolutely required).

Now that you have two data files to use, it's time to create the solution used to access them. The following section describes the two projects you require to create a complete LINQ to RDF solution.

Defining the client project

Begin by creating the user application. The project will include all of the forms that the user relies on to interact with the RDF files. The following steps help you configure the client project.

  • Right click the client project entry and choose Add Reference from the context menu. Visual Studio displays the Add Reference dialog box.
  • Choose the Browse tab. Highlight the SemWeb.DLL and LinqToRdf.DLL files located in the \Program Files\LinqToRdf folder of your hard drive. Click OK. Visual Studio adds the required references to your project.
  • Open the AssemblyInfo.CS file and add a reference to LinqToRdf.
  • Type the following attribute into the AssemblyInfo.CS file. The attribute tells the LINQ to RDF provider what the call the ontology you create in Contacts.N3 and provides the base URI for that ontology so you don't have to type it every time you need it in the remaining ontology-specific code.
[assembly: Ontology(
 BaseUri = 
  "http://aabs.purl.org/ontologies/2007/04/Contacts#",
 Name = "Contacts",
 Prefix = "Contacts",
 UrlOfOntology =
 "http://aabs.purl.org/ontologies/2007/04/Contacts#")]
  • Open the application code file and add a reference to LinqToRdf. At this point, the client project is configured for use.

Defining the host project

The RDF query process requires a host. The application sends requests to this host, which then serves up the data. In short, the host is a kind of Web service or in LINQ parlance, a data source. The following steps help you configure the host project.

  • Add to the client project a Web project. The example uses the name RDFHost for this project. The Web project provides a means for posting the query to the SPARQL host.
  • Right click the host project entry and choose Add Reference from the context menu. Visual Studio displays the Add Reference dialog box.
  • Choose the Browse tab. Highlight the SemWeb.Sparql.DLL file located in the \Program Files\LinqToRdf folder of your hard drive. Click OK. Visual Studio adds the required references to your project. At this point, the host project is configured for use.


 
 
>>> More Architecture Articles          >>> More By John Mueller
 



Microsoft's Future: A Chat With Their CTO, Barry Briggs

Play Video >

All Videos >

Julia explores the Robotics Studio!

Read now >

Messages to Bill Gates!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.