The Intellidimension Semantics.SDK provides a simple object-oriented API for working with RDF data using Microsoft .NET. The most basic structure for managing RDF data is a graph, which is a collection of RDF statements. This article will focus on the basic interface the Semantics.SDK provides for manipulating statements in a graph.
In-memory Graphs
The Semantics.SDK provides an abstract base class DataSource that provides an interface for working with a RDF graph. The most commonly used implementation of a DataSource is the GraphDataSource which implements an in-memory RDF graph.
Adding Statements
One of several ways statements can be added to any DataSource via its Add method.
GraphDataSource g = new GraphDataSource();
g.Add(new RdfUri("http://www.intellidimension.com/sdj.pdf"),
new RdfUri("http://www.w3.org/2000/01/rdf-schema#label"),
new RdfLiteral("Getting Started With Graphs"));
The parameters to the Add method specify the subject, predicate and object values, respectively, of the statement to be added to the graph. There are several overloads to the Add method.
Removing Statements
Similarly, a statement can be removed from a DataSource by calling it’s Remove method and specify the subject, predicate and object values of the statement to be removed. Any one of these parameters can be set to null. In that case the parameters for the Remove method act as a statement mask where each null value is treated as a wildcard value for matching statements in the DataSource. In this manner multiple or all the statement can be removed from the DataSource with a single call.
Getting Statements
A DataSource object is a collection of RDF statements each represented by an instance of the class Statement. All DataSource objects provide access to the collection of statements via their GetStatements method. The code below shows how to iterate over all the statements in a DataSource.
foreach (Statement stmt in g.GetStatements())
{
RdfValue s = stmt.Subject;
RdfValue p = stmt.Predicate;
RdfValue o = stmt.Object;
}
For each Statement the Subject, Predicate and Object properties are retrieved as an instance of the RdfValue class.
The RdfValue Class
The RdfValue class is an abstract base class that is used to represent subject, predicate and object values of a statement. A RDF resource is represented by the RdfUri class and a RDF literal is represented by the RdfLiteral class.
double d = (double)(RdfLiteral)stmt.Object;
DateTime dt = (DateTime)(RdfLiteral)stmt.Object;
The RdfValue class is widely used throughout the Semantics.SDK and it supports some conveniences such as conversion operators to simplify the integration with other data types in .NET.
Conclusion
This article provided a brief introduction into how to manipulate statements in an RDF graph. The Semantics.SDK provides other capabilities such as issuing queries to graphs that will be discussed in future.
0 comments:
Post a Comment