44using System . Threading . Tasks ;
55using Roastery . Util ;
66
7- namespace Roastery . Agents
7+ namespace Roastery . Agents ;
8+
9+ abstract class Agent
810{
9- abstract class Agent
10- {
11- protected delegate Task Behavior ( CancellationToken cancellationToken ) ;
11+ protected delegate Task Behavior ( CancellationToken cancellationToken ) ;
1212
13- readonly int _meanBehaviorIntervalMilliseconds ;
13+ readonly int _meanBehaviorIntervalMilliseconds ;
1414
15- protected Agent ( int meanBehaviorIntervalMilliseconds )
16- {
17- _meanBehaviorIntervalMilliseconds = meanBehaviorIntervalMilliseconds ;
18- }
15+ protected Agent ( int meanBehaviorIntervalMilliseconds )
16+ {
17+ _meanBehaviorIntervalMilliseconds = meanBehaviorIntervalMilliseconds ;
18+ }
1919
20- public static Task Run ( Agent agent , CancellationToken cancellationToken )
21- {
22- return Task . Run ( ( ) => agent . RunBehaviorsAsync ( cancellationToken ) , cancellationToken ) ;
23- }
20+ public static Task Run ( Agent agent , CancellationToken cancellationToken )
21+ {
22+ return Task . Run ( ( ) => agent . RunBehaviorsAsync ( cancellationToken ) , cancellationToken ) ;
23+ }
2424
25- async Task RunBehaviorsAsync ( CancellationToken cancellationToken )
26- {
27- var behaviors = GetBehaviors ( ) . ToArray ( ) ;
28- if ( behaviors . Length == 0 )
29- return ;
25+ async Task RunBehaviorsAsync ( CancellationToken cancellationToken )
26+ {
27+ var behaviors = GetBehaviors ( ) . ToArray ( ) ;
28+ if ( behaviors . Length == 0 )
29+ return ;
3030
31- while ( ! cancellationToken . IsCancellationRequested )
32- {
33- var behavior = Distribution . Uniform ( behaviors ) ;
31+ while ( ! cancellationToken . IsCancellationRequested )
32+ {
33+ var behavior = Distribution . Uniform ( behaviors ) ;
3434
35- await Task . Delay ( ( int ) Distribution . Uniform ( 0 , _meanBehaviorIntervalMilliseconds ) , cancellationToken ) ;
35+ await Task . Delay ( ( int ) Distribution . Uniform ( 0 , _meanBehaviorIntervalMilliseconds ) , cancellationToken ) ;
3636
37- try
38- {
39- await behavior ( cancellationToken ) ;
40- }
41- catch
42- {
43- // Exceptions are swallowed here; agents can log exceptions if they wish
44- }
37+ try
38+ {
39+ await behavior ( cancellationToken ) ;
40+ }
41+ catch
42+ {
43+ // Exceptions are swallowed here; agents can log exceptions if they wish
44+ }
4545
46- await Task . Delay ( _meanBehaviorIntervalMilliseconds / 2 +
47- ( int ) ( _meanBehaviorIntervalMilliseconds * Distribution . Uniform ( ) ) , cancellationToken ) ;
46+ await Task . Delay ( _meanBehaviorIntervalMilliseconds / 2 +
47+ ( int ) ( _meanBehaviorIntervalMilliseconds * Distribution . Uniform ( ) ) , cancellationToken ) ;
4848
49- }
5049 }
51-
52- protected abstract IEnumerable < Behavior > GetBehaviors ( ) ;
5350 }
54- }
51+
52+ protected abstract IEnumerable < Behavior > GetBehaviors ( ) ;
53+ }
0 commit comments