我正在编写一些python代码来动态构建一些任意长度的SPARQL查询。
我有一个看起来像这样的子查询:
?topObj a up:Topological_Domain_Annotation;
rdfs:comment '{}';
up:range ?range.
?proteins a up:Protein .
?proteins up:annotation ?otherTop .
?otherTop a up:Topological_Domain_Annotation;
rdfs:comment '{}';
up:range ?otherRange.""",
我要去的地方,根据用户指定的输入,将{}设置为.format()。我想要一种方法来堆叠这些可能与其他对象匹配的子查询中的多个。 rdfs:comment '{}';
行。但是,当然,我用来遍历所需对象的变量名绑定在第一个子查询中。
最佳解决方案是:
编辑:
这是使用随机变量方法将上述模板连接在一起的示例查询。
SELECT DISTINCT ?proteins
WHERE {
<http://purl.uniprot.org/uniprot/P04439> up:annotation ?1WGQM.
?1WGQM a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range ?VLIT1 .
<http://purl.uniprot.org/uniprot/P01911> up:annotation ?FIICT.
?FIICT a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range ?W89B2 .
<http://purl.uniprot.org/uniprot/P10321> up:annotation ?WU6G3.
?WU6G3 a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range ?ZSIQ3 .
?proteins a up:Protein .
?proteins up:annotation ?otherTop .
?otherTop a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range ?OTHERRANGE .
}
LIMIT 10
只需使用BNode而不是为这些变量生成唯一的名称,例如这些东西:
SELECT DISTINCT ?proteins
WHERE {
<http://purl.uniprot.org/uniprot/P04439> up:annotation [
a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range []
].
<http://purl.uniprot.org/uniprot/P01911> up:annotation [
a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range []
] .
<http://purl.uniprot.org/uniprot/P10321> up:annotation [
a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range []
].
?proteins a up:Protein .
?proteins up:annotation [
a up:Topological_Domain_Annotation;
rdfs:comment ?topology;
up:range []
].
}
LIMIT 10
HTH