04: Processing RDF With SPARQL in ASP.NET Flashcards
- *Interface**
- *(for nodes)**
Core Concepts of dotNetRDF
a node in each RDF graph represents the value of an RDF term
IGraph Interface
Core Concepts of dotNetRDF
Interface for graphs, an RDF document forms a graph in the mathematical sense, so we represent sets of triples as graphs
ITripleStore Interface
Core Concepts of dotNetRDF
a collection of one or more graphs
Nodes
- sometimes referred to as an RDF term
-
interface primarily provides information about
- the type of the node
- the graph it is associated with
Node Types:
- IBlankNode - anonymous node
- ILiteralNode - node with textual value
- IUriNode - URI Node
- IGraphLiteralNode - represents a sub-graph
- IVariableNode - represents a variable
Creating Nodes
dotNetRDF Syntax
//create graph
IGraph g = new Graph();
g.BaseUri = UriFactory.Create(“http://example.org/”);
//METHOD 1: Creating a URI Node that refers to some specific URI
IUriNode dotNetRDF = g.CreateUriNode(UriFactory.Create(“http://www.dotnetrdf.org”));
//METHOD 2: Creating a URI Node using a prefixed name
g.NamespaceMap.AddNamespace(“ex”, UriFactory.Create(“http://example.org/namespace/”));
IUriNode pname = g.CreateUriNode(“ex:demo”);
//resulting URI is http://example.org/namespace/demo
Creating an Anonymous Blank Node
dotNetRDF Syntax
IGraph g = new Graph();
IBlankNode anon = g.CreateBlankNode();
Creating a Plain Literal
dotNetRDF Syntax
IGraph g = new Graph();
ILiteralNode plain = g.CreateLiteralNode(“some value”);
Creating a Language Specified Literal
dotNetRDF Syntax
ILiteralNode hello = g.CreateLiteralNode(“hello”,”en”);
ILiteralNode bonjour = g.CreateLiteralNode(“bonjour”,”fr”);
Creating a Typed Literal
dotNetRDF Syntax
ILiteralNode number = g.CreateLiteralNode(“1”, UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeInteger));
ILiteralNode t = g.CreateLiteralNode(“true”, UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeBoolean));
Creating Triples
dotNetRDF Syntax
//need graph first
IGraph g = new Graph();
//create nodes
IUriNode dotNetRDF =
g.CreateUriNode(UriFactory.Create(“http://www.dotnetrdf.org”));
IUriNode createdBy =
g.CreateUriNode(UriFactory.Create(“http://example.org/createdBy”));
ILiteralNode robVesse =
g.CreateLiteralNode(“Rob Vesse”);
//assert triple
Triple t =
new Triple(dotNetRDF, createdBy, robVesse);
g.Assert(t);
Writing “Hello World!”
dotNetRDF Syntax
using System;
using System.Collections.Generic;
using VDS.RDF;
using VDS.RDF.Writing;
public class HelloWorld {
public static void Main(String[] args) {
IGraph g = new Graph();
IUriNode dotNetRDF =
g.CreateUriNode(UriFactory.Create(“http://www.dotnetrdf.org”));
IUriNode says =
g.CreateUriNode(UriFactory.Create(“http://example.org/says”));
ILiteralNode helloWorld =
g.CreateLiteralNode(“Hello World”);
ILiteralNode bonjourMonde =
g. CreateLiteralNode(“Bonjour tout le Monde”, “fr”);
g. Assert(new Triple(dotNetRDF, says, helloWorld));
g. Assert(new Triple(dotNetRDF, says, bonjourMonde));
}
}
Output to Console
(using “Hello World” code)
dotNetRDF Syntax
foreach (Triplet t in g.Triples)
{
Console.WriteLine(t.ToString());
}
Console.ReadLine();
//output, NOT in RDF syntax
http: //www.dotnetrdf.org , http://example.org/says , Hello World
http: //www.dotnetrdf.org , http://example.org/says , Bonjour tout le Monde@fr
Output to RDF file
(using “Hello World” code)
dotNetRDF Syntax
//to make output in RDF file, add
RdfXmlWriter rdfxmlwriter = new RdfXmlWriter();
rdfxmlwriter.Save(g, “HelloWorld.rdf”);
NTriples Output
(using “Hello World” code)
dotNetRDF Syntax
NTriplesWriter ntwriter = new NTriplesWriter();
ntwriter.Save(g, “HelloWorld.nt”);
//output
http:> <http: “Hello World”. http:> <http: “Bonjour tout le Monde”@fr.</http:></http:></http:></http:>
Reading from Files
dotNetRDF Syntax
try
{
IGraph g = new Graph();
NTriplesParser ntparser = new NTriplesParser();
ntparser.Load(g, “Example.ext”);
}
catch (RdfParseException parseEx)
{
//indicates a parser error
Console.WriteLine(“Parser Error”);
Console.WriteLine(parseEx.Message);
}
catch (RdfException rdfEx)
{
//represents an RDF error e.g. illegal triplet for the given syntax, undefined namespace
Console.WriteLine(“RDF Error);
Console.WriteLine(rdfEx.Message);
}
***note:
//must use VDS.RDF.Parsing namespace
using VDS.RDF.Parsing;
Purpose of UriLoader Class
- to read some RDF from a URI by providing the static UriLoader class which provides a Load(IGraph g, Uri u) method
How the URILoader Class Works
IGraph g = new Graph();
UriLoader.Load(g.newUri(“http://…”));
- automatically selects the correct Parser to use based on the returned Content-Type header of the HTTP response
- may throw RdfException if input URI is not valid
- WebException if error occurs in retrieving URI using HTTP
What is the output of the following code?

All triples in foaf version 1 (RDF file)
Purpose of Triple Stores
- used to represent collections of graphs and to allow you to work with larger quantities of RDF easily
//assuming we already have a store
IGraph g = store[new Uri(“http://example.org/graph”)];
- can keep several graphs at the same time, can be read as follows
TripleStore store = new TripleStore();
TriG trigparser = new TriGParser(); //for loading a store
trigparser.Load(store, “Example.trig”);

How to check if a graph exists in store?
if (store.HasGraph(new Uri(“http://example.org/”))) {
Console.WriteLine(“Graph exists”);
}
else {
Console.WriteLine(“Graph doesn’t exist);
}
How to access a graph in store?
if (store.HasGraph(new Uri(“http://example.org/someGraph”))) {
Graph g = store.Graph(new Uri(“http://example.org/someGraph”));
}
Loading and Saving Stores

Write the following SPARQL query in dotNetRDF:
PREFIX vcard: http://www.w3.org/2001/vcard-rdf/3.0#
SELECT ?givenName
WHERE
{
?y vcard:Family “Smith”
?y vard:Given ?givenName .
}
using System;
using VDS.RDF;
using VDS.RDF.Query;
using VDS.RDF.QueryBuilder;
static void WithPrefix()
{
var prefixes = new NamespaceMapper(true);
prefixes.AddNamespace(“vcard”, new Uri(“http://www.w3.org/2001/vcard-rdf/3.0#”));
string y = “y”;
var givenName = new SparqlVariable(“givenName”);
var queryBuilder =
QueryBuilder
.Select(new SparqlVariable[] {givenName})
.Where(
(triplePatternBuilder) ->
{
triplePatternBuilder
.Subject(y)
.PredicateUri(“vcard:Family”);
.Object(“Smith”);
triplePatternBuilder
.Subject(y)
.PredicateUri(“vcard:Given”)
.Object(givenName);
}
);
queryBuilder.Prefixes = prefixes;
Console.WriteLine(queryBuilder.BuildQuery().ToString());
}
Query with SPARQL
(SPARQL Query Parser, Processing)
dotNetRDF Syntax
using System;
using VDS.RDF;
using VDS.RDF.Parsing;
using VDS.RDF.Query;
using VDS.RDF.Writing.Formatting;
public class QueryParsingExample
{
public static void Main(String[] args)
{
//instance of SparqlQueryParser
SparqlQueryParser parser = new SparqlQueryParser();
//parse SPARQL string to query
SparqlQuery query = parser.ParseFromString(“SELECT * WHERE {?s a ?type}”);
//process the query
TripleStore store = new TripleStore();
InMemoryDataset ds = new InMemoryDataset(store, new Uri(“http://mydefaultgraph.org));
//get query processor
ISparqlQueryProcessor processor = new LeviathanQueryProcessor(ds);
SparqlResultSet results = processor.ProcessQuery(query);
if (results is IGraph) {
//print results
IGraph g = (IGraph)results;
NTriplesFormatter formatter = new NTriplesFormatter();
foreach (Triple t in g.Triples) {
Console.WriteLine(t.ToString(formatter));
}
}
}
}
Purpose of ProcessQuery(SparqlQuery query)
takes in a SparqlQuery and returns either a SparqlResultSet or an IGraph instance
Remote SPARQL Endpoints
- can be queried using the SparqlRemoteEndpoint class, a wrapper class around a remote endpoint which sends queries to the endpoint and then turns the response into a SparqlResultSet or IGraph as appropriate
- a remote endpoint is a combination of an endpoint URI and an optional default Graph URI
- A SparqlRemoteEndpoint provides strongly typed methods for making queries meaning you don’t need to type check and cast the result
Remote Query
using System;
using VDS.RDF.Parsing;
using VDS.RDF.Query;
public class SparqlRemoteEndpointExample
{
public static void Main(String[] args)
{
//define remote endpoint
//use DBPedia SPARQL endpoint with defaultGraph set to DBPedia
SparqlRemoteEndpoint endpoint= new SparqlRemoteEndpoint(new Uri(“http://dbpedia.org/sparql”), “http://dbpedia.org”);
//make SELECT query against the Endpoint
SparqlResultSet results = endpoint.QueryWithResultSet(“SELECT DISTINCT ?Concept WHERE {[] a ?Concept}”);
foreach (SparqlResult result in results)
{s
Console.WriteLine(result.ToString());
}
}
}
Fill the following boxes:

