RDF, SPARQL and SHACL, in plain C#
RDFSharp lets a .NET application speak RDF. You build graphs from resources and literals, keep them in stores that remember where each fact came from, ask them questions in SPARQL and check them against SHACL shapes. Every page of this manual shows it on one real case, the catalog of a wine cooperative that grows chapter after chapter.
A first taste
The cooperative wants its red wines under 20 euros, with their English names. Here is the question in SPARQL and the same question built in C# with RDFSharp’s query engine.
PREFIX vr: <https://vallerosa.example/vocab#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?label ?price
WHERE {
?wine a vr:RedWine ;
rdfs:label ?label ;
vr:price ?price .
FILTER (LANGMATCHES(LANG(?label), "en"))
FILTER (?price < 20)
}
RDFGraph catalog = await RDFGraph.FromFileAsync(RDFModelEnums.RDFFormats.Turtle, "vallerosa-catalog.ttl");
RDFNamespace vr = new RDFNamespace("vr", "https://vallerosa.example/vocab#");
RDFResource redWine = new RDFResource(vr + "RedWine");
RDFResource hasPrice = new RDFResource(vr + "price");
RDFPlainLiteral englishTag = new RDFPlainLiteral("en");
RDFTypedLiteral priceCap = new RDFTypedLiteral("20", RDFModelEnums.RDFDatatypes.XSD_DECIMAL);
RDFVariable wine = new RDFVariable("wine");
RDFVariable label = new RDFVariable("label");
RDFVariable price = new RDFVariable("price");
RDFSelectQuery query = new RDFSelectQuery()
.AddPrefix(vr)
.AddPrefix(RDFNamespaceRegister.GetByPrefix("rdfs"))
.AddPatternGroup(new RDFPatternGroup()
.AddPattern(new RDFPattern(wine, RDFVocabulary.RDF.TYPE, redWine))
.AddPattern(new RDFPattern(wine, RDFVocabulary.RDFS.LABEL, label))
.AddPattern(new RDFPattern(wine, hasPrice, price))
.AddFilter(new RDFFilter(new RDFLangMatchesExpression(label,
new RDFConstantExpression(englishTag))))
.AddFilter(new RDFFilter(new RDFComparisonExpression(RDFQueryEnums.RDFComparisonFlavors.LessThan,
price, new RDFConstantExpression(priceCap)))))
.AddProjectionVariable(label)
.AddProjectionVariable(price);
RDFSelectQueryResult result = await query.ApplyToGraphAsync(catalog);
| ?LABEL | ?PRICE |
|---|---|
Vallerosa Red@EN |
14.50^^http://www.w3.org/2001/XMLSchema#decimal |
Three layers, one library
RDFSharp ships as a NuGet package targeting netstandard2.0, net8.0 and net10.0. Inside it, three layers build on each other.
Describe the data
Resources, literals, triples and graphs. Namespaces and datatypes. The standard RDF formats. SHACL shapes to say what good data looks like.
RDFSharp.StoreRemember where it came from
Quadruples add a context to every triple, so the same fact told by two sources stays two facts. In memory or on a database.
RDFSharp.QueryAsk and change it
SPARQL 1.1 queries and updates, composed in C# or parsed from text, over graphs, stores, federations and remote endpoints.
The family
RDFSharp.Extensions
Store providers that keep quadruples on real databases, from SQLite, PostgreSQL and SQL Server to MongoDB, Neo4j and Azure Table, behind the same RDFStore API.
OWLSharp
OWL2 ontologies, reasoning and SWRL rules, built on top of RDFSharp. Its own manual picks up the Vallerosa case where this one leaves it.