【发布时间】:2016-05-04 10:25:10
【问题描述】:
这是一个关于为什么我的代码不起作用的问题,而不是关于如何做某事的问题,这就是我没有向您提供数据的原因,但是如果你想要,我可以给你,但数据将是我的相对较小的本体。
这是我的代码
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)
}
这段代码只是我实际查询中的一个块,还有另一个块带来了?item 变量。
如您所见,我的代码有 ?item rdf:type ?itemClass ,但 ?item 的绑定之一不是来自 ?itemClass 的类型,所以整个可选将不会执行(对于那个绑定),所以当我们出去的时候可选的,有这一行
BIND(if(bound(?weight), ?weight, ?defaultNoUserContext) AS ?userContextWeight)
if 部分将给出false,因此?userContextWeight 应绑定到?defaultNoUserContext。但是,我的代码不会对这些 items 产生任何东西(根本没有任何价值)。请问你知道为什么吗?
再次,如果您需要数据,我非常欢迎给您,谢谢
更新
现在我看得更清楚了,我想要的是:
即使该项目不属于?itemClass,我也需要为?userContextWeight 提供一个默认值。
查看更新
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)
}
在更新中,我使用bind检查该项目是否属于该类
bind (if ( exists {?item rdf:type ?itemClass .}, true , false) as ?doesItemBelongToUserContextItemClass)
然后在optional之外我这样做
BIND( if ( !?doesItemBelongToUserContextItemClass , ?defaultNoUserContext ,if(bound(?weight), ?weight, ?defaultNoUserContext)) AS ?userContextWeight)
我的问题是?userContextWeight的值当项目不属于该类时是?weightNotMatched,但它应该是?defaultNoUserContext(请再看最后一次绑定)
有什么想法吗?
更新 2
!?doesItemBelongToUserContextItemClass 我的意思是我们在代数中学习的普通布尔不是,也许这里和那里不一样?这可能是问题所在?
更新 3
我看到了这个
bind (if ( exists {?item a ?itemClass }, true , false) as ?doesItemBelongToUserContextItemClass)
总是将true 提供给doesItemBelongToUserContextItemClass,即使这是不正确的,对于特定的item,它不是来自itemClass。
现在我确定问题出在这里,因为我打印了 doesItemBelongToUserContextItemClass 的值并且它始终是正确的但这是不正确的,我们已经接近了,所以只要解决这个问题就可以了解决问题
【问题讨论】:
-
我不确定你在问什么。最外层的可选项包括一个非可选的“?item rdf:type ?itemClass .”,所以如果项目没有正确的类型,那么整个最外层的可选项不匹配。如果您希望 item 类在 optional 中是可选的,则需要这样编写。
-
@JoshuaTaylor 你的评论很有帮助,我现在可以看到一些新的东西,我正在尝试自己,我会回复你
-
@JoshuaTaylor 我更新了我的问题,我想我们已经接近解决方案了,这只是一件小事,你能检查一下更新吗? (如果您的代码不清楚,我准备好了)
-
@JoshuaTaylor 我刚刚更新了我的问题,听起来最后一个
bind语句中的第一个if语句永远不会给出真实的。 -
@JoshuaTaylor 我做了第三次更新,你能检查一下吗?
标签: sparql rdf semantic-web owl ontology