解析 RDF - dotnetrdf c#

Parsing RDF - dotnetrdf c#

这是我第一次看 RDF,尝试过后,我不知道如何解析它。我正在查看 AFF4 文件系统中使用的一些 RDF(Turtle 格式),这是其中的一部分:

<aff4://0295fab8-94b7-4435-bdb3-932cf48e40bd>
    a                          aff4:ImageStream ;
    aff4:chunkSize             "32768"^^xsd:int ;
    aff4:chunksInSegment       "2048"^^xsd:int ;
    aff4:compressionMethod     <http://code.google.com/p/snappy/> ;
    aff4:imageStreamHash       "82798a275176aa141a2993ca8931535b1303545a0954473f5c5e55b4d8d5a8e3ebdb9e9323e5ecfaf65f8d379a8e2b9150750f5cf44851cf4edb6a2e05372f42"^^aff4:SHA512 ;
    aff4:imageStreamIndexHash  "039eb2da046cfb8c8d40e6f9b42aae501fb36f9b09b5f29d660d3637f87c37c98c3ee3b995265adff1d2b971fa795317333bf50200e72fdfe9fa96acdb88b6d0"^^aff4:SHA512 ;
    aff4:size                  "185335808"^^xsd:long ;
    aff4:stored                : ;
    aff4:target                <aff4://92015053-5f7b-4e5a-a1e7-901d8943cf1f> ;
    aff4:version               "1"^^xsd:int .

文件中有很多这样的东西,但我不知道如何访问其中的任何一个,到目前为止我已经拼凑起来了:

private static void ParseInformationStream(Stream informationStream)
    {

        Console.WriteLine("Parsing information.turtle file: ");

        informationStream.Position = 0;

        TurtleParser turtleParser = new TurtleParser();
        Graph graph = new Graph();
        turtleParser.Load(graph, new StreamReader(informationStream));

        foreach (var triple in graph.Triples)
        {
           Console.WriteLine(triple.Subject);
        }

    }

这会打印出一些数据,但是如果我想具体访问 aff4:compressionMethod(节点?),我该怎么做呢?我一直在阅读有关 Sparql 的信息,但对于我需要的东西来说,这一切似乎有点过分了。

如有任何意见或建议,我们将不胜感激。

您可以使用 IGraph 接口的方法来访问解析图的内容。例如,以下将检索所有图像流(在 Turtle 中 "a" 是 rdf:type 谓词的快捷方式)并打印出每个流的压缩方法:

// Get the node for rdf:type
var rdfType = graph.CreateUriNode(new Uri(RdfSpecsHelper.RdfType));
// Get the node for the aff4:ImageStream type
var imageStream = graph.GetUriNode("aff4:ImageStream");
// Get the node for the aff4:compressionMethod predicate
var compressionMethod = graph.CreateUriNode("aff4:compressionMethod");
// Get the streams (the subject of x a aff4:ImageStream in the Turtle)
var imageStreams = graph.GetTriplesWithPredicateObject(rdfType, imageStream).Select(t => t.Subject);
foreach (var streamInstance in imageStreams)
{
    // Get the first compressionMethod value for the stream instance
    var compression = graph.GetTriplesWithSubjectPredicate(streamInstance, compressionMethod)
        .Select(t => t.Object).FirstOrDefault();
    Console.WriteLine("Stream " + streamInstance + " uses compression method " + compression);
}

有关访问 dotNetRDF 图中的节点和三元组的更多信息,请参阅 https://github.com/dotnetrdf/dotnetrdf/wiki/UserGuide-Working-With-Graphs