简体   繁体   English

为什么可选绑定不起作用

[英]Why is the optional binding not working

This is a question about why my code is not working, not about how to do something, that's why I didn't provide you with a data, but if you want, i can give you, but the data will be my relatively small ontology.这是一个关于为什么我的代码不起作用的问题,而不是关于如何做某事,这就是为什么我没有给你提供数据,但如果你想要,我可以给你,但数据将是我相对较小的本体.

this is my code这是我的代码

OPTIONAL
      { 

    VALUES ?user { rs:ania }
        ?userContext  rdf:type       rs:UserContext ;
                  rs:appliedOnItems  ?itemClass ;
                  rs:appliedOnUsers  ?userClass .

        ?item  rdf:type  ?itemClass .

        OPTIONAL
          { ?userContext  rs:hasWeightIfContextMatched  ?weightMatched }
        OPTIONAL
          { ?userContext  rs:hasWeightIfContextDoesNotMatch  ?weightNotMatched }
        OPTIONAL
          { ?userContext  rs:doNotRecommendInCaseNotMatch  true
            BIND(1 AS ?skip_)
          }
      bind(if (bound(?skip_) && (not EXISTS {?user a ?userClass}) , ?skip_, 0) as ?skip1)


    values (?defaultUserMatched ?defaultUserNotMatched) {(1 0.5)}
        BIND(if(EXISTS { ?user  rdf:type  ?userClass }, coalesce(?weightMatched, ?defaultUserMatched), coalesce(?weightNotMatched, ?defaultUserNotMatched)) AS ?weight)


      }
    values ?defaultNoUserContext {1}
    BIND(if(bound(?skip1), ?skip1, 0) as ?skip)
    BIND(if(bound(?weight), ?weight, ?defaultNoUserContext) AS ?userContextWeight)

  }

this code is just a block in my real query, there is another block that brings the ?item variable.这段代码只是我真实查询中的一个块,还有另一个块带来了?item变量。

as you see, my code has ?item rdf:type ?itemClass , but one of the bindings for ?item is not from the type of ?itemClass so the whole optional will not execute (for that binding), so when we go out of the optional, there is this line如您所见,我的代码具有?item rdf:type ?itemClass ,但是?item的绑定之一不是来自?itemClass的类型,因此整个可选项不会执行(对于该绑定),所以当我们离开时可选的,有这一行

BIND(if(bound(?weight), ?weight, ?defaultNoUserContext) AS ?userContextWeight)

the if part will give false so the ?userContextWeight should be bound to ?defaultNoUserContext . if部分将给出false所以?userContextWeight应该绑定到?defaultNoUserContext However, my code doesn't produce anything (any value at all) to these items .但是,我的代码不会为这些项目产生任何东西(根本没有任何价值)。 do you know why please?你知道为什么吗?

again if you need data, i am more than welcome to give you, thanks再次,如果您需要数据,我非常欢迎给您,谢谢

Update更新

Now I see better, what i want is:现在我看得更好了,我想要的是:

even if the item doesn't belong to the ?itemClass , i need to give a default value for the ?userContextWeight .即使该项目不属于?itemClass ,我也需要为?userContextWeight提供一个默认值。

look at the update看更新

 OPTIONAL
      { 
     VALUES ?user { rs:ania }

        ?userContext  rdf:type       rs:UserContext ;
                  rs:appliedOnItems  ?itemClass ;
                  rs:appliedOnUsers  ?userClass .

    bind (if (  exists {?item  rdf:type  ?itemClass .}, true , false) as ?doesItemBelongToUserContextItemClass)

    OPTIONAL
          { ?userContext  rs:hasWeightIfContextMatched  ?weightMatched }
        OPTIONAL
          { ?userContext  rs:hasWeightIfContextDoesNotMatch  ?weightNotMatched }
        OPTIONAL
          { ?userContext  rs:doNotRecommendInCaseNotMatch  true
            BIND(1 AS ?skip_)
          }
      bind(if (bound(?skip_) && (not EXISTS {?user a ?userClass}) , ?skip_, 0) as ?skip1)


    values (?defaultUserMatched ?defaultUserNotMatched) {(1 0.5)}
        BIND(if(EXISTS { ?user  rdf:type  ?userClass }, coalesce(?weightMatched, ?defaultUserMatched), coalesce(?weightNotMatched, ?defaultUserNotMatched)) AS ?weight)
      }
    values ?defaultNoUserContext {1}
    BIND(if(bound(?skip1), ?skip1, 0) as ?skip)

  BIND( if ( !?doesItemBelongToUserContextItemClass , ?defaultNoUserContext ,if(bound(?weight), ?weight, ?defaultNoUserContext)) AS ?userContextWeight)

  }

