首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SPARQL查询RDF本体

SPARQL查询RDF本体
EN

Stack Overflow用户
提问于 2014-12-03 15:09:09
回答 1查看 223关注 0票数 0

我正在尝试这个sparql查询,但我很难找出问题所在。我想要所有有这些成分或更少的收据。ie:接收1:番茄接收2:番茄和盐接收3:番茄,盐,洋葱。

我有以下问题:

代码语言:javascript
复制
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份收据,比如说一个庞大的数据库,我想拿那些比西红柿和盐成分更少的收据为例。那么,对于如何修复我的查询,我有什么想法吗?

我要试着解释一下自己:

例如:

比方说,我有一个庞大的数据库,里面有数百个食谱。其中一些是: Recipe1:番茄和盐,Receipe2:洋葱和肉,Receipe3:番茄和醋,Receipe4:巧克力和牛奶

在我的询问中,我说我想要西红柿,盐和醋的食谱。在这种情况下,我想要Recipe1和Recipe3,因为recipe1有番茄和盐,recipe3有番茄和醋,比我说的少一个成分。现在让我们说,我添加了更多的菜谱:

Recipe4:番茄Recipe5: Recipe6:番茄,醋,盐和洋葱Recipe6:番茄,醋,盐

如果我再次执行我的查询,我希望有recipe1、recipe3、recipe4和recipe5。因为那些食谱是那些有这些成分或更少的。我不想让recipe6的原因比我要求的成分更多,甚至recipe7也不想。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-03 17:09:51

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

代码语言:javascript
复制
@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的内容;我这样做只是为了获得每个菜谱的配料列表。)

代码语言:javascript
复制
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)
代码语言:javascript
复制
----------------------------------------------
| 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"    |
----------------------------------------------

如果你想让所有三种配料都有食谱,你可以换一条线。

代码语言:javascript
复制
having (count(distinct ?ingredient) < 3)

对下列任何一种:

代码语言:javascript
复制
having (count(distinct ?ingredient) <= 3)

代码语言:javascript
复制
having (count(distinct ?ingredient) < 4)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27275096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档