Monday, May 4, 2009

Reading and writing a RDF Graph

The Intellidimension Semantics.SDK contains a suite of RDF readers and formatters for .NET. These allow applications to read and write serialized RDF data from and to streams. RdfReader and RdfFormatter The Semantics.SDK has two abstract base classes that provide the interfaces for reading and formatting RDF data, respectively. These base classes provide an interface that allows each to operate on an instance of a stream object that must be created in the application code prior to using either of these classes. This allows the RdfReader and RdfFormatter classes to operate on virtually any source that supports the TextReader or TextWriter interface as defined in System.IO. The RdfReader class is design to read data from a stream into an instance of a DataSource. While the RdfFormatter class will write data from an instance of a DataSource to a stream. RDF Syntaxes The Semantics.SDK supports most of the standard RDF syntaxes. The table below lists the RDF syntax along with the RdfReader and RdfFormatter classes that implement the serialization for that syntax. RDF/XML: RdfXmlReader, RdfXmlFormatter N Triples: NTriplesReader, NTriplesFormatter Turtle: TurtleReader, TurtleFormatter RDFa: RdfaReader Reading RDF In the Semantics.SDK, all RDF graphs derive from the base class DataSource. This base class defines several overloaded Read methods for loading RDF data into an instance of a DataSource. The Read method is used to load RDF data into the DataSource from a string, stream or location specified as a URI. Each of these overloaded methods provides the option of specifying the default base URI for any relative URIs used in the RDF data. The code below shows an example of how to read a RDF/XML file into an in-memory graph. Note, that one of the overloaded Read methods is a generic method in which the class of the RdfReader is specified.
GraphDataSource g = new GraphDataSource();
StreamReader s = new StreamReader(@"c:\sample.rdf");
g.Read(s);
 
Writing RDF The DataSource class also defines several overloaded Format methods for writing RDF data from an instance of a DataSource to a string or stream. Like the Read method, the Format method also has an overloaded generic form in which the class of the RdfFormatter is specified. The code below shows an example of how to store the contents of an in-memory graph to a file using the N Triples syntax.
GraphDataSource g = new GraphDataSource();
StreamWriter s =
    new StreamWriter(@"c:\sample.rdf");
g.Format(s);
 
The code below shows an example of how to store the contents of an in-memory graph to a string using the turtle syntax.
GraphDataSource g = new GraphDataSource();
String s = g.Format();
 
Conclusion This article provided a brief introduction into how to serialize RDF data to and from a stream. The Semantics.SDK also allows RDF data to be stored and retrieved from a Microsoft SQL Server® database using Intellidimension Semantics.Server. This will be discussed in detail in future articles.

0 comments:

Post a Comment