in the update, i check if the item belongs to the class or not using this bind在更新中,我检查该项目是否属于该类或不使用此bind

 bind (if (  exists {?item  rdf:type  ?itemClass .}, true , false) as ?doesItemBelongToUserContextItemClass)

and then outside the optional i do this然后在optional之外我这样做

BIND( if ( !?doesItemBelongToUserContextItemClass , ?defaultNoUserContext ,if(bound(?weight), ?weight, ?defaultNoUserContext)) AS ?userContextWeight)

my problem is the value of ?userContextWeight when the item doesn't belong to the class is ?weightNotMatched , but it should be ?defaultNoUserContext (look again at the last binding please)我的问题是当项目不属于该类时?userContextWeight的值是?weightNotMatched ,但它应该是?defaultNoUserContext (请再次查看最后一个绑定)

any idea please?有什么想法吗?

Update 2更新 2

by !?doesItemBelongToUserContextItemClass I mean the normal boolean not that we study in algebra, maybe here it is not the same as there?通过!?doesItemBelongToUserContextItemClass我的意思是正常的布尔值不是我们在代数中学习的,也许这里和那里不一样? this could be the problem?这可能是问题吗?

Update 3更新 3

I see that this我看到这个

 bind (if ( exists {?item  a  ?itemClass }, true , false) as ?doesItemBelongToUserContextItemClass)

always gives true to doesItemBelongToUserContextItemClass even though that is not correct, for a specific item , it is not from the itemClass .始终为doesItemBelongToUserContextItemClass提供true ,即使这是不正确的,对于特定的item ,它不是来自itemClass

now i am sure that the problem is here, because i printed the value of the doesItemBelongToUserContextItemClass and it is always true but that is not correct, we are close, so just solving this will solve the question现在我确定问题出在这里,因为我打印了doesItemBelongToUserContextItemClass的值,它总是正确的,但这不正确,我们已经接近了,所以只要解决这个问题就可以解决问题

Your query is big enough that I'm having a very hard time making sense of it, but my best guess is that you're running into a situation where your exists expression doesn't have all the variables bound that you need to make it test what you want it to test.你的查询足够大,我很难理解它,但我最好的猜测是你遇到了这样一种情况,即你的存在表达式没有绑定你需要的所有变量测试您希望它测试的内容。 Here's some very simple data:这是一些非常简单的数据:

@prefix : <urn:ex:>

:s a :D .
:t a :E .

Now, take a look at this query and the results:现在,看看这个查询和结果:

prefix : <urn:ex:>

select * where {
  #-- Find an individual ?a and (one of)
  #-- the classes that it belongs to.
  ?a a ?aClass .

  optional {
    #-- Find an individual ?b and (one of)
    #-- the classes that it belongs to.
    ?b a ?bClass .

    #-- And bind ?isCommonClass to true or
    #-- false, to indicate whether ?b is
    #-- also an element of ?aClass.
    bind(exists{?b a ?aClass} as ?isCommonClass)
  }
}
---------------------------------------------
| a  | aClass | b  | bClass | isCommonClass |
=============================================
| :s | :D     | :s | :D     | true          |
| :s | :D     | :t | :E     | true          |
| :t | :E     | :s | :D     | true          |
| :t | :E     | :t | :E     | true          |
---------------------------------------------

?isCommonClass is always true, even though it seems like it should be true when ?a and ?b are the same, and false otherwise. ?isCommonClass 始终为真,即使当 ?a 和 ?b 相同时它似乎应该为真,否则为假。 I think that what's happening here is that the bind gets evaluated in a context where either ?b or ?aClass isn't set yet, so the exists is actually checking for something more general.认为这里发生的事情是在尚未设置 ?b 或 ?aClass 的上下文中评估绑定,因此存在实际上是在检查更一般的东西。 We can test this by moving the bind outside of the optional :我们可以通过将绑定移动到optional之外来测试:

select * where {
  ?a a ?aClass .

  optional {
    ?b a ?bClass .
  }

  bind(exists{?b a ?aClass} as ?isCommonClass)
}
---------------------------------------------
| a  | aClass | b  | bClass | isCommonClass |
=============================================
| :s | :D     | :s | :D     | true          |
| :s | :D     | :t | :E     | false         |
| :t | :E     | :s | :D     | false         |
| :t | :E     | :t | :E     | true          |
---------------------------------------------

Here, we get the results we'd expect, with ?isCommonClass being true exactly when ?a and ?b are the same.在这里,我们得到了预期的结果,当 ?a 和 ?b 相同时, ?isCommonClass 为真。

The query snippet in the question doesn't provide enough to be sure that this is what's happening, and the query you provided in the comments is too big for anyone else to check, but this seems like a very good candidate for what's going on in your situation.问题中的查询片段不足以确保这是正在发生的事情,并且您在评论中提供的查询太大,其他人无法检查,但这似乎是正在发生的事情的一个很好的候选者你的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM