首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析RDF - dotnetrdf c#

解析RDF - dotnetrdf c#
EN

Stack Overflow用户
提问于 2018-10-04 13:21:16
回答 1查看 1.1K关注 0票数 0

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

代码语言:javascript
复制
<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 .

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

代码语言:javascript
复制
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 4:压缩方法(节点?)具体来说,我该怎么做呢?我一直在读关于斯派克的书,但我需要的东西似乎有点过分了。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-04 15:32:10

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

代码语言:javascript
复制
// 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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52647830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档