Skip to content

Commit

Permalink
feat: add basic Desireset wrapper (#29)
Browse files Browse the repository at this point in the history
* feat: add DesireSet

* docs: add code documentation

* feat: add UpdateStatus to DesireSet

* test: add DesireSet tests

* fix: add in keyword and fix xml comment

* chore: remove redundant using directive

* refactor: use expression body definition
  • Loading branch information
JensSteenmetz authored Apr 1, 2024
1 parent 33ea7e2 commit c0b9203
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Aplib.Core/Desire/DesireSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Aplib.Core.Belief;
using Aplib.Core.Desire.Goals;

namespace Aplib.Core.Desire
{
/// <inheritdoc />
public class DesireSet<TBeliefSet> : IDesireSet<TBeliefSet>
where TBeliefSet : IBeliefSet
{
/// <summary>
/// Stores the main goal structure of the agent.
/// </summary>
private IGoalStructure<TBeliefSet> _mainGoal { get; }

/// <inheritdoc />
public CompletionStatus Status => _mainGoal.Status;

/// <summary>
/// Initializes a new instance of the <see cref="DesireSet{TBeliefSet}" /> class.
/// </summary>
/// <param name="mainGoal">The main goal structure that the agent needs to complete.</param>
public DesireSet(IGoalStructure<TBeliefSet> mainGoal)
=> _mainGoal = mainGoal;

/// <inheritdoc />
public IGoal GetCurrentGoal(TBeliefSet beliefSet)
=> _mainGoal.GetCurrentGoal(beliefSet);

/// <inheritdoc />
public void UpdateStatus(TBeliefSet beliefSet)
=> _mainGoal.UpdateStatus(beliefSet);
}
}
27 changes: 27 additions & 0 deletions Aplib.Core/Desire/IDesireSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Aplib.Core.Belief;
using Aplib.Core.Desire.Goals;

namespace Aplib.Core.Desire
{
/// <summary>
/// Represents a set of goals that the agent has.
/// This is the main structure that the agent will use to determine what it should do next.
/// </summary>
/// <typeparam name="TBeliefSet"></typeparam>
public interface IDesireSet<in TBeliefSet> : ICompletable
where TBeliefSet : IBeliefSet
{
/// <summary>
/// Gets the current goal using the given <see cref="IBeliefSet" />.
/// </summary>
/// <param name="beliefSet">The belief set of the agent.</param>
/// <returns>The current goal to be fulfilled.</returns>
IGoal GetCurrentGoal(TBeliefSet beliefSet);

/// <summary>
/// Updates the status of this <see cref="IDesireSet{TBeliefSet}"/>.
/// </summary>
/// <param name="beliefSet">The belief set of the agent.</param>
void UpdateStatus(TBeliefSet beliefSet);
}
}
75 changes: 75 additions & 0 deletions Aplib.Tests/Core/Desire/DesireSetTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Aplib.Core;
using Aplib.Core.Belief;
using Aplib.Core.Desire;
using Aplib.Core.Desire.Goals;
using FluentAssertions;
using Moq;

namespace Aplib.Tests.Core.Desire;

public class DesireSetTests
{
/// <summary>
/// Given a desire set,
/// When the GetCurrentGoal method is called,
/// Then the desire set should return the current goal of the current goal structure.
/// </summary>
[Fact]
public void DesireSet_WhenGetCurrentGoalIsCalled_ReturnsCurrentGoal()
{
// Arrange
IBeliefSet beliefSet = Mock.Of<IBeliefSet>();
IGoal goal = Mock.Of<IGoal>();
Mock<IGoalStructure<IBeliefSet>> goalStructure = new();
goalStructure
.Setup(g => g.GetCurrentGoal(It.IsAny<IBeliefSet>()))
.Returns(goal);
Mock<DesireSet<IBeliefSet>> desireSet = new(goalStructure.Object);

// Act
IGoal currentGoal = desireSet.Object.GetCurrentGoal(beliefSet);

// Assert
currentGoal.Should().Be(goal);
}

/// <summary>
/// Given a desire set,
/// When the status is updated,
/// Then the status of the goal structures are updated.
/// </summary>
[Fact]
public void DesireSet_WhenStatusUpdated_ShouldUpdateGoalStructuresStatus()
{
// Arrange
Mock<IGoalStructure<IBeliefSet>> goalStructure = new();
Mock<DesireSet<IBeliefSet>> desireSet = new(goalStructure.Object);

// Act
desireSet.Object.UpdateStatus(It.IsAny<IBeliefSet>());

// Assert
goalStructure.Verify(g => g.UpdateStatus(It.IsAny<IBeliefSet>()), Times.Once());
}

/// <summary>
/// Given a desire set,
/// When the status is checked,
/// Then the status should be the same as the main goal structure status.
/// </summary>
[Fact]
public void DesireSet_WhenStatusIsChecked_ShouldBeSameAsMainGoal()
{
// Arrange
Mock<IGoalStructure<IBeliefSet>> goalStructure = new();
goalStructure.Setup(g => g.Status).Returns(CompletionStatus.Success);
Mock<DesireSet<IBeliefSet>> desireSet = new(goalStructure.Object);

// Act
CompletionStatus status = desireSet.Object.Status;
CompletionStatus expectedStatus = goalStructure.Object.Status;

// Assert
status.Should().Be(expectedStatus);
}
}

0 comments on commit c0b9203

Please sign in to comment.