我正在探索 Apple 的 GameplayKit API,目前我对 GKRuleSystem 评估其规则的方式感到非常困惑。文档说显着性属性决定了评估的顺序,首先评估更高的显着性规则(https://developer.apple.com/library/ios/documentation/GameplayKit/Reference/GKRule_Class/index.html#// apple_ref/occ/instp/GKRule/salience)。但在实践中,这似乎不会发生:
let twoRuleSystem = GKRuleSystem()
let stateConditionA = "A"
let stateConditionB = "B"
let fact = "A"
let stronglyAssert = NSPredicate(format: "state.\(stateConditionA) = true")
let weaklyRetract = NSPredicate(format: "state.\(stateConditionB) = true")
let strongRule = GKRule(predicate: stronglyAssert, assertingFact: fact, grade: 1.0)
let weakRule = GKRule(predicate: weaklyRetract, retractingFact: fact, grade: 0.25)
strongRule.salience = 10
weakRule.salience = 1
twoRuleSystem.addRulesFromArray([strongRule, weakRule])
twoRuleSystem.state[stateConditionA] = true
twoRuleSystem.state[stateConditionB] = true
twoRuleSystem.reset()
twoRuleSystem.evaluate()
print("The fact \(fact) evaluates to \(twoRuleSystem.gradeForFact(fact))")
// prints "The fact A evaluates to 1.0"
如果我颠倒这两个事实的显着性,那么结果是“事实 A 评估为 0.75”。
我希望当我的 strongRule 的显着性为 10 时,它将首先被评估,并且事实 A 将被断言为 1.0。然后将评估weakRule,并以0.25 的值收回事实A,从而得出0.75 的事实等级。但这仅在weakRule 具有更高的显着性时才有效。
这让我更加困惑,因为如果首先评估弱规则,我希望事实 A 的值为 0;事实的等级只能是 0 到 1.0 之间的值。然后我希望 strongRule 被评估并以 1.0 的成绩断言事实 A。但事实并非如此。