1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
<html>
<head>
<title>SemWeb: Docs: Hello World</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css"/>
</head>
<body>
<p><a href="index.html">SemWeb Documentation</a></p>
<h1>SemWeb Hello World</h1>
<p>Here's an example of using SemWeb to construct an RDF/XML or Notation 3 file.</p>
<p>Create a new file called <tt>helloworld.cs</tt>. If you're using Visual Studio, create this in a new project and reference <tt>SemWeb.dll</tt> provided in the download package.</p>
<p>First, use the relevant namespaces and create a simple class. Also add a constant for the RDF namespace.</p>
<pre class="code">
using System;
using SemWeb;
public class Example {
const string RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
public static void Main() {
}
}
</pre>
<p>In <tt>Main()</tt>, create a new <tt>MemoryStore</tt>, which is an in-memory store of RDF statements:</p>
<pre class="code">
MemoryStore store = new MemoryStore();
</pre>
<p>The next thing to do is create some entities for the things you will be asserting statements about. You can do that simply by creating a new <tt>Entity</tt> object:</p>
<pre class="code">
Entity computer = new Entity("http://example.org/computer");
Entity description = new Entity("http://example.org/description");
</pre>
<p>The <tt>Entity</tt> class has an implicit conversion operator from strings, which means you can just assign a string to an entity variable to save typing:</p>
<pre class="code">
Entity says = "http://example.org/says";
Entity wants = "http://example.org/wants";
</pre>
<p>Blank nodes, a.k.a. anonymous entities, are created using the <tt>BNode</tt> class, which is a subclass of <tt>Entity</tt>.</p>
<pre class="code">
Entity desire = new BNode();
</pre>
<p>Next, we create statements using these entities. To create a statement, use the <tt>Statement</tt> constructor, which takes a subject, predicate, and object. Note that <tt>Statement</tt>s are <i>structs</i>, not classes, but this probably won't affect you.</p>
<pre class="code">
Statement assertion = new Statement(computer, says, new Literal("Hello world!"));
</pre>
<p>Another conversion operator is defined to make it easy for you to create Literals out of strings.
Unlike the one that is implict for Entities, this one is <i>explicit</i>, which means you
always need to write out the cast. We could have written the previous line like this insted:
<pre class="code">
Statement assertion = new Statement(computer, says, (Literal)"Hello world!");
</pre>
<p>Statements have to be put into a <tt>Store</tt> like this:</p>
<pre class="code">
store.Add(assertion);
</pre>
<p>I'll condense that into this:</p>
<pre class="code">
store.Add(new Statement(computer, says, (Literal)"Hello world!"));
store.Add(new Statement(computer, wants, desire));
store.Add(new Statement(desire, description, (Literal)"to be human"));
store.Add(new Statement(desire, RDF+"type", (Entity)"http://example.org/Desire"));
</pre>
<p>A store is a collection of statements. In true RDF, the order and number of occurrences of a statement doesn't matter because a graph is simply a set of statements. Some SemWeb stores act like sets, rather than collections. The memory store is not one of these.</p>
<p>Lastly, we want to write out the contents of the store to an RDF/XML file. We do this by creating a new <tt>RdfXmlWriter</tt> object, sending the store's contents to the writer, and then <b>disposing the writer</b>. It's very important to dispose of writers so they can finish, so you should always wrap writers with the <i>using</i> C# directive.</p>
<pre class="code">
using (RdfWriter writer = new RdfXmlWriter(Console.Out)) {
writer.Write(store);
}
</pre>
<p>If you're using Mono, like I do, to compile and run the program, run the following commands. Be sure to put <tt>SemWeb.dll</tt> in the current directory so Mono can find it at compile time, and then at run time.</p>
<pre class="code">
mcs helloworld.cs -r:SemWeb.dll
mono helloworld.exe
</pre>
<p>If you're using VisualStudio, put this in a new project, reference <tt>SemWeb.dll</tt>
and compile and run it.</p>
<p>Here's the output:</p>
<pre class="code"><?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:exampleorg="http://example.org/">
<rdf:Description rdf:about="http://example.org/computer">
<exampleorg:says>Hello world!</exampleorg:says>
<exampleorg:wants>
<exampleorg:Desire>
<exampleorg:description>to be human</exampleorg:description>
</exampleorg:Desire>
</exampleorg:wants>
</rdf:Description>
</rdf:RDF></pre>
<p>We didn't provide the writer with any namespace prefixes, so it made one up. To provide a prefix for a namespace, use the <tt>Namespaces</tt> property of the writer:</p>
<pre class="code">
using (RdfWriter writer = new RdfXmlWriter(Console.Out)) {
writer.Namespaces.AddNamespace("http://example.org/", "ex");
...
</pre>
<p>You need to set the namespaces before any statements are streamed to the writer. Here's the final output:</p>
<pre class="code" file="../examples/helloworld.output.rdf"><?xml version="1.0"?>
<rdf:RDF xmlns:ex="http://example.org/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="http://example.org/computer">
<ex:says>Hello world!</ex:says>
<ex:wants>
<ex:Desire>
<ex:description>to be human</ex:description>
</ex:Desire>
</ex:wants>
</rdf:Description>
</rdf:RDF></pre>
<p>To write out the statements in Notation 3 format, just use the <tt>N3Writer</tt> class instead. It produces this output:</p>
<pre class="code">
@prefix ex: <http://example.org/> .
ex:computer ex:says "Hello world!" ;
ex:wants _:bnode0 .
_:bnode0 ex:description "to be human" ;
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ex:Desire .
</pre>
<p>Here's the complete program:</p>
<pre class="code" file="../examples/helloworld.cs">// This example creates a few RDF statements and adds
// them to a MemoryStore. Then it writes out the
// statements in RDF/XML format to the console. Note
// that the implicit string-to-Entity and string-to-
// Literal conversion operators are being used.
using System;
using SemWeb;
public class Example {
const string RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
public static void Main() {
MemoryStore store = new MemoryStore();
Entity computer = new Entity("http://example.org/computer");
Entity says = "http://example.org/says";
Entity wants = "http://example.org/wants";
Entity desire = new BNode();
Entity description = new Entity("http://example.org/description");
store.Add(new Statement(computer, says, (Literal)"Hello world!"));
store.Add(new Statement(computer, wants, desire));
store.Add(new Statement(desire, description, (Literal)"to be human"));
store.Add(new Statement(desire, RDF+"type", (Entity)"http://example.org/Desire"));
using (RdfWriter writer = new RdfXmlWriter(Console.Out)) {
writer.Namespaces.AddNamespace("http://example.org/", "ex");
writer.Write(store);
}
}
}
</pre>
</body>
</html>
|