SPARQL查询RDF本体 [英] SPARQL query RDF ontology

查看:0
本文介绍了SPARQL查询RDF本体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这个SPARQL查询,但找不出问题所在。我想要所有含有这些成分或更少成分的收据。即: 收件人1:西红柿 收纳品2:番茄和盐 第三种:番茄、盐、洋葱。

我有以下问题:

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 reduced ?r (count (?i) as ?cuantos) WHERE {

    ?x rdf:type rec:Receta .
    ?x rdfs:label ?r.

    filter not exists {
        ?x rec:Ingrediente ?i
        filter( ?i not in (rec:Tomato, rec:Salt) )
    }
}
GROUP BY ?r

我只想显示第一个。不是第二个,也不是最后一个。

这只是一个例子,但我实际上想要用3个以上的接收器来做这件事,比方说一个巨大的数据库,我想以成分少于番茄和盐的接收器为例。那么,我如何修复我的查询才能做到这一点呢?

我要试着解释一下:

例如:

假设我有一个包含数百个食谱的大型数据库。其中一些是: 菜谱1:西红柿和盐 收据2:洋葱和肉 回忆录3:番茄和醋 收据4:巧克力和牛奶

在我的查询中,我说我想要番茄、盐和醋的食谱。所以在这种情况下,我想要配方1和配方3,因为配料1有西红柿和盐,配料3有西红柿和醋,比我说的少一种配料。现在假设我添加了更多食谱:

秘诀4:番茄 食谱5:盐 菜谱6:番茄、醋、盐和洋葱 解决方案6:番茄、醋、盐

如果我再次执行查询,我希望获得Receipe1、Receipe3、Receipe4和Receipe5。缘由 那些食谱就是那些含有这些成分或更少的食谱。我不想让收货的成分比我要求的多,甚至连收货都不要。

谢谢!

推荐答案

如果我理解正确的话,您正在尝试指定一组配料,然后选择其配料是该配料集的一个属性子集的食谱(或者那些其配料只是一个子集的食谱;您问题中的示例没有说明这一点)。无论如何,与其用自然语言写出容易出错的例子,不如提供我们可以使用的数据,在这种情况下,这样做一点也不难。例如,下面是一个包含9个食谱的小海龟文件,每个食谱都有一些成分:

@prefix : <urn:ex:> .

:recipe1 :ingredient :tomato, :salt .
:recipe2 :ingredient :onion, :meat .
:recipe3 :ingredient :tomato, :vinegar .
:recipe4 :ingredient :chocolate, :milk .
:recipe5 :ingredient :tomato .
:recipe6 :ingredient :salt .
:recipe7 :ingredient :tomato, :vinegar, :salt, :onion .
:recipe8 :ingredient :tomato, :vinegar, :salt .
:recipe9 :ingredient :vinegar, :salt .

现在,我们可以编写一个查询来检索其所有配料都在指定集合中的食谱,并且只保留那些不同配料少于(或小于或等于)特定数量的食谱。(您不必包括GROUP_CONCAT内容;我这样做只是为了获得每个食谱的配料列表。)

prefix : <urn:ex:>

select ?recipe (group_concat(?ingredient;separator=', ') as ?ingredients) {
  ?recipe :ingredient ?ingredient
  filter not exists {
    ?recipe :ingredient ?other_ingredient
    filter( ?other_ingredient not in ( :salt, :tomato, :vinegar ) )
  }
}
group by ?recipe
having (count(distinct ?ingredient) < 3)
----------------------------------------------
| recipe   | ingredients                     |
==============================================
| :recipe9 | "urn:ex:salt, urn:ex:vinegar"   |
| :recipe5 | "urn:ex:tomato"                 |
| :recipe6 | "urn:ex:salt"                   |
| :recipe3 | "urn:ex:tomato, urn:ex:vinegar" |
| :recipe1 | "urn:ex:tomato, urn:ex:salt"    |
----------------------------------------------

如果要允许食谱包含所有三种成分,只需更改行

having (count(distinct ?ingredient) < 3)

发送至以下任一地址:

having (count(distinct ?ingredient) <= 3)

having (count(distinct ?ingredient) < 4)

这篇关于SPARQL查询RDF本体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