-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip: overgave aan Thijs * feat: implement GoalStructure * chore: fix .editorconfig * feat: add interfaces for easy testing * feat: simplify goal structure * feat: make Goal and Goalstructure inherit from interfaces. * feat: change implementation of goal structures. The goal structures have been altered so that they now first update the state, and then choose a goal. They also have a mechanism for interrupting and reinstating them. * test: test the goal structures, and rework some of the tests to work with moq * chore: conform to sonar * fix: conform to Jens * test: fix tests * feat: make goal structure type contravariant * feat: remove interruptable * docs: fix the wording of CompletionStatus.cs * chore: conform to sonar * fix: improve GoalStructure tests * refactor: rename State to Status * fix: conform to tboefijn Co-authored-by: Thijs Boerefijn <[email protected]> * fix: conform to tboefijn, again Co-authored-by: Thijs Boerefijn <[email protected]> --------- Co-authored-by: Joachim Dekker <[email protected]> Co-authored-by: Jens Steenmetz <[email protected]> Co-authored-by: Thijs Boerefijn <[email protected]>
- Loading branch information
1 parent
aa8db8c
commit 33ea7e2
Showing
18 changed files
with
1,025 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace Aplib.Core | ||
{ | ||
/// <summary> | ||
/// Represents the state of a completable object. | ||
/// </summary> | ||
public enum CompletionStatus | ||
{ | ||
/// <summary> | ||
/// Represents the status of a completable object that is not yet completed. | ||
/// </summary> | ||
Unfinished, | ||
|
||
/// <summary> | ||
/// Represents the status of a completable object that has been successfully completed. | ||
/// </summary> | ||
Success, | ||
|
||
/// <summary> | ||
/// Represents the status of a completable object that has failed to complete. | ||
/// </summary> | ||
Failure | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using Aplib.Core.Belief; | ||
using Aplib.Core.Desire.Goals; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Aplib.Core.Desire | ||
{ | ||
/// <summary> | ||
/// Represents a goal structure that will complete if any of its children complete. | ||
/// </summary> | ||
/// <remarks> | ||
/// The children of this goal structure will be executed in the order they are given. | ||
/// </remarks> | ||
/// <typeparam name="TBeliefSet">The beliefset of the agent.</typeparam> | ||
public class FirstOfGoalStructure<TBeliefSet> : GoalStructure<TBeliefSet>, IDisposable | ||
where TBeliefSet : IBeliefSet | ||
{ | ||
private IEnumerator<IGoalStructure<TBeliefSet>> _childrenEnumerator { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FirstOfGoalStructure{TBeliefSet}" /> class. | ||
/// </summary> | ||
/// <param name="children">The children of the goal structure.</param> | ||
public FirstOfGoalStructure(IList<IGoalStructure<TBeliefSet>> children) : base(children) | ||
{ | ||
_childrenEnumerator = children.GetEnumerator(); | ||
_childrenEnumerator.MoveNext(); | ||
_currentGoalStructure = _childrenEnumerator.Current; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override IGoal GetCurrentGoal(TBeliefSet beliefSet) => _currentGoalStructure!.GetCurrentGoal(beliefSet); | ||
|
||
/// <inheritdoc /> | ||
public override void UpdateStatus(TBeliefSet beliefSet) | ||
{ | ||
// Loop through all the children until one of them is unfinished or successful. | ||
// This loop is here to prevent tail recursion. | ||
while (true) | ||
{ | ||
if (Status == CompletionStatus.Success) return; | ||
_currentGoalStructure!.UpdateStatus(beliefSet); | ||
|
||
switch (_currentGoalStructure.Status) | ||
{ | ||
case CompletionStatus.Unfinished: | ||
return; | ||
case CompletionStatus.Success: | ||
Status = CompletionStatus.Success; | ||
return; | ||
} | ||
|
||
if (_childrenEnumerator.MoveNext()) | ||
{ | ||
_currentGoalStructure = _childrenEnumerator.Current; | ||
Status = CompletionStatus.Unfinished; | ||
|
||
// Update the Status of the new goal structure | ||
continue; | ||
} | ||
|
||
Status = CompletionStatus.Failure; | ||
return; | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
/// <summary> | ||
/// Disposes of the goal structure. | ||
/// </summary> | ||
/// <param name="disposing">Whether we are actually disposing.</param> | ||
protected virtual void Dispose(bool disposing) => _childrenEnumerator.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Aplib.Core.Belief; | ||
using Aplib.Core.Desire.Goals; | ||
using System.Collections.Generic; | ||
|
||
namespace Aplib.Core.Desire | ||
{ | ||
/// <summary> | ||
/// Describes a structure of goals that need to be fulfilled. | ||
/// </summary> | ||
public abstract class GoalStructure<TBeliefSet> : IGoalStructure<TBeliefSet> where TBeliefSet : IBeliefSet | ||
{ | ||
/// <inheritdoc /> | ||
public CompletionStatus Status { get; protected set; } | ||
|
||
/// <summary> | ||
/// The children of the goal structure. | ||
/// </summary> | ||
protected readonly IEnumerable<IGoalStructure<TBeliefSet>> _children; | ||
|
||
/// <summary> | ||
/// The goal structure that is currently being fulfilled. | ||
/// </summary> | ||
protected IGoalStructure<TBeliefSet>? _currentGoalStructure; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="GoalStructure{TBeliefSet}" /> class. | ||
/// </summary> | ||
/// <param name="children">The children of the goal structure.</param> | ||
protected GoalStructure(IEnumerable<IGoalStructure<TBeliefSet>> children) => _children = children; | ||
|
||
/// <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> | ||
public abstract IGoal GetCurrentGoal(TBeliefSet beliefSet); | ||
|
||
/// <summary> | ||
/// Updates the state of the goal structure. | ||
/// </summary> | ||
/// <param name="beliefSet">The belief set of the agent.</param> | ||
public abstract void UpdateStatus(TBeliefSet beliefSet); | ||
} | ||
} |
Oops, something went wrong.