如果实例具有属性,则SPARQL其他实例也必须 [英] SPARQL if an instance has a property, others must as well

查看:133
本文介绍了如果实例具有属性,则SPARQL其他实例也必须的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特定的实例和一个SPARQL查询,该查询检索与该实例相似的其他实例.类似地,我的意思是其他实例至少具有与特定实例相同的公共属性和值,或者具有与特定实例相同的类.

I have a specific instance and a SPARQL query that retrieves other instances similar to that one. By similar, I mean that the other instances have at least one common property and value in common with the specific instance, or have a class in common with the specific instance.

现在,我想扩展查询,以便如果特定实例具有关键"属性的值,那么唯一被认为相似的实例就是那些具有关键属性(而不是至少具有一个共同的属性和价值).

Now, I'd like to extend the query such that if the specific instance has a value for a "critical" property, then the only instances that are considered similar are those that also have that critical property (as opposed to just having at least one property and value in common).

例如,这是一些示例数据,其中instance1具有predicate2的值,该值是isCriticalPredicate的子属性.

For instance, here is some sample data in which instance1 has a value for predicate2 which is a subproperty of isCriticalPredicate.

@prefix : <http://example.org/rs#>
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

:instance1  a   :class1.
:instance1  :predicate1 :instance3.
:instance1  :predicate2 :instance3.  # (@) critical property

:instance2  a   :class1.
:instance2  :predicate1 :instance3.

:instance4  :predicate2 :instance3.

:predicate1 :hasSimilarityValue 0.6.

:predicate2 rdfs:subPropertyOf   :isCriticalPredicate.
:predicate2 :hasSimilarityValue 0.6.

:class1 :hasSimilarityValue 0.4.

这是一个查询,其中?x是特定实例instance1.该查询仅检索instance4,这是正确的.但是,如果我从instance1(标有 @ 的行)中删除关键属性,则不会得到任何结果,但应该得到instance2,因为它具有与instance1相同的属性.我该如何解决?

Here is a query in which ?x is the specific instance, instance1. The query retrieves just instance4, which is correct. However, if I remove the critical property from instance1 (line labeled with @), I get no results, but should get instance2, since it has a property in common with instance1. How can I fix this?

PREFIX : <http://example.org/rs#>

select ?item (SUM(?similarity * ?importance * ?levelImportance) as ?summedSimilarity) 
(group_concat(distinct ?becauseOf ; separator = " , ") as ?reason) where
{
  values ?x {:instance1}
  bind (4/7 as ?levelImportance)
  {
    values ?instanceImportance {1}
    ?x  ?p  ?instance.
    ?item   ?p  ?instance.
    ?p  :hasSimilarityValue ?similarity
      bind (?p as ?becauseOf)
    bind (?instanceImportance as ?importance)
  }
  union
  {
      values ?classImportance {1}
    ?x  a   ?class.
    ?item   a   ?class.
    ?class  :hasSimilarityValue ?similarity
      bind (?class as ?becauseOf)
        bind (?classImportance as ?importance)
  }
  filter (?x != ?item)

    ?x :isCriticalPredicate ?y.
    ?item   :isCriticalPredicate ?y.

}
group by ?item

推荐答案

我之前已经说过,我再说一遍:最小数据非常有帮助.从您过去的问题中,我知道您的工作项目在属性等方面具有相似性值,但是对于您面临的问题,这些都不是真正重要的.这是一些只有几个实例,属性值和一个指定为关键的属性的数据:

I've said it before and I'll say it again: minimal data is very helpful. From your past questions, I know that your working project has similarity values on properties and the like, but none of that really matters for the problem at hand. Here's some data that just has a few instances, property values, and one property designated as critical:

@prefix : <urn:ex:>

:p a :criticalProperty .

:a :p :u ;   #-- :a has a critical property, so 
   :q :v .   #-- only :d can be similar to it.

:c :q :v ;   #-- :c has no critical properties, so
   :r :w .   #-- both :a and :d can be similar to it.

:d :p :u ;
   :q :v .

这样的查询中的技巧是过滤出有问题的结果,而不是尝试选择没有问题的结果.从逻辑上讲,这些含义相同,但是在编写查询时,更容易考虑违反约束的情况,并尝试过滤出违反约束的结果.在这种情况下,您希望过滤出实例具有关键属性和值但类似实例没有的所有结果.

The trick in a query like this is to filter out the results that have the problem, not to try to select the ones that don't. Logically, those mean the same thing, but in writing the query, it's easier to think about constraint violation, and to try to filter out the results that violate the constraint. In this case, you want to filter out any results where the instance has a critical property and value but the similar instance doesn't.

prefix : <urn:ex:>

select ?i (group_concat(distinct ?j) as ?js) where {

  #-- :a has a critical property, but
  #-- :c does not, so these are useful
  #-- starting points
  values ?i { :a :c }

  #-- get ?j instances that have a value
  #-- in common with ?i.
  ?i ?property ?value .
  ?j ?property ?value .

  #-- make sure that ?i and ?j aren't
  #-- the same instance
  filter (?i != ?j)

  #-- make sure that there is no critical
  #-- property value that ?i has that
  #-- ?j does not also have
  filter not exists {
    ?i ?criticalProperty ?criticalValue .
    ?criticalProperty a :criticalProperty .
    filter not exists {
      ?j ?criticalProperty ?criticalValue .
    }
  }
}
group by ?i

----------------------------
| i  | js                  |
============================
| :a | "urn:ex:d"          |
| :c | "urn:ex:d urn:ex:a" |
----------------------------

相关

还有一些其他问题也涉及持续的满意度/违规行为,可能对您的阅读很有帮助.尽管并非所有这些过滤器都使用嵌套的过滤器不存在,但大多数过滤器确实具有过滤器不存在的模式{…过滤<负条件> } .

Related

There are some other questions that also touch on constaint satisfaction/violation that might be useful reading. While not all of these use nested filter not exists, most of them do have a pattern of filter not exists { … filter <negative condition> }.

  • Find lists containing ALL values in a set? (This actually uses a nested filter not exists.)
  • Is it possible to express a recursive definition in SPARQL?
  • Count values that satisfy a constraint and return 0 for those that don't satisfy it in SPARQL
  • SPARQL: return all intersections fulfilled by specified or equivalent classes (There's another nested filter not exists, in the last query in the answer.)

这篇关于如果实例具有属性,则SPARQL其他实例也必须的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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