objc2_gameplay_kit/generated/GKRuleSystem.rs
1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::ffi::*;
4use core::ptr::NonNull;
5use objc2::__framework_prelude::*;
6use objc2_foundation::*;
7
8use crate::*;
9
10extern_class!(
11 /// A rule system consists of 3 things:
12 /// - The current state, which upon creation is considered the inital state.
13 /// - The current set of rules.
14 /// - The current set of facts.
15 ///
16 /// Each time a fact is added to the system, the set of rules are evaluated in order and their actions executed in the system if their predicates are true.
17 /// Rules can be fuzzy, allowing predicates and facts to be asserted to a degree of confidence instead of just boolean on/off.
18 ///
19 /// The facts can be any kind of objects as long as they correctly determine equality using isEqual:
20 /// The simplest approach is to use strings or dictionaries as they provide the most flexibility in defining
21 /// facts, but user defined classes work just as well and may describe the problem space better.
22 ///
23 /// The fact set is at all times a fuzzy set, as defined by fact membership in the set being modulated by their
24 /// grade of membership. The rules may use the grade of membership to predicate their actions and in such a
25 /// manner create fuzzy logic. The fuzzy logic Zadeh operators are available on the system itself in order
26 /// to query multiple facts for combined membership grade.
27 ///
28 /// See also [Apple's documentation](https://developer.apple.com/documentation/gameplaykit/gkrulesystem?language=objc)
29 #[unsafe(super(NSObject))]
30 #[derive(Debug, PartialEq, Eq, Hash)]
31 pub struct GKRuleSystem;
32);
33
34extern_conformance!(
35 unsafe impl NSObjectProtocol for GKRuleSystem {}
36);
37
38impl GKRuleSystem {
39 extern_methods!(
40 /// Initializes a clean rule system with no state, rules or facts.
41 #[unsafe(method(init))]
42 #[unsafe(method_family = init)]
43 pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
44
45 /// Explicitly evaluate the agenda of the rule system based on the current state and the current set of facts.
46 ///
47 /// This may in turn assert or retract more facts or change the state of the system, including activating more
48 /// rules in the agenda.
49 #[unsafe(method(evaluate))]
50 #[unsafe(method_family = none)]
51 pub unsafe fn evaluate(&self);
52
53 /// The implementation-defined state. If any changes are made on this outside the system you must
54 /// call evaluate to have the system take account of the changes.
55 ///
56 ///
57 /// See: evaluate
58 ///
59 /// # Safety
60 ///
61 /// The returned generic should be of the correct type.
62 #[unsafe(method(state))]
63 #[unsafe(method_family = none)]
64 pub unsafe fn state(&self) -> Retained<NSMutableDictionary>;
65
66 /// The current set of rules that will be used to set the agenda when rules are first added to the system.
67 /// They will also be used to refill the agenda whenever it is set.
68 ///
69 /// This is at all times the union of the agenda and executed sets.
70 ///
71 ///
72 /// See: agenda
73 ///
74 /// See: executed
75 #[unsafe(method(rules))]
76 #[unsafe(method_family = none)]
77 pub unsafe fn rules(&self) -> Retained<NSArray<GKRule>>;
78
79 /// Adds a rule to the system. Also adds it to the agenda in salience order.
80 #[unsafe(method(addRule:))]
81 #[unsafe(method_family = none)]
82 pub unsafe fn addRule(&self, rule: &GKRule);
83
84 /// Adds rules to the system. Also adds them to the agenda in salience order.
85 #[unsafe(method(addRulesFromArray:))]
86 #[unsafe(method_family = none)]
87 pub unsafe fn addRulesFromArray(&self, rules: &NSArray<GKRule>);
88
89 /// Removes all rules from the system. This also removes them from the agenda and executed sets.
90 #[unsafe(method(removeAllRules))]
91 #[unsafe(method_family = none)]
92 pub unsafe fn removeAllRules(&self);
93
94 /// The current set of rules to be evaluated, in salience order, where if the salience is equivalent
95 /// the order of insertion into the agenda is used to decide which is first.
96 /// Adjust salience of your rules to adjust the order the next time the agenda is reset. Changing salience
97 /// on a rule currently in the agenda does not change its order in the agenda.
98 ///
99 /// This is at all times the difference between the rules and executed sets.
100 ///
101 ///
102 /// See: rules
103 ///
104 /// See: executed
105 ///
106 /// See: reset
107 #[unsafe(method(agenda))]
108 #[unsafe(method_family = none)]
109 pub unsafe fn agenda(&self) -> Retained<NSArray<GKRule>>;
110
111 /// The current set of rules that have already executed. Rules in this set will not be executed again until
112 /// the system is reset.
113 ///
114 /// This is at all times the difference between the rules and agenda sets.
115 ///
116 ///
117 /// See: rules
118 ///
119 /// See: agenda
120 ///
121 /// See: reset
122 #[unsafe(method(executed))]
123 #[unsafe(method_family = none)]
124 pub unsafe fn executed(&self) -> Retained<NSArray<GKRule>>;
125
126 /// The current set of facts. Facts have a grade of membership that is >= 0.0. Query the system for the
127 /// individual grades of membership with gradeForFact:
128 ///
129 ///
130 /// See: gradeForFact:
131 #[unsafe(method(facts))]
132 #[unsafe(method_family = none)]
133 pub unsafe fn facts(&self) -> Retained<NSArray>;
134
135 /// Returns the current membership grade for the given fact, which is 0.0 if the fact
136 /// is not a member of the current set of facts.
137 ///
138 ///
139 /// Returns: The membership grade of the given fact, in the range [0.0, 1.0].
140 ///
141 /// # Safety
142 ///
143 /// `fact` should be of the correct type.
144 #[unsafe(method(gradeForFact:))]
145 #[unsafe(method_family = none)]
146 pub unsafe fn gradeForFact(&self, fact: &ProtocolObject<dyn NSObjectProtocol>) -> c_float;
147
148 /// Returns the combined membership grade for the all the given facts.
149 ///
150 /// This performs the logical AND operation between the given facts.
151 ///
152 ///
153 /// Returns: The membership grade by applying the AND operator on the given facts, in the range [0.0, 1.0].
154 ///
155 /// # Safety
156 ///
157 /// `facts` generic should be of the correct type.
158 #[unsafe(method(minimumGradeForFacts:))]
159 #[unsafe(method_family = none)]
160 pub unsafe fn minimumGradeForFacts(&self, facts: &NSArray) -> c_float;
161
162 /// Returns the maximum membership grade for the any one of the given facts.
163 ///
164 /// This performs the logical OR operation between the given facts.
165 ///
166 ///
167 /// Returns: The membership grade by applying the OR operator on the given facts, in the range [0.0, 1.0].
168 ///
169 /// # Safety
170 ///
171 /// `facts` generic should be of the correct type.
172 #[unsafe(method(maximumGradeForFacts:))]
173 #[unsafe(method_family = none)]
174 pub unsafe fn maximumGradeForFacts(&self, facts: &NSArray) -> c_float;
175
176 /// Asserts a fact with membership grade of 1.0.
177 ///
178 /// This will cause the current rules to be evaluated, which may in turn assert or retract more facts
179 /// or change the state of the system.
180 ///
181 /// This is shorthand for calling assertFact:grade: with a grade of 1.0
182 ///
183 ///
184 /// See: assertFact:grade:
185 ///
186 /// See: evaluate
187 ///
188 /// See: NSObject.isEqual:
189 ///
190 /// # Safety
191 ///
192 /// `fact` should be of the correct type.
193 #[unsafe(method(assertFact:))]
194 #[unsafe(method_family = none)]
195 pub unsafe fn assertFact(&self, fact: &ProtocolObject<dyn NSObjectProtocol>);
196
197 /// Asserts a fact with the supplied membership grade.
198 ///
199 /// This will cause the current rules to be evaluated, which may in turn assert or retract more facts
200 /// or change the state of the system.
201 ///
202 ///
203 /// See: evaluate
204 ///
205 /// # Safety
206 ///
207 /// `fact` should be of the correct type.
208 #[unsafe(method(assertFact:grade:))]
209 #[unsafe(method_family = none)]
210 pub unsafe fn assertFact_grade(
211 &self,
212 fact: &ProtocolObject<dyn NSObjectProtocol>,
213 grade: c_float,
214 );
215
216 /// Retracts a fact, setting its membership grade to 0, which also removes it from the fact set.
217 ///
218 /// This will cause the current rules to be evaluated, which may in turn assert or retract more facts
219 /// or change the state of the system.
220 ///
221 /// This is short hand for calling retractFact:grade: with a grade of 1.0
222 ///
223 ///
224 /// See: retractFact:grade:
225 ///
226 /// See: evaluate
227 ///
228 /// # Safety
229 ///
230 /// `fact` should be of the correct type.
231 #[unsafe(method(retractFact:))]
232 #[unsafe(method_family = none)]
233 pub unsafe fn retractFact(&self, fact: &ProtocolObject<dyn NSObjectProtocol>);
234
235 /// Retracts a fact, reducing its membership grade by the supplied grade. If this brings the grade to 0
236 /// it is also removed from the fact set.
237 ///
238 /// This will cause the current rules to be evaluated, which may in turn assert or retract more facts
239 /// or change the state of the system.
240 ///
241 ///
242 /// See: evaluate
243 ///
244 /// # Safety
245 ///
246 /// `fact` should be of the correct type.
247 #[unsafe(method(retractFact:grade:))]
248 #[unsafe(method_family = none)]
249 pub unsafe fn retractFact_grade(
250 &self,
251 fact: &ProtocolObject<dyn NSObjectProtocol>,
252 grade: c_float,
253 );
254
255 /// Clears the agenda and executed sets and removes all facts currently in the system.
256 /// It then fills the agenda with rules from the rule set, in salience order.
257 ///
258 /// See: rules
259 ///
260 /// See: facts
261 #[unsafe(method(reset))]
262 #[unsafe(method_family = none)]
263 pub unsafe fn reset(&self);
264 );
265}
266
267/// Methods declared on superclass `NSObject`.
268impl GKRuleSystem {
269 extern_methods!(
270 #[unsafe(method(new))]
271 #[unsafe(method_family = new)]
272 pub unsafe fn new() -> Retained<Self>;
273 );
274}
275
276extern_class!(
277 /// The concrete class that the GKRuleSystem uses to evaluate the current state and facts with predicated rules.
278 /// These are sharable between systems, so don't retain any state in the rules themselves. Use the system-provided
279 /// state storage.
280 ///
281 ///
282 /// See: GKRuleSystem.state
283 ///
284 /// See also [Apple's documentation](https://developer.apple.com/documentation/gameplaykit/gkrule?language=objc)
285 #[unsafe(super(NSObject))]
286 #[derive(Debug, PartialEq, Eq, Hash)]
287 pub struct GKRule;
288);
289
290extern_conformance!(
291 unsafe impl NSObjectProtocol for GKRule {}
292);
293
294impl GKRule {
295 extern_methods!(
296 /// Salience defines the order in the rule agenda that the system will evaluate. A rule with higher salience will
297 /// be evaluated before another rule in the agenda that has a lower salience.
298 ///
299 /// Defaults to 0.
300 ///
301 /// See: GKRuleSystem.agenda
302 #[unsafe(method(salience))]
303 #[unsafe(method_family = none)]
304 pub unsafe fn salience(&self) -> NSInteger;
305
306 /// Setter for [`salience`][Self::salience].
307 #[unsafe(method(setSalience:))]
308 #[unsafe(method_family = none)]
309 pub unsafe fn setSalience(&self, salience: NSInteger);
310
311 /// Called by the rule system when it is this rule's turn to be evaluated. If the predicate returns YES then
312 /// the action for the rule will be performed. Once the action is performed the rule will move to the system's
313 /// executed list until the agenda is reset.
314 ///
315 ///
316 /// See: performAction
317 ///
318 /// See: GKRuleSystem.agenda
319 ///
320 /// See: GKRuleSystem.executed
321 ///
322 /// See: GKRuleSystem.reset
323 ///
324 /// Returns: YES is the predicate passes and the action needs to be performed, NO otherwise.
325 #[unsafe(method(evaluatePredicateWithSystem:))]
326 #[unsafe(method_family = none)]
327 pub unsafe fn evaluatePredicateWithSystem(&self, system: &GKRuleSystem) -> bool;
328
329 /// Performs the action consequence for the rule. This will only be called if the predicate evaluates to YES.
330 /// Any facts asserted or retracted by the action on the system will cause the system to evaluate the agenda
331 /// rule set again once the action completes.
332 #[unsafe(method(performActionWithSystem:))]
333 #[unsafe(method_family = none)]
334 pub unsafe fn performActionWithSystem(&self, system: &GKRuleSystem);
335
336 /// Create a data-driven rule that uses NSPredicate and a single assert as the action.
337 ///
338 /// # Safety
339 ///
340 /// `fact` should be of the correct type.
341 #[unsafe(method(ruleWithPredicate:assertingFact:grade:))]
342 #[unsafe(method_family = none)]
343 pub unsafe fn ruleWithPredicate_assertingFact_grade(
344 predicate: &NSPredicate,
345 fact: &ProtocolObject<dyn NSObjectProtocol>,
346 grade: c_float,
347 ) -> Retained<Self>;
348
349 /// Short hand for data-driven rule that uses NSPredicate and a single retract as the action.
350 ///
351 /// # Safety
352 ///
353 /// `fact` should be of the correct type.
354 #[unsafe(method(ruleWithPredicate:retractingFact:grade:))]
355 #[unsafe(method_family = none)]
356 pub unsafe fn ruleWithPredicate_retractingFact_grade(
357 predicate: &NSPredicate,
358 fact: &ProtocolObject<dyn NSObjectProtocol>,
359 grade: c_float,
360 ) -> Retained<Self>;
361
362 #[cfg(feature = "block2")]
363 /// Short hand for making a rule that uses blocks for the predicate and action. This rule is not able to be archived
364 /// using NSKeyedArchiver so use a subclass or NSPredicate based rule if serialization of the rule is needed.
365 #[unsafe(method(ruleWithBlockPredicate:action:))]
366 #[unsafe(method_family = none)]
367 pub unsafe fn ruleWithBlockPredicate_action(
368 predicate: &block2::DynBlock<dyn Fn(NonNull<GKRuleSystem>) -> Bool>,
369 action: &block2::DynBlock<dyn Fn(NonNull<GKRuleSystem>)>,
370 ) -> Retained<Self>;
371 );
372}
373
374/// Methods declared on superclass `NSObject`.
375impl GKRule {
376 extern_methods!(
377 #[unsafe(method(init))]
378 #[unsafe(method_family = init)]
379 pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
380
381 #[unsafe(method(new))]
382 #[unsafe(method_family = new)]
383 pub unsafe fn new() -> Retained<Self>;
384 );
385}
386
387extern_class!(
388 /// A convenient subclass of GKRule that leverages existing NSPRedicate functionality for evaluating the predicate
389 /// of the rule.
390 ///
391 /// See also [Apple's documentation](https://developer.apple.com/documentation/gameplaykit/gknspredicaterule?language=objc)
392 #[unsafe(super(GKRule, NSObject))]
393 #[derive(Debug, PartialEq, Eq, Hash)]
394 pub struct GKNSPredicateRule;
395);
396
397extern_conformance!(
398 unsafe impl NSObjectProtocol for GKNSPredicateRule {}
399);
400
401impl GKNSPredicateRule {
402 extern_methods!(
403 /// The NSPredicate that is used inside this subclass's implementation of evaluatePredicateWithSystem:
404 /// In order to effectively use this class you must still override performActionWithSystem:
405 ///
406 ///
407 /// See: GKRule.evaluatePredicateWithSystem:
408 #[unsafe(method(predicate))]
409 #[unsafe(method_family = none)]
410 pub unsafe fn predicate(&self) -> Retained<NSPredicate>;
411
412 /// Initializes a new rule with the given NSPredicate
413 #[unsafe(method(initWithPredicate:))]
414 #[unsafe(method_family = init)]
415 pub unsafe fn initWithPredicate(
416 this: Allocated<Self>,
417 predicate: &NSPredicate,
418 ) -> Retained<Self>;
419
420 /// Overridden here to call the predicate's evaluateWithObject:substitutionVariables:, using sys
421 /// as the object and the system's state dictionary as the source of the substitution variables.
422 ///
423 ///
424 /// Returns: YES if the NSPredicate evaluation passes and the action needs to be performed, NO otherwise.
425 #[unsafe(method(evaluatePredicateWithSystem:))]
426 #[unsafe(method_family = none)]
427 pub unsafe fn evaluatePredicateWithSystem(&self, system: &GKRuleSystem) -> bool;
428 );
429}
430
431/// Methods declared on superclass `GKRule`.
432impl GKNSPredicateRule {
433 extern_methods!(
434 /// Create a data-driven rule that uses NSPredicate and a single assert as the action.
435 ///
436 /// # Safety
437 ///
438 /// `fact` should be of the correct type.
439 #[unsafe(method(ruleWithPredicate:assertingFact:grade:))]
440 #[unsafe(method_family = none)]
441 pub unsafe fn ruleWithPredicate_assertingFact_grade(
442 predicate: &NSPredicate,
443 fact: &ProtocolObject<dyn NSObjectProtocol>,
444 grade: c_float,
445 ) -> Retained<Self>;
446
447 /// Short hand for data-driven rule that uses NSPredicate and a single retract as the action.
448 ///
449 /// # Safety
450 ///
451 /// `fact` should be of the correct type.
452 #[unsafe(method(ruleWithPredicate:retractingFact:grade:))]
453 #[unsafe(method_family = none)]
454 pub unsafe fn ruleWithPredicate_retractingFact_grade(
455 predicate: &NSPredicate,
456 fact: &ProtocolObject<dyn NSObjectProtocol>,
457 grade: c_float,
458 ) -> Retained<Self>;
459
460 #[cfg(feature = "block2")]
461 /// Short hand for making a rule that uses blocks for the predicate and action. This rule is not able to be archived
462 /// using NSKeyedArchiver so use a subclass or NSPredicate based rule if serialization of the rule is needed.
463 #[unsafe(method(ruleWithBlockPredicate:action:))]
464 #[unsafe(method_family = none)]
465 pub unsafe fn ruleWithBlockPredicate_action(
466 predicate: &block2::DynBlock<dyn Fn(NonNull<GKRuleSystem>) -> Bool>,
467 action: &block2::DynBlock<dyn Fn(NonNull<GKRuleSystem>)>,
468 ) -> Retained<Self>;
469 );
470}
471
472/// Methods declared on superclass `NSObject`.
473impl GKNSPredicateRule {
474 extern_methods!(
475 #[unsafe(method(init))]
476 #[unsafe(method_family = init)]
477 pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
478
479 #[unsafe(method(new))]
480 #[unsafe(method_family = new)]
481 pub unsafe fn new() -> Retained<Self>;
482 );
483}