RDFSharp NuGet

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.

The family

Improve this page