-
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.
- Loading branch information
1 parent
dc07266
commit 5347df3
Showing
8 changed files
with
195 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Aplib.Core.Belief; | ||
using Aplib.Core.Desire.Goals; | ||
using System.Collections.Generic; | ||
|
||
namespace Aplib.Core.Desire | ||
{ | ||
public class FirstOfGoalStructure : SequentialGoalStructure | ||
{ | ||
protected Goal? _currentGoal; | ||
|
||
/// <inheritdoc /> | ||
public override Goal? DetermineCurrentGoal(IBeliefSet beliefSet) | ||
{ | ||
_currentGoal ??= GetNextGoal(beliefSet); // Should only happen the first time this method is called | ||
if (_currentGoal is null) return null; // Can be the case when no next goal can be found | ||
|
||
switch (_currentGoal!.GetState(beliefSet)) | ||
{ | ||
case GoalState.Success: | ||
return null; | ||
case GoalState.Failure: | ||
_currentGoal = GetNextGoal(beliefSet); | ||
return DetermineCurrentGoal(beliefSet); | ||
|
||
default: | ||
return _currentGoal; | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public FirstOfGoalStructure(IList<GoalStructure> children) : base(children) | ||
{ } | ||
} | ||
} |
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,35 @@ | ||
using Aplib.Core.Belief; | ||
using Aplib.Core.Desire.Goals; | ||
using System.Collections.Generic; | ||
|
||
namespace Aplib.Core.Desire | ||
{ | ||
public abstract class GoalStructure | ||
{ | ||
protected IList<GoalStructure> _children; | ||
protected Goal? _currentGoal; | ||
public GoalStructureState GoalStructureState { get; set; } = GoalStructureState.Unfinished; // TODO | ||
|
||
protected GoalStructure(IList<GoalStructure> children) | ||
{ | ||
_children = children; | ||
} | ||
|
||
public void Interrupt() | ||
{ | ||
// Premature optimisation: | ||
// foreach (var processedChild in GetAllProcessedChildren()) | ||
// processedChild.Interrupt(); | ||
foreach (GoalStructure child in _children) | ||
child.Interrupt(); | ||
|
||
ProcessInterrupt(); | ||
} | ||
|
||
public abstract Goal? DetermineCurrentGoal(IBeliefSet beliefSet); | ||
|
||
protected abstract void ProcessInterrupt(); | ||
} | ||
|
||
public enum GoalStructureState { Unfinished, Success, 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
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,25 @@ | ||
using Aplib.Core.Belief; | ||
using Aplib.Core.Desire.Goals; | ||
using static Aplib.Core.Desire.Goals.GoalState; | ||
|
||
namespace Aplib.Core.Desire | ||
{ | ||
public class PrimitiveGoalStructure : GoalStructure | ||
{ | ||
/// <inheritdoc /> | ||
public PrimitiveGoalStructure(Goal goal) : base(children: System.Array.Empty<GoalStructure>()) | ||
{ | ||
// We use _currentGoal as 'the' goal. It will never change hereafter. | ||
_currentGoal = goal; | ||
} | ||
|
||
public override Goal? DetermineCurrentGoal(IBeliefSet beliefSet) => _currentGoal!.GetState(beliefSet) switch | ||
{ | ||
Unfinished => _currentGoal, | ||
_ => null | ||
}; | ||
|
||
/// <inheritdoc /> | ||
protected override void ProcessInterrupt() { /* Do nothing */ } | ||
} | ||
} |
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,20 @@ | ||
using Aplib.Core.Belief; | ||
using Aplib.Core.Desire.Goals; | ||
using static Aplib.Core.Desire.Goals.GoalState; | ||
|
||
namespace Aplib.Core.Desire | ||
{ | ||
public class RepeatGoalStructure : PrimitiveGoalStructure | ||
{ | ||
/// <inheritdoc /> | ||
public RepeatGoalStructure(Goal goal) : base(goal) | ||
{ } | ||
|
||
/// <inheritdoc /> | ||
public override Goal? DetermineCurrentGoal(IBeliefSet beliefSet) => _currentGoal.GetState(beliefSet) switch | ||
{ | ||
Unfinished or Failure => _currentGoal, | ||
Success => null | ||
}; | ||
} | ||
} |
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,57 @@ | ||
using Aplib.Core.Belief; | ||
using Aplib.Core.Desire.Goals; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Aplib.Core.Desire | ||
{ | ||
public class SequentialGoalStructure : GoalStructure, IDisposable | ||
{ | ||
protected readonly IEnumerator<GoalStructure> _childrenEnumerator; // TODO autoproperty? | ||
|
||
|
||
/// <inheritdoc /> | ||
public override Goal? DetermineCurrentGoal(IBeliefSet beliefSet) => GetNextGoal(beliefSet); | ||
|
||
/// <summary> | ||
/// Returns the goal from the first child which returns a goal. | ||
/// </summary> | ||
/// <param name="beliefSet"></param> | ||
/// <returns>TODO</returns> | ||
protected Goal? GetNextGoal(IBeliefSet beliefSet) | ||
{ | ||
// throw new NotImplementedException("Why move to the next one immediatly?"); | ||
// if (!_childrenEnumerator.MoveNext()) | ||
// return null; | ||
// return _childrenEnumerator.Current!.DetermineCurrentGoal(beliefSet) ?? GetNextGoal(beliefSet); | ||
|
||
Goal? currentGoal = _childrenEnumerator.Current!.DetermineCurrentGoal(beliefSet); // TODO what happens once Current is undefined? | ||
if (currentGoal is not null) return currentGoal; | ||
// TODO what if the previous goal becomes uncompleted due to e.g. enemies? | ||
|
||
return _childrenEnumerator.MoveNext() ? DetermineCurrentGoal(beliefSet) : null; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void ProcessInterrupt() => _childrenEnumerator.Reset(); | ||
|
||
/// <inheritdoc /> | ||
public SequentialGoalStructure(IList<GoalStructure> children) : base(children) | ||
{ | ||
if (!children.Any()) | ||
throw new ArgumentException("Collection of children is empty", nameof(children)); | ||
_childrenEnumerator = _children.GetEnumerator(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() | ||
{ | ||
_childrenEnumerator.Dispose(); | ||
|
||
// Tell the Garbage Collector that it does not need to run the finalizer of this class anymore | ||
// https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1816#rule-description | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
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,8 @@ | ||
using FluentAssertions; | ||
|
||
namespace Aplib.Tests.Desire; | ||
|
||
public class GoalStructureTests | ||
{ | ||
|
||
} |
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