避免指定显式枚举资源的数量?

时间:2014-12-09 19:17:47

标签: rdf sparql having rdfs

我的SPARQL查询出现问题。我想要的食谱含量少于我提供的食材数量。这是查询:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rec:<http://www.receta.org#>
select ?recipe  (count(distinct ?Ingrediente) as ?oi) {
  ?recipe rec:Ingrediente ?Ingrediente
  filter not exists {
    ?recipe rec:Ingrediente ?other_ingredient
    filter( ?other_ingredient not in (rec:Cebolla,rec:Tomate, rec:Aceite, rec:Sal) )
  }}
group by ?recipe
having(  (count(distinct ?Ingrediente)) < 4)
order by (count(distinct ?Ingrediente))

这是一个例子。让我们说我有这些食谱:

  • prueba3:cebolla
  • prueba:cebolla,aceite
  • prueba2:cebolla,aceite,tomate
  • prueba4:cebolla,tomate,aceite,sal

然后我想打印:

  • prueba3:cebolla
  • prueba:cebolla,aceite
  • prueba2:cebolla,aceite,tomate

我不想打印 prueba4 ,因为它完全相同数量的成分。

查询的主要问题是&#34;有&#34; line:having((count(distinct ?Ingrediente)) < 4)。在这种情况下这是有效的,但我不想放4,我想以通用的方式做,所以每次添加更多的成分时我都不必改变这个数字。

那么我怎么能改变那条线?谢谢!

1 个答案:

答案 0 :(得分:0)

拥有&#39;在SELECT绑定之后执行子句,因此您可以使用以下内容:

PREFIX ...
select ?recipe  (count(distinct ?Ingrediente) as ?oi) (4 AS ?count)
WHERE {
   ?recipe rec:Ingrediente ?Ingrediente
   filter not exists {
   ?recipe rec:Ingrediente ?other_ingredient
   filter( ?other_ingredient not in (rec:Cebolla,rec:Tomate, rec:Aceite, rec:Sal) )
 }}
group by ?recipe
having(?oi < ?count)
order by ?oi

... where?count可以从根据数据查询的值或一些三重匹配的计数中计算出来。