From abc2941dd41b3cb5a2adbf243e2f347cc6462307 Mon Sep 17 00:00:00 2001 From: Thijs Boerefijn <51719415+Tboefijn@users.noreply.github.com> Date: Wed, 6 Mar 2024 15:14:35 +0100 Subject: [PATCH] feat: add Tactic --- .editorconfig | 18 ++- Aplib.Core/Aplib.Core.csproj | 2 +- Aplib.Core/Tactics/AnyOfTactic.cs | 50 ++++++++ Aplib.Core/Tactics/FirstOfTactic.cs | 42 +++++++ Aplib.Core/Tactics/PrimitiveTactic.cs | 35 ++++++ Aplib.Core/Tactics/Tactic.cs | 41 +++++++ Aplib.Tests/Core/Tactics/TacticTests.cs | 145 ++++++++++++++++++++++++ 7 files changed, 328 insertions(+), 5 deletions(-) create mode 100644 Aplib.Core/Tactics/AnyOfTactic.cs create mode 100644 Aplib.Core/Tactics/FirstOfTactic.cs create mode 100644 Aplib.Core/Tactics/PrimitiveTactic.cs create mode 100644 Aplib.Core/Tactics/Tactic.cs create mode 100644 Aplib.Tests/Core/Tactics/TacticTests.cs diff --git a/.editorconfig b/.editorconfig index b5a9de53..8e894e4e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -192,25 +192,29 @@ csharp_preserve_single_line_statements = true # Naming rules -dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion +dotnet_naming_rule.interface_should_be_begins_with_i.severity = warning dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i -dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion +dotnet_naming_rule.types_should_be_pascal_case.severity = warning dotnet_naming_rule.types_should_be_pascal_case.symbols = types dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case -dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion +dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = warning dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case +dotnet_naming_rule.private_members_with_underscore.severity = warning +dotnet_naming_rule.private_members_with_underscore.symbols = private_fields +dotnet_naming_rule.private_members_with_underscore.style = prefix_underscore + # Symbol specifications dotnet_naming_symbols.interface.applicable_kinds = interface dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected dotnet_naming_symbols.interface.required_modifiers = -dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum +dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum, field dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected dotnet_naming_symbols.types.required_modifiers = @@ -218,6 +222,9 @@ dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, meth dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected dotnet_naming_symbols.non_field_members.required_modifiers = +dotnet_naming_symbols.private_fields.applicable_kinds = field, property +dotnet_naming_symbols.private_fields.applicable_accessibilities = private + # Naming styles dotnet_naming_style.pascal_case.required_prefix = @@ -229,3 +236,6 @@ dotnet_naming_style.begins_with_i.required_prefix = I dotnet_naming_style.begins_with_i.required_suffix = dotnet_naming_style.begins_with_i.word_separator = dotnet_naming_style.begins_with_i.capitalization = pascal_case + +dotnet_naming_style.prefix_underscore.capitalization = camel_case +dotnet_naming_style.prefix_underscore.required_prefix = _ \ No newline at end of file diff --git a/Aplib.Core/Aplib.Core.csproj b/Aplib.Core/Aplib.Core.csproj index 39c0aa88..7da3b872 100644 --- a/Aplib.Core/Aplib.Core.csproj +++ b/Aplib.Core/Aplib.Core.csproj @@ -7,7 +7,7 @@ - + diff --git a/Aplib.Core/Tactics/AnyOfTactic.cs b/Aplib.Core/Tactics/AnyOfTactic.cs new file mode 100644 index 00000000..dc776461 --- /dev/null +++ b/Aplib.Core/Tactics/AnyOfTactic.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; + +namespace Aplib.Core.Tactics +{ + /// + /// Represents a tactic that executes any of the provided sub-tactics. + /// + public class AnyOfTactic : Tactic + { + /// + /// Gets or sets the sub-tactics of the tactic. + /// + protected LinkedList SubTactics { get; set; } + + /// + /// Initializes a new instance of the class with the specified sub-tactics. + /// + /// The list of sub-tactics. + public AnyOfTactic(List subTactics) + { + SubTactics = new(); + + foreach (Tactic tactic in subTactics) + { + _ = SubTactics.AddLast(tactic); + } + } + + /// + /// Initializes a new instance of the class with the specified sub-tactics and guard condition. + /// + /// The list of sub-tactics. + /// The guard condition. + public AnyOfTactic(List subTactics, Func guard) : this(subTactics) => Guard = guard; + + /// + public override List GetFirstEnabledActions() + { + List primitiveTactics = new(); + + foreach (Tactic subTactic in SubTactics) + { + primitiveTactics.AddRange(subTactic.GetFirstEnabledActions()); + } + + return primitiveTactics; + } + } +} diff --git a/Aplib.Core/Tactics/FirstOfTactic.cs b/Aplib.Core/Tactics/FirstOfTactic.cs new file mode 100644 index 00000000..fe0d954e --- /dev/null +++ b/Aplib.Core/Tactics/FirstOfTactic.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; + +namespace Aplib.Core.Tactics +{ + /// + /// Represents a tactic that executes the first enabled action from a list of sub-tactics. + /// + public class FirstOfTactic : AnyOfTactic + { + /// + /// Initializes a new instance of the class with the specified sub-tactics. + /// + /// The list of sub-tactics. + public FirstOfTactic(List subTactics) : base(subTactics) + { + } + + /// + /// Initializes a new instance of the class with the specified sub-tactics and guard condition. + /// + /// The list of sub-tactics. + /// The guard condition. + public FirstOfTactic(List subTactics, Func guard) : base(subTactics, guard) + { + } + + /// + public override List GetFirstEnabledActions() + { + foreach (Tactic subTactic in SubTactics) + { + List firstOfTactics = subTactic.GetFirstEnabledActions(); + + if (firstOfTactics.Count > 0) + return firstOfTactics; + } + + return new(); + } + } +} diff --git a/Aplib.Core/Tactics/PrimitiveTactic.cs b/Aplib.Core/Tactics/PrimitiveTactic.cs new file mode 100644 index 00000000..34129353 --- /dev/null +++ b/Aplib.Core/Tactics/PrimitiveTactic.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; + +namespace Aplib.Core.Tactics +{ + /// + /// Represents a primitive tactic in the Aplib.Core namespace. + /// + public class PrimitiveTactic : Tactic + { + /// + /// Gets or sets the action of the primitive tactic. + /// + public readonly Action Action; + + /// + /// Initializes a new instance of the class with the specified action. + /// + /// The action of the primitive tactic. + public PrimitiveTactic(Action action) => Action = action; + + /// + /// Initializes a new instance of the class with the specified action and guard. + /// + /// The action of the primitive tactic. + /// The guard of the tactic. + public PrimitiveTactic(Action action, Func guard) : base(guard) => Action = action; + + /// + public override List GetFirstEnabledActions() => IsActionable() ? new() { this } : new(); + + /// + public override bool IsActionable() => base.IsActionable() && Action.IsActionable(); + } +} diff --git a/Aplib.Core/Tactics/Tactic.cs b/Aplib.Core/Tactics/Tactic.cs new file mode 100644 index 00000000..3697cc30 --- /dev/null +++ b/Aplib.Core/Tactics/Tactic.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; + +namespace Aplib.Core.Tactics +{ + /// + /// Represents a tactic in the Aplib.Core namespace. + /// + public abstract class Tactic + { + /// + /// Gets or sets the guard of the tactic. + /// + protected Func Guard { get; set; } = () => true; + + /// + /// Initializes a new instance of the . + /// + protected Tactic() + { + } + + /// + /// Initializes a new instance of the class with a specified guard. + /// + /// The guard of the tactic. + protected Tactic(Func guard) => Guard = guard; + + /// + /// Gets the first enabled primitive actions. + /// + /// A list of primitive tactics that are enabled. + public abstract List GetFirstEnabledActions(); + + /// + /// Determines whether the tactic is actionable. + /// + /// True if the tactic is actionable, false otherwise. + public virtual bool IsActionable() => Guard(); + } +} diff --git a/Aplib.Tests/Core/Tactics/TacticTests.cs b/Aplib.Tests/Core/Tactics/TacticTests.cs new file mode 100644 index 00000000..a6e9f7c1 --- /dev/null +++ b/Aplib.Tests/Core/Tactics/TacticTests.cs @@ -0,0 +1,145 @@ +using Aplib.Core.Tactics; +using Action = Aplib.Core.Action; + +namespace Aplib.Tests.Core.Tactics; +public class TacticTests +{ + private readonly Action _emptyAction = new(() => { }); + + private static bool TrueGuard() => true; + + private static bool FalseGuard() => false; + + /// + /// Given a parent of type with two subtactics, + /// When getting the next tactic, + /// Then the result should be the first subtactic. + /// + [Fact] + public void GetFirstEnabledActions_WhenTacticTypeIsFirstOf_ReturnsEnabledPrimitiveTactics() + { + // Arrange + PrimitiveTactic tactic1 = new(_emptyAction); + PrimitiveTactic tactic2 = new(_emptyAction); + FirstOfTactic parentTactic = new([tactic1, tactic2]); + + // Act + List enabledActions = parentTactic.GetFirstEnabledActions(); + + // Assert + Assert.Contains(tactic1, enabledActions); + } + + /// + /// Given a parent of type with two subtactics and a guard that is true, + /// When getting the next tactic, + /// Then the result should be the first subtactic. + /// + [Fact] + public void GetFirstEnabledActions_WhenTacticTypeIsFirstOfAndGuardEnabled_ReturnsEnabledPrimitiveTactics() + { + // Arrange + PrimitiveTactic tactic1 = new(_emptyAction); + PrimitiveTactic tactic2 = new(_emptyAction); + FirstOfTactic parentTactic = new([tactic1, tactic2], TrueGuard); + + // Act + List enabledActions = parentTactic.GetFirstEnabledActions(); + + // Assert + Assert.Contains(tactic1, enabledActions); + } + + /// + /// Given a parent of type with two subtactics, + /// When getting the next tactic, + /// Then the result should contain all the subtactics. + /// + [Fact] + public void GetFirstEnabledActions_WhenTacticTypeIsAnyOf_ReturnsEnabledPrimitiveTactics() + { + // Arrange + PrimitiveTactic tactic1 = new(_emptyAction); + PrimitiveTactic tactic2 = new(_emptyAction); + AnyOfTactic parentTactic = new([tactic1, tactic2]); + + // Act + List enabledActions = parentTactic.GetFirstEnabledActions(); + + // Assert + Assert.Contains(tactic1, enabledActions); + Assert.Contains(tactic2, enabledActions); + } + + /// + /// Given a primitive tactic with an actionable action, + /// When getting the first enabled actions, + /// Then the result should contain the primitive tactic. + /// + [Fact] + public void GetFirstEnabledActions_WhenTacticTypeIsPrimitiveAndActionIsActionable_ReturnsEnabledPrimitiveTactic() + { + // Arrange + PrimitiveTactic tactic = new(_emptyAction, TrueGuard); + + // Act + List enabledActions = tactic.GetFirstEnabledActions(); + + // Assert + Assert.Contains(tactic, enabledActions); + } + + /// + /// Given a primitive tactic with a non-actionable action, + /// When getting the first enabled actions, + /// Then the result should be an empty list. + /// + [Fact] + public void GetFirstEnabledActions_WhenTacticTypeIsPrimitiveAndActionIsNotActionable_ReturnsEmptyList() + { + // Arrange + PrimitiveTactic tactic = new(_emptyAction, FalseGuard); + + // Act + List enabledActions = tactic.GetFirstEnabledActions(); + + // Assert + Assert.Empty(enabledActions); + } + + /// + /// Given a tactic with a guard that returns true, + /// When checking if the tactic is actionable, + /// Then the result should be true. + /// + [Fact] + public void IsActionable_WhenGuardReturnsTrue_ReturnsTrue() + { + // Arrange + PrimitiveTactic tactic = new(_emptyAction, TrueGuard); + + // Act + bool isActionable = tactic.IsActionable(); + + // Assert + Assert.True(isActionable); + } + + /// + /// Given a tactic with a guard that returns false, + /// When checking if the tactic is actionable, + /// Then the result should be false. + /// + [Fact] + public void IsActionable_WhenGuardReturnsFalse_ReturnsFalse() + { + // Arrange + PrimitiveTactic tactic = new(_emptyAction, FalseGuard); + + // Act + bool isActionable = tactic.IsActionable(); + + // Assert + Assert.False(isActionable); + } +}