04: Processing RDF With SPARQL in ASP.NET Flashcards

1
Q
  • *Interface**
  • *(for nodes)**

Core Concepts of dotNetRDF

A

a node in each RDF graph represents the value of an RDF term

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

IGraph Interface

Core Concepts of dotNetRDF

A

Interface for graphs, an RDF document forms a graph in the mathematical sense, so we represent sets of triples as graphs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

ITripleStore Interface

Core Concepts of dotNetRDF

A

a collection of one or more graphs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Nodes

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Creating Nodes

dotNetRDF Syntax

A

//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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Creating an Anonymous Blank Node

dotNetRDF Syntax

A

IGraph g = new Graph();

IBlankNode anon = g.CreateBlankNode();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Creating a Plain Literal

dotNetRDF Syntax

A

IGraph g = new Graph();

ILiteralNode plain = g.CreateLiteralNode(“some value”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Creating a Language Specified Literal

dotNetRDF Syntax

A

ILiteralNode hello = g.CreateLiteralNode(“hello”,”en”);

ILiteralNode bonjour = g.CreateLiteralNode(“bonjour”,”fr”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Creating a Typed Literal

dotNetRDF Syntax

A

ILiteralNode number = g.CreateLiteralNode(“1”, UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeInteger));

ILiteralNode t = g.CreateLiteralNode(“true”, UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeBoolean));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Creating Triples

dotNetRDF Syntax

A

//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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Writing “Hello World!”

dotNetRDF Syntax

A

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));

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Output to Console

(using “Hello World” code)

dotNetRDF Syntax

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Output to RDF file

(using “Hello World” code)

dotNetRDF Syntax

A

//to make output in RDF file, add

RdfXmlWriter rdfxmlwriter = new RdfXmlWriter();

rdfxmlwriter.Save(g, “HelloWorld.rdf”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

NTriples Output

(using “Hello World” code)

dotNetRDF Syntax

A

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:>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Reading from Files

dotNetRDF Syntax

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Purpose of UriLoader Class

A
  • to read some RDF from a URI by providing the static UriLoader class which provides a Load(IGraph g, Uri u) method
17
Q

How the URILoader Class Works

A

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
18
Q

What is the output of the following code?

A

All triples in foaf version 1 (RDF file)

19
Q

Purpose of Triple Stores

A
  • 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”);

20
Q

How to check if a graph exists in store?

A

if (store.HasGraph(new Uri(“http://example.org/”))) {

Console.WriteLine(“Graph exists”);

}

else {

Console.WriteLine(“Graph doesn’t exist);

}

21
Q

How to access a graph in store?

A

if (store.HasGraph(new Uri(“http://example.org/someGraph”))) {

Graph g = store.Graph(new Uri(“http://example.org/someGraph”));

}

22
Q

Loading and Saving Stores

A
23
Q

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 .

}

A

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());

}

24
Q

Query with SPARQL

(SPARQL Query Parser, Processing)

dotNetRDF Syntax

A

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));

}

}

}

}

25
Q

Purpose of ProcessQuery(SparqlQuery query)

A

takes in a SparqlQuery and returns either a SparqlResultSet or an IGraph instance

26
Q

Remote SPARQL Endpoints

A
  • 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
27
Q

Remote Query

A

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());

}

}

}

28
Q

Fill the following boxes:

A