semweb-1.05+dfsg/ 0000755 0001750 0001750 00000000000 10774502134 013141 5 ustar meebey meebey semweb-1.05+dfsg/examples/ 0000755 0001750 0001750 00000000000 10774502134 014757 5 ustar meebey meebey semweb-1.05+dfsg/examples/loadfromweb.cs 0000644 0001750 0001750 00000002020 10774502134 017601 0 ustar meebey meebey // This example loads an RDF file from the web.
// It uses RdfReader.LoadFromUri, passing a URI,
// which downloads the corresponding URI and looks
// at its MIME type to determine what kind of
// RdfReader should be used to parse the file. A
// default URI (http://www.mozilla.org/news.rdf)
// can be used, or pass a URI on the command line
// to use a different data source.
using System;
using SemWeb;
public class LoadFromWeb {
public static void Main(string[] args) {
string uri = "http://www.mozilla.org/news.rdf";
if (args.Length > 0)
uri = args[0];
MemoryStore store = new MemoryStore();
// Here's one way...
store.Import(RdfReader.LoadFromUri(new Uri(uri)));
using (RdfWriter writer = new N3Writer(Console.Out))
store.Select(writer);
// Or....
RdfReader file = RdfReader.LoadFromUri(new Uri(uri));
file.Select(new StatementPrinter());
}
class StatementPrinter : StatementSink {
public bool Add(Statement assertion) {
Console.WriteLine(assertion.ToString());
return true;
}
}
}
semweb-1.05+dfsg/examples/README.txt 0000644 0001750 0001750 00000002351 10774502134 016456 0 ustar meebey meebey BUILDING THE EXAMPLES
If you're in Linux, just run make to compile all the examples.
Each of the files **.cs in this directory is meant to be compiled on is own.
A description of each program is at the top of each source file.
If you're using Mono, it'll help to copy or symlink the SemWeb.dll assembly
into this directory: ln -s ../bin/SemWeb.dll .
To compile an example: mcs example.cs -r:SemWeb.dll
To run it: mono example.exe
If you're using Visual Studio, create a new project, reference SemWeb.dll in
the bin directory, and add the exampleXX.cs file to the project.
EXAMPLE RDF DATA
In linux, run "./getsomedata.sh" to download some RDF data files. You'll
end up with four files about the U.S. Congress: people.rdf, bills.2005.rdf,
bills.2005.cosponsors.rdf, and also congress.n3 which contains everything
in the first three.
Otherwise, you can grab the files at:
http://www.govtrack.us/data/rdf/people.rdf.gz
http://www.govtrack.us/data/rdf/bills.2005.rdf.gz and
http://www.govtrack.us/data/rdf/bills.2005.cosponsors.rdf.gz
You'll need to un-gzip them. You might also want to merge them
into a single file with:
mono ../bin/rdfstorage.exe --out n3:congress.n3 people.rdf bills.2005.rdf bills.2005.cosponsors.rdf
semweb-1.05+dfsg/examples/rdfs.cs 0000644 0001750 0001750 00000003371 10774502134 016250 0 ustar meebey meebey // This example demonstrates basic RDFS reasoning.
using System;
using System.IO;
using SemWeb;
using SemWeb.Inference;
public class EulerTest {
public static void Main() {
// Create the instance data
MemoryStore dataModel = new MemoryStore();
BNode me = new BNode("me");
BNode you = new BNode("you");
Entity rdfType = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
Entity rdfsLabel= "http://www.w3.org/2000/01/rdf-schema#label";
Entity foafPerson = "http://xmlns.com/foaf/0.1/Person";
Entity foafAgent = "http://xmlns.com/foaf/0.1/Agent";
Entity foafName = "http://xmlns.com/foaf/0.1/name";
dataModel.Add(new Statement(me, rdfType, foafPerson));
dataModel.Add(new Statement(you, rdfType, foafPerson));
dataModel.Add(new Statement(me, foafName, (Literal)"John Doe"));
dataModel.Add(new Statement(you, foafName, (Literal)"Sam Smith"));
// Create the RDFS engine and apply it to the data model.
RDFS engine = new RDFS();
engine.LoadSchema(RdfReader.LoadFromUri(new Uri("http://xmlns.com/foaf/0.1/index.rdf")));
dataModel.AddReasoner(engine);
// Query the data model
// Ask for who are typed as Agents. Note that the people are
// typed as foaf:Person, and the schema asserts that foaf:Person
// is a subclass of foaf:Agent.
Console.WriteLine("Who are Agents?");
foreach (Entity r in dataModel.SelectSubjects(rdfType, foafAgent))
Console.WriteLine("\t" + r);
// Ask for the rdfs:labels of everyone. Note that the data model
// has foaf:names for the people, and the schema asserts that
// foaf:name is a subproperty of rdfs:label.
Console.WriteLine("People's labels:");
foreach (Statement s in dataModel.Select(new Statement(null, rdfsLabel, null)))
Console.WriteLine("\t" + s);
}
}
semweb-1.05+dfsg/examples/Makefile 0000644 0001750 0001750 00000000500 10774502134 016412 0 ustar meebey meebey all: *.cs
ln -sf ../bin/SemWeb.dll .;
ln -sf ../bin/SemWeb.Sparql.dll .;
ln -sf ../bin/sparql-core.dll .;
ln -sf ../bin/IVKM.GNU.Classpath.dll .;
for sample in `ls *.cs`; do mcs -debug $$sample -r:SemWeb.dll -r:SemWeb.Sparql.dll ; done
runtests: all
mono helloworld.exe > helloworld.output.rdf
clean:
rm *.exe*
semweb-1.05+dfsg/examples/sparql1.cs 0000644 0001750 0001750 00000004566 10774502134 016704 0 ustar meebey meebey using System;
using SemWeb;
using SemWeb.Remote;
using SemWeb.Query;
public class Sparql1 {
public static void Main() {
string endpoint = "http://www.rdfabout.com/sparql";
string ex1 = "PREFIX foaf: \n"
+ "SELECT ?name \n"
+ "WHERE { [] foaf:name ?name . }\n"
+ "LIMIT 10 \n";
string ex2 = "PREFIX foaf: \n"
+ "ASK \n"
+ "WHERE { [] foaf:name ?name . }\n";
string ex3 = "PREFIX foaf: \n"
+ "CONSTRUCT { ?person foaf:name2 ?name } \n"
+ "WHERE { ?person foaf:name ?name . }\n"
+ "LIMIT 10 \n";
SparqlHttpSource source = new SparqlHttpSource(endpoint);
Console.WriteLine("RunSparqlQuery(ex1, Console.Out):");
source.RunSparqlQuery(ex1, Console.Out);
Console.WriteLine();
Console.WriteLine("RunSparqlQuery(ex1, SparqlXmlQuerySink):");
source.RunSparqlQuery(ex1, new SparqlXmlQuerySink(Console.Out));
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("RunSparqlQuery(ex2, bool):");
bool result;
source.RunSparqlQuery(ex2, out result);
Console.WriteLine(result);
Console.WriteLine();
Console.WriteLine("RunSparqlQuery(ex3, N3Writer):");
using (N3Writer writer = new N3Writer(Console.Out))
source.RunSparqlQuery(ex3, writer);
Console.WriteLine();
Console.WriteLine("Select(subject,__,__)");
using (N3Writer writer = new N3Writer(Console.Out))
source.Select(new Statement("http://www.rdfabout.com/rdf/usgov/congress/people/M000303", null, null), writer);
Console.WriteLine();
Console.WriteLine("Query(...) A");
Variable a = new Variable("a");
QueryOptions qo = new QueryOptions();
qo.Limit = 10;
source.Query(new Statement[] {
new Statement(a, "http://xmlns.com/foaf/0.1/name", (Literal)"John McCain"),
new Statement(a, new Variable("b"), new Variable("c")),
}, qo, new SparqlXmlQuerySink(Console.Out));
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Query(...) B");
QueryResultBuffer qb = new QueryResultBuffer();
source.Query(new Statement[] {
new Statement(a, "http://xmlns.com/foaf/0.1/name", (Literal)"John McCain"),
new Statement(a, new Variable("b"), new Variable("c")),
}, qo, qb);
foreach (VariableBindings b in qb) {
Console.WriteLine("a => " + b["a"]);
Console.WriteLine("b => " + b["b"]);
Console.WriteLine();
}
Console.WriteLine();
}
}
semweb-1.05+dfsg/examples/euler.cs 0000644 0001750 0001750 00000005124 10774502134 016424 0 ustar meebey meebey // This example demonstrates general reasoning with
// the Euler engine based on Jos De Roo's Euler proof
// mechanism. The example is based on the "graph"
// example from Euler.
using System;
using System.IO;
using SemWeb;
using SemWeb.Inference;
public class EulerTest {
public static void Main() {
// Create the instance data
MemoryStore dataModel = new MemoryStore();
BNode paris = new BNode("paris");
BNode orleans = new BNode("orleans");
BNode chartres = new BNode("chartres");
BNode amiens = new BNode("amiens");
BNode blois = new BNode("blois");
BNode bourges = new BNode("bourges");
BNode tours = new BNode("tours");
BNode lemans = new BNode("lemans");
BNode angers = new BNode("angers");
BNode nantes = new BNode("nantes");
Entity oneway = new Entity("http://www.agfa.com/w3c/euler/graph.axiom#oneway");
Entity path = new Entity("http://www.agfa.com/w3c/euler/graph.axiom#path");
dataModel.Add(new Statement(paris, oneway, orleans));
dataModel.Add(new Statement(paris, oneway, chartres));
dataModel.Add(new Statement(paris, oneway, amiens));
dataModel.Add(new Statement(orleans, oneway, blois));
dataModel.Add(new Statement(orleans, oneway, bourges));
dataModel.Add(new Statement(blois, oneway, tours));
dataModel.Add(new Statement(chartres, oneway, lemans));
dataModel.Add(new Statement(lemans, oneway, angers));
dataModel.Add(new Statement(lemans, oneway, tours));
dataModel.Add(new Statement(angers, oneway, nantes));
// Create the inference rules by reading them from a N3 string.
string rules =
"@prefix : .\n" +
"\n" +
"{ ?a :oneway ?b } => { ?a :path ?b } .\n" +
"{ ?a :path ?b . ?b :path ?c . } => { ?a :path ?c } .\n";
// Create our question in the form of a statement to test.
Statement question = new Statement(paris, path, nantes);
// Create the Euler engine
Euler engine = new Euler(new N3Reader(new StringReader(rules)));
// First Method of Inference:
// Ask the engine whether there is a path from paris to nantes.
// The Prove method will return a list of proofs, or an empty
// array if it could not find a proof.
foreach (Proof p in engine.Prove(dataModel, new Statement[] { question })) {
Console.WriteLine(p.ToString());
}
// Second Method of Inference:
// Apply the engine to the data model and then use the data
// model's Contains method to see if the statement is "in"
// the model + reasoning.
dataModel.AddReasoner(engine);
Console.WriteLine("Euler Says the Question is: " + dataModel.Contains(question));
}
}
semweb-1.05+dfsg/examples/containers.cs 0000644 0001750 0001750 00000002314 10774502134 017453 0 ustar meebey meebey // This example deals with RDF containers. You can use the rdfs:member
// property to match any rdf:_### (i.e. rdf:li) property. Or,
// use SelectObjects on the Store, which will return the items
// in sorted order.
using System;
using SemWeb;
public class Containers {
const string RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
const string RDFS = "http://www.w3.org/2000/01/rdf-schema#";
public static void Main() {
MemoryStore store = new MemoryStore();
Entity container = new Entity("http://www.example.org/#container");
store.Add(new Statement(container, RDF+"type", (Entity)(RDF+"Bag")));
store.Add(new Statement(container, RDF+"_3", (Literal)"Three"));
store.Add(new Statement(container, RDF+"_2", (Literal)"Two"));
store.Add(new Statement(container, RDF+"_1", (Literal)"One"));
// use the rdfs:member property to match for any rdf:_### predicates.
Entity rdfs_member = (Entity)(RDFS+"member");
using (RdfWriter writer = new N3Writer(Console.Out)) {
writer.Namespaces.AddNamespace(RDF, "rdf");
store.Select(new Statement(container, rdfs_member, null), writer);
}
foreach (Resource r in store.SelectObjects(container, rdfs_member))
Console.WriteLine(r);
}
}
semweb-1.05+dfsg/examples/filters.cs 0000644 0001750 0001750 00000002065 10774502134 016761 0 ustar meebey meebey using System;
using System.Collections;
using SemWeb;
public class Filters {
const string RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
const string RDFS = "http://www.w3.org/2000/01/rdf-schema#";
public static void Main() {
MemoryStore store = new MemoryStore();
Entity container = new Entity("http://www.example.org/#container");
store.Add(new Statement(container, RDF+"type", (Entity)(RDF+"Bag")));
store.Add(new Statement(container, RDF+"_3", (Literal)"Three"));
store.Add(new Statement(container, RDF+"_2", (Literal)"Two"));
store.Add(new Statement(container, RDF+"_1", (Literal)"One"));
// use the rdfs:member property to match for any rdf:_### predicates.
Entity rdfs_member = (Entity)(RDFS+"member");
using (RdfWriter writer = new N3Writer(Console.Out)) {
writer.Namespaces.AddNamespace(RDF, "rdf");
SelectFilter f = new SelectFilter();
//f.Predicates = new Entity[] { rdfs_member };
f.LiteralFilters = new LiteralFilter[] { new SemWeb.Filters.StringContainsFilter("n") };
store.Select(f, writer);
}
}
}
semweb-1.05+dfsg/examples/select.cs 0000644 0001750 0001750 00000002350 10774502134 016565 0 ustar meebey meebey using System;
using SemWeb;
public class Select {
const string RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
const string FOAF = "http://xmlns.com/foaf/0.1/";
const string example_foaf_file = "http://rdfweb.org/people/danbri/rdfweb/danbri-foaf.rdf";
static readonly Entity rdftype = RDF+"type";
static readonly Entity foafPerson = FOAF+"Person";
static readonly Entity foafknows = FOAF+"knows";
static readonly Entity foafname = FOAF+"name";
public static void Main() {
Store store = new MemoryStore();
store.Import(RdfReader.LoadFromUri(new Uri(example_foaf_file)));
// Dump out the data, for fun
using (RdfWriter writer = new RdfXmlWriter(Console.Out)) {
writer.Write(store);
}
Console.WriteLine("These are the people in the file:");
foreach (Statement s in store.Select(new Statement(null, rdftype, foafPerson))) {
foreach (Resource r in store.SelectObjects(s.Subject, foafname))
Console.WriteLine(r);
}
Console.WriteLine();
Console.WriteLine("And here's RDF/XML just for some of the file:");
using (RdfWriter w = new RdfXmlWriter(Console.Out)) {
store.Select(new Statement(null, foafname, null), w);
store.Select(new Statement(null, foafknows, null), w);
}
Console.WriteLine();
}
}
semweb-1.05+dfsg/examples/getsomedata.sh 0000755 0001750 0001750 00000001051 10774502134 017610 0 ustar meebey meebey #!/bin/sh
# This downloads some RDF data about the U.S. Congress.
echo Download RDF file from GovTrack.us...
wget -nc http://www.govtrack.us/data/rdf/people.rdf.gz
wget -nc http://www.govtrack.us/data/rdf/bills.109.rdf.gz
wget -nc http://www.govtrack.us/data/rdf/bills.109.cosponsors.rdf.gz
echo Uncompressing them...
gunzip people.rdf.gz
gunzip bills.109.rdf.gz
gunzip bills.109.cosponsors.rdf.gz
echo Merging them together into congress.n3...
mono ../bin/rdfstorage.exe --out n3:congress.n3 \
people.rdf bills.109.rdf bills.109.cosponsors.rdf
semweb-1.05+dfsg/examples/sqlite.cs 0000644 0001750 0001750 00000001565 10774502134 016616 0 ustar meebey meebey using System;
using SemWeb;
public class Sqlite {
const string RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
const string FOAF = "http://xmlns.com/foaf/0.1/";
static readonly Entity rdftype = RDF+"type";
static readonly Entity foafPerson = FOAF+"Person";
static readonly Entity foafname = FOAF+"name";
public static void Main() {
Store store = Store.Create("sqlite:rdf:Uri=file:foaf.sqlite;version=3");
Entity newPerson = new Entity("http://www.example.org/me");
store.Add(new Statement(newPerson, rdftype, foafPerson));
store.Add(new Statement(newPerson, foafname, (Literal)"New Guy"));
Console.WriteLine("These are the people in the file:");
foreach (Entity s in store.SelectSubjects(rdftype, foafPerson)) {
foreach (Resource r in store.SelectObjects(s, foafname))
Console.WriteLine(r);
}
Console.WriteLine();
store.Dispose();
}
}
semweb-1.05+dfsg/examples/query.cs 0000644 0001750 0001750 00000002706 10774502134 016460 0 ustar meebey meebey // This example runs a query.
using System;
using System.IO;
using SemWeb;
using SemWeb.Query;
public class Example {
public static void Main(string[] argv) {
if (argv.Length < 3) {
Console.WriteLine("Usage: query.exe format queryfile datafile");
return;
}
string format = argv[0];
string queryfile = argv[1];
string datafile = argv[2];
Query query;
if (format == "rsquary") {
// Create a simple-entailment "RSquary" query
// from the N3 file.
query = new GraphMatch(new N3Reader(queryfile));
} else {
// Create a SPARQL query by reading the file's
// contents.
query = new SparqlEngine(new StreamReader(queryfile));
}
// Load the data file from disk
MemoryStore data = new MemoryStore();
data.Import(new N3Reader(datafile));
// First, print results in SPARQL XML Results format...
// Create a result sink where results are written to.
QueryResultSink sink = new SparqlXmlQuerySink(Console.Out);
// Run the query.
query.Run(data, sink);
// Second, print the results via our own custom QueryResultSink...
query.Run(data, new PrintQuerySink());
}
public class PrintQuerySink : QueryResultSink {
public override bool Add(VariableBindings result) {
foreach (Variable var in result.Variables) {
if (var.LocalName != null && result[var] != null) {
Console.WriteLine(var.LocalName + " ==> " + result[var].ToString());
}
Console.WriteLine();
}
return true;
}
}
}
semweb-1.05+dfsg/examples/helloworld.cs 0000644 0001750 0001750 00000002175 10774502134 017466 0 ustar meebey meebey // 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);
}
}
}
semweb-1.05+dfsg/examples/simple.cs 0000644 0001750 0001750 00000001047 10774502134 016601 0 ustar meebey meebey // This example reads an RDF/XML file on standard input
// and writes it back out in N3 format to standard output.
using System;
using SemWeb;
public class Simple {
public static void Main() {
MemoryStore store = new MemoryStore();
store.Import(new RdfXmlReader(Console.In));
// The 'using' is important because it is necessary
// to Close or Dispose the writer once writing is
// complete so that the final statement is closed
// with a period.
using (RdfWriter writer = new N3Writer(Console.Out))
writer.Write(store);
}
}
semweb-1.05+dfsg/doc/ 0000755 0001750 0001750 00000000000 10774502134 013706 5 ustar meebey meebey semweb-1.05+dfsg/doc/stylesheet.css 0000644 0001750 0001750 00000000257 10774502134 016615 0 ustar meebey meebey pre.code {
margin: 1em;
padding: .5em;
background-color: #FAFAFF;
border: 1px solid #CCCCDD;
}
tt {
letter-spacing: 1px;
font-size: 105%;
background-color: #FFFFEE;
}
semweb-1.05+dfsg/doc/index.html 0000644 0001750 0001750 00000002457 10774502134 015713 0 ustar meebey meebey
SemWeb: Documentation
SemWeb: A .NET Library for RDF and the Semantic Web
This documentation describes version 0.85 of SemWeb.
To get started, you should use the pre-compiled binaries
provided in the download package. These are located in the
bin directory. You will need to reference SemWeb.dll
in all of the examples.
The documentation is split into several pages:
Library Overview: A design document explaining the basics of the library.
In the previous tutorial you saw the Select method of StatementSources, which streams all of the statements into a sink. The name "Select" was derived from SQL syntax, where it's used to retrieve rows that match a criteria. In SemWeb, Select is used to retreive statements matching a filter. (And as with SQL, when no criteria is given all statements are retrieved.)
A template is a Statement but with subject, predicate, and object possibly null. nulls are wildcards. So here are a few examples:
store.Select(sink); // selects all statements, streaming them into sink
store.Select(new Statement(null, null, null), sink); // the same
store.Select(Statement.All, sink); // the same, but shorthand
store.Select(new Statement(subj, null, null), sink); // any statement with that particular subject entity
store.Select(new Statement(subj, pred, null), sink); // any statement with that particular subject and predicate
store.Select(new Statement(subj, pred, obj), sink); // just that statement, if it exists in the store
The sink can be any StatementSink. This includes RdfWriters, which would let
you write out just a part of a store to a file, and Stores like the MemoryStore
so that you can move statements between data sources.
Stores provide a few convenience methods. Two methods are provided for getting all of the subjects found with a given predicate and object, and similarly all objects found with a given subject and predicate. This can be used to move around in a graph:
foreach (Resource r in store.SelectObjects(person, foafname))
Console.WriteLine("His name is: " + r);
Other convenience methods are overrides of Select that rather than sending the results to a sink, load them into memory so that you may for-each over them:
You obviously shouldn't use these on data sources of unbounded size as you wouldn't necessarily want to load the results all into memory.
These convenience methods are only provided in the Store class and its subclasses.
If you want to use them on data from a file or other data source that doesn't extend Store,
load the data into a MemoryStore:
Store store = new MemoryStore();
store.Import(RdfReader.LoadFromUri(new Uri("http://dannyayers.com/misc/foaf/foaf.rdf")));
Here's an example program that loads a FOAF document and extracts some information using Select:
using System;
using SemWeb;
public class Select {
const string RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
const string FOAF = "http://xmlns.com/foaf/0.1/";
static readonly Entity rdftype = RDF+"type";
static readonly Entity foafPerson = FOAF+"Person";
static readonly Entity foafknows = FOAF+"knows";
static readonly Entity foafname = FOAF+"name";
public static void Main() {
Store store = new MemoryStore();
store.Import(RdfReader.LoadFromUri(new Uri("http://dannyayers.com/misc/foaf/foaf.rdf")));
Console.WriteLine("These are the people in the file:");
foreach (Statement s in store.Select(new Statement(null, rdftype, foafPerson))) {
foreach (Resource r in store.SelectObjects(s.Subject, foafname))
Console.WriteLine(r);
}
Console.WriteLine();
Console.WriteLine("And here's RDF/XML just for some of the file:");
using (RdfWriter w = new RdfXmlWriter(Console.Out)) {
store.Select(new Statement(null, foafname, null), w);
store.Select(new Statement(null, foafknows, null), w);
}
Console.WriteLine();
}
}
Other notes
RDF collections, like Bag, Alt, and Seq, in RDF/XML use a strange
rdf:li pseudo-property. rdf:li isn't actually a
property. It is an abbreviation for rdf:_1, rdf:_2,
etc. in that order. Thus when you select for members of a collection,
you can't use rdf:li. However, RDFS defines the property
rdfs:member which rdf:_## properties are all
subproperties of. The SemWeb stores all recognize the rdfs:member
predicate and will match it to any of the rdf:_## predicates.
In addition, the SelectObjects method of the Store
class will automatically sort the objects by their collection order,
where possible, when you call the method with the rdfs:member predicate.
Here's an example of that:
// This example deals with RDF containers. You can use the rdfs:member
// property to match any rdf:_### (i.e. rdf:li) property. Or,
// use SelectObjects on the Store, which will return the items
// in sorted order.
using System;
using SemWeb;
public class Containers {
const string RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
const string RDFS = "http://www.w3.org/2000/01/rdf-schema#";
public static void Main() {
MemoryStore store = new MemoryStore();
Entity container = new Entity("http://www.example.org/#container");
store.Add(new Statement(container, RDF+"type", (Entity)(RDF+"Bag")));
store.Add(new Statement(container, RDF+"_3", (Literal)"Three"));
store.Add(new Statement(container, RDF+"_2", (Literal)"Two"));
store.Add(new Statement(container, RDF+"_1", (Literal)"One"));
// use the rdfs:member property to match for any rdf:_### predicates.
Entity rdfs_member = (Entity)(RDFS+"member");
using (RdfWriter writer = new N3Writer(Console.Out)) {
writer.Namespaces.AddNamespace(RDF, "rdf");
store.Select(new Statement(container, rdfs_member, null), writer);
}
foreach (Resource r in store.SelectObjects(container, rdfs_member))
Console.WriteLine(r);
}
}
LoadFromUri will determine the format of the file (XML or N3) based on the MIME type sent back from the server, or if that fails, the file extension.
You don't have to put the contents of a file into memory. All of the RdfReaders are "streaming", which means it doesn't need to load the entire file before getting statements out of it. The reader can stream statements to a "sink" as soon as each is read.
In fact, readers are one type of StatementSource. StatementSources provide a Select method which streams statements to a StatementSink, providing a corresponding Add method.
Stores are one type of sink. You saw the MemoryStore's Add method in the last tutorial.
The following is pretty much equivalent to the code above:
For a MemoryStore, there is no difference. Other stores may override Import with transaction or locking mechanics to speed up the process of loading a large data file.
By creating your own sink, you can process statements in a streaming way:
class StatementPrinter : StatementSink {
public bool Add(Statement assertion) {
Console.WriteLine(assertion.ToString());
return true;
}
}
This class implements a sink that simply prints out each statement to the console as each statement is received. It returns true to indicate to the source to continue. Returning false would end the streaming.
The Statement type is a struct that contains three fields:
Subject, Predicate, and Object (actually it contains
a Meta field too). Since RDF statements only have entities as
subjects and predicates, the first two fields are typed as Entity,
while the last field is typed as Resource. If you want to
access the URIs and literal values within the statement, you might write
something like this:
Console.WriteLine("Subject: " + assertion.Subject.Uri);
Console.WriteLine("Predicate: " + assertion.Predicate.Uri);
if (assertion.Object is Entity)
Console.WriteLine("Object: " + ((Entity)assertion.Object).Uri + " (it's an entity)");
else
Console.WriteLine("Object: " + ((Literal)assertion.Object).Value + " (it's a literal)");
Of course, beware that BNodes are entities without URIs, and the Uri
property will return null in that case.
To stream the statements from the web directly to the custom sink, use:
webrdf.Select(new StatementPrinter());
There's one final twist. Stores, as I mentioned, are a type of sink, letting you stream
statements into them. They are simultaneously a type of source! This lets you stream statements
out of them too, just as you stream statements out of the RdfXmlReader and into your
own class.
semweb-1.05+dfsg/doc/selectfilter.html 0000644 0001750 0001750 00000004604 10774502134 017265 0 ustar meebey meebey
SemWeb: Docs: Selecting Many Things at Once
For anything but the MemoryStore, there is considerable overhead in each request for information from an underlying database. With the MySQL store, for instance, a call to Select makes a SQL SELECT query on the database. There is overhead in constructing, transmitting across processes, and parsing the query, and then the response. This makes repeated calls to Select (the method) much slower than they should be if they could be combined into a single SQL SELECT.
SemWeb has an advanced API call, an overload of Select, taking a SelectFilter argument, which allows the caller to query for statements matching a more complex filter than the simple form of Select. In the simple form, the subject, predicate, object, and meta can be either 1) specified or 2) a wildcard. In the more complex API, there is a third option, which is specifying a range of possible values. Schematically, these three calls to select:
Further, these permutations can be condensed into a single call:
Select(X, A, null)
Select(Y, A, null)
Select(Z, A, null)
Select(X, B, null)
Select(Y, B, null)
Select(Z, B, null)
Select(X, C, null)
Select(Y, C, null)
Select(Z, C, null)
Select( { X, Y, Z}, { A, B, C}, null);
The statements returned from the complex Select are those that match any of the provided resources.
The actual syntax uses arrays of entities or, for objects, resources. As an example, one use case of this is to fetch the foaf:name of many entities in one call.
SelectFilter filter = new SelectFilter(); // for now all wildcards
filter.Subjects = new Entity[] { X, Y, Z };
filter.Predicates = new Entity[] { foaf_name };
// filter.Objects, filter.Metas are left as wildcards
store.Select(filter, sink);
The sink receives all statements whose subject is X, Y, or Z, and whose predicate is foaf_name.