简体   繁体   English

如何使用sparql从RDF Triple中提取谓词(与谓词进行比较)

[英]how to extract subject (Name) in comparison with predicate from a RDF Triple using sparql

http://www.example.com/teach.rdfs/John   http://www.example.com/teach.rdfs#position "Full Professor"        

http://www.example.com/teach.rdfs/John   http://www.example.com/teach.rdfs#course"Math"

http://www.example.com/teach.rdfs/John   http://www.example.com/teach.rdfs#student"Undergraduate"

http://www.example.com/teach.rdfs/Arthur http://www.example.com/teach.rdfs#position "Assistant Professor"

http://www.example.com/teach.rdfs/Arthur http://www.example.com/teach.rdfs#course"Web Engineering"

http://www.example.com/teach.rdfs/Arthur http://www.example.com/teach.rdfs#student"Graduate"

IF these are the triples and i want to Find assistant professors who teach Graduate 如果这些是三元组,我想找到教授研究生的助理教授

 Lecturer       Position

 Arthur         Assistant Professor 

how it is possible to extract the above date using SPARQL 如何使用SPARQL提取上述日期

Your data isn't in any legal RDF serialization that I know of, but it is fairly easy to get it into the N3 serialization. 据我所知,您的数据没有采用任何合法的RDF序列化方式,但是将其导入N3序列化相当容易。 It is rather unusual to see both http://.../teach.rdfs# and http://.../teach.rdfs/ used as prefixes in the same document. http://.../teach.rdfs#http://.../teach.rdfs/用作同一文档中的前缀是很不寻常的。 It's common to see one or the other, but not both. 经常看到一个或另一个,但不能同时看到两个。 It's not illegal though, so we can work with it. 但这不是非法的,因此我们可以使用它。 In N3 format, here is your data as a file, data.n3 : 在N3格式中,这是您的数据文件data.n3

@prefix teach1: <http://www.example.com/teach.rdfs/> .
@prefix teach2: <http://www.example.com/teach.rdfs#> .

teach1:John teach2:position "Full Professor" .
teach1:John teach2:course "Math" .
teach1:John teach2:student "Undergraduate" .
teach1:Arthur teach2:position "Assistant Professor" .
teach1:Arthur teach2:course "Web Engineering" .
teach1:Arthur teach2:student "Graduate" .

The query is pretty simple too. 查询也非常简单。 Here is as, as a file called query.sparql : 就像一个名为query.sparql的文件query.sparql

PREFIX teach1: <http://www.example.com/teach.rdfs/>
PREFIX teach2: <http://www.example.com/teach.rdfs#>

SELECT ?lecturer ?position WHERE {
  VALUES ?position { "Assistant Professor" }
  ?lecturer teach2:position ?position ;
            teach2:student "Graduate" .
}

The only thing that is a bit unusual about this query is the use of VALUES ?position { "Assistant Professor" } . 此查询唯一与众不同的地方是使用VALUES ?position { "Assistant Professor" } The reason that I used the VALUES form is that your desired results included the "Assistant Professor" in the output. 我使用VALUES表格的原因是,您想要的结果在输出中包括"Assistant Professor" If we exclude the VALUES ... part, we can rewrite the pattern as 如果我们排除VALUES ...部分,则可以将模式重写为

  ?lecturer teach2:position "Assistant Professor" ;
            teach2:student "Graduate" .

and still find the same ?lecturer s, but there is no variable bound to "Assistant Professor" . 并且仍然找到相同的?lecturer ,但"Assistant Professor"没有变数。 With the data and query in hand, we can run the query against the data using Jena's ARQ command line tools: 有了数据和查询,我们可以使用Jena的ARQ命令行工具针对数据运行查询:

$ arq --query query.sparql --data data.n3 
-----------------------------------------
| lecturer      | position              |
=========================================
| teach1:Arthur | "Assistant Professor" |
-----------------------------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM