RDF API FAQ

Questions related to nodes in a graph:

  1. How do I obtain a node from a graph?
  2. How do I iterate the predicates of a node?
  3. How do I iterate the object values of a predicate in a node?
  4. How do I obtain a single object value of a predicate in a node?
  5. How do I set an object value for a predicate in a node?
  6. How do I add an aditional object value to a predicate in a node?
  7. How do I replace an existing object value of a predicate in a node?
  8. How do I remove an object value from a predicate in a node?
  9. How do I remove a single object value from a multi-value predicate in a node
  10. How do I create a new node in a graph?
  11. How do I remove a node from a graph?

Questions related to RdfValues:

  1. How do I create a literal RdfValue?
  2. How do I create a typed literal RdfValue?
  3. How do I create a typed literal RdfValue with a language tag?
  4. How do I create a URI RdfValue?
  5. How do I modify an RdfValue after its been created?
  6. How do I use operators on an RdfValue?
  7. How do I distinguish between the different types of RdfValues?

Questions related to datasources:

  1. How do I query a datasource using SPARQL?
  2. How do I iterate over the results obtained from querying a datasource?
  3. How do I add query results to another datasource?
  4. How do I create a datasource?
  5. How do I load a datasource from a file or other resource?
  6. How do I store a datasource in a file?
  7. How do I query multiple datasources at once?
  8. How do I add a statement to a datasource?
  9. How do I remove a statement from a datasource?
  10. How do I easily get all the statements in a datasource?

Questions related to RdfReaders:


Questions related to RdfFormatters:


Questions related to models:


Questions related to SparqlExpressions:


How do I obtain a node from a graph?

The GraphDataSource object supports the array notation operator []. Simply use the URI of the subject as the index value.

  1. public void Get_a_Node_from_a_Graph()  
  2. {  
  3.     String subject = "http://www.intellidimension.com/people#JBarron";  
  4.   
  5.     GraphDataSource graph = new GraphDataSource();  
  6.   
  7.     graph.Add(subject, "http://xmlns.com/foaf/0.1/name"new RdfLiteral("Justin Barron"));  
  8.     graph.Add(subject, "http://xmlns.com/foaf/0.1/mbox"new RdfLiteral("jbarron@intellidimension.com"));  
  9.   
  10.     RdfNode node = graph[subject];  
  11.   
  12.     Console.WriteLine(node);  
  13. }  

Output:

http://www.intellidimension.com/people#JBarron

Back to Top

How do I iterate the predicates of a node?

