如何在 python rdflib 中运行 IN 和 NOT IN SPARQL 语句以删除两个图的交集

发布于 2024-11-03 09:17:20 字数 436 浏览 0 评论 0 原文

我正在尝试在 SPARQL 的 python 实现(现在在 rdfextras),但似乎语法无法识别。

让我们考虑两个集合(A 和 B)。我想输出 A 组中的内容,删除 B 组中的内容。

SELECT ?title WHERE {
   some logic defining ?item and ?SetB
   FILTER (?item NOT IN ?SetB)
}

也许这个特定的东西是在 SPARQL 1.1 中添加的,并且不受 rdfextra 支持,在这种情况下,我希望有一个解决方法(或者如何在不使用 NOT IN 关键字的情况下做到这一点)

I'm trying to use the IN and NOT IN statements (that were if I understand correctly, introduced in SPARQL 1.1) on the python implementation of SPARQL (now in rdfextras) but it seems that the syntax is not recognised.

Let's consider two sets (A and B). I want to output what is in Set A, removing what is in Set B.

SELECT ?title WHERE {
   some logic defining ?item and ?SetB
   FILTER (?item NOT IN ?SetB)
}

Maybe this particular thing was added in SPARQL 1.1 and is not supported by rdfextra, in which case I would love to have a workaround (or how to do it without using the NOT IN keyword)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宛菡 2024-11-10 09:17:20

我尝试过类似的查询,也遇到了解析异常。我已经浏览了rdflib的SPARQL解析器代码并且似乎不存在处理 INNOT IN 的规则。我假设这个功能没有实现。

无论如何,我不确定你是否正确使用它。查看 NOT IN 定义>SPARQL 1.1 规范 ...它定义了要针对表达式列表使用的 IN 运算符。因此,你会这样做:

FILTER (?item NOT IN (?SetB))

而且我不完全确定是否可以在右侧使用变量,因为中的所有示例规范使用术语。 编辑:请参阅 RobV 消息,可以在 RLH 中使用变量

通过一个查询解决方法

一种可能的解决方案,可能适合您是使用OPTIONALbound(rdflib都支持)。类似...

SELECT ?title WHERE {
   some logic defining ?item
   OPTIONAL {
   some logic defining ?SetB
   }
   FILTER (bound(?SetB) && ?setB != ?item)
}

如果不了解您的查询的更多信息,我无法就这种情况提供更好的建议。

使用两个查询的解决方法

使用 rdlib 解决此问题的最简单方法是使用过滤器和两个查询,第一个查询检索 ?SetB 的所有可能值。您动态创建的第二个查询中的 Ant
一个过滤器:

SELECT ?title WHERE {
   some logic defining ?item
   FILTER (?item != <setb_val1> && ?item != <setb_val2> &&
   ... && ?item != <setb_val2>)
}

I have tried a similar query and also got a parsing exception. I have gone through rdflib's SPARQL parser code and it doesn't seem to exist a rule to handle IN or NOT IN. I would assume that this functionality is not implemented.

Anyway, I am not sure you are using it correctly. Having a look at the NOT IN definition in the SPARQL 1.1 spec ... it defines the IN operator to be used against a list of expressions. Therefore, you'd do:

FILTER (?item NOT IN (?SetB))

And I am not completely sure if you can use variables in the right-hand side because all the examples in the spec use terms. edit: see RobV message , it's possible to use variables in the RLH

workaround with one query

One possible solution, that might work for you is to use OPTIONAL and bound (both supported in rdflib). Something like ...

SELECT ?title WHERE {
   some logic defining ?item
   OPTIONAL {
   some logic defining ?SetB
   }
   FILTER (bound(?SetB) && ?setB != ?item)
}

Without knowing more about your query I cannot advice better with this case.

workaround with two queries

The easiest way to solve this with rdlib is to use filters and two queries, the first query retrieves all the posible values for ?SetB. Ant in the second query you dynamically create
a filter:

SELECT ?title WHERE {
   some logic defining ?item
   FILTER (?item != <setb_val1> && ?item != <setb_val2> &&
   ... && ?item != <setb_val2>)
}
怪异←思 2024-11-10 09:17:20

没有具体细节很难回答,但听起来你想要 MINUS

SELECT ?title WHERE {
    ?item ... ITEM CRITERIA ...
    MINUS { ?item ... SET CRITERIA ... }
}

例如:

SELECT ?title WHERE {
    ?item ex:colour "red" .       # item is red
    MINUS { ?item ex:size "big" } # but not in set of big things
}

NOT IN 有点误导:据我所知,它在列表表达式上运行,不是您可以定义的列表。

Difficult to answer without specifics, but it sounds like you want MINUS:

SELECT ?title WHERE {
    ?item ... ITEM CRITERIA ...
    MINUS { ?item ... SET CRITERIA ... }
}

for example:

SELECT ?title WHERE {
    ?item ex:colour "red" .       # item is red
    MINUS { ?item ex:size "big" } # but not in set of big things
}

NOT IN is a bit misleading: as far as I can tell it operates over a list expression, not a list you can define.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文