Skip to content

Instantly share code, notes, and snippets.

@patoi
Last active August 16, 2017 18:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patoi/9f40f06747e731373fe6 to your computer and use it in GitHub Desktop.
Save patoi/9f40f06747e731373fe6 to your computer and use it in GitHub Desktop.
Playing with GameplayKit rule system 1.
//: 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