使用“图表” SPARQL 中的关键字用于获取远程图

发布于 2024-10-17 11:49:21 字数 790 浏览 6 评论 0原文

我希望使用 SPARQL 来完成相对基本的任务:给定一个 FOAF 图,我想解析我在其中找到的元素,获取它们的标签(如果存在),然后将它们用作新图查找有关这些人的信息。

例如,您可以想象一个简单的用例,我想运行 SPARQL 查询来列出我所有最喜欢的食物(根据我的 FOAF 文件),以及我所有朋友最喜欢的食物。

这是目前的样子。请注意,出于测试目的,目前我尝试对下面的查询执行的操作是通过 ?name3 变量获取朋友的姓名。运行此查询不会返回 ?graph 和 ?name3 的任何结果,即使我知道 rdfs:seeAlso 链接到一些有效的 RDF 文件,其中至少两个应该具有 name 属性。感谢您提供的任何意见!

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?name1 ?name2 ?graph ?name3
FROM <my-rdf-file>
WHERE { 
    ?person1 foaf:knows ?person2 .
    ?person1 foaf:name ?name1 .
    ?person2 foaf:name  ?name2 .
    OPTIONAL { 
        ?person2 rdfs:seeAlso ?graph .
        GRAPH ?graph {
            ?person3 foaf:name ?name3 .         
        }   
    }       
}

I'm looking to use SPARQL for a relatively basic task: Given a FOAF graph, I'd like to parse the elements I find in there, get their tags (if they exist) and then, use those as new graphs from which to find information about those people.

So for instance, you could imagine a simple use case where I want to run a SPARQL query to list all of my favorite foods (as per my FOAF file), and also the favorite foods of all my friends.

Here is what this looks like at the moment. Note that for testing purposes, at the moment all I'm trying to do with the query below is fetch the name of the friend, through the ?name3 variable. Running this query doesn't return any results for ?graph and ?name3, even though I know that the rdfs:seeAlso link to some valid RDF files, of which at least two should have a name attribute. Thanks for any input you might have!

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?name1 ?name2 ?graph ?name3
FROM <my-rdf-file>
WHERE { 
    ?person1 foaf:knows ?person2 .
    ?person1 foaf:name ?name1 .
    ?person2 foaf:name  ?name2 .
    OPTIONAL { 
        ?person2 rdfs:seeAlso ?graph .
        GRAPH ?graph {
            ?person3 foaf:name ?name3 .         
        }   
    }       
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惯饮孤独 2024-10-24 11:49:21

GRAPH 不会隐式地将远程数据提取到存储中,这会带来太大的安全风险。可能在某些系统中您可以启用此功能,但它是非标准的。

但是,在 SPARQL 1.1 更新中,有一个关键字 LOAD,尽管它可以执行此操作,但您可以编写:

LOAD <uri>

它将把图形提取到存储中,因此您可以编写:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?graph
FROM <my-rdf-file>
WHERE { 
    ?person1 foaf:knows ?person2 .
    ?person1 foaf:name ?name1 .
    ?person2 foaf:name  ?name2 .
    OPTIONAL { 
        ?person2 rdfs:seeAlso ?graph .   
    }       
}

将 ?graph 的绑定馈送到一组 LOAD 语句中,然后运行您的原始查询。

注意,在某些系统中,例如4store,您需要启用LOAD,默认情况下不允许,因此请检查您所使用的商店的文档。

GRAPH doesn't implicitly fetch remote data into the store, that would be too much of a security risk. There may be some systems where you can enable this, but it's non-standard.

However, in SPARQL 1.1 Update, there's a keyword LOAD, which does this though, you can write:

LOAD <uri>

Which will fetch the graph into the store, so you could write:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?graph
FROM <my-rdf-file>
WHERE { 
    ?person1 foaf:knows ?person2 .
    ?person1 foaf:name ?name1 .
    ?person2 foaf:name  ?name2 .
    OPTIONAL { 
        ?person2 rdfs:seeAlso ?graph .   
    }       
}

Feed the bindings for ?graph into a set of LOAD statements, and then run your original query.

N.B. in some systems, e.g. 4store you need to enable LOAD, it's not allowed by default, so check the documentation of the store you're using.

鹊巢 2024-10-24 11:49:21

除了使用 LOAD 之外,一旦您发现了需要包含的图表(按照上面 Steve 的示例),使用 FROM NAMED 来说明结果查询中的所有相关图表是否合理,即

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?name1 ?name2 ?name3
FROM <my-rdf-file>
FROM NAMED <discovered-graph-URI-1>
FROM NAMED <discovered-graph-URI-n>
WHERE { 
GRAPH <my-rdf-file> {
    ?person1 foaf:knows ?person2 .
    ?person1 foaf:name ?name1 .
    ?person2 foaf:name  ?name2 .
 }
    OPTIONAL { 
        ?person2 rdfs:seeAlso <discovered-graph-URI-1> .
        GRAPH <discovered-graph-URI-1> {
            ?person3 foaf:name ?name3 .         
        }   
        ?person2 rdfs:seeAlso <discovered-graph-URI-n> .
        GRAPH <discovered-graph-URI-n> {
            ?person3 foaf:name ?name3 .         
        }  
    }       
}

这样您就可以绕过安全问题,并且您不必在自己的三元组存储中维护数据。

Instead of using LOAD, once you have discovered the graphs that you need to include, as per Steve's example above, wouldn't it be reasonable to alternatively use FROM NAMED , stating all the relevant graphs in the resulting query i.e

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?name1 ?name2 ?name3
FROM <my-rdf-file>
FROM NAMED <discovered-graph-URI-1>
FROM NAMED <discovered-graph-URI-n>
WHERE { 
GRAPH <my-rdf-file> {
    ?person1 foaf:knows ?person2 .
    ?person1 foaf:name ?name1 .
    ?person2 foaf:name  ?name2 .
 }
    OPTIONAL { 
        ?person2 rdfs:seeAlso <discovered-graph-URI-1> .
        GRAPH <discovered-graph-URI-1> {
            ?person3 foaf:name ?name3 .         
        }   
        ?person2 rdfs:seeAlso <discovered-graph-URI-n> .
        GRAPH <discovered-graph-URI-n> {
            ?person3 foaf:name ?name3 .         
        }  
    }       
}

That way you get round the security issues and you don't have to maintain the data in your own triplestore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文