| | | 1 | | namespace Intentum.Core.Behavior; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Fluent builder for creating BehaviorSpace instances with a more readable API. |
| | | 5 | | /// </summary> |
| | | 6 | | public sealed class BehaviorSpaceBuilder |
| | | 7 | | { |
| | | 8 | | private const string WithActorRequiredMessage = "Must call WithActor before Action"; |
| | | 9 | | |
| | 79 | 10 | | private readonly BehaviorSpace _space = new(); |
| | | 11 | | private string? _currentActor; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Sets the current actor for subsequent actions. |
| | | 15 | | /// </summary> |
| | | 16 | | public BehaviorSpaceBuilder WithActor(string actor) |
| | 78 | 17 | | { |
| | 78 | 18 | | _currentActor = actor; |
| | 78 | 19 | | return this; |
| | 78 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Adds an action for the current actor. |
| | | 24 | | /// </summary> |
| | | 25 | | public BehaviorSpaceBuilder Action(string action) |
| | 121 | 26 | | { |
| | 121 | 27 | | if (_currentActor == null) |
| | 1 | 28 | | throw new InvalidOperationException(WithActorRequiredMessage); |
| | | 29 | | |
| | 120 | 30 | | _space.Observe(new BehaviorEvent( |
| | 120 | 31 | | _currentActor, |
| | 120 | 32 | | action, |
| | 120 | 33 | | DateTimeOffset.UtcNow)); |
| | | 34 | | |
| | 120 | 35 | | return this; |
| | 120 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Adds an action for the current actor with a specific timestamp. |
| | | 40 | | /// </summary> |
| | | 41 | | public BehaviorSpaceBuilder Action(string action, DateTimeOffset occurredAt) |
| | 1 | 42 | | { |
| | 1 | 43 | | if (_currentActor == null) |
| | 0 | 44 | | throw new InvalidOperationException(WithActorRequiredMessage); |
| | | 45 | | |
| | 1 | 46 | | _space.Observe(new BehaviorEvent( |
| | 1 | 47 | | _currentActor, |
| | 1 | 48 | | action, |
| | 1 | 49 | | occurredAt)); |
| | | 50 | | |
| | 1 | 51 | | return this; |
| | 1 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Adds an action for the current actor with metadata. |
| | | 56 | | /// </summary> |
| | | 57 | | public BehaviorSpaceBuilder Action(string action, IReadOnlyDictionary<string, object>? metadata) |
| | 2 | 58 | | { |
| | 2 | 59 | | if (_currentActor == null) |
| | 0 | 60 | | throw new InvalidOperationException(WithActorRequiredMessage); |
| | | 61 | | |
| | 2 | 62 | | _space.Observe(new BehaviorEvent( |
| | 2 | 63 | | _currentActor, |
| | 2 | 64 | | action, |
| | 2 | 65 | | DateTimeOffset.UtcNow, |
| | 2 | 66 | | metadata)); |
| | | 67 | | |
| | 2 | 68 | | return this; |
| | 2 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Adds an action for the current actor with timestamp and metadata. |
| | | 73 | | /// </summary> |
| | | 74 | | public BehaviorSpaceBuilder Action(string action, DateTimeOffset occurredAt, IReadOnlyDictionary<string, object>? me |
| | 0 | 75 | | { |
| | 0 | 76 | | if (_currentActor == null) |
| | 0 | 77 | | throw new InvalidOperationException(WithActorRequiredMessage); |
| | | 78 | | |
| | 0 | 79 | | _space.Observe(new BehaviorEvent( |
| | 0 | 80 | | _currentActor, |
| | 0 | 81 | | action, |
| | 0 | 82 | | occurredAt, |
| | 0 | 83 | | metadata)); |
| | | 84 | | |
| | 0 | 85 | | return this; |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Adds a complete behavior event directly. |
| | | 90 | | /// </summary> |
| | | 91 | | public BehaviorSpaceBuilder Observe(BehaviorEvent behaviorEvent) |
| | 1 | 92 | | { |
| | 1 | 93 | | _space.Observe(behaviorEvent); |
| | 1 | 94 | | return this; |
| | 1 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Builds and returns the BehaviorSpace instance. |
| | | 99 | | /// </summary> |
| | | 100 | | public BehaviorSpace Build() |
| | 78 | 101 | | { |
| | 78 | 102 | | return _space; |
| | 78 | 103 | | } |
| | | 104 | | } |