如何在项目中使用HasPropertyValueSelector?

时间:2019-01-22 21:41:04

标签: c# .net parsing dotnetrdf

我是dotNetRDF的新手,我可能遇到名称空间或框架的问题。

尽管我要在我的项目中添加名称空间,但是以下代码不起作用。

系统给出了找不到命名空间的错误。

HasPropertyValueSelector sel = new HasPropertyValueSelector(rdfType, carnivore);

所有代码都在下面;

using System;
using System.Collections.Generic;
using VDS.RDF;
using VDS.RDF.Parsing;
using VDS.RDF.Query;
        static void Main(string[] args)
        {
            Graph g = new Graph();
            UriLoader.Load(g, new Uri("http://example.org/animals"));
            IUriNode rdfType = g.CreateUriNode("rdf:type");
            IUriNode carnivore = g.CreateUriNode("ex:Carnivore");

           ***HasPropertyValueSelector sel = new HasPropertyValueSelector(rdfType, carnivore);***
           IEnumerable<Triple> carnivores = g.GetTriples(sel);

            Graph ourlist = new Graph();
            ourlist.NamespaceMap.AddNamespace("ex", new Uri("http://example.org/"));

            IUriNode rdfType2 = ourlist.CreateUriNode("rdf:type");
            IUriNode animal = ourlist.CreateUriNode("ex:Animal");

            foreach (Triple t in carnivores)
            {

               ourlist.Assert(new Triple(Tools.CopyNode(t.Subject, ourlist), rdfType2, animal));
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

缺少的第一步是在创建使用名称空间的URI节点之前定义名称空间。这是通过在IGraph接口上找到的NamespaceMapper完成的。您可以详细了解here,但一个简单的例子是:

IGraph g = new Graph();

//Define the Namespaces we want to use
g.NamespaceMap.AddNamespace("rdf", new Uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#"));
g.NamespaceMap.AddNamespace("ex", new Uri("http://example.org/"));

//Define the same Triple as the previous example
UriNode rdfType = g.CreateUriNode("rdf:type");
UriNode exCarnivore = g.CreateUriNode("ex:Carnivore");

var carnivores = g.GetTriplesWithPredicateObject(rdfType, exCarnivore);
相关问题