gpt4 book ai didi

c# - dotNetRDF 中的 sparql 查询

转载 作者:行者123 更新时间:2023-11-30 16:54:28 37 4
gpt4 key购买 nike

我已经通过曼彻斯特 OWL 语法转换器将众所周知的 pizza.owl 本体转换为 RDF 文件 pizza.rdf。我已经写了这段代码,但我没有得到任何结果,但也没有错误,也没有。如何使用谓词 MushroomTopping 获取三元组?

 TripleStore store = new TripleStore();
store.LoadFromFile(@"C:\pizza.rdf");
Object results = store.ExecuteQuery("PREFIX pizza:<http://example.org/> SELECT * WHERE { ?X ?Y pizza:MushroomTopping . }
if (results is SparqlResultSet)
{
//Print out the Results
//Console.WriteLine("working up to this ");
SparqlResultSet rset = (SparqlResultSet)results;
foreach (SparqlResult result in rset.Results)
{
Console.WriteLine(result.ToString());
}
}

最佳答案

参见 Querying with SPARQL文档特别是关于 Common Errors 的部分其中说:

A common error with making queries is that queries by default typically operate only over the unnamed default graph in the store (depending on your query processor). Therefore executing queries may yield no results depending on what graphs your data is in and whether you configured your dataset correctly. Please see the SPARQL Datasets page for discussions of configuring different kinds of dataset. You can also look at Debugging SPARQL Queries for a method to debug what is happening with your query when using the in-memory SPARQL engine.

The typical cause of this is that when you call LoadFromFile() or LoadFromUri() the library automatically assigns the graph a name based on the data source so when you add it a store instance it is a named graph rather than the default graph. The easiest way to resolve this is to simply set the BaseUri property of your graph instance to null after loading it and before you execute queries with it.

添加的重点是我的,因为这准确描述了您的情况。加载的图表根据其来源的文件命名,命名图表也是如此,您商店的默认图表保持为空。

另请注意,由于这个问题,您使用的 ExecuteQuery() 方法已被特别弃用,并且您会收到有关使用过时方法的编译器警告。此方法可能会在未来的版本中删除,因此应避免使用。

有两种方法可以解决这个问题,首先,如上面引用的文档中所述,我们可以从图中删除名称,以便将其视为默认图:

Graph g = new Graph();
g.LoadFromFile(@"C:\pizza.rdf");
g.BaseUri = null;
store.Add(g);

或者您可以通过创建 query processor 来完全避免使用已弃用的方法。和一个数据集,让您可以直接控制将哪个图用作默认值:

Graph g = new Graph();
g.LoadFromFile(@"C:\pizza.rdf");
ISparqlDataset ds = new InMemoryDataset(g);
LeviathanQueryProcessor processor = new LeviathanQueryProcessor(ds);
Object results = processor.ProcessQuery("# Your query");

关于c# - dotNetRDF 中的 sparql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30497174/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com