An enumeration of predicates can be obtained from a node by accessing its GetPredicates() method.

  1. public void Iterate_Predicates_of_a_Node()  
  2. {  
  3.     String subject = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.   
  6.     GraphDataSource graph = new GraphDataSource();  
  7.   
  8.     graph.Add(subject, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.     graph.Add(subject, foaf + "mbox"new RdfLiteral("jbarron@intellidimension.com"));  
  10.     graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#GChappell"));  
  11.   
  12.     foreach (RdfUri predicate in graph[subject].GetPredicates())  
  13.         Console.WriteLine(predicate);  
  14. }  

Output:

http://xmlns.com/foaf/0.1/knows
http://xmlns.com/foaf/0.1/mbox
http://xmlns.com/foaf/0.1/name

Back to Top

How do I iterate the object values of a predicate in a node?

The RdfNode object supports the array notation operator []. Simply use the URI of the predicate as the index value to obtain an enumeration of RdfValue objects.

  1. public void Iterate_Object_Values_of_a_Predicate_in_a_Node()  
  2. {  
  3.      String subject = "http://www.intellidimension.com/people#JBarron";  
  4.      String foaf = "http://xmlns.com/foaf/0.1/";  
  5.   
  6.      GraphDataSource graph = new GraphDataSource();  
  7.   
  8.      graph.Add(subject, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.      graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#GChappell"));  
  10.      graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#DRepchick"));  
  11.   
  12.      RdfNode node = graph[subject];  
  13.   
  14.      foreach (RdfValue obj in node[foaf + "knows"])  
  15.           Console.WriteLine(obj);  
  16. }  

Output:

http://www.intellidimension.com/people#DRepchick
http://www.intellidimension.com/people#GChappell

Back to Top

How do I obtain a single object value of a predicate in a node?

The RdfNode object supports the array notation operator []. Simply use the URI of the predicate as the index value.

  1. public void Get_a_Single_Object_Value_for_a_Predicate_in_a_Node()  
  2. {  
  3.     String subject = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.   
  6.     GraphDataSource graph = new GraphDataSource();  
  7.   
  8.     graph.Add(subject, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.     graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#GChappell"));  
  10.     graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#DRepchick"));  
  11.   
  12.     RdfNode node = graph[subject];  
  13.   
  14.     RdfValue val = node[foaf + "name"];  
  15.   
  16.     Console.WriteLine(val);  
  17. }  

Output:

Justin Barron

Back to Top

How do I set object value for a predicate in a node?

The Set(RdfUri, RdfValue) method of the RdfNode object allows you to set an object value for a predicate in a node. Doing so will remove any object values currently associated with the predicate.

  1. public void Set_an_Object_Value_for_a_Predicate_in_a_Node()  
  2. {  
  3.     String subject = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.   
  6.     GraphDataSource graph = new GraphDataSource();  
  7.   
  8.     graph.Add(subject, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.     graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#GChappell"));  
  10.     graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#DRepchick"));  
  11.   
  12.     RdfNode node = graph[subject];  
  13.   
  14.     node.Set(foaf + "knows"new RdfLiteral("Bill Gates"));  
  15.   
  16.     RdfValue val = node[foaf + "knows"];  
  17.     Console.WriteLine(val);  
  18. }  

Output:

Bill Gates

Back to Top

How do I add an aditional object value to a predicate in a node?

The Add(RdfUri, RdfValue) method of the RdfNode object allows you to add an additional object value for a predicate in a node.

  1. public void Add_an_Additional_Object_Value_to_a_Predicate_in_a_Node()  
  2. {  
  3.     String subject = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.   
  6.     GraphDataSource graph = new GraphDataSource();  
  7.   
  8.     graph.Add(subject, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.     graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#GChappell"));  
  10.     graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#DRepchick"));  
  11.   
  12.     RdfNode node = graph[subject];  
  13.   
  14.     node.Add(foaf + "knows"new RdfLiteral("Bill Gates"));  
  15.   
  16.     foreach (RdfValue val in node[foaf + "knows"])  
  17.         Console.WriteLine(val);  
  18. }  

Output:

http://www.intellidimension.com/people#DRepchick
Bill Gates
http://www.intellidimension.com/people#GChappell

Back to Top

How do I replace an existing object value of a predicate in a node?

The Set(RdfUri, RdfValue, RdfValue) method of the RdfNode object allows you to replace an existing object value for a predicate in a node without compromising other object values associated with the predicate.

  1. public void Replace_an_Existing_Object_Value_of_a_Predicate_in_a_Node()  
  2. {  
  3.     String subject = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.   
  6.     GraphDataSource graph = new GraphDataSource();  
  7.   
  8.     graph.Add(subject, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.     graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#GChappell"));  
  10.     graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#DRepchick"));  
  11.   
  12.     RdfNode node = graph[subject];  
  13.   
  14.     node.Set(foaf + "name"new RdfLiteral("J B"), new RdfLiteral("Justin Barron"));  
  15.   
  16.     RdfValue val = node[foaf + "name"];  
  17.     Console.WriteLine(val);  
  18. }  

Output:

J B

Back to Top

How do I remove an object value from a predicate in a node?

The Remove(RdfUri) method of the RdfNode object allows you to remove all existing object values for a predicate in a node.

  1. public void Remove_an_Object_Value_from_a_Predicate_in_a_Node()  
  2. {  
  3.     String subject = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.   
  6.     GraphDataSource graph = new GraphDataSource();  
  7.   
  8.     graph.Add(subject, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.     graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#GChappell"));  
  10.     graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#DRepchick"));  
  11.   
  12.     RdfNode node = graph[subject];  
  13.   
  14.     node.Remove(foaf + "knows");  
  15.     foreach (RdfUri predicate in node.GetPredicates())  
  16.         Console.WriteLine(predicate);  
  17. }  

Output:

http://xmlns.com/foaf/0.1/name

Back to Top

How do I remove a single object value from a multi-value predicate in a node?

The Remove(RdfUri, RdfValue) method of the RdfNode object allows you to remove a specific object value for a predicate in a node without compromising other object values that may be associated with the predicate.

  1. public void Remove_a_Single_Object_Value_from_a_Multi_Value_Predicate_in_a_Node()  
  2. {  
  3.     String subject = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.   
  6.     GraphDataSource graph = new GraphDataSource();  
  7.   
  8.     graph.Add(subject, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.     graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#GChappell"));  
  10.     graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#DRepchick"));  
  11.     graph.Add(subject, foaf + "knows"new RdfLiteral("Bill Gates"));  
  12.   
  13.     RdfNode node = graph[subject];  
  14.   
  15.     node.Remove(foaf + "knows"new RdfLiteral("Bill Gates"));  
  16.   
  17.     foreach (RdfValue value in node[foaf + "knows"])  
  18.         Console.WriteLine(value);  
  19. }  

Output:

http://www.intellidimension.com/people#DRepchick
http://www.intellidimension.com/people#GChappell

Back to Top

How do I create a new node in a graph?

A new node can be created in a graph by instantiating a new instance of RdfNode and passing the associated datasource and subject to its constructor.

  1. public void Create_a_New_Node_in_a_Graph()  
  2. {  
  3.     String subject = "http://www.intellidimension.com/people#JBarron";  
  4.     String drepchick = "http://www.intellidimension.com/people#DRepchick";  
  5.     String foaf = "http://xmlns.com/foaf/0.1/";  
  6.   
  7.     GraphDataSource graph = new GraphDataSource();  
  8.   
  9.     graph.Add(subject, foaf + "name"new RdfLiteral("Justin Barron"));  
  10.     graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#GChappell"));  
  11.     graph.Add(subject, foaf + "knows"new RdfUri(drepchick));  
  12.   
  13.     RdfNode node = new RdfNode(graph, drepchick);  
  14.   
  15.     node.Add(foaf + "name"new RdfLiteral("Derrish Repchick"));  
  16.     node.Add(foaf + "knows"new RdfUri(subject));  
  17.   
  18.     foreach (Statement statement in graph.GetStatements(drepchick, nullnull))  
  19.         Console.WriteLine(statement.Subject + " " + statement.Predicate + " " + statement.Object);  
  20. }  

Output:

http://www.intellidimension.com/people#DRepchick http://xmlns.com/foaf/0.1/knows http://www.intellidimension.com/people#JBarron
http://www.intellidimension.com/people#DRepchick http://xmlns.com/foaf/0.1/name Derrish Repchick

Back to Top

How do I remove a node from a graph?

The RemoveAll() method of RdfNode will remove the node and all of its associated predicates and object values from the graph.

  1. public void Remove_a_Node_from_a_Graph()  
  2. {  
  3.     String subject = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.   
  6.     GraphDataSource graph = new GraphDataSource();  
  7.   
  8.     graph.Add(subject, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.     graph.Add(subject, foaf + "knows"new RdfUri("http://www.intellidimension.com/people#GChappell"));  
  10.   
  11.     graph[subject].RemoveAll();  
  12.   
  13.     Console.WriteLine(graph.Count);  
  14. }  

Output:

0

Back to Top

How do I create a literal RdfValue?

A basic RdfLiteral can be created by simply assigning a value to an RdfdLiteral object.

  1. public void Create_a_Literal_RdfValue()  
  2. {  
  3.     RdfLiteral literal = "My Literal";  
  4.   
  5.     Console.WriteLine(literal);  
  6. }  

Output:

My Literal

Back to Top

How do I create a typed literal RdfValue?

A typed literal can either be created explicitly or implicitly. Explicit declaration is accomplished by invoking the RdfLiteral(string, string) constructor with the literal's value and datatype as parameters. The NS class contains several built-in datatypes that can be statically referenced.

Implicit declaration is accomplished by simply assigning a value to an RdfValue object. Implicit conversion is supported for the following native .NET datatypes: bool, DatTime, Decimal, double, float, Int32, and string.

  1. public void Create_a_Typed_Literal_RdfValue()  
  2. {  
  3.     //Explicit Typing  
  4.     RdfLiteral strLiteral = new RdfLiteral("My Literal", NS.XsdString);  
  5.     RdfLiteral nLiteral = new RdfLiteral("2", NS.XsdInt);  
  6.     RdfLiteral bLiteral = new RdfLiteral("true", NS.XsdBoolean);  
  7.   
  8.     Console.WriteLine("Explicit Typing");  
  9.     Console.WriteLine(strLiteral.Value + "\t" + strLiteral.Datatype);  
  10.     Console.WriteLine(nLiteral.Value + "\t" + nLiteral.Datatype);  
  11.     Console.WriteLine(bLiteral.Value + "\t" + bLiteral.Datatype);  
  12.   
  13.     //Implicit Typing  
  14.     RdfLiteral implicitStrLiteral = "My Literal";  
  15.     RdfLiteral implicitIntLiteral = 7;  
  16.     RdfLiteral implicitBlnLiteral = false;  
  17.   
  18.     Console.WriteLine("\nImplicit Typing");  
  19.     Console.WriteLine(implicitStrLiteral.Value + "\t" + implicitStrLiteral.Datatype);  
  20.     Console.WriteLine(implicitIntLiteral.Value + "\t" + implicitIntLiteral.Datatype);  
  21.     Console.WriteLine(implicitBlnLiteral.Value + "\t" + implicitBlnLiteral.Datatype);  
  22. }  

Output:

Explicit Typing
My Literal     http://www.w3.org/2001/XMLSchema#string
2     http://www.w3.org/2001/XMLSchema#int
true     http://www.w3.org/2001/XMLSchema#boolean

Implicit Typing
My Literal
7     http://www.w3.org/2001/XMLSchema#int
false     http://www.w3.org/2001/XMLSchema#boolean

Back to Top

How do I create a typed literal RdfValue with a language tag?

To create a typed literal with a language tag, simply invoke the RdfLiteral(string, string, string) constructor with the literal's value, datatype, and language passed as parameters.

  1. public void Create_a_Typed_Literal_RdfValue_With_a_Language_Tag()  
  2. {  
  3.     RdfLiteral literal = new RdfLiteral("My Literal", NS.XsdString, "en");  
  4.   
  5.     Console.WriteLine(literal.Value + "\n" + literal.Datatype + "\n" + literal.Lang);  
  6. }  

Output:

My Literal
http://www.w3.org/2001/XMLSchema#string
en

Back to Top

How do I create a URI RdfValue?

The RdfUri object supports implicit casting from string values. Simply assign a URI string to an RdfUri object.

The RdfUri constructor may also be used, which accepts both string and System.Uri parameters.

  1. public void Create_a_URI_RdfValue()  
  2. {  
  3.     RdfUri uri = "http://www.intellidimension.com/people#JBarron";  
  4.   
  5.     Console.WriteLine(uri);  
  6. }  

Output:

http://www.intellidimension.com/people#JBarron

Back to Top

How do I modify an RdfValue after its been created?

RdfValue objects are immutable. Their values can not be changed after construction.

Back to Top

How do I use operators on an RdfValue?

All RdfValue objects support the equality operators == and !=.

The RdfLiteral object also supports the additive and multiplicative operators +, -, *, and /.

  1. public void Use_Operators_on_an_RdfValue()  
  2. {  
  3.     RdfLiteral num1 = 20;  
  4.     RdfLiteral num2 = 5;  
  5.   
  6.     RdfUri uri1 = "http://www.intellidimension.com/people#DRepchick";  
  7.     RdfUri uri2 = "http://www.intellidimension.com/people#GChappell";  
  8.   
  9.     Console.WriteLine(num1 == num2);  
  10.     Console.WriteLine(uri1 != uri2);  
  11.     Console.WriteLine(num1 + num2);  
  12.     Console.WriteLine(num1 - num2);  
  13.     Console.WriteLine(num1 * num2);  
  14.     Console.WriteLine(num1 / num2);  
  15. }  

Output:

False
True
25
15
100
4

Back to Top

How do I distinguish between the different types of RdfValues?

An RdfValue object can be identified as one of its derived classes by using the is keyword, as demonstrated in the example below.

  1. public void Distinguish_Between_the_Different_Types_of_RdfValues()  
  2. {  
  3.     RdfValue value = new RdfLiteral("My Literal");  
  4.   
  5.     if (value is RdfLiteral)  
  6.         Console.WriteLine(value + " is a literal.");  
  7.   
  8.     value = new RdfUri("http://www.intellidimension.com/");  
  9.   
  10.     if (value is RdfUri)  
  11.         Console.WriteLine(value + " is a URI.");  
  12. }  

Output:

My Literal is a literal.
http://www.intellidimension.com/ is a URI.

Back to Top

How do I query a datasource using SPARQL?

All DataSource objects have a Query(String) method that will execute the given SPARQL query on the graph and return the results as a Table.

  1. public void Query_a_Datasource_Using_SPARQL()  
  2. {  
  3.     String justin = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.     String derrish = "http://www.intellidimension.com/people#DRepchick";  
  6.   
  7.     GraphDataSource graph = new GraphDataSource();  
  8.   
  9.     graph.Add(justin, foaf + "name"new RdfLiteral("Justin Barron"));  
  10.     graph.Add(justin, foaf + "mbox"new RdfLiteral("jbarron@intellidimension.com"));  
  11.     graph.Add(justin, foaf + "knows"new RdfUri(derrish));  
  12.   
  13.     SparqlExpression query = SparqlExpression.Parse(@"PREFIX foaf: <" + foaf + @"> 
  14.                                                       SELECT ?s ?name ?mbox  
  15.                                                       WHERE { ?s foaf:name ?name . 
  16.                                                               ?s foaf:mbox ?mbox }");  
  17.     Table results = graph.Query(query.ToString());  
  18.   
  19.     for (int i = 0; i < results.ColumnCount; i++)  
  20.         Console.Write(results.GetColumnName(i) + "\t");  
  21.     Console.WriteLine();  
  22.     foreach (TableRow row in results.GetRows())  
  23.         Console.WriteLine(row);  
  24. }  

Output:

s name mbox
{<http://www.intellidimension.com/people#JBarron> "Justin Barron" "jbarron@intellidimension.com"}

Back to Top

How do I iterate over the results obtained from querying a datasource?

Since query results are returned as a Table, simply use the GetRows() method of Table to obtain an enumeration of result rows as TableRow objects.

  1. public void Iterate_Over_Query_Results()  
  2. {  
  3.     String justin = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.     String derrish = "http://www.intellidimension.com/people#DRepchick";  
  6.   
  7.     GraphDataSource graph = new GraphDataSource();  
  8.   
  9.     graph.Add(justin, foaf + "name"new RdfLiteral("Justin Barron"));  
  10.     graph.Add(justin, foaf + "mbox"new RdfLiteral("jbarron@intellidimension.com"));  
  11.     graph.Add(justin, foaf + "knows"new RdfUri(derrish));  
  12.   
  13.     SparqlExpression query = SparqlExpression.Parse(@"SELECT ?s ?p ?o  
  14.                                                       WHERE { ?s ?p ?o }");  
  15.     Table results = graph.Query(query.ToString());  
  16.   
  17.     for (int i = 0; i < results.ColumnCount; i++)  
  18.         Console.Write(results.GetColumnName(i) + "\t");  
  19.   
  20.     foreach (TableRow row in results.GetRows())  
  21.     {  
  22.         Console.WriteLine();  
  23.   
  24.         for (int i = 0; i < row.Count; i++)  
  25.             Console.Write(row[i] + "\t");  
  26.     }  
  27. }  

Output:

s p o
http://www.intellidimension.com/people#JBarron http://xmlns.com/foaf/0.1/knows Derrish Repchick
http://www.intellidimension.com/people#JBarron http://xmlns.com/foaf/0.1/mbox jbarron@intellidimension.com
http://www.intellidimension.com/people#JBarron http://xmlns.com/foaf/0.1/name Justin Barron

Back to Top

How do I add query results to another datasource?

All DataSource objects can be initialized with a Table object. Since query results are returned as a Table, simply invoke the GraphDataSource(Table) constructor with the resuts from the query.

Note: In order to add query results to a datasource, they must be in triple form with RdfUris for the context (optional), subject, and predicate; and an RdfValue for the object.

  1. public void Add_Query_Results_to_a_Datasource()  
  2. {  
  3.     String justin = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.     String derrish = "http://www.intellidimension.com/people#DRepchick";  
  6.   
  7.     GraphDataSource graph = new GraphDataSource();  
  8.   
  9.     graph.Add(justin, foaf + "name"new RdfLiteral("Justin Barron"));  
  10.     graph.Add(justin, foaf + "mbox"new RdfLiteral("jbarron@intellidimension.com"));  
  11.     graph.Add(justin, foaf + "knows"new RdfUri(derrish));  
  12.   
  13.     SparqlExpression query = SparqlExpression.Parse(@"SELECT ?s ?p ?o  
  14.                                                       WHERE { ?s ?p ?o }");  
  15.     Table results = graph.Query(query.ToString());  
  16.   
  17.     GraphDataSource resultsGraph = new GraphDataSource(results);  
  18.   
  19.     Console.WriteLine(resultsGraph.Count);  
  20. }  

Output:

3

Back to Top

How do I create a datasource?

A empty datasource for an RDF graph can be created simply by instantiating a new GraphDataSource, as shown below.

  1. public void Create_a_Datasource()  
  2. {  
  3.     GraphDataSource graph = new GraphDataSource();  
  4.   
  5.     Console.WriteLine(graph.Count);  
  6. }  

Output:

0

Back to Top

How do I load a datasource from a file or other resource?

All DataSource objects, in conjunction with an RdfReader, have overloaded Read<> methods that are capable of loading information from an input string, Uri (local or remote), and TextReader. The following example demonstrates how to load a datasource from an input String, a web Uri, and a StreamReader in conjunction with the NTriplesFormatter object.

  1. public void Load_a_Datasource_from_a_File_or_Other_Resource()  
  2. {  
  3.     GraphDataSource graph = new GraphDataSource();  
  4.   
  5.     //Load from string input  
  6.     graph.Read<NTriplesReader>("<http://www.intellidimension.com/people#JBarron> " +  
  7.                                "<http://xmlns.com/foaf/0.1/knows> " +  
  8.                                "<http://www.intellidimension.com/people#DRepchick> .");  
  9.   
  10.     Console.WriteLine("Loaded from string: " + graph.Count);  
  11.   
  12.     graph = new GraphDataSource();  
  13.   
  14.     //Load from file or uri  
  15.     graph.Read<NTriplesReader>(new Uri("http://www.w3.org/2000/10/rdf-tests/rdfcore/ntriples/test.nt"));  
  16.   
  17.     Console.WriteLine("Loaded from uri: " + graph.Count);  
  18.   
  19.     graph = new GraphDataSource();  
  20.   
  21.     //Load from StreamReader  
  22.     StreamReader reader = new StreamReader(WebRequest.Create("http://www.w3.org/2000/10/rdf-tests/rdfcore/ntriples/test.nt").GetResponse().GetResponseStream());  
  23.     graph.Read<NTriplesReader>(reader);  
  24.   
  25.     Console.WriteLine("Loaded from StreamReader: " + graph.Count);  
  26. }  

Output:

Loaded from string: 1
Loaded from uri: 30
Loaded from StreamReader: 30

Back to Top

How do I store a datasource in a file?

All DataSource objects have a Format<> method that works in conjunction with an RdfFormatter object to either return the formatted data as a string, or output it to the specified TextWriter. The example below demonstrates the latter.

  1. public void Store_a_Datasource_in_a_File()  
  2. {  
  3.     GraphDataSource graph = new GraphDataSource();  
  4.     graph.Read<NTriplesReader>(new Uri("http://www.w3.org/2000/10/rdf-tests/rdfcore/ntriples/test.nt"));  
  5.   
  6.     Console.WriteLine("Statements Read from Web: " + graph.Count);  
  7.   
  8.     String path = "c:\\MyPath\\output.nt";  
  9.   
  10.     //Store datasource in file as RdfXml  
  11.     using (StreamWriter writer = new StreamWriter(File.Open(path, FileMode.Create, FileAccess.Write)))  
  12.         graph.Format<RdfXmlFormatter>(writer);  
  13.   
  14.     //Reload datasource form file to verify contents  
  15.     graph = new GraphDataSource();  
  16.     using (StreamReader reader = new StreamReader(File.Open(path, FileMode.Open, FileAccess.Read)))  
  17.         graph.Read<RdfXmlReader>(reader);  
  18.   
  19.     Console.WriteLine("Statements Stored in File: " + graph.Count);  
  20. }  

Output:

Statements Read from Web: 30
Statements Stored in File: 30

Back to Top

How do I query multiple datasources at once?

The ClientModel object makes it easy to manage and query multiple datasources. To add a DataSource to the model, simply use the ClientModel.DataSources.Add(String, DataSource) method.

To query all datasources in the model, use the QueryAll<>(String) method of ClientModel in conjunction with a QueryParser and query String.

To query a subset of datasources in the model, specify each datasource in the query String and use the Query<>(String) method of ClientModel in conjunction with the desired QueryParser.

The following example demonstrates both the QueryAll<>(String) and Query<>(String) methods of ClientModel.

  1. public void Query_Mutiple_Datasources_at_Once()  
  2. {  
  3.     String justin = "http://www.intellidimension.com/people#JBarron";  
  4.     String derrish = "http://www.intellidimension.com/people#DRepchick";  
  5.     String geoff = "http://www.intellidimension.com/people#GChappell";  
  6.     String foaf = "http://xmlns.com/foaf/0.1/";  
  7.   
  8.     GraphDataSource jGraph = new GraphDataSource();  
  9.     jGraph.Add(justin, foaf + "name"new RdfLiteral("Justin Barron"));  
  10.     jGraph.Add(justin, foaf + "mbox"new RdfLiteral("jbarron@intellidimension.com"));  
  11.     jGraph.Add(justin, foaf + "knows"new RdfUri(derrish));  
  12.   
  13.     GraphDataSource dGraph = new GraphDataSource();  
  14.     dGraph.Add(derrish, foaf + "name"new RdfLiteral("Derrish Repchick"));  
  15.     dGraph.Add(derrish, foaf + "mbox"new RdfLiteral("drepchick@intellidimension.com"));  
  16.     dGraph.Add(derrish, foaf + "knows"new RdfUri(geoff));  
  17.   
  18.     GraphDataSource gGraph = new GraphDataSource();  
  19.     gGraph.Add(geoff, foaf + "name"new RdfLiteral("Geoff Chappell"));  
  20.     gGraph.Add(geoff, foaf + "mbox"new RdfLiteral("gchappell@intellidimension.com"));  
  21.     gGraph.Add(geoff, foaf + "knows"new RdfUri(derrish));  
  22.   
  23.     ClientModel model = new ClientModel();  
  24.     model.DataSources.Add(justin, jGraph);  
  25.     model.DataSources.Add(derrish, dGraph);  
  26.     model.DataSources.Add(geoff, gGraph);  
  27.   
  28.     SparqlExpression query = SparqlExpression.Parse(String.Format(@"PREFIX foaf: <{0}> 
  29.                                                                     SELECT ?s ?p ?o  
  30.                                                                     WHERE {{ ?s ?p ?o }} ", foaf));  
  31.   
  32.     Table results = model.QueryAll<SparqlParser>(query.ToString());  
  33.   
  34.     //Write query results to console  
  35.     for (int i = 0; i < results.ColumnCount; i++)  
  36.         Console.Write(results.GetColumnName(i) + "\t");  
  37.   
  38.     Console.WriteLine();  
  39.     foreach (TableRow row in results.GetRows())  
  40.         Console.WriteLine(row.ToString());  
  41.   
  42.     query = SparqlExpression.Parse(String.Format(@"PREFIX foaf: <{0}> 
  43.                                                    SELECT ?s ?p ?o  
  44.                                                    FROM <{1}> 
  45.                                                    FROM <{2}> 
  46.                                                    WHERE {{ ?s ?p ?o }}", foaf, justin, derrish));  
  47.   
  48.     results = model.Query<SparqlParser>(query.ToString());  
  49.   
  50.     //Write query results to console  
  51.     Console.WriteLine();  
  52.     for (int i = 0; i < results.ColumnCount; i++)  
  53.         Console.Write(results.GetColumnName(i) + "\t");  
  54.   
  55.     Console.WriteLine();  
  56.     foreach (TableRow row in results.GetRows())  
  57.         Console.WriteLine(row.ToString());  
  58. }  

Output:

s p o
{<http://www.intellidimension.com/people#DRepchick> <http://xmlns.com/foaf/0.1/knows> <http://www.intellidimension.com/people#GChappell>} {<http://www.intellidimension.com/people#DRepchick> <http://xmlns.com/foaf/0.1/mbox> "drepchick@intellidimension.com"}
{<http://www.intellidimension.com/people#DRepchick> <http://xmlns.com/foaf/0.1/name> "Derrish Repchick"}
{<http://www.intellidimension.com/people#GChappell> <http://xmlns.com/foaf/0.1/knows> <http://www.intellidimension.com/people#DRepchick>}
{<http://www.intellidimension.com/people#GChappell> <http://xmlns.com/foaf/0.1/mbox> "gchappell@intellidimension.com"}
{<http://www.intellidimension.com/people#GChappell> <http://xmlns.com/foaf/0.1/name> "Geoff Chappell"}
{<http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/knows> <http://www.intellidimension.com/people#DRepchick>}
{<http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/mbox> "jbarron@intellidimension.com"}
{<http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/name> "Justin Barron"}

s p o
{<http://www.intellidimension.com/people#DRepchick> <http://xmlns.com/foaf/0.1/knows> <http://www.intellidimension.com/people#GChappell>}
{<http://www.intellidimension.com/people#DRepchick> <http://xmlns.com/foaf/0.1/mbox> "drepchick@intellidimension.com"}
{<http://www.intellidimension.com/people#DRepchick> <http://xmlns.com/foaf/0.1/name> "Derrish Repchick"}
{<http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/knows> <http://www.intellidimension.com/people#DRepchick>}
{<http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/mbox> "jbarron@intellidimension.com"}
{<http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/name> "Justin Barron"}

Back to Top

How do I add a statement to a datasource?

All DataSource objects have overloaded Add methods that facilitate adding statements to the datasource. Statements may be added as individual Statement objects, a collection of Statement objects, or by manually specifying the context (optional), subject, predicate, and object of the statement to be added. The following example demonstrates all three cases.

  1. public void Add_a_Statement_to_a_Datasource()  
  2. {  
  3.     String justin = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.     String derrish = "http://www.intellidimension.com/people#DRepchick";  
  6.   
  7.     GraphDataSource graph = new GraphDataSource();  
  8.     Statement s1 = new Statement(justin, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.     Statement s2 = new Statement(justin, foaf + "mbox"new RdfLiteral("jbarron@intellidimension.com"));  
  10.     Statement s3 = new Statement(justin, foaf + "knows"new RdfUri(derrish));  
  11.   
  12.     //Add as a Statement object  
  13.     graph.Add(s1);  
  14.   
  15.     //Add collection of statements  
  16.     List<Statement> list = new List<Statement>();  
  17.     list.Add(s2);  
  18.     list.Add(s3);  
  19.     graph.Add(list);  
  20.   
  21.     //Add subject, predicate, and object (context optional)  
  22.     graph.Add(justin, foaf + "knows"new RdfLiteral("Bill Gates"));  
  23.   
  24.     Console.WriteLine("# Statements in Graph: " + graph.Count);  
  25. }  

Output:

# Statements in Graph: 4

Back to Top

How do I remove a statement from a datasource?

All DataSource objects have overloaded Remove methods that facilitate removing statements from the datasource. Statements may be removed one Statement object at a time, a collection of Statement objects, or by manually specifying the context (optional), subject, predicate, and object of the statement to be removed. The following example demonstrates two of these cases.

  1. public void Remove_a_Statement_from_a_Datasource()  
  2. {  
  3.     String justin = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.     String derrish = "http://www.intellidimension.com/people#DRepchick";  
  6.   
  7.     GraphDataSource graph = new GraphDataSource();  
  8.     graph.Add(justin, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.     graph.Add(justin, foaf + "mbox"new RdfLiteral("jbarron@intellidimension.com"));  
  10.     graph.Add(justin, foaf + "knows"new RdfUri(derrish));  
  11.   
  12.     Console.WriteLine("# Statements in Graph: " + graph.Count);  
  13.   
  14.     graph.Remove(new Statement(justin, foaf + "knows"new RdfUri(derrish)));  
  15.     graph.Remove(justin, foaf + "mbox"new RdfLiteral("jbarron@intellidimension.com"));  
  16.   
  17.     Console.WriteLine("# Statements Remaining: " + graph.Count);  
  18. }  

Output:

# Statements in Graph: 3
# Statements Remaining: 1

Back to Top

How do I easily get all the statements in a datasource?

An enumeration of Statement objects can be obtained from a DataSource by using its GetStatements() method, as demonstrated below.

  1. public void Get_All_the_Statements_in_a_Datasource()  
  2. {  
  3.     String justin = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.     String derrish = "http://www.intellidimension.com/people#DRepchick";  
  6.   
  7.     GraphDataSource graph = new GraphDataSource();  
  8.     graph.Add(justin, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.     graph.Add(justin, foaf + "mbox"new RdfLiteral("jbarron@intellidimension.com"));  
  10.     graph.Add(justin, foaf + "knows"new RdfUri(derrish));  
  11.   
  12.     foreach (Statement statement in graph.GetStatements())  
  13.         Console.WriteLine(statement);  
  14. }  

Output:

{<http://www.intellidimension.com/1999/02/server#null> <http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/knows> "Derrish Repchick"}
{<http://www.intellidimension.com/1999/02/server#null> <http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/mbox> "jbarron@intellidimension.com"}
{<http://www.intellidimension.com/1999/02/server#null> <http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/name> "Justin Barron"}

Back to Top

How do I parse an RDF/XML or N-Triples string and place the statements in a datasource?

To parse a string containing RDF-formatted data, simply invoke the Read<>(String) method of a DataSource object with the desired RdfReader and string input.

  1. public void Parse_RdfXml_or_NTriples_String()  
  2. {  
  3.     String rdfXml = @"<?xml version=""1.0"" encoding=""utf-16""?> 
  4.                         <rdf:RDF xmlns:rdf=""http://www.w3.org/1999/02/22-rdf-syntax-ns#"" xmlns:ns1=""http://xmlns.com/foaf/0.1/""> 
  5.                           <rdf:Description rdf:about=""http://www.intellidimension.com/people#JBarron""> 
  6.                             <ns1:knows>Derrish Repchick</ns1:knows> 
  7.                             <ns1:mbox>jbarron@intellidimension.com</ns1:mbox> 
  8.                             <ns1:name>Justin Barron</ns1:name> 
  9.                           </rdf:Description> 
  10.                         </rdf:RDF>";  
  11.   
  12.     GraphDataSource graph = new GraphDataSource();  
  13.     graph.Read<RdfXmlReader>(rdfXml);  
  14.     Console.WriteLine("# RdfXml Statements: " + graph.Count);  
  15.   
  16.     String justin = "http://www.intellidimension.com/people#JBarron";  
  17.     String foaf = "http://xmlns.com/foaf/0.1/";  
  18.     String derrish = "http://www.intellidimension.com/people#DRepchick";  
  19.   
  20.     String nTriples = String.Format(@"<{0}> <{1}knows> <{2}>. 
  21.                         <{0}> <{1}mbox> ""jbarron@intellidimension.com"". 
  22.                         <{0}> <{1}name> ""Justin Barron"".", justin, foaf, derrish);  
  23.   
  24.     graph = new GraphDataSource();  
  25.     graph.Read<NTriplesReader>(nTriples);  
  26.     Console.WriteLine("# N-Triples Statements: " + graph.Count);  
  27. }  

Output:

# RdfXml Statements: 3
# N-Triples Statements: 3

Back to Top

How do I set the base URI for relative URI's when parsing an RDF document?

All DataSource objects have overloaded Read<> methods that accept an additional Uri parameter. Use this parameter to specify a base URI for the document.

  1. public void Set_the_Base_URI_for_Relative_URIs_in_an_RDF_Document()  
  2. {  
  3.     String foaf = "http://xmlns.com/foaf/0.1/";  
  4.     String rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";  
  5.     String baseUri = "http://www.intellidimension.com/people";  
  6.   
  7.     String rdfXml = String.Format(@"<?xml version=""1.0"" encoding=""utf-16""?> 
  8.                                     <rdf:RDF xmlns:rdf=""{0}"" xmlns:ns1=""{1}""> 
  9.                                       <rdf:Description rdf:about=""#JBarron""> 
  10.                                         <ns1:knows rdf:resource=""#DRepchick"" /> 
  11.                                         <ns1:mbox>jbarron@intellidimension.com</ns1:mbox> 
  12.                                         <ns1:name>Justin Barron</ns1:name> 
  13.                                       </rdf:Description> 
  14.                                     </rdf:RDF>", rdf, foaf);  
  15.   
  16.     GraphDataSource graph = new GraphDataSource();  
  17.     graph.Read<RdfXmlReader>(rdfXml, new Uri(baseUri));  
  18.   
  19.     Console.WriteLine("# Statements Read: " + graph.Count);  
  20.   
  21.     foreach (Statement statement in graph.GetStatements())  
  22.         Console.WriteLine(statement);  
  23. }  

Output:

# Statements Read: 3
{<http://www.intellidimension.com/1999/02/server#null> <http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/knows> "Derrish Repchick"}
{<http://www.intellidimension.com/1999/02/server#null> <http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/mbox> "jbarron@intellidimension.com"}
{<http://www.intellidimension.com/1999/02/server#null> <http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/name> "Justin Barron"}

Back to Top

How do I handle RdfReader parsing errors?

If an RdfReader encounters a problem, it will throw an RdfReaderException. When thrown, an RdfReaderException will provide additional details of the error through its Line, Offset, and Message properties.

  1. public void Handling_RdfReader_Parsing_Errors()  
  2. {  
  3.     String nTriples = "<http://www.intelldimension.com/people#JBarron> \"This is not a valid predicate.\" \"My Object\".";  
  4.   
  5.     GraphDataSource graph = new GraphDataSource();  
  6.   
  7.     bool errorEncountered = false;  
  8.     try  
  9.     {  
  10.         graph.Read<NTriplesReader>(nTriples);  
  11.     }  
  12.     catch (RdfReaderException e)  
  13.     {  
  14.         errorEncountered = true;  
  15.         Console.WriteLine(String.Format("Exception caught on line {0}, offset {1}: {2}", e.Line, e.Offset, e.Message));  
  16.     }  
  17. }  

Output:

Exception caught on line 1, offset 48: predicate expected

Back to Top

How do I auto-detect the format of RDF data contained in a string, file, or URI?

Auto-detection of an RDF format is not currently supported. This feature will be available in a future release.

Back to Top

How do I format a datasource as RDF/XML or N-Triples?

All DataSource objects have a Format<> method that works in conjunction with an RdfFormatter object to either return the formatted data as a string, or output it to the specified TextWriter. The example below demonstrates the latter by outputting a datasource to the console as both RDF/XML and N-Triples.

  1. public void Format_a_Datasource_as_RdfXml_or_NTriples()  
  2. {  
  3.     String justin = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.     String derrish = "http://www.intellidimension.com/people#DRepchick";  
  6.   
  7.     GraphDataSource graph = new GraphDataSource();  
  8.     graph.Add(justin, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.     graph.Add(justin, foaf + "mbox"new RdfLiteral("jbarron@intellidimension.com"));  
  10.     graph.Add(justin, foaf + "knows"new RdfUri(derrish));  
  11.   
  12.     graph.Format<RdfXmlFormatter>(Console.Out);  
  13.     Console.WriteLine();  
  14.     Console.WriteLine();  
  15.     graph.Format<NTriplesFormatter>(Console.Out);  
  16. }  

Output:

<?xml version="1.0" encoding="utf-16"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:ns1="http://xmlns.com/foaf/0.1/">
  <rdf:Description rdf:about="http://www.intellidimension.com/people#JBarron">
    <ns1:knows rdf:resource="http://www.intellidimension.com/people#DRepchick" />
    <ns1:mbox>jbarron@intellidimension.com</ns1:mbox>
    <ns1:name>Justin Barron</ns1:name>
  </rdf:Description>
</rdf:RDF>

<http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/knows> <http://www.intellidimension.com/people#DRepchick>.
<http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/mbox> "jbarron@intellidimension.com".
<http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/name> "Justin Barron".

Back to Top

How do I specify namespaces for URIs when using RdfFormatter?

Specifying namespace URIs can be done by instantiating an RdfXmlFormatter object with RdfFormatterOptions. The options can then be accessed and modified, as shown in the example below.

  1. public void Specify_Namespaces_for_URIs_with_RdfFormatter()  
  2. {  
  3.     String justin = "http://www.intellidimension.com/people#JBarron";  
  4.     String foaf = "http://xmlns.com/foaf/0.1/";  
  5.     String derrish = "http://www.intellidimension.com/people#DRepchick";  
  6.   
  7.     GraphDataSource graph = new GraphDataSource();  
  8.     graph.Add(justin, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.     graph.Add(justin, foaf + "knows"new RdfUri(derrish));  
  10.   
  11.     RdfXmlFormatter rxf = new RdfXmlFormatter(graph, new RdfFormatterOptions());  
  12.     rxf.Options.Namespaces.Add(foaf, "foaf");  
  13.   
  14.     rxf.Write(Console.Out);  
  15. }  

Output:

<?xml version="1.0" encoding="utf-16"?>
<rdf:RDF xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about="http://www.intellidimension.com/people#JBarron">
    <foaf:knows rdf:resource="http://www.intellidimension.com/people#DRepchick" />
    <foaf:name>Justin Barron</foaf:name>
  </rdf:Description>
</rdf:RDF>

Back to Top

How do I create a model?

A model can be created simply by instantiating a new ClientModel object.

  1. public void Create_a_Model()  
  2. {  
  3.     //Instantiate ClientModel  
  4.     ClientModel model = new ClientModel();  
  5. }  

Back to Top

How do I add/remove datasources from a model?

A datasource can be added to a model by using the ClientModel.DataSources.Add(String, DataSource) method. Likewise, a datasource can be removed by using the ClientModel.DataSources.Remove(String) method. Both cases are demonstrated below.

  1. public void Add_or_Remove_Datasources_from_a_Model()  
  2. {  
  3.     String justin = "http://www.intellidimension.com/people#JBarron";  
  4.     String derrish = "http://www.intellidimension.com/people#DRepchick";  
  5.     String foaf = "http://xmlns.com/foaf/0.1/";  
  6.   
  7.     GraphDataSource graph = new GraphDataSource();  
  8.     graph.Add(justin, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.     graph.Add(justin, foaf + "knows"new RdfUri(derrish));  
  10.   
  11.     ClientModel model = new ClientModel();  
  12.   
  13.     Console.WriteLine(String.Format("# Datasources in Model Before Add: {0}", model.DataSources.Count));  
  14.   
  15.     model.DataSources.Add(justin, graph);  
  16.   
  17.     Console.WriteLine(String.Format("# Datasources in Model After Add: {0}", model.DataSources.Count));  
  18.   
  19.     model.DataSources.Remove(justin);  
  20.   
  21.     Console.WriteLine(String.Format("# Datasources in Model After Remove: {0}", model.DataSources.Count));  
  22. }  

Output:

# Datasources in Model Before Add: 0
# Datasources in Model After Add: 1
# Datasources in Model After Remove: 0

Back to Top

How do I query a model?

The ClientModel object makes it easy to manage and query multiple datasources. To add a DataSource to the model, simply use the ClientModel.DataSources.Add(String, DataSource) method.

To query all datasources in the model, use the QueryAll<>(String) method of ClientModel in conjunction with a QueryParser and query String.

To query a subset of datasources in the model, specify each datasource in the query String and use the Query<>(String) method of ClientModel in conjunction with the desired QueryParser.

The following example demonstrates both the QueryAll<>(String) and Query<>(String) methods of ClientModel.

  1. public void Query_a_Model()  
  2. {  
  3.     String justin = "http://www.intellidimension.com/people#JBarron";  
  4.     String derrish = "http://www.intellidimension.com/people#DRepchick";  
  5.     String geoff = "http://www.intellidimension.com/people#GChappell";  
  6.     String foaf = "http://xmlns.com/foaf/0.1/";  
  7.   
  8.     GraphDataSource jGraph = new GraphDataSource();  
  9.     jGraph.Add(justin, foaf + "name"new RdfLiteral("Justin Barron"));  
  10.     jGraph.Add(justin, foaf + "mbox"new RdfLiteral("jbarron@intellidimension.com"));  
  11.     jGraph.Add(justin, foaf + "knows"new RdfUri(derrish));  
  12.   
  13.     GraphDataSource dGraph = new GraphDataSource();  
  14.     dGraph.Add(derrish, foaf + "name"new RdfLiteral("Derrish Repchick"));  
  15.     dGraph.Add(derrish, foaf + "mbox"new RdfLiteral("drepchick@intellidimension.com"));  
  16.     dGraph.Add(derrish, foaf + "knows"new RdfUri(geoff));  
  17.   
  18.     GraphDataSource gGraph = new GraphDataSource();  
  19.     gGraph.Add(geoff, foaf + "name"new RdfLiteral("Geoff Chappell"));  
  20.     gGraph.Add(geoff, foaf + "mbox"new RdfLiteral("gchappell@intellidimension.com"));  
  21.     gGraph.Add(geoff, foaf + "knows"new RdfUri(derrish));  
  22.   
  23.     //Add the datasources to the model  
  24.     ClientModel model = new ClientModel();  
  25.     model.DataSources.Add(justin, jGraph);  
  26.     model.DataSources.Add(derrish, dGraph);  
  27.     model.DataSources.Add(geoff, gGraph);  
  28.   
  29.     //#Query All Datasources in the Model  
  30.     SparqlExpression query = SparqlExpression.Parse(String.Format(@"PREFIX foaf: <{0}> 
  31.                                                                     SELECT ?s ?p ?o  
  32.                                                                     WHERE {{ ?s ?p ?o }} ", foaf));  
  33.     Table results = model.QueryAll<SparqlParser>(query.ToString());  
  34.   
  35.     //Write results column names to console  
  36.     for (int i = 0; i < results.ColumnCount; i++)  
  37.         Console.Write(results.GetColumnName(i) + "\t");  
  38.   
  39.     //Write query results to console  
  40.     Console.WriteLine();  
  41.     foreach (TableRow row in results.GetRows())  
  42.         Console.WriteLine(row.ToString());  
  43.   
  44.     //#Query One or More Datasources in the Model  
  45.     query = SparqlExpression.Parse(String.Format(@"PREFIX foaf: <{0}> 
  46.                                                    SELECT ?s ?p ?o  
  47.                                                    FROM <{1}> 
  48.                                                    FROM <{2}> 
  49.                                                    WHERE {{ ?s ?p ?o }}", foaf, justin, derrish));  
  50.     results = model.Query<SparqlParser>(query.ToString());  
  51.   
  52.     //Write results column names to console  
  53.     Console.WriteLine();  
  54.     for (int i = 0; i < results.ColumnCount; i++)  
  55.         Console.Write(results.GetColumnName(i) + "\t");  
  56.   
  57.     //Write query results to console  
  58.     Console.WriteLine();  
  59.     foreach (TableRow row in results.GetRows())  
  60.         Console.WriteLine(row.ToString());  
  61. }  

Output:

s p o
{<http://www.intellidimension.com/people#DRepchick> <http://xmlns.com/foaf/0.1/knows> <http://www.intellidimension.com/people#GChappell>}
{<http://www.intellidimension.com/people#DRepchick> <http://xmlns.com/foaf/0.1/mbox> "drepchick@intellidimension.com"}
{<http://www.intellidimension.com/people#DRepchick> <http://xmlns.com/foaf/0.1/name> "Derrish Repchick"}
{<http://www.intellidimension.com/people#GChappell> <http://xmlns.com/foaf/0.1/knows> <http://www.intellidimension.com/people#DRepchick>}
{<http://www.intellidimension.com/people#GChappell> <http://xmlns.com/foaf/0.1/mbox> "gchappell@intellidimension.com"}
{<http://www.intellidimension.com/people#GChappell> <http://xmlns.com/foaf/0.1/name> "Geoff Chappell"}
{<http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/knows> <http://www.intellidimension.com/people#DRepchick>}
{<http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/mbox> "jbarron@intellidimension.com"}
{<http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/name> "Justin Barron"}

s p o
{<http://www.intellidimension.com/people#DRepchick> <http://xmlns.com/foaf/0.1/knows> <http://www.intellidimension.com/people#GChappell>}
{<http://www.intellidimension.com/people#DRepchick> <http://xmlns.com/foaf/0.1/mbox> "drepchick@intellidimension.com"}
{<http://www.intellidimension.com/people#DRepchick> <http://xmlns.com/foaf/0.1/name> "Derrish Repchick"}
{<http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/knows> <http://www.intellidimension.com/people#DRepchick>}
{<http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/mbox> "jbarron@intellidimension.com"}
{<http://www.intellidimension.com/people#JBarron> <http://xmlns.com/foaf/0.1/name> "Justin Barron"}

Back to Top

Can I serialize an entire model?

Currently, it is not possible to serialize an entire model. However, it is possible to serialize individual datasources.

Back to Top

Can a datasource be added to multiple models?

It is possible to add a datasource to multiple models; but it is important to know that a model does not copy a datasource, it only references it. Therefore, if you add a datasource to multiple models, any changes made to that datasource will affect each model. This behavior is demonstrated below.

  1. public void Add_a_Datasource_to_Multiple_Models()  
  2. {  
  3.     String justin = "http://www.intellidimension.com/people#JBarron";  
  4.     String derrish = "http://www.intellidimension.com/people#DRepchick";  
  5.     String foaf = "http://xmlns.com/foaf/0.1/";  
  6.   
  7.     GraphDataSource graph = new GraphDataSource();  
  8.     graph.Add(justin, foaf + "name"new RdfLiteral("Justin Barron"));  
  9.     graph.Add(justin, foaf + "knows"new RdfUri(derrish));  
  10.   
  11.     ClientModel m1 = new ClientModel();  
  12.     m1.DataSources.Add(justin, graph);  
  13.   
  14.     ClientModel m2 = new ClientModel();  
  15.     m2.DataSources.Add(justin, graph);  
  16.   
  17.     Console.WriteLine("M1: " + m1.DataSources[justin].Count);  
  18.     Console.WriteLine("M2: " + m2.DataSources[justin].Count);  
  19.   
  20.     Console.WriteLine();  
  21.     m1.DataSources[justin].Remove(justin, foaf + "knows"new RdfUri(derrish));  
  22.   
  23.     Console.WriteLine("M1: " + m1.DataSources[justin].Count);  
  24.     Console.WriteLine("M2: " + m2.DataSources[justin].Count);  
  25. }  

Output:

M1: 2
M2: 2

M1: 1
M2: 1

Back to Top

How do I validate the syntax of a SPARQL query?

The SparqlExpression class provides functionality for validating SPARQL query syntax. Simply use the TryParse(string, out SparqlExpression) method, as demonstrated below.

  1. public void Validate_the_Syntax_of_a_SPARQL_Query()  
  2. {  
  3.     String query = "SELECT ?s ?p ?o WHERE { ?s ?p ?o . }";  
  4.     SparqlExpression exp;  
  5.   
  6.     if (SparqlExpression.TryParse(query, out exp))  
  7.         Console.WriteLine("Syntax is valid.");  
  8.     else  
  9.         Console.WriteLine("Syntax is invalid.");  
  10.   
  11.     query = "SELECT ?s ?p ?o WHERE { ? }";  
  12.   
  13.     if (SparqlExpression.TryParse(query, out exp))  
  14.         Console.WriteLine("Syntax is valid.");  
  15.     else  
  16.         Console.WriteLine("Syntax is invalid.");  
  17. }  

Output:

Syntax is valid.
Syntax is invalid.

Back to Top

How do I handle errors when parsing a SPARQL query?

The SparqlExpression class will throw an exception if an error is encountered during a parse operation. To handle, simply catch the SparqlQueryFormatException object.

  1. public void Hanlding_Errors_When_Parsing_a_SPARQL_Query()  
  2. {  
  3.     String query = "SELECT ?s ?p ?o WHERE { ? }";  
  4.   
  5.     try  
  6.     {  
  7.         SparqlExpression.Parse(query);  
  8.     }  
  9.     catch (SparqlQueryFormatException e)  
  10.     {  
  11.         Console.WriteLine("Parsing Exception Caught: '{0}'", e.Message);  
  12.     }  
  13. }  

Output:

Parsing Exception Caught: 'Expected GroupGraphPattern'

Back to Top