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;