是否可以在SPARQL查询中使用嵌套删除?

时间:2016-05-24 08:21:11

标签: sparql blank-nodes

我想使用查询来使用唯一ID对资源进行重复数据删除。插入/删除 - 查询不起作用,因为必须创建的节点少于删除的节点。是否可以使用类似的东西?

insert {
    ?new a mails:Account.
    ?new mails:hasID ?id.
    ?new rdfs:label ?label
  }
where {
    {
        select distinct ?id ?label where {
            ?account a mails:Account.
            ?account mails:hasID ?id.
            ?account rdfs:label ?label
        }
    }
    bind(bnode() as ?new)
    {
        delete where {
            ?account mails:hasID ?id
        }
    }
}

1 个答案:

答案 0 :(得分:1)

只是“因为必须创建的节点少于删除的节点”并不一定意味着您不能使用正常的插入/删除。 RDF是基于集合的表示;如果多次插入相同的三元组,则与插入一次相同。如果要标准化一组三元组,可以使用带参数的 bnode 为查询结果创建相同的空白节点:(强调添加):

  

BNODE函数构造一个与所有不同的空白节点   正在查询的数据集中的空白节点与所有空白区别   通过调用此构造函数为其他查询解决方案创建的节点。   如果使用无参数形式,则每个调用都会产生不同的结果   空白节点。 如果使用带有简单文字的表单,则每次调用   导致不同的简单文字的不同空白节点,以及   相同的空白节点,用于具有相同简单文字的调用   一个解决方案映射的表达式。

这意味着您可以:

insert {
  ?new a mails:Account.
  ?new mails:hasID ?id.
  ?new rdfs:label ?label
}
delete {
  ?account mails:hasId ?id
}
where {
  ?account a mails:Account.
  ?account mails:hasID ?id.
  ?account rdfs:label ?label

  #-- One new bnode is created for each *distinct*
  #-- ?id value.  If two accounts have the same 
  #-- ?id value, then they will get the same bnode().
  bind (bnode(str(?id)) as ?new)
}

如果您尝试将所有帐户合并为一个,即使它们具有不同的ID,您也可以将常量值传递到 bnode 函数,例如,

bind (bnode("") as ?new)
相关问题