Playing with GameplayKit rule system 1.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Check divisible by 3 and 5 with GameplayKit Rule System | |
import GameplayKit | |
let divisibleBy3Rule = GKRule(blockPredicate: { system in | |
(system.state["value"] as! Int) % 3 == 0 | |
}, action: { system in | |
system.assertFact("divisibleBy3and5", grade: 0.5) | |
system.assertFact("divisibleBy3", grade: 1) | |
}) | |
let divisibleBy5rule = GKRule(blockPredicate: { system in | |
return (system.state["value"] as! Int) % 5 == 0 | |
}, action: { system in | |
system.assertFact("divisibleBy3and5", grade: 0.5) | |
system.assertFact("divisibleBy5", grade: 1) | |
}) | |
let ruleSystem = GKRuleSystem() | |
ruleSystem.addRule(divisibleBy3Rule) | |
ruleSystem.addRule(divisibleBy5rule) | |
for i in 1...100 { | |
ruleSystem.state["value"] = Int(i) | |
ruleSystem.evaluate() | |
print("\(i), divisible by 3 and 5 (grade of fact) is \(ruleSystem.gradeForFact("divisibleBy3and5") * 100)% ") | |
ruleSystem.reset() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment