我正在探索Apple的GameplayKit API,目前我很困惑GKRuleSystem評估其規則的方式。該文件表明,顯着性屬性決定了評估順序,首先評估高顯着性規則(https://developer.apple.com/library/ios/documentation/GameplayKit/Reference/GKRule_Class/index.html#//apple_ref/occ/instp/GKRule/salience)。但在實踐中,這似乎並沒有發生:您如何確定GKRuleSystem中GKRules的評估順序?
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"
如果我扭轉兩個事實的顯着性,那麼結果是「一個事實,計算結果爲0.75」。
我認爲當我的strongRule有10個突出顯示時,它將首先被評估,事實A將被置爲1.0值。然後將評估weakRule,事實A將以0.25的值收回,導致事實等級爲0.75。但是這隻有在弱規則具有更高的顯着性時纔有效。
這使我更加困惑,因爲如果首先評估weakRule,我會認爲事實A的值爲0;事實的等級只能是從0到1.0的值。然後我會期待strongRule得到評估並斷言事實A的等級爲1.0。但那不是發生了什麼事。