<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.11.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "GameplayKit",
  "identifier" : "/documentation/GameplayKit/GKRuleSystem",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "GameplayKit"
    ],
    "preciseIdentifier" : "c:objc(cs)GKRuleSystem"
  },
  "title" : "GKRuleSystem"
}
-->

# GKRuleSystem

A list of rules, together with a context for evaluating them and interpreting results, for use in constructing data-driven logic or fuzzy logic systems.

```
class GKRuleSystem
```

## Overview

A [`GKRuleSystem`](/documentation/GameplayKit/GKRuleSystem) object manages a list of rules ([`GKRule`](/documentation/GameplayKit/GKRule) objects). A rule system also offers methods for evaluating its list of rules in a context defined by two features: a [`state`](/documentation/GameplayKit/GKRuleSystem/state) dictionary containing information to be tested by rules, and a set of [`facts`](/documentation/GameplayKit/GKRuleSystem/facts) representing the conclusions drawn as a result of rule evaluation. You can evaluate facts based on a binary truth state—that is, a fact either is or is not in the set—or on a continuously variable membership grade, representing different levels of veracity, confidence, or strength for use in fuzzy logic.

You construct a rule system by creating [`GKRule`](/documentation/GameplayKit/GKRule) objects and adding them to the system’s list of rules. There are multiple ways to construct rules: for greater reusability, use the methods listed in Creating Data-Driven Rules; or for greater flexibility, use the [`init(blockPredicate:action:)`](/documentation/GameplayKit/GKRule/init(blockPredicate:action:)) method or create a custom subclass of [`GKRule`](/documentation/GameplayKit/GKRule) or [`GKNSPredicateRule`](/documentation/GameplayKit/GKNSPredicateRule). Then, add rules to the system with the methods listed in Managing a System’s List of Rules below.

To evaluate a system, call the [`evaluate()`](/documentation/GameplayKit/GKRuleSystem/evaluate()) method. This method processes each rule in the system in the order it appears in the system’s [`agenda`](/documentation/GameplayKit/GKRuleSystem/agenda) list. You set this order with the [`salience`](/documentation/GameplayKit/GKRule/salience) property of each rule, or with the order in which you add rules to the system. As the system processes each rule, it tests the rule’s [`evaluatePredicate(in:)`](/documentation/GameplayKit/GKRule/evaluatePredicate(in:)) method to determine whether the rule is satisfied in the context of the system. If the rule’s predicate is satisfied, the system executes the rule’s [`performAction(in:)`](/documentation/GameplayKit/GKRule/performAction(in:)) method and moves the rule to the [`executed`](/documentation/GameplayKit/GKRuleSystem/executed) list (so the further evaluation of the agenda doesn’t repeatedly trigger the rule’s action).

Rules typically use the system’s [`state`](/documentation/GameplayKit/GKRuleSystem/state) dictionary as input and its set of [`facts`](/documentation/GameplayKit/GKRuleSystem/facts) as output. (However, more complex systems can include sets of rules whose predicates test facts or whose actions mutate the system’s state.) After evaluating a rule system, you can examine the set of facts it has produced using the methods listed in Drawing Conclusions from Facts below. You can then use the presence of a fact in the set, the value of its membership grade, or the combined membership grades of a group of facts to influence the behaviors in your game.

For more information about rules and rule systems, read [Rule Systems](https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/RuleSystems.html#//apple_ref/doc/uid/TP40015172-CH10) in [GameplayKit Programming Guide](https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/index.html#//apple_ref/doc/uid/TP40015172).

## Topics

### Creating a Rule System

[`-  init`](/documentation/GameplayKit/GKRuleSystem/init())

Initializes a new, empty rule system.

### Managing State Information

[`state`](/documentation/GameplayKit/GKRuleSystem/state)

A dictionary of state information to be evaluated by the system’s rules.

### Managing a System’s List of Rules

[`rules`](/documentation/GameplayKit/GKRuleSystem/rules)

The list of rules to be executed when evaluating the system.

[`-  addRule:`](/documentation/GameplayKit/GKRuleSystem/add(_:)-76jb5)

Adds the specified rule to the system.

[`-  addRulesFromArray:`](/documentation/GameplayKit/GKRuleSystem/add(_:)-7u5zw)

Adds the specified list of rules to the system.

[`-  removeAllRules`](/documentation/GameplayKit/GKRuleSystem/removeAllRules())

Removes all rules from the system.

### Evaluating a Rule System

[`-  evaluate`](/documentation/GameplayKit/GKRuleSystem/evaluate())

Evaluates the rule system, executing the list of rules in its agenda.

[`agenda`](/documentation/GameplayKit/GKRuleSystem/agenda)

The list of rules to be considered when evaluating the system.

[`executed`](/documentation/GameplayKit/GKRuleSystem/executed)

The list of rules whose actions have been performed during evaluation of the system.

[`-  reset`](/documentation/GameplayKit/GKRuleSystem/reset())

Returns the rule system to its original agenda and clears all facts.

### Asserting and Retracting Facts

[`facts`](/documentation/GameplayKit/GKRuleSystem/facts)

The list of facts claimed by the rule system.

[`-  assertFact:`](/documentation/GameplayKit/GKRuleSystem/assertFact(_:))

Adds the specified fact to the fact set with a membership grade of 1.0, and reevaluates the rules in the system’s agenda.

[`-  assertFact:grade:`](/documentation/GameplayKit/GKRuleSystem/assertFact(_:grade:))

Increases the membership grade of the specified fact by the specified amount, adding it to the fact set if necessary, and reevaluates the rules in the system’s agenda.

[`-  retractFact:`](/documentation/GameplayKit/GKRuleSystem/retractFact(_:))

Removes the specified fact from the fact set, and reevaluates the rules in the system’s agenda.

[`-  retractFact:grade:`](/documentation/GameplayKit/GKRuleSystem/retractFact(_:grade:))

Reduces the membership grade of the specified fact by the specified amount, removing it from the fact set if necessary, and reevaluates the rules in the system’s agenda.

### Drawing Conclusions from Facts

[`-  gradeForFact:`](/documentation/GameplayKit/GKRuleSystem/grade(forFact:))

Returns the membership grade of the specified fact.

[`-  minimumGradeForFacts:`](/documentation/GameplayKit/GKRuleSystem/minimumGrade(forFacts:))

Returns the lowest membership grade among the specified facts.

[`-  maximumGradeForFacts:`](/documentation/GameplayKit/GKRuleSystem/maximumGrade(forFacts:))

Returns the highest membership grade among the specified facts.

## Relationships

### Conforms To

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Hashable`](/documentation/Swift/Hashable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`CVarArg`](/documentation/Swift/CVarArg)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)