Questions related to nodes in a graph:
- How do I obtain a node from a graph?
- How do I iterate the predicates of a node?
- How do I iterate the object values of a predicate in a node?
- How do I obtain a single object value of a predicate in a node?
- How do I set an object value for a predicate in a node?
- How do I add an aditional object value to a predicate in a node?
- How do I replace an existing object value of a predicate in a node?
- How do I remove an object value from a predicate in a node?
- How do I remove a single object value from a multi-value predicate in a node
- How do I create a new node in a graph?
- How do I remove a node from a graph?
Questions related to RdfValues:
- How do I create a literal RdfValue?
- How do I create a typed literal RdfValue?
- How do I create a typed literal RdfValue with a language tag?
- How do I create a URI RdfValue?
- How do I modify an RdfValue after its been created?
- How do I use operators on an RdfValue?
- How do I distinguish between the different types of RdfValues?
Questions related to datasources:
- How do I query a datasource using SPARQL?
- How do I iterate over the results obtained from querying a datasource?
- How do I add query results to another datasource?
- How do I create a datasource?
- How do I load a datasource from a file or other resource?
- How do I store a datasource in a file?
- How do I query multiple datasources at once?
- How do I add a statement to a datasource?
- How do I remove a statement from a datasource?
- 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.
- public void Get_a_Node_from_a_Graph()
- {
- String subject = "http://www.intellidimension.com/people#JBarron";
-
- GraphDataSource graph = new GraphDataSource();
-
- graph.Add(subject, "http://xmlns.com/foaf/0.1/name", new RdfLiteral("Justin Barron"));
- graph.Add(subject, "http://xmlns.com/foaf/0.1/mbox", new RdfLiteral("jbarron@intellidimension.com"));
-
- RdfNode node = graph[subject];
-
- Console.WriteLine(node);
- }
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.
- public void Iterate_Predicates_of_a_Node()
- {
- String subject = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
-
- GraphDataSource graph = new GraphDataSource();
-
- graph.Add(subject, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(subject, foaf + "mbox", new RdfLiteral("jbarron@intellidimension.com"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#GChappell"));
-
- foreach (RdfUri predicate in graph[subject].GetPredicates())
- Console.WriteLine(predicate);
- }
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.
- public void Iterate_Object_Values_of_a_Predicate_in_a_Node()
- {
- String subject = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
-
- GraphDataSource graph = new GraphDataSource();
-
- graph.Add(subject, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#GChappell"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#DRepchick"));
-
- RdfNode node = graph[subject];
-
- foreach (RdfValue obj in node[foaf + "knows"])
- Console.WriteLine(obj);
- }
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.
- public void Get_a_Single_Object_Value_for_a_Predicate_in_a_Node()
- {
- String subject = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
-
- GraphDataSource graph = new GraphDataSource();
-
- graph.Add(subject, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#GChappell"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#DRepchick"));
-
- RdfNode node = graph[subject];
-
- RdfValue val = node[foaf + "name"];
-
- Console.WriteLine(val);
- }
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.
- public void Set_an_Object_Value_for_a_Predicate_in_a_Node()
- {
- String subject = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
-
- GraphDataSource graph = new GraphDataSource();
-
- graph.Add(subject, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#GChappell"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#DRepchick"));
-
- RdfNode node = graph[subject];
-
- node.Set(foaf + "knows", new RdfLiteral("Bill Gates"));
-
- RdfValue val = node[foaf + "knows"];
- Console.WriteLine(val);
- }
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.
- public void Add_an_Additional_Object_Value_to_a_Predicate_in_a_Node()
- {
- String subject = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
-
- GraphDataSource graph = new GraphDataSource();
-
- graph.Add(subject, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#GChappell"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#DRepchick"));
-
- RdfNode node = graph[subject];
-
- node.Add(foaf + "knows", new RdfLiteral("Bill Gates"));
-
- foreach (RdfValue val in node[foaf + "knows"])
- Console.WriteLine(val);
- }
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.
- public void Replace_an_Existing_Object_Value_of_a_Predicate_in_a_Node()
- {
- String subject = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
-
- GraphDataSource graph = new GraphDataSource();
-
- graph.Add(subject, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#GChappell"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#DRepchick"));
-
- RdfNode node = graph[subject];
-
- node.Set(foaf + "name", new RdfLiteral("J B"), new RdfLiteral("Justin Barron"));
-
- RdfValue val = node[foaf + "name"];
- Console.WriteLine(val);
- }
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.
- public void Remove_an_Object_Value_from_a_Predicate_in_a_Node()
- {
- String subject = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
-
- GraphDataSource graph = new GraphDataSource();
-
- graph.Add(subject, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#GChappell"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#DRepchick"));
-
- RdfNode node = graph[subject];
-
- node.Remove(foaf + "knows");
- foreach (RdfUri predicate in node.GetPredicates())
- Console.WriteLine(predicate);
- }
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.
- public void Remove_a_Single_Object_Value_from_a_Multi_Value_Predicate_in_a_Node()
- {
- String subject = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
-
- GraphDataSource graph = new GraphDataSource();
-
- graph.Add(subject, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#GChappell"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#DRepchick"));
- graph.Add(subject, foaf + "knows", new RdfLiteral("Bill Gates"));
-
- RdfNode node = graph[subject];
-
- node.Remove(foaf + "knows", new RdfLiteral("Bill Gates"));
-
- foreach (RdfValue value in node[foaf + "knows"])
- Console.WriteLine(value);
- }
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.
- public void Create_a_New_Node_in_a_Graph()
- {
- String subject = "http://www.intellidimension.com/people#JBarron";
- String drepchick = "http://www.intellidimension.com/people#DRepchick";
- String foaf = "http://xmlns.com/foaf/0.1/";
-
- GraphDataSource graph = new GraphDataSource();
-
- graph.Add(subject, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#GChappell"));
- graph.Add(subject, foaf + "knows", new RdfUri(drepchick));
-
- RdfNode node = new RdfNode(graph, drepchick);
-
- node.Add(foaf + "name", new RdfLiteral("Derrish Repchick"));
- node.Add(foaf + "knows", new RdfUri(subject));
-
- foreach (Statement statement in graph.GetStatements(drepchick, null, null))
- Console.WriteLine(statement.Subject + " " + statement.Predicate + " " + statement.Object);
- }
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.
- public void Remove_a_Node_from_a_Graph()
- {
- String subject = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
-
- GraphDataSource graph = new GraphDataSource();
-
- graph.Add(subject, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(subject, foaf + "knows", new RdfUri("http://www.intellidimension.com/people#GChappell"));
-
- graph[subject].RemoveAll();
-
- Console.WriteLine(graph.Count);
- }
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.
- public void Create_a_Literal_RdfValue()
- {
- RdfLiteral literal = "My Literal";
-
- Console.WriteLine(literal);
- }
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
.
- public void Create_a_Typed_Literal_RdfValue()
- {
-
- RdfLiteral strLiteral = new RdfLiteral("My Literal", NS.XsdString);
- RdfLiteral nLiteral = new RdfLiteral("2", NS.XsdInt);
- RdfLiteral bLiteral = new RdfLiteral("true", NS.XsdBoolean);
-
- Console.WriteLine("Explicit Typing");
- Console.WriteLine(strLiteral.Value + "\t" + strLiteral.Datatype);
- Console.WriteLine(nLiteral.Value + "\t" + nLiteral.Datatype);
- Console.WriteLine(bLiteral.Value + "\t" + bLiteral.Datatype);
-
-
- RdfLiteral implicitStrLiteral = "My Literal";
- RdfLiteral implicitIntLiteral = 7;
- RdfLiteral implicitBlnLiteral = false;
-
- Console.WriteLine("\nImplicit Typing");
- Console.WriteLine(implicitStrLiteral.Value + "\t" + implicitStrLiteral.Datatype);
- Console.WriteLine(implicitIntLiteral.Value + "\t" + implicitIntLiteral.Datatype);
- Console.WriteLine(implicitBlnLiteral.Value + "\t" + implicitBlnLiteral.Datatype);
- }
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.
- public void Create_a_Typed_Literal_RdfValue_With_a_Language_Tag()
- {
- RdfLiteral literal = new RdfLiteral("My Literal", NS.XsdString, "en");
-
- Console.WriteLine(literal.Value + "\n" + literal.Datatype + "\n" + literal.Lang);
- }
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.
- public void Create_a_URI_RdfValue()
- {
- RdfUri uri = "http://www.intellidimension.com/people#JBarron";
-
- Console.WriteLine(uri);
- }
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 /
.
- public void Use_Operators_on_an_RdfValue()
- {
- RdfLiteral num1 = 20;
- RdfLiteral num2 = 5;
-
- RdfUri uri1 = "http://www.intellidimension.com/people#DRepchick";
- RdfUri uri2 = "http://www.intellidimension.com/people#GChappell";
-
- Console.WriteLine(num1 == num2);
- Console.WriteLine(uri1 != uri2);
- Console.WriteLine(num1 + num2);
- Console.WriteLine(num1 - num2);
- Console.WriteLine(num1 * num2);
- Console.WriteLine(num1 / num2);
- }
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.
- public void Distinguish_Between_the_Different_Types_of_RdfValues()
- {
- RdfValue value = new RdfLiteral("My Literal");
-
- if (value is RdfLiteral)
- Console.WriteLine(value + " is a literal.");
-
- value = new RdfUri("http://www.intellidimension.com/");
-
- if (value is RdfUri)
- Console.WriteLine(value + " is a URI.");
- }
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
.
- public void Query_a_Datasource_Using_SPARQL()
- {
- String justin = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
- String derrish = "http://www.intellidimension.com/people#DRepchick";
-
- GraphDataSource graph = new GraphDataSource();
-
- graph.Add(justin, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(justin, foaf + "mbox", new RdfLiteral("jbarron@intellidimension.com"));
- graph.Add(justin, foaf + "knows", new RdfUri(derrish));
-
- SparqlExpression query = SparqlExpression.Parse(@"PREFIX foaf: <" + foaf + @">
-
SELECT ?s ?name ?mbox
-
WHERE { ?s foaf:name ?name .
-
?s foaf:mbox ?mbox }");
- Table results = graph.Query(query.ToString());
-
- for (int i = 0; i < results.ColumnCount; i++)
- Console.Write(results.GetColumnName(i) + "\t");
- Console.WriteLine();
- foreach (TableRow row in results.GetRows())
- Console.WriteLine(row);
- }
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.
- public void Iterate_Over_Query_Results()
- {
- String justin = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
- String derrish = "http://www.intellidimension.com/people#DRepchick";
-
- GraphDataSource graph = new GraphDataSource();
-
- graph.Add(justin, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(justin, foaf + "mbox", new RdfLiteral("jbarron@intellidimension.com"));
- graph.Add(justin, foaf + "knows", new RdfUri(derrish));
-
- SparqlExpression query = SparqlExpression.Parse(@"SELECT ?s ?p ?o
-
WHERE { ?s ?p ?o }");
- Table results = graph.Query(query.ToString());
-
- for (int i = 0; i < results.ColumnCount; i++)
- Console.Write(results.GetColumnName(i) + "\t");
-
- foreach (TableRow row in results.GetRows())
- {
- Console.WriteLine();
-
- for (int i = 0; i < row.Count; i++)
- Console.Write(row[i] + "\t");
- }
- }
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 RdfUri
s for the context (optional), subject, and predicate; and an RdfValue
for the object.
- public void Add_Query_Results_to_a_Datasource()
- {
- String justin = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
- String derrish = "http://www.intellidimension.com/people#DRepchick";
-
- GraphDataSource graph = new GraphDataSource();
-
- graph.Add(justin, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(justin, foaf + "mbox", new RdfLiteral("jbarron@intellidimension.com"));
- graph.Add(justin, foaf + "knows", new RdfUri(derrish));
-
- SparqlExpression query = SparqlExpression.Parse(@"SELECT ?s ?p ?o
-
WHERE { ?s ?p ?o }");
- Table results = graph.Query(query.ToString());
-
- GraphDataSource resultsGraph = new GraphDataSource(results);
-
- Console.WriteLine(resultsGraph.Count);
- }
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.
- public void Create_a_Datasource()
- {
- GraphDataSource graph = new GraphDataSource();
-
- Console.WriteLine(graph.Count);
- }
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.
- public void Load_a_Datasource_from_a_File_or_Other_Resource()
- {
- GraphDataSource graph = new GraphDataSource();
-
-
- graph.Read<NTriplesReader>("<http://www.intellidimension.com/people#JBarron> " +
- "<http://xmlns.com/foaf/0.1/knows> " +
- "<http://www.intellidimension.com/people#DRepchick> .");
-
- Console.WriteLine("Loaded from string: " + graph.Count);
-
- graph = new GraphDataSource();
-
-
- graph.Read<NTriplesReader>(new Uri("http://www.w3.org/2000/10/rdf-tests/rdfcore/ntriples/test.nt"));
-
- Console.WriteLine("Loaded from uri: " + graph.Count);
-
- graph = new GraphDataSource();
-
-
- StreamReader reader = new StreamReader(WebRequest.Create("http://www.w3.org/2000/10/rdf-tests/rdfcore/ntriples/test.nt").GetResponse().GetResponseStream());
- graph.Read<NTriplesReader>(reader);
-
- Console.WriteLine("Loaded from StreamReader: " + graph.Count);
- }
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.
- public void Store_a_Datasource_in_a_File()
- {
- GraphDataSource graph = new GraphDataSource();
- graph.Read<NTriplesReader>(new Uri("http://www.w3.org/2000/10/rdf-tests/rdfcore/ntriples/test.nt"));
-
- Console.WriteLine("Statements Read from Web: " + graph.Count);
-
- String path = "c:\\MyPath\\output.nt";
-
-
- using (StreamWriter writer = new StreamWriter(File.Open(path, FileMode.Create, FileAccess.Write)))
- graph.Format<RdfXmlFormatter>(writer);
-
-
- graph = new GraphDataSource();
- using (StreamReader reader = new StreamReader(File.Open(path, FileMode.Open, FileAccess.Read)))
- graph.Read<RdfXmlReader>(reader);
-
- Console.WriteLine("Statements Stored in File: " + graph.Count);
- }
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
.
- public void Query_Mutiple_Datasources_at_Once()
- {
- String justin = "http://www.intellidimension.com/people#JBarron";
- String derrish = "http://www.intellidimension.com/people#DRepchick";
- String geoff = "http://www.intellidimension.com/people#GChappell";
- String foaf = "http://xmlns.com/foaf/0.1/";
-
- GraphDataSource jGraph = new GraphDataSource();
- jGraph.Add(justin, foaf + "name", new RdfLiteral("Justin Barron"));
- jGraph.Add(justin, foaf + "mbox", new RdfLiteral("jbarron@intellidimension.com"));
- jGraph.Add(justin, foaf + "knows", new RdfUri(derrish));
-
- GraphDataSource dGraph = new GraphDataSource();
- dGraph.Add(derrish, foaf + "name", new RdfLiteral("Derrish Repchick"));
- dGraph.Add(derrish, foaf + "mbox", new RdfLiteral("drepchick@intellidimension.com"));
- dGraph.Add(derrish, foaf + "knows", new RdfUri(geoff));
-
- GraphDataSource gGraph = new GraphDataSource();
- gGraph.Add(geoff, foaf + "name", new RdfLiteral("Geoff Chappell"));
- gGraph.Add(geoff, foaf + "mbox", new RdfLiteral("gchappell@intellidimension.com"));
- gGraph.Add(geoff, foaf + "knows", new RdfUri(derrish));
-
- ClientModel model = new ClientModel();
- model.DataSources.Add(justin, jGraph);
- model.DataSources.Add(derrish, dGraph);
- model.DataSources.Add(geoff, gGraph);
-
-
SparqlExpression query = SparqlExpression.Parse(String.Format(@"PREFIX foaf: <{0}>
-
SELECT ?s ?p ?o
-
WHERE {{ ?s ?p ?o }} ", foaf));
-
-
Table results = model.QueryAll<SparqlParser>(query.ToString());
-
-
- for (int i = 0; i < results.ColumnCount; i++)
- Console.Write(results.GetColumnName(i) + "\t");
-
- Console.WriteLine();
- foreach (TableRow row in results.GetRows())
- Console.WriteLine(row.ToString());
-
- query = SparqlExpression.Parse(String.Format(@"PREFIX foaf: <{0}>
-
SELECT ?s ?p ?o
-
FROM <{1}>
-
FROM <{2}>
-
WHERE {{ ?s ?p ?o }}", foaf, justin, derrish));
-
- results = model.Query<SparqlParser>(query.ToString());
-
-
- Console.WriteLine();
- for (int i = 0; i < results.ColumnCount; i++)
- Console.Write(results.GetColumnName(i) + "\t");
-
- Console.WriteLine();
- foreach (TableRow row in results.GetRows())
- Console.WriteLine(row.ToString());
- }
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.
- public void Add_a_Statement_to_a_Datasource()
- {
- String justin = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
- String derrish = "http://www.intellidimension.com/people#DRepchick";
-
- GraphDataSource graph = new GraphDataSource();
- Statement s1 = new Statement(justin, foaf + "name", new RdfLiteral("Justin Barron"));
- Statement s2 = new Statement(justin, foaf + "mbox", new RdfLiteral("jbarron@intellidimension.com"));
- Statement s3 = new Statement(justin, foaf + "knows", new RdfUri(derrish));
-
-
- graph.Add(s1);
-
-
- List<Statement> list = new List<Statement>();
- list.Add(s2);
- list.Add(s3);
- graph.Add(list);
-
-
- graph.Add(justin, foaf + "knows", new RdfLiteral("Bill Gates"));
-
- Console.WriteLine("# Statements in Graph: " + graph.Count);
- }
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.
- public void Remove_a_Statement_from_a_Datasource()
- {
- String justin = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
- String derrish = "http://www.intellidimension.com/people#DRepchick";
-
- GraphDataSource graph = new GraphDataSource();
- graph.Add(justin, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(justin, foaf + "mbox", new RdfLiteral("jbarron@intellidimension.com"));
- graph.Add(justin, foaf + "knows", new RdfUri(derrish));
-
- Console.WriteLine("# Statements in Graph: " + graph.Count);
-
- graph.Remove(new Statement(justin, foaf + "knows", new RdfUri(derrish)));
- graph.Remove(justin, foaf + "mbox", new RdfLiteral("jbarron@intellidimension.com"));
-
- Console.WriteLine("# Statements Remaining: " + graph.Count);
- }
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.
- public void Get_All_the_Statements_in_a_Datasource()
- {
- String justin = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
- String derrish = "http://www.intellidimension.com/people#DRepchick";
-
- GraphDataSource graph = new GraphDataSource();
- graph.Add(justin, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(justin, foaf + "mbox", new RdfLiteral("jbarron@intellidimension.com"));
- graph.Add(justin, foaf + "knows", new RdfUri(derrish));
-
- foreach (Statement statement in graph.GetStatements())
- Console.WriteLine(statement);
- }
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.
- public void Parse_RdfXml_or_NTriples_String()
- {
- String rdfXml = @"<?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>Derrish Repchick</ns1:knows>
-
<ns1:mbox>jbarron@intellidimension.com</ns1:mbox>
-
<ns1:name>Justin Barron</ns1:name>
-
</rdf:Description>
- </rdf:RDF>";
-
- GraphDataSource graph = new GraphDataSource();
- graph.Read<RdfXmlReader>(rdfXml);
- Console.WriteLine("# RdfXml Statements: " + graph.Count);
-
- String justin = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
- String derrish = "http://www.intellidimension.com/people#DRepchick";
-
- String nTriples = String.Format(@"<{0}> <{1}knows> <{2}>.
-
<{0}> <{1}mbox> ""jbarron@intellidimension.com"".
-
<{0}> <{1}name> ""Justin Barron"".", justin, foaf, derrish);
-
- graph = new GraphDataSource();
- graph.Read<NTriplesReader>(nTriples);
- Console.WriteLine("# N-Triples Statements: " + graph.Count);
- }
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.
- public void Set_the_Base_URI_for_Relative_URIs_in_an_RDF_Document()
- {
- String foaf = "http://xmlns.com/foaf/0.1/";
- String rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
- String baseUri = "http://www.intellidimension.com/people";
-
- String rdfXml = String.Format(@"<?xml version=""1.0"" encoding=""utf-16""?>
-
<rdf:RDF xmlns:rdf=""{0}"" xmlns:ns1=""{1}"">
-
<rdf:Description rdf:about=""#JBarron"">
-
<ns1:knows rdf:resource=""#DRepchick"" />
-
<ns1:mbox>jbarron@intellidimension.com</ns1:mbox>
-
<ns1:name>Justin Barron</ns1:name>
-
</rdf:Description>
-
</rdf:RDF>", rdf, foaf);
-
- GraphDataSource graph = new GraphDataSource();
- graph.Read<RdfXmlReader>(rdfXml, new Uri(baseUri));
-
- Console.WriteLine("# Statements Read: " + graph.Count);
-
- foreach (Statement statement in graph.GetStatements())
- Console.WriteLine(statement);
- }
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.
- public void Handling_RdfReader_Parsing_Errors()
- {
- String nTriples = "<http://www.intelldimension.com/people#JBarron> \"This is not a valid predicate.\" \"My Object\".";
-
- GraphDataSource graph = new GraphDataSource();
-
- bool errorEncountered = false;
- try
- {
- graph.Read<NTriplesReader>(nTriples);
- }
- catch (RdfReaderException e)
- {
- errorEncountered = true;
- Console.WriteLine(String.Format("Exception caught on line {0}, offset {1}: {2}", e.Line, e.Offset, e.Message));
- }
- }
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.
- public void Format_a_Datasource_as_RdfXml_or_NTriples()
- {
- String justin = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
- String derrish = "http://www.intellidimension.com/people#DRepchick";
-
- GraphDataSource graph = new GraphDataSource();
- graph.Add(justin, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(justin, foaf + "mbox", new RdfLiteral("jbarron@intellidimension.com"));
- graph.Add(justin, foaf + "knows", new RdfUri(derrish));
-
- graph.Format<RdfXmlFormatter>(Console.Out);
- Console.WriteLine();
- Console.WriteLine();
- graph.Format<NTriplesFormatter>(Console.Out);
- }
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.
- public void Specify_Namespaces_for_URIs_with_RdfFormatter()
- {
- String justin = "http://www.intellidimension.com/people#JBarron";
- String foaf = "http://xmlns.com/foaf/0.1/";
- String derrish = "http://www.intellidimension.com/people#DRepchick";
-
- GraphDataSource graph = new GraphDataSource();
- graph.Add(justin, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(justin, foaf + "knows", new RdfUri(derrish));
-
- RdfXmlFormatter rxf = new RdfXmlFormatter(graph, new RdfFormatterOptions());
- rxf.Options.Namespaces.Add(foaf, "foaf");
-
- rxf.Write(Console.Out);
- }
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.
- public void Create_a_Model()
- {
-
- ClientModel model = new ClientModel();
- }
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.
- public void Add_or_Remove_Datasources_from_a_Model()
- {
- String justin = "http://www.intellidimension.com/people#JBarron";
- String derrish = "http://www.intellidimension.com/people#DRepchick";
- String foaf = "http://xmlns.com/foaf/0.1/";
-
- GraphDataSource graph = new GraphDataSource();
- graph.Add(justin, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(justin, foaf + "knows", new RdfUri(derrish));
-
- ClientModel model = new ClientModel();
-
- Console.WriteLine(String.Format("# Datasources in Model Before Add: {0}", model.DataSources.Count));
-
- model.DataSources.Add(justin, graph);
-
- Console.WriteLine(String.Format("# Datasources in Model After Add: {0}", model.DataSources.Count));
-
- model.DataSources.Remove(justin);
-
- Console.WriteLine(String.Format("# Datasources in Model After Remove: {0}", model.DataSources.Count));
- }
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
.
- public void Query_a_Model()
- {
- String justin = "http://www.intellidimension.com/people#JBarron";
- String derrish = "http://www.intellidimension.com/people#DRepchick";
- String geoff = "http://www.intellidimension.com/people#GChappell";
- String foaf = "http://xmlns.com/foaf/0.1/";
-
- GraphDataSource jGraph = new GraphDataSource();
- jGraph.Add(justin, foaf + "name", new RdfLiteral("Justin Barron"));
- jGraph.Add(justin, foaf + "mbox", new RdfLiteral("jbarron@intellidimension.com"));
- jGraph.Add(justin, foaf + "knows", new RdfUri(derrish));
-
- GraphDataSource dGraph = new GraphDataSource();
- dGraph.Add(derrish, foaf + "name", new RdfLiteral("Derrish Repchick"));
- dGraph.Add(derrish, foaf + "mbox", new RdfLiteral("drepchick@intellidimension.com"));
- dGraph.Add(derrish, foaf + "knows", new RdfUri(geoff));
-
- GraphDataSource gGraph = new GraphDataSource();
- gGraph.Add(geoff, foaf + "name", new RdfLiteral("Geoff Chappell"));
- gGraph.Add(geoff, foaf + "mbox", new RdfLiteral("gchappell@intellidimension.com"));
- gGraph.Add(geoff, foaf + "knows", new RdfUri(derrish));
-
-
- ClientModel model = new ClientModel();
- model.DataSources.Add(justin, jGraph);
- model.DataSources.Add(derrish, dGraph);
- model.DataSources.Add(geoff, gGraph);
-
-
-
SparqlExpression query = SparqlExpression.Parse(String.Format(@"PREFIX foaf: <{0}>
-
SELECT ?s ?p ?o
-
WHERE {{ ?s ?p ?o }} ", foaf));
-
Table results = model.QueryAll<SparqlParser>(query.ToString());
-
-
- for (int i = 0; i < results.ColumnCount; i++)
- Console.Write(results.GetColumnName(i) + "\t");
-
-
- Console.WriteLine();
- foreach (TableRow row in results.GetRows())
- Console.WriteLine(row.ToString());
-
-
- query = SparqlExpression.Parse(String.Format(@"PREFIX foaf: <{0}>
-
SELECT ?s ?p ?o
-
FROM <{1}>
-
FROM <{2}>
-
WHERE {{ ?s ?p ?o }}", foaf, justin, derrish));
- results = model.Query<SparqlParser>(query.ToString());
-
-
- Console.WriteLine();
- for (int i = 0; i < results.ColumnCount; i++)
- Console.Write(results.GetColumnName(i) + "\t");
-
-
- Console.WriteLine();
- foreach (TableRow row in results.GetRows())
- Console.WriteLine(row.ToString());
- }
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.
- public void Add_a_Datasource_to_Multiple_Models()
- {
- String justin = "http://www.intellidimension.com/people#JBarron";
- String derrish = "http://www.intellidimension.com/people#DRepchick";
- String foaf = "http://xmlns.com/foaf/0.1/";
-
- GraphDataSource graph = new GraphDataSource();
- graph.Add(justin, foaf + "name", new RdfLiteral("Justin Barron"));
- graph.Add(justin, foaf + "knows", new RdfUri(derrish));
-
- ClientModel m1 = new ClientModel();
- m1.DataSources.Add(justin, graph);
-
- ClientModel m2 = new ClientModel();
- m2.DataSources.Add(justin, graph);
-
- Console.WriteLine("M1: " + m1.DataSources[justin].Count);
- Console.WriteLine("M2: " + m2.DataSources[justin].Count);
-
- Console.WriteLine();
- m1.DataSources[justin].Remove(justin, foaf + "knows", new RdfUri(derrish));
-
- Console.WriteLine("M1: " + m1.DataSources[justin].Count);
- Console.WriteLine("M2: " + m2.DataSources[justin].Count);
- }
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.
- public void Validate_the_Syntax_of_a_SPARQL_Query()
- {
- String query = "SELECT ?s ?p ?o WHERE { ?s ?p ?o . }";
- SparqlExpression exp;
-
- if (SparqlExpression.TryParse(query, out exp))
- Console.WriteLine("Syntax is valid.");
- else
- Console.WriteLine("Syntax is invalid.");
-
- query = "SELECT ?s ?p ?o WHERE { ? }";
-
- if (SparqlExpression.TryParse(query, out exp))
- Console.WriteLine("Syntax is valid.");
- else
- Console.WriteLine("Syntax is invalid.");
- }
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.
- public void Hanlding_Errors_When_Parsing_a_SPARQL_Query()
- {
- String query = "SELECT ?s ?p ?o WHERE { ? }";
-
- try
- {
- SparqlExpression.Parse(query);
- }
- catch (SparqlQueryFormatException e)
- {
- Console.WriteLine("Parsing Exception Caught: '{0}'", e.Message);
- }
- }
Output:
Parsing Exception Caught: 'Expected GroupGraphPattern'
Back to Top