dotNetRDF 中的 sparql 查询
sparql query in dotNetRDF
我已经通过曼彻斯特 OWL 语法转换器将众所周知的 pizza.owl
ontology 转换为 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 documentation specifically the section on 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");
RobV 的答案很重要,你最终可能 运行 进入它,但我认为最有可能的问题是你在查询中的前缀是错误的。这是您的查询:
PREFIX pizza:<http://example.org/>
SELECT * WHERE {
?X ?Y pizza:MushroomTopping .
}
不过,如果您查看数据中的一些三元组,您会看到,例如:
<http://www.co-ode.org/ontologies/pizza/pizza.owl#RocketTopping> <http://www.w3.org/2002/07/owl#disjointWith> <http://www.co-ode.org/ontologies/pizza/pizza.owl#MushroomTopping> .
pizza: 前缀应该是 http://www.co-ode.org/ontologies/pizza/pizza.owl #。如果你做了那个改变,那么你可能会得到更有意义的结果,比如这些(但我 运行 这是在耶拿,所以结果可能 看起来 有点不同):
prefix pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
select ?s ?p {
?s ?p pizza:MushroomTopping
}
-------------------------------------------------------------------------------
| s | p |
===============================================================================
| _:b0 | <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> |
| pizza:CaperTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
| pizza:PepperTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
| _:b1 | <http://www.w3.org/2002/07/owl#someValuesFrom> |
| pizza:RocketTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
| _:b2 | <http://www.w3.org/2002/07/owl#someValuesFrom> |
| _:b3 | <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> |
| pizza:OnionTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
| _:b4 | <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> |
| _:b5 | <http://www.w3.org/2002/07/owl#someValuesFrom> |
| pizza:TomatoTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
| pizza:GarlicTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
| _:b6 | <http://www.w3.org/2002/07/owl#someValuesFrom> |
| _:b7 | <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> |
| pizza:OliveTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
| pizza:ArtichokeTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
-------------------------------------------------------------------------------
How can I get the triples with predicate MushroomTopping?
请注意,RDF 三元组的形式为 (subject, predicate, object)。当你写一个 SPARQL 查询时,你会写:
select ?subject ?predicate ?object where {
?subject ?predicate ?object
}
您编写的查询在 object 位置有 pizza:MushroomTopping。幸运的是,在这种情况下,这可能更有意义,因为 pizza:MushroomTopping 是 OWL class,而不是属性,所以你不太可能看到它出现在谓词位置。
我已经通过曼彻斯特 OWL 语法转换器将众所周知的 pizza.owl
ontology 转换为 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 documentation specifically the section on 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");
RobV 的答案很重要,你最终可能 运行 进入它,但我认为最有可能的问题是你在查询中的前缀是错误的。这是您的查询:
PREFIX pizza:<http://example.org/>
SELECT * WHERE {
?X ?Y pizza:MushroomTopping .
}
不过,如果您查看数据中的一些三元组,您会看到,例如:
<http://www.co-ode.org/ontologies/pizza/pizza.owl#RocketTopping> <http://www.w3.org/2002/07/owl#disjointWith> <http://www.co-ode.org/ontologies/pizza/pizza.owl#MushroomTopping> .
pizza: 前缀应该是 http://www.co-ode.org/ontologies/pizza/pizza.owl #。如果你做了那个改变,那么你可能会得到更有意义的结果,比如这些(但我 运行 这是在耶拿,所以结果可能 看起来 有点不同):
prefix pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
select ?s ?p {
?s ?p pizza:MushroomTopping
}
-------------------------------------------------------------------------------
| s | p |
===============================================================================
| _:b0 | <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> |
| pizza:CaperTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
| pizza:PepperTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
| _:b1 | <http://www.w3.org/2002/07/owl#someValuesFrom> |
| pizza:RocketTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
| _:b2 | <http://www.w3.org/2002/07/owl#someValuesFrom> |
| _:b3 | <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> |
| pizza:OnionTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
| _:b4 | <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> |
| _:b5 | <http://www.w3.org/2002/07/owl#someValuesFrom> |
| pizza:TomatoTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
| pizza:GarlicTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
| _:b6 | <http://www.w3.org/2002/07/owl#someValuesFrom> |
| _:b7 | <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> |
| pizza:OliveTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
| pizza:ArtichokeTopping | <http://www.w3.org/2002/07/owl#disjointWith> |
-------------------------------------------------------------------------------
How can I get the triples with predicate MushroomTopping?
请注意,RDF 三元组的形式为 (subject, predicate, object)。当你写一个 SPARQL 查询时,你会写:
select ?subject ?predicate ?object where {
?subject ?predicate ?object
}
您编写的查询在 object 位置有 pizza:MushroomTopping。幸运的是,在这种情况下,这可能更有意义,因为 pizza:MushroomTopping 是 OWL class,而不是属性,所以你不太可能看到它出现在谓词位置。