"Graph" 中的 "identifier" 有什么作用?

What does the "identifier" in "Graph" do?

我尝试这样查询数据库:

from rdflib import Graph, Literal, URIRef
from rdflib.namespace import RDF, SKOS
from rdflib.plugins.stores import sparqlstore


# define endpoint according to https://www.stardog.com/docs/
endpoint = 'http://path/to/query'  # http://<server>:<port>/{db}/query

# create store
store = sparqlstore.SPARQLUpdateStore()

# I only want to query
store.open(endpoint)
store.setCredentials('me', 'my_pw')

# What does this actually do? That runs through
default_graph = URIRef('some:stuff')
ng = Graph(store, identifier=default_graph)
# # If identifier is not defined, it crashes
# ng = Graph(store)

rq = """
SELECT ?foo ?bar 
WHERE {
  ?something a <http://path/to/data/.ttl#SomeValues>.
  ?something <http://path/to/data/.ttl#foo> ?foo.
  ?something <http://path/to/data/.ttl#bar> ?bar.                       
}
"""

query_res = ng.query(rq)
for s, l in query_res:
    print(s, l)

很遗憾,我目前没有得到任何结果:

<head><variable name="foo"></variable><variable name="bar"></variable></head><results></results></sparql>

我的问题是,Graph 中的 identifier 在做什么,即这是否重要,如果重要,应该如何定义。当我不定义它时,代码崩溃:

Response: b'{"message":"No separator character found in the URI: N53e412e0f3a74d6eab7ed6da163463bf"}'

如果我输入任何其他有冒号或斜杠的东西,它就会运行(但查询仍然没有 return 任何东西)。

谁能简单解释一下,应该在那里放什么,这是否可能是查询不成功的原因(查询命令本身是正确的;当我从另一个工具调用它时,它工作正常)?

Graph constructor allows to identify an RDFLib graph. If the value is None, then blank nodeidentifier 参数用作标识符。

但是,如果 store 值是 SPARQLUpdateStore,则 identifier 值也用作 SPARQL 协议的 default-graph-uri,因此不能一个空白节点。

因此,问题是:远程三元组中默认的"unnamed"图的名称是什么?

来自 Stardog's documentation:

Naming

Stardog includes aliases for several commonly used sets of named graphs. These non-standard extensions are provided for convenience and can be used wherever named graph IRIs are expected. This includes SPARQL queries & updates, property graph operations and configuration values. Following is a list of special named graph IRIs.

          Named Graph IRI                             Refers to                
--------------------------------  ---------------------------------------------
tag:stardog:api:context:default   the default (no) context graph              
tag:stardog:api:context:all       all contexts, including the default graph    
tag:stardog:api:context:named     all named graphs, excluding the default graph

我找不到任何 public 的私有 Stardog 端点(似乎 ABS 的端点已关闭)。 DBpedia 上的示例:

from rdflib import Graph, URIRef
from rdflib.plugins.stores import sparqlstore

store = sparqlstore.SPARQLUpdateStore()
store.open('http://dbpedia.org/sparql')

default_graph = URIRef('http://people.aifb.kit.edu/ath/#DBpedia_PageRank') 
ng = Graph(store, identifier=default_graph)

rq = """
    SELECT ?foo ?foobar {
      ?foo ?foobar ?bar                       
    } LIMIT 100
"""

query_res = ng.query(rq)
for s, l in query_res:
    print(s, l)

结果与 should be. Even in your code, the name of unnamed graph is the only problem, the results obtained are correct SPARQL XML results 相似。


P.S。也许您可以尝试 instead of 来达到您的目的。