首页 > 解决方案 > 使用 dotNetRDF 的 SPARQL 查询返回正确数量的结果但值为空,为什么?

问题描述

我已经在 protege 中的披萨本体上测试了我的 SPARQL 查询,并且查询工作正常。问题是当我尝试使用 dotNetRDF 对同一个本体文件运行相同的查询时。查询返回正确数量的结果,但它们似乎都是空的。

谁能指出这里可能存在什么问题?

使用 Protege 查询结果:

在此处输入图像描述

使用 dotNetRDF 查询结果:

在此处输入图像描述

这是我在 C# 中的代码:

OntologyGraph ontGraph = new OntologyGraph();
FileLoader.Load(ontGraph, "PizzaOntology.owl");
ontGraph.BaseUri = null;

#region SPARQL query
SparqlParameterizedString queryString = new SparqlParameterizedString();

queryString.Namespaces.AddNamespace("rdf", new Uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#"));
queryString.Namespaces.AddNamespace("owl", new Uri("http://www.w3.org/2002/07/owl#"));
queryString.Namespaces.AddNamespace("rdfs", new Uri("http://www.w3.org/2000/01/rdf-schema#"));
queryString.Namespaces.AddNamespace("xsd", new Uri("http://www.w3.org/2001/XMLSchema#"));
queryString.Namespaces.AddNamespace("ont", new Uri("http://www.semanticweb.org/ontologies/pizza.owl#"));

queryString.CommandText = "SELECT ?subject";
queryString.CommandText += "WHERE { ?subject rdfs:subClassOf ont:PizzaBase }";

SparqlQueryParser parser = new SparqlQueryParser();
SparqlQuery query = parser.ParseFromString(queryString);
#endregion


#region Query Processor
TripleStore store = new TripleStore();
store.Add(ontGraph);

InMemoryDataset ds = new InMemoryDataset(store, true);
ISparqlQueryProcessor processor = new LeviathanQueryProcessor(ds);

object results = processor.ProcessQuery(query);
if (results is SparqlResultSet)
{
    //Print out the Results
    SparqlResultSet rset = (SparqlResultSet)results;
    foreach (SparqlResult result in rset)
    {
        Console.WriteLine(result.ToString());
    }
}
#endregion

标签: c#sparqlontologyprotegedotnetrdf

解决方案


推荐阅读