From 92f9063eda38dc261774d8ed3c1a69b6ddc28509 Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Mon, 18 Mar 2024 18:33:11 +0100 Subject: [PATCH 01/21] feat: add Belief, IBelief and Beliefset --- Aplib.Core/Belief/Belief.cs | 35 ++++++++++++++++++++++++++++++++++ Aplib.Core/Belief/Beliefset.cs | 23 ++++++++++++++++++++++ Aplib.Core/Belief/IBelief.cs | 7 +++++++ 3 files changed, 65 insertions(+) create mode 100644 Aplib.Core/Belief/Belief.cs create mode 100644 Aplib.Core/Belief/Beliefset.cs create mode 100644 Aplib.Core/Belief/IBelief.cs diff --git a/Aplib.Core/Belief/Belief.cs b/Aplib.Core/Belief/Belief.cs new file mode 100644 index 00000000..5c34a2cc --- /dev/null +++ b/Aplib.Core/Belief/Belief.cs @@ -0,0 +1,35 @@ +using System; + +namespace Aplib.Core.Belief +{ + public class Belief : IBelief + { + private readonly TReference _reference; + + private readonly Func _getResourceFromReference; + + private readonly Func _updateIf = () => true; + + private TResource _resource; + + public Belief(TReference reference, Func getResourceFromReference) + { + _reference = reference; + _getResourceFromReference = getResourceFromReference; + _resource = _getResourceFromReference(_reference); + } + + public Belief(TReference reference, Func getResourceFromReference, Func updateIf) + : this(reference, getResourceFromReference) + { + _updateIf = updateIf; + } + + public static implicit operator TResource(Belief belief) => belief._resource; + + public void UpdateBelief() + { + if (_updateIf()) _resource = _getResourceFromReference(_reference); + } + } +} diff --git a/Aplib.Core/Belief/Beliefset.cs b/Aplib.Core/Belief/Beliefset.cs new file mode 100644 index 00000000..415090de --- /dev/null +++ b/Aplib.Core/Belief/Beliefset.cs @@ -0,0 +1,23 @@ +using System.Linq; + +namespace Aplib.Core.Belief +{ + public class Beliefset + { + private readonly IBelief[] _beliefs; + + public Beliefset() + { + _beliefs = + GetType().GetFields() + .Where(field => field.GetType().IsAssignableFrom(typeof(IBelief))) + .Select(field => (IBelief)field.GetValue(this)) + .ToArray(); + } + + public void UpdateBeliefs() + { + foreach (IBelief belief in _beliefs) belief.UpdateBelief(); + } + } +} diff --git a/Aplib.Core/Belief/IBelief.cs b/Aplib.Core/Belief/IBelief.cs new file mode 100644 index 00000000..de8f68cf --- /dev/null +++ b/Aplib.Core/Belief/IBelief.cs @@ -0,0 +1,7 @@ +namespace Aplib.Core.Belief +{ + public interface IBelief + { + void UpdateBelief(); + } +} From 1c015f99b89d58b8b809c3c501463497e46c357a Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Mon, 18 Mar 2024 21:27:00 +0100 Subject: [PATCH 02/21] docs: add documentation --- Aplib.Core/Belief/Belief.cs | 42 ++++++++++++++++++++++++++++++++++ Aplib.Core/Belief/Beliefset.cs | 15 +++++++++++- Aplib.Core/Belief/IBelief.cs | 7 ++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/Aplib.Core/Belief/Belief.cs b/Aplib.Core/Belief/Belief.cs index 5c34a2cc..9c968383 100644 --- a/Aplib.Core/Belief/Belief.cs +++ b/Aplib.Core/Belief/Belief.cs @@ -2,16 +2,43 @@ namespace Aplib.Core.Belief { + /// + /// The class represents a belief. + /// Some object reference is used to generate/update a resource + /// (i.e., some piece of information on the game state as perceived by an agent). + /// It implements the interface. + /// It supports implicit conversion to . + /// + /// The type of the reference used to generate/update the resource. + /// The type of the resource the belief represents. public class Belief : IBelief { + /// + /// The reference used to generate/update the resource. + /// private readonly TReference _reference; + /// + /// A function that takes a reference and generates/updates a resource. + /// private readonly Func _getResourceFromReference; + /// + /// A function that sets a condition on when the resource should be updated. + /// private readonly Func _updateIf = () => true; + /// + /// The resource represented by the belief (i.e., some piece of information on the game state as perceived by an agent). + /// private TResource _resource; + /// + /// Initializes a new instance of the class with a reference, + /// and a function to generate/update the resource using the reference. + /// + /// The reference used to generate/update the resource. + /// A function that takes a reference and generates/updates a resource. public Belief(TReference reference, Func getResourceFromReference) { _reference = reference; @@ -19,14 +46,29 @@ public Belief(TReference reference, Func getResourceFromR _resource = _getResourceFromReference(_reference); } + /// + /// Initializes a new instance of the + /// + /// The reference used to generate/update the resource. + /// A function that takes a reference and generates/updates a resource. + /// A function that sets a condition on when the resource should be updated. public Belief(TReference reference, Func getResourceFromReference, Func updateIf) : this(reference, getResourceFromReference) { _updateIf = updateIf; } + /// + /// Implicit conversion operator to allow a object + /// to be used where a is expected. + /// + /// The object to convert. public static implicit operator TResource(Belief belief) => belief._resource; + /// + /// Generates/updates the resource if the updateIf condition is satisfied. + /// The resource is then updated by calling the getResourceFromReference function. + /// public void UpdateBelief() { if (_updateIf()) _resource = _getResourceFromReference(_reference); diff --git a/Aplib.Core/Belief/Beliefset.cs b/Aplib.Core/Belief/Beliefset.cs index 415090de..fef443b8 100644 --- a/Aplib.Core/Belief/Beliefset.cs +++ b/Aplib.Core/Belief/Beliefset.cs @@ -2,10 +2,20 @@ namespace Aplib.Core.Belief { - public class Beliefset + /// + /// The class can be inherited to define a set of beliefs for an agent. + /// All public fields of type that are defined in the inheriting class + /// are automatically updated when calling . + /// + public abstract class Beliefset { private readonly IBelief[] _beliefs; + /// + /// Initializes a new instance of the class. + /// All public fields of type that are defined in the inheriting class + /// are automatically updated when calling . + /// public Beliefset() { _beliefs = @@ -15,6 +25,9 @@ public Beliefset() .ToArray(); } + /// + /// Updates all objects of type that are defined as public fields in . + /// public void UpdateBeliefs() { foreach (IBelief belief in _beliefs) belief.UpdateBelief(); diff --git a/Aplib.Core/Belief/IBelief.cs b/Aplib.Core/Belief/IBelief.cs index de8f68cf..7620a9e9 100644 --- a/Aplib.Core/Belief/IBelief.cs +++ b/Aplib.Core/Belief/IBelief.cs @@ -1,7 +1,14 @@ namespace Aplib.Core.Belief { + /// + /// The interface defines a contract for classes that represent a belief. + /// A belief is some piece of information on the game state as perceived by an agent. + /// public interface IBelief { + /// + /// Updates the belief based on information of the game state. + /// void UpdateBelief(); } } From 4e74455527d44c5e518b5a4c5cce2e2bccf8600c Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Mon, 18 Mar 2024 22:35:36 +0100 Subject: [PATCH 03/21] test: add tests for Belief --- Aplib.Tests/Core/Belief/BeliefTests.cs | 93 ++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Aplib.Tests/Core/Belief/BeliefTests.cs diff --git a/Aplib.Tests/Core/Belief/BeliefTests.cs b/Aplib.Tests/Core/Belief/BeliefTests.cs new file mode 100644 index 00000000..4a1a2daa --- /dev/null +++ b/Aplib.Tests/Core/Belief/BeliefTests.cs @@ -0,0 +1,93 @@ +using Aplib.Core.Belief; + +namespace Aplib.Tests.Core.Belief; +public class BeliefTests +{ + private static TReference ID(TReference reference) => reference; + + private static int GetCount(IEnumerable reference) => reference.Count(); + + private static bool TrueUpdateIf() => true; + + private static bool FalseUpdateIf() => false; + + + /// + /// Given a Belief instance, + /// When it is assigned to a variable of its resource type, + /// Then it is implicitly converted to its resource type. + /// + [Fact] + public void Belief_IsImplicitlyConvertedToResourceType_WhenAssignedToResourceType() + { + // Arrange + string reference = "def"; + Belief belief = new(reference, ID); + + // Act + string resource = belief; + + // Assert + Assert.Equal("def", resource); + } + + /// + /// Given a Belief instance with an updateIf condition that is satisfied, + /// When UpdateBelief is called, + /// Then the resource is updated. + /// + [Fact] + public void UpdateBelief_UpdatesResource_WhenUpdateIfConditionIsSatisfied() + { + // Arrange + List list = []; + Belief, int> belief = new(list, GetCount, TrueUpdateIf); + + // Act + list.Add(69); + belief.UpdateBelief(); + + // Assert + Assert.Equal(1, belief); + } + + /// + /// Given a Belief instance with an updateIf condition that is not satisfied, + /// When UpdateBelief is called, + /// Then the resource is not updated. + /// + [Fact] + public void UpdateBelief_DoesNotUpdateResource_WhenUpdateIfConditionIsNotSatisfied() + { + // Arrange + List list = []; + Belief, int> belief = new(list, GetCount, FalseUpdateIf); + + // Act + list.Add(69); + belief.UpdateBelief(); + + // Assert + Assert.Equal(0, belief); + } + + /// + /// Given a Belief instance with a reference, + /// When the reference is assigned to and UpdateBelief is called, + /// Then the resource is not updated. + /// + [Fact] + public void UpdateBelief_DoesNotUpdateResource_WhenReferenceIsAssignedTo() + { + // Arrange + string def = "def"; + Belief belief = new(def, ID, TrueUpdateIf); + + // Act + def = "abc"; + belief.UpdateBelief(); + + // Assert + Assert.NotEqual(def, belief); + } +} From 87b1a949c36a6b92c817f15c8ed8687850681d28 Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Mon, 18 Mar 2024 22:59:33 +0100 Subject: [PATCH 04/21] test: add tests for Beliefset --- Aplib.Tests/Core/Belief/BeliefsetTests.cs | 44 +++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Aplib.Tests/Core/Belief/BeliefsetTests.cs diff --git a/Aplib.Tests/Core/Belief/BeliefsetTests.cs b/Aplib.Tests/Core/Belief/BeliefsetTests.cs new file mode 100644 index 00000000..c84373e6 --- /dev/null +++ b/Aplib.Tests/Core/Belief/BeliefsetTests.cs @@ -0,0 +1,44 @@ +using Aplib.Core.Belief; + +namespace Aplib.Tests.Core.Belief; +public class BeliefsetTests +{ + /// + /// Given a Beliefset instance with multiple beliefs, + /// When UpdateBeliefs is called, + /// Then all beliefs are updated. + /// + [Fact] + public void UpdateBeliefs_UpdatesAllBeliefs_WhenCalled() + { + // Arrange + var beliefset = new TestBeliefset(); + + // Act + beliefset.UpdateBeliefs(); + + // Assert + Assert.True(beliefset.Belief1.Updated); + Assert.True(beliefset.Belief2.Updated); + } + + private class TestBeliefset : Beliefset + { + public SimpleBelief Belief1 = new(); + public SimpleBelief Belief2 = new(); + + } + + /// + /// A simple belief that can be used to test whether has been called. + /// + private class SimpleBelief : IBelief + { + public bool Updated { get; private set; } = false; + + public void UpdateBelief() + { + Updated = true; + } + } +} From e7d52fc0538077e1e22c77f6ec1fdd3277c253b3 Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Mon, 18 Mar 2024 23:07:29 +0100 Subject: [PATCH 05/21] fix: fix Reflection bug in Beliefset --- Aplib.Core/Belief/Beliefset.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Aplib.Core/Belief/Beliefset.cs b/Aplib.Core/Belief/Beliefset.cs index fef443b8..445bea10 100644 --- a/Aplib.Core/Belief/Beliefset.cs +++ b/Aplib.Core/Belief/Beliefset.cs @@ -20,7 +20,7 @@ public Beliefset() { _beliefs = GetType().GetFields() - .Where(field => field.GetType().IsAssignableFrom(typeof(IBelief))) + .Where(field => typeof(IBelief).IsAssignableFrom(field.FieldType)) .Select(field => (IBelief)field.GetValue(this)) .ToArray(); } From 85313f726294a69f657dec05fb24118b88eebe07 Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Mon, 18 Mar 2024 23:30:34 +0100 Subject: [PATCH 06/21] fix: make Beliefset constructor protected --- Aplib.Core/Belief/Beliefset.cs | 2 +- Aplib.Tests/Core/Belief/BeliefsetTests.cs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Aplib.Core/Belief/Beliefset.cs b/Aplib.Core/Belief/Beliefset.cs index 445bea10..4af7ac42 100644 --- a/Aplib.Core/Belief/Beliefset.cs +++ b/Aplib.Core/Belief/Beliefset.cs @@ -16,7 +16,7 @@ public abstract class Beliefset /// All public fields of type that are defined in the inheriting class /// are automatically updated when calling . /// - public Beliefset() + protected Beliefset() { _beliefs = GetType().GetFields() diff --git a/Aplib.Tests/Core/Belief/BeliefsetTests.cs b/Aplib.Tests/Core/Belief/BeliefsetTests.cs index c84373e6..326a22f5 100644 --- a/Aplib.Tests/Core/Belief/BeliefsetTests.cs +++ b/Aplib.Tests/Core/Belief/BeliefsetTests.cs @@ -12,7 +12,7 @@ public class BeliefsetTests public void UpdateBeliefs_UpdatesAllBeliefs_WhenCalled() { // Arrange - var beliefset = new TestBeliefset(); + TestBeliefset beliefset = new(); // Act beliefset.UpdateBeliefs(); @@ -26,7 +26,6 @@ private class TestBeliefset : Beliefset { public SimpleBelief Belief1 = new(); public SimpleBelief Belief2 = new(); - } /// From 335d76799a423cc00c1c31f8ef9ea0af23ae3366 Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Mon, 18 Mar 2024 23:59:37 +0100 Subject: [PATCH 07/21] docs: change summaries to remarks --- Aplib.Core/Belief/Belief.cs | 4 +++- Aplib.Core/Belief/Beliefset.cs | 8 ++++++-- Aplib.Core/Belief/IBelief.cs | 4 +++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Aplib.Core/Belief/Belief.cs b/Aplib.Core/Belief/Belief.cs index 9c968383..f99fc3f5 100644 --- a/Aplib.Core/Belief/Belief.cs +++ b/Aplib.Core/Belief/Belief.cs @@ -6,9 +6,11 @@ namespace Aplib.Core.Belief /// The class represents a belief. /// Some object reference is used to generate/update a resource /// (i.e., some piece of information on the game state as perceived by an agent). + /// + /// /// It implements the interface. /// It supports implicit conversion to . - /// + /// /// The type of the reference used to generate/update the resource. /// The type of the resource the belief represents. public class Belief : IBelief diff --git a/Aplib.Core/Belief/Beliefset.cs b/Aplib.Core/Belief/Beliefset.cs index 4af7ac42..85d2cc16 100644 --- a/Aplib.Core/Belief/Beliefset.cs +++ b/Aplib.Core/Belief/Beliefset.cs @@ -4,18 +4,22 @@ namespace Aplib.Core.Belief { /// /// The class can be inherited to define a set of beliefs for an agent. + /// + /// /// All public fields of type that are defined in the inheriting class /// are automatically updated when calling . - /// + /// public abstract class Beliefset { private readonly IBelief[] _beliefs; /// /// Initializes a new instance of the class. + /// + /// /// All public fields of type that are defined in the inheriting class /// are automatically updated when calling . - /// + /// protected Beliefset() { _beliefs = diff --git a/Aplib.Core/Belief/IBelief.cs b/Aplib.Core/Belief/IBelief.cs index 7620a9e9..fdaf9e61 100644 --- a/Aplib.Core/Belief/IBelief.cs +++ b/Aplib.Core/Belief/IBelief.cs @@ -2,8 +2,10 @@ { /// /// The interface defines a contract for classes that represent a belief. - /// A belief is some piece of information on the game state as perceived by an agent. /// + /// + /// A belief is some piece of information on the game state as perceived by an agent. + /// public interface IBelief { /// From a5c2b7bfc19945057cec5df4708d75710f92be0f Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Tue, 19 Mar 2024 00:34:34 +0100 Subject: [PATCH 08/21] fix: adhere to naming conventions for unit tests --- Aplib.Tests/Core/Belief/BeliefTests.cs | 8 ++++---- Aplib.Tests/Core/Belief/BeliefsetTests.cs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Aplib.Tests/Core/Belief/BeliefTests.cs b/Aplib.Tests/Core/Belief/BeliefTests.cs index 4a1a2daa..b6297fcb 100644 --- a/Aplib.Tests/Core/Belief/BeliefTests.cs +++ b/Aplib.Tests/Core/Belief/BeliefTests.cs @@ -18,7 +18,7 @@ public class BeliefTests /// Then it is implicitly converted to its resource type. /// [Fact] - public void Belief_IsImplicitlyConvertedToResourceType_WhenAssignedToResourceType() + public void Belief_AssignedToResourceType_IsImplicitlyConvertedToResourceType() { // Arrange string reference = "def"; @@ -37,7 +37,7 @@ public void Belief_IsImplicitlyConvertedToResourceType_WhenAssignedToResourceTyp /// Then the resource is updated. /// [Fact] - public void UpdateBelief_UpdatesResource_WhenUpdateIfConditionIsSatisfied() + public void UpdateBelief_UpdateIfConditionIsSatisfied_UpdatesResource() { // Arrange List list = []; @@ -57,7 +57,7 @@ public void UpdateBelief_UpdatesResource_WhenUpdateIfConditionIsSatisfied() /// Then the resource is not updated. /// [Fact] - public void UpdateBelief_DoesNotUpdateResource_WhenUpdateIfConditionIsNotSatisfied() + public void UpdateBelief_UpdateIfConditionIsNotSatisfied_DoesNotUpdateResource() { // Arrange List list = []; @@ -77,7 +77,7 @@ public void UpdateBelief_DoesNotUpdateResource_WhenUpdateIfConditionIsNotSatisfi /// Then the resource is not updated. /// [Fact] - public void UpdateBelief_DoesNotUpdateResource_WhenReferenceIsAssignedTo() + public void UpdateBelief_ReferenceIsAssignedTo_DoesNotUpdateResource() { // Arrange string def = "def"; diff --git a/Aplib.Tests/Core/Belief/BeliefsetTests.cs b/Aplib.Tests/Core/Belief/BeliefsetTests.cs index 326a22f5..eae13862 100644 --- a/Aplib.Tests/Core/Belief/BeliefsetTests.cs +++ b/Aplib.Tests/Core/Belief/BeliefsetTests.cs @@ -9,7 +9,7 @@ public class BeliefsetTests /// Then all beliefs are updated. /// [Fact] - public void UpdateBeliefs_UpdatesAllBeliefs_WhenCalled() + public void UpdateBeliefs_Called_UpdatesAllBeliefs() { // Arrange TestBeliefset beliefset = new(); From 768cffd9c34c84af379027739ceb98619342cdb1 Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Wed, 20 Mar 2024 17:22:13 +0100 Subject: [PATCH 09/21] chore: change name Beliefset to BeliefSet --- Aplib.Core/Belief/Beliefset.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Aplib.Core/Belief/Beliefset.cs b/Aplib.Core/Belief/Beliefset.cs index 85d2cc16..da133c42 100644 --- a/Aplib.Core/Belief/Beliefset.cs +++ b/Aplib.Core/Belief/Beliefset.cs @@ -3,24 +3,24 @@ namespace Aplib.Core.Belief { /// - /// The class can be inherited to define a set of beliefs for an agent. + /// The class can be inherited to define a set of beliefs for an agent. /// /// /// All public fields of type that are defined in the inheriting class /// are automatically updated when calling . /// - public abstract class Beliefset + public abstract class BeliefSet { private readonly IBelief[] _beliefs; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// /// All public fields of type that are defined in the inheriting class /// are automatically updated when calling . /// - protected Beliefset() + protected BeliefSet() { _beliefs = GetType().GetFields() @@ -30,7 +30,7 @@ protected Beliefset() } /// - /// Updates all objects of type that are defined as public fields in . + /// Updates all objects of type that are defined as public fields in . /// public void UpdateBeliefs() { From 87c11cd8aec71e640d5ae79665a67e702ec53902 Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Wed, 20 Mar 2024 17:22:13 +0100 Subject: [PATCH 10/21] chore: change name Beliefset to BeliefSet --- Aplib.Core/Belief/Beliefset.cs | 10 +++++----- Aplib.Tests/Core/Belief/BeliefsetTests.cs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Aplib.Core/Belief/Beliefset.cs b/Aplib.Core/Belief/Beliefset.cs index 85d2cc16..da133c42 100644 --- a/Aplib.Core/Belief/Beliefset.cs +++ b/Aplib.Core/Belief/Beliefset.cs @@ -3,24 +3,24 @@ namespace Aplib.Core.Belief { /// - /// The class can be inherited to define a set of beliefs for an agent. + /// The class can be inherited to define a set of beliefs for an agent. /// /// /// All public fields of type that are defined in the inheriting class /// are automatically updated when calling . /// - public abstract class Beliefset + public abstract class BeliefSet { private readonly IBelief[] _beliefs; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// /// All public fields of type that are defined in the inheriting class /// are automatically updated when calling . /// - protected Beliefset() + protected BeliefSet() { _beliefs = GetType().GetFields() @@ -30,7 +30,7 @@ protected Beliefset() } /// - /// Updates all objects of type that are defined as public fields in . + /// Updates all objects of type that are defined as public fields in . /// public void UpdateBeliefs() { diff --git a/Aplib.Tests/Core/Belief/BeliefsetTests.cs b/Aplib.Tests/Core/Belief/BeliefsetTests.cs index eae13862..a508417b 100644 --- a/Aplib.Tests/Core/Belief/BeliefsetTests.cs +++ b/Aplib.Tests/Core/Belief/BeliefsetTests.cs @@ -12,7 +12,7 @@ public class BeliefsetTests public void UpdateBeliefs_Called_UpdatesAllBeliefs() { // Arrange - TestBeliefset beliefset = new(); + TestBeliefSet beliefset = new(); // Act beliefset.UpdateBeliefs(); @@ -22,7 +22,7 @@ public void UpdateBeliefs_Called_UpdatesAllBeliefs() Assert.True(beliefset.Belief2.Updated); } - private class TestBeliefset : Beliefset + private class TestBeliefSet : BeliefSet { public SimpleBelief Belief1 = new(); public SimpleBelief Belief2 = new(); From 255ce7a4e5a652d01ce55d8603bf7cf3ebb5f1e3 Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Thu, 21 Mar 2024 16:26:41 +0100 Subject: [PATCH 11/21] fix: change name TResource to TObservation and add more tests --- Aplib.Core/Belief/Belief.cs | 64 ++++++------- Aplib.Tests/Core/Belief/BeliefTests.cs | 22 ++--- Aplib.Tests/Core/Belief/BeliefsetTests.cs | 107 +++++++++++++++++++++- 3 files changed, 146 insertions(+), 47 deletions(-) diff --git a/Aplib.Core/Belief/Belief.cs b/Aplib.Core/Belief/Belief.cs index f99fc3f5..42135b29 100644 --- a/Aplib.Core/Belief/Belief.cs +++ b/Aplib.Core/Belief/Belief.cs @@ -3,77 +3,77 @@ namespace Aplib.Core.Belief { /// - /// The class represents a belief. - /// Some object reference is used to generate/update a resource + /// The class represents a belief. + /// Some object reference is used to generate/update a observation /// (i.e., some piece of information on the game state as perceived by an agent). /// /// /// It implements the interface. - /// It supports implicit conversion to . + /// It supports implicit conversion to . /// - /// The type of the reference used to generate/update the resource. - /// The type of the resource the belief represents. - public class Belief : IBelief + /// The type of the reference used to generate/update the observation. + /// The type of the observation the belief represents. + public class Belief : IBelief { /// - /// The reference used to generate/update the resource. + /// The reference used to generate/update the observation. /// private readonly TReference _reference; /// - /// A function that takes a reference and generates/updates a resource. + /// A function that takes a reference and generates/updates an observation. /// - private readonly Func _getResourceFromReference; + private readonly Func _getObservationFromReference; /// - /// A function that sets a condition on when the resource should be updated. + /// A function that sets a condition on when the observation should be updated. /// private readonly Func _updateIf = () => true; /// - /// The resource represented by the belief (i.e., some piece of information on the game state as perceived by an agent). + /// The observation represented by the belief (i.e., some piece of information on the game state as perceived by an agent). /// - private TResource _resource; + private TObservation _observation; /// - /// Initializes a new instance of the class with a reference, - /// and a function to generate/update the resource using the reference. + /// Initializes a new instance of the class with a reference, + /// and a function to generate/update the observation using the reference. /// - /// The reference used to generate/update the resource. - /// A function that takes a reference and generates/updates a resource. - public Belief(TReference reference, Func getResourceFromReference) + /// A function that takes a reference and generates/updates an observation. + /// A function that takes a reference and generates/updates an observation. + public Belief(TReference reference, Func getObservationFromReference) { _reference = reference; - _getResourceFromReference = getResourceFromReference; - _resource = _getResourceFromReference(_reference); + _getObservationFromReference = getObservationFromReference; + _observation = _getObservationFromReference(_reference); } /// - /// Initializes a new instance of the + /// Initializes a new instance of the /// - /// The reference used to generate/update the resource. - /// A function that takes a reference and generates/updates a resource. - /// A function that sets a condition on when the resource should be updated. - public Belief(TReference reference, Func getResourceFromReference, Func updateIf) - : this(reference, getResourceFromReference) + /// The reference used to generate/update the observation. + /// A function that takes a reference and generates/updates an observation. + /// A function that sets a condition on when the observation should be updated. + public Belief(TReference reference, Func getObservationFromReference, Func updateIf) + : this(reference, getObservationFromReference) { _updateIf = updateIf; } /// - /// Implicit conversion operator to allow a object - /// to be used where a is expected. + /// Implicit conversion operator to allow a object + /// to be used where a is expected. /// - /// The object to convert. - public static implicit operator TResource(Belief belief) => belief._resource; + /// The object to convert. + public static implicit operator TObservation(Belief belief) => belief._observation; /// - /// Generates/updates the resource if the updateIf condition is satisfied. - /// The resource is then updated by calling the getResourceFromReference function. + /// Generates/updates the observation if the updateIf condition is satisfied. + /// The observation is then updated by calling the getObservationFromReference function. /// public void UpdateBelief() { - if (_updateIf()) _resource = _getResourceFromReference(_reference); + if (_updateIf()) _observation = _getObservationFromReference(_reference); } } } diff --git a/Aplib.Tests/Core/Belief/BeliefTests.cs b/Aplib.Tests/Core/Belief/BeliefTests.cs index b6297fcb..21ec01ff 100644 --- a/Aplib.Tests/Core/Belief/BeliefTests.cs +++ b/Aplib.Tests/Core/Belief/BeliefTests.cs @@ -14,30 +14,30 @@ public class BeliefTests /// /// Given a Belief instance, - /// When it is assigned to a variable of its resource type, - /// Then it is implicitly converted to its resource type. + /// When it is assigned to a variable of its observation type, + /// Then it is implicitly converted to its observation type. /// [Fact] - public void Belief_AssignedToResourceType_IsImplicitlyConvertedToResourceType() + public void Belief_AssignedToObservationType_IsImplicitlyConvertedToObservationType() { // Arrange string reference = "def"; Belief belief = new(reference, ID); // Act - string resource = belief; + string observation = belief; // Assert - Assert.Equal("def", resource); + Assert.Equal("def", observation); } /// /// Given a Belief instance with an updateIf condition that is satisfied, /// When UpdateBelief is called, - /// Then the resource is updated. + /// Then the observation is updated. /// [Fact] - public void UpdateBelief_UpdateIfConditionIsSatisfied_UpdatesResource() + public void UpdateBelief_UpdateIfConditionIsSatisfied_UpdatesObservation() { // Arrange List list = []; @@ -54,10 +54,10 @@ public void UpdateBelief_UpdateIfConditionIsSatisfied_UpdatesResource() /// /// Given a Belief instance with an updateIf condition that is not satisfied, /// When UpdateBelief is called, - /// Then the resource is not updated. + /// Then the observation is not updated. /// [Fact] - public void UpdateBelief_UpdateIfConditionIsNotSatisfied_DoesNotUpdateResource() + public void UpdateBelief_UpdateIfConditionIsNotSatisfied_DoesNotUpdateObservation() { // Arrange List list = []; @@ -74,10 +74,10 @@ public void UpdateBelief_UpdateIfConditionIsNotSatisfied_DoesNotUpdateResource() /// /// Given a Belief instance with a reference, /// When the reference is assigned to and UpdateBelief is called, - /// Then the resource is not updated. + /// Then the observation is not updated. /// [Fact] - public void UpdateBelief_ReferenceIsAssignedTo_DoesNotUpdateResource() + public void UpdateBelief_ReferenceIsAssignedTo_DoesNotUpdateObservation() { // Arrange string def = "def"; diff --git a/Aplib.Tests/Core/Belief/BeliefsetTests.cs b/Aplib.Tests/Core/Belief/BeliefsetTests.cs index a8192612..2b8acef4 100644 --- a/Aplib.Tests/Core/Belief/BeliefsetTests.cs +++ b/Aplib.Tests/Core/Belief/BeliefsetTests.cs @@ -4,17 +4,18 @@ namespace Aplib.Tests.Core.Belief; public class BeliefSetTests { /// - /// Given a BeliefSet instance with multiple beliefs, + /// Given a BeliefSet instance with multiple public field beliefs, /// When UpdateBeliefs is called, /// Then all beliefs are updated. /// [Fact] - public void UpdateBeliefs_Called_UpdatesAllBeliefs() + public void UpdateBeliefs_PublicBeliefFields_UpdatesAllBeliefs() { // Arrange - TestBeliefSet beliefSet = new(); + TestBeliefSetPublic beliefSet = new(); // Act + // UpdateBeliefs should set Updated to true for all beliefs. beliefSet.UpdateBeliefs(); // Assert @@ -22,19 +23,117 @@ public void UpdateBeliefs_Called_UpdatesAllBeliefs() Assert.True(beliefSet.Belief2.Updated); } - private class TestBeliefSet : BeliefSet + /// + /// Given a BeliefSet instance with multiple public property beliefs, + /// When UpdateBeliefs is called, + /// Then all beliefs are updated. + /// + [Fact] + public void UpdateBeliefs_PublicBeliefProperties_DoesNotUpdateAnyBeliefs() { + // Arrange + TestBeliefSetProperties beliefSet = new(); + + // Act + // UpdateBeliefs should *not* set Updated to true for any belief. + beliefSet.UpdateBeliefs(); + + // Assert + Assert.False(beliefSet.Belief1.Updated); + Assert.False(beliefSet.Belief2.Updated); + } + + /// + /// Given a BeliefSet instance with multiple private field beliefs, + /// When UpdateBeliefs is called, + /// Then all beliefs are updated. + /// + [Fact] + public void UpdateBeliefs_PrivateBeliefFields_DoesNotUpdateAnyBeliefs() + { + // Arrange + TestBeliefSetPrivate beliefSet = new(); + + // Act + // UpdateBeliefs should *not* set Updated to true for any belief. + beliefSet.UpdateBeliefs(); + + // Assert + Assert.False(beliefSet.Belief1.Updated); + Assert.False(beliefSet.Belief2.Updated); + } + + /// + /// A test belief set that contains two public simple beliefs. + /// + private class TestBeliefSetPublic : BeliefSet + { + /// + /// Belief that sets Updated to true when UpdateBelief is called. + /// public SimpleBelief Belief1 = new(); + + /// + /// Belief that sets Updated to true when UpdateBelief is called. + /// public SimpleBelief Belief2 = new(); } + + /// + /// A test belief set that contains two simple public property beliefs. + /// + private class TestBeliefSetProperties : BeliefSet + { + /// + /// Belief that sets Updated to true when UpdateBelief is called. + /// + public SimpleBelief Belief1 { get; } = new(); + + /// + /// Belief that sets Updated to true when UpdateBelief is called. + /// + public SimpleBelief Belief2 { get; } = new(); + + } + + + /// + /// A test belief set that contains two private simple beliefs. + /// + private class TestBeliefSetPrivate : BeliefSet + { + /// + /// Belief that sets Updated to true when UpdateBelief is called. + /// + private SimpleBelief _belief1 = new(); + + /// + /// Belief that sets Updated to true when UpdateBelief is called. + /// + private SimpleBelief _belief2 = new(); + + /// + public SimpleBelief Belief1 => _belief1; + + /// + public SimpleBelief Belief2 => _belief2; + + } + /// /// A simple belief that can be used to test whether has been called. /// private class SimpleBelief : IBelief { + /// + /// Stores whether has been called. + /// public bool Updated { get; private set; } = false; + /// + /// Sets to true. + /// public void UpdateBelief() { Updated = true; From e7a80e2b3c467d3915dea042c73404b47465b710 Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Thu, 21 Mar 2024 17:04:27 +0100 Subject: [PATCH 12/21] fix: improve documentation and namings --- Aplib.Core/Belief/Belief.cs | 40 ++++++++++++++------------ Aplib.Tests/Core/Belief/BeliefTests.cs | 14 ++++----- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/Aplib.Core/Belief/Belief.cs b/Aplib.Core/Belief/Belief.cs index 42135b29..b02648c1 100644 --- a/Aplib.Core/Belief/Belief.cs +++ b/Aplib.Core/Belief/Belief.cs @@ -3,32 +3,32 @@ namespace Aplib.Core.Belief { /// - /// The class represents a belief. - /// Some object reference is used to generate/update a observation + /// The class represents the agent's belief of a single object. + /// Some object reference is used to generate/update an observation /// (i.e., some piece of information on the game state as perceived by an agent). /// /// /// It implements the interface. /// It supports implicit conversion to . /// - /// The type of the reference used to generate/update the observation. - /// The type of the observation the belief represents. + /// The type of the object reference used to generate/update the observation. + /// The type of the observation that the belief represents. public class Belief : IBelief { /// - /// The reference used to generate/update the observation. + /// The object reference used to generate/update the observation. /// private readonly TReference _reference; /// - /// A function that takes a reference and generates/updates an observation. + /// A function that takes an object reference and generates/updates an observation. /// private readonly Func _getObservationFromReference; /// - /// A function that sets a condition on when the observation should be updated. + /// A condition on when the observation should be updated. /// - private readonly Func _updateIf = () => true; + private readonly Func _shouldUpdate = () => true; /// /// The observation represented by the belief (i.e., some piece of information on the game state as perceived by an agent). @@ -36,11 +36,11 @@ public class Belief : IBelief private TObservation _observation; /// - /// Initializes a new instance of the class with a reference, - /// and a function to generate/update the observation using the reference. + /// Initializes a new instance of the class with an object reference, + /// and a function to generate/update the observation using the object reference. /// - /// A function that takes a reference and generates/updates an observation. - /// A function that takes a reference and generates/updates an observation. + /// A function that takes an object reference and generates/updates an observation. + /// A function that takes an object reference and generates/updates an observation. public Belief(TReference reference, Func getObservationFromReference) { _reference = reference; @@ -49,15 +49,17 @@ public Belief(TReference reference, Func getObservatio } /// - /// Initializes a new instance of the + /// Initializes a new instance of the class with an object reference, + /// a function to generate/update the observation using the object reference, + /// and a condition on when the observation should be updated. /// - /// The reference used to generate/update the observation. - /// A function that takes a reference and generates/updates an observation. - /// A function that sets a condition on when the observation should be updated. - public Belief(TReference reference, Func getObservationFromReference, Func updateIf) + /// The object reference used to generate/update the observation. + /// A function that takes an object reference and generates/updates an observation. + /// A condition on when the observation should be updated. + public Belief(TReference reference, Func getObservationFromReference, Func shouldUpdate) : this(reference, getObservationFromReference) { - _updateIf = updateIf; + _shouldUpdate = shouldUpdate; } /// @@ -73,7 +75,7 @@ public Belief(TReference reference, Func getObservatio /// public void UpdateBelief() { - if (_updateIf()) _observation = _getObservationFromReference(_reference); + if (_shouldUpdate()) _observation = _getObservationFromReference(_reference); } } } diff --git a/Aplib.Tests/Core/Belief/BeliefTests.cs b/Aplib.Tests/Core/Belief/BeliefTests.cs index 21ec01ff..c890218f 100644 --- a/Aplib.Tests/Core/Belief/BeliefTests.cs +++ b/Aplib.Tests/Core/Belief/BeliefTests.cs @@ -7,9 +7,9 @@ public class BeliefTests private static int GetCount(IEnumerable reference) => reference.Count(); - private static bool TrueUpdateIf() => true; + private static bool TrueShouldUpdate() => true; - private static bool FalseUpdateIf() => false; + private static bool FalseShouldUpdate() => false; /// @@ -37,11 +37,11 @@ public void Belief_AssignedToObservationType_IsImplicitlyConvertedToObservationT /// Then the observation is updated. /// [Fact] - public void UpdateBelief_UpdateIfConditionIsSatisfied_UpdatesObservation() + public void UpdateBelief_ShouldUpdateConditionIsSatisfied_UpdatesObservation() { // Arrange List list = []; - Belief, int> belief = new(list, GetCount, TrueUpdateIf); + Belief, int> belief = new(list, GetCount, TrueShouldUpdate); // Act list.Add(69); @@ -57,11 +57,11 @@ public void UpdateBelief_UpdateIfConditionIsSatisfied_UpdatesObservation() /// Then the observation is not updated. /// [Fact] - public void UpdateBelief_UpdateIfConditionIsNotSatisfied_DoesNotUpdateObservation() + public void UpdateBelief_ShouldUpdateConditionIsNotSatisfied_DoesNotUpdateObservation() { // Arrange List list = []; - Belief, int> belief = new(list, GetCount, FalseUpdateIf); + Belief, int> belief = new(list, GetCount, FalseShouldUpdate); // Act list.Add(69); @@ -81,7 +81,7 @@ public void UpdateBelief_ReferenceIsAssignedTo_DoesNotUpdateObservation() { // Arrange string def = "def"; - Belief belief = new(def, ID, TrueUpdateIf); + Belief belief = new(def, ID, TrueShouldUpdate); // Act def = "abc"; From bc9b91a9a7764b66d5e8bd18678d66c8db98ed93 Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Thu, 21 Mar 2024 18:14:59 +0100 Subject: [PATCH 13/21] fix: improve IBelief and Belief summaries --- Aplib.Core/Belief/Belief.cs | 4 ++-- Aplib.Core/Belief/IBelief.cs | 5 +---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Aplib.Core/Belief/Belief.cs b/Aplib.Core/Belief/Belief.cs index b02648c1..58b33046 100644 --- a/Aplib.Core/Belief/Belief.cs +++ b/Aplib.Core/Belief/Belief.cs @@ -5,7 +5,7 @@ namespace Aplib.Core.Belief /// /// The class represents the agent's belief of a single object. /// Some object reference is used to generate/update an observation - /// (i.e., some piece of information on the game state as perceived by an agent). + /// (i.e., some piece of information of the game state as perceived by an agent). /// /// /// It implements the interface. @@ -31,7 +31,7 @@ public class Belief : IBelief private readonly Func _shouldUpdate = () => true; /// - /// The observation represented by the belief (i.e., some piece of information on the game state as perceived by an agent). + /// The observation represented by the belief (i.e., some piece of information of the game state as perceived by an agent). /// private TObservation _observation; diff --git a/Aplib.Core/Belief/IBelief.cs b/Aplib.Core/Belief/IBelief.cs index fdaf9e61..c361f9f0 100644 --- a/Aplib.Core/Belief/IBelief.cs +++ b/Aplib.Core/Belief/IBelief.cs @@ -1,11 +1,8 @@ namespace Aplib.Core.Belief { /// - /// The interface defines a contract for classes that represent a belief. + /// A belief represents/encapsulates an observation (i.e., piece of information of the game state as perceived by an agent). /// - /// - /// A belief is some piece of information on the game state as perceived by an agent. - /// public interface IBelief { /// From 76245f889be8cde63cf753d3558c88774b36408d Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Thu, 21 Mar 2024 20:16:35 +0100 Subject: [PATCH 14/21] fix: more improvements to docs and tests --- Aplib.Core/Belief/Belief.cs | 2 +- Aplib.Core/Belief/Beliefset.cs | 16 +++----- Aplib.Tests/Core/Belief/BeliefTests.cs | 46 +++++++++++++---------- Aplib.Tests/Core/Belief/BeliefsetTests.cs | 4 ++ 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/Aplib.Core/Belief/Belief.cs b/Aplib.Core/Belief/Belief.cs index 58b33046..3a4fbb77 100644 --- a/Aplib.Core/Belief/Belief.cs +++ b/Aplib.Core/Belief/Belief.cs @@ -70,7 +70,7 @@ public Belief(TReference reference, Func getObservatio public static implicit operator TObservation(Belief belief) => belief._observation; /// - /// Generates/updates the observation if the updateIf condition is satisfied. + /// Generates/updates the observation if the shouldUpdate condition is satisfied. /// The observation is then updated by calling the getObservationFromReference function. /// public void UpdateBelief() diff --git a/Aplib.Core/Belief/Beliefset.cs b/Aplib.Core/Belief/Beliefset.cs index da133c42..fd97a0cf 100644 --- a/Aplib.Core/Belief/Beliefset.cs +++ b/Aplib.Core/Belief/Beliefset.cs @@ -4,26 +4,22 @@ namespace Aplib.Core.Belief { /// /// The class can be inherited to define a set of beliefs for an agent. - /// - /// /// All public fields of type that are defined in the inheriting class /// are automatically updated when calling . - /// + /// public abstract class BeliefSet { private readonly IBelief[] _beliefs; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class, + /// and stores all public fields of type (that have been defined in the inheriting class) in an array. + /// All public fields are then automatically updated when calling . /// - /// - /// All public fields of type that are defined in the inheriting class - /// are automatically updated when calling . - /// protected BeliefSet() { - _beliefs = - GetType().GetFields() + _beliefs = GetType() + .GetFields() .Where(field => typeof(IBelief).IsAssignableFrom(field.FieldType)) .Select(field => (IBelief)field.GetValue(this)) .ToArray(); diff --git a/Aplib.Tests/Core/Belief/BeliefTests.cs b/Aplib.Tests/Core/Belief/BeliefTests.cs index c890218f..12392e0f 100644 --- a/Aplib.Tests/Core/Belief/BeliefTests.cs +++ b/Aplib.Tests/Core/Belief/BeliefTests.cs @@ -1,16 +1,24 @@ using Aplib.Core.Belief; +using System.Linq; namespace Aplib.Tests.Core.Belief; + +/// +/// Describes a set of tests for the class. +/// public class BeliefTests { - private static TReference ID(TReference reference) => reference; - - private static int GetCount(IEnumerable reference) => reference.Count(); - - private static bool TrueShouldUpdate() => true; - - private static bool FalseShouldUpdate() => false; + /// + /// A constant 'true' method for testing. + /// + /// True. + private static bool AlwaysUpdate() => true; + /// + /// A constant 'false' method for testing. + /// + /// False. + private static bool NeverUpdate() => false; /// /// Given a Belief instance, @@ -18,21 +26,21 @@ public class BeliefTests /// Then it is implicitly converted to its observation type. /// [Fact] - public void Belief_AssignedToObservationType_IsImplicitlyConvertedToObservationType() + public void Belief_AssignedToObservationType_IsCorrectlyImplicitlyConvertedToObservationType() { // Arrange - string reference = "def"; - Belief belief = new(reference, ID); + string def = "def"; + Belief belief = new(def, reference => reference); // Act string observation = belief; // Assert - Assert.Equal("def", observation); + Assert.Equal(def, observation); } /// - /// Given a Belief instance with an updateIf condition that is satisfied, + /// Given a Belief instance with an shouldUpdate condition that is satisfied, /// When UpdateBelief is called, /// Then the observation is updated. /// @@ -41,18 +49,18 @@ public void UpdateBelief_ShouldUpdateConditionIsSatisfied_UpdatesObservation() { // Arrange List list = []; - Belief, int> belief = new(list, GetCount, TrueShouldUpdate); + Belief, int> belief = new(list, reference => reference.Count, AlwaysUpdate); // Act list.Add(69); belief.UpdateBelief(); // Assert - Assert.Equal(1, belief); + Assert.Equal(list.Count, belief); } /// - /// Given a Belief instance with an updateIf condition that is not satisfied, + /// Given a Belief instance with an shouldUpdate condition that is not satisfied, /// When UpdateBelief is called, /// Then the observation is not updated. /// @@ -61,14 +69,14 @@ public void UpdateBelief_ShouldUpdateConditionIsNotSatisfied_DoesNotUpdateObserv { // Arrange List list = []; - Belief, int> belief = new(list, GetCount, FalseShouldUpdate); + Belief, int> belief = new(list, reference => reference.Count, NeverUpdate); // Act - list.Add(69); + list.Add(420); belief.UpdateBelief(); // Assert - Assert.Equal(0, belief); + Assert.NotEqual(list.Count, belief); } /// @@ -81,7 +89,7 @@ public void UpdateBelief_ReferenceIsAssignedTo_DoesNotUpdateObservation() { // Arrange string def = "def"; - Belief belief = new(def, ID, TrueShouldUpdate); + Belief belief = new(def, reference => reference, AlwaysUpdate); // Act def = "abc"; diff --git a/Aplib.Tests/Core/Belief/BeliefsetTests.cs b/Aplib.Tests/Core/Belief/BeliefsetTests.cs index 2b8acef4..941763e0 100644 --- a/Aplib.Tests/Core/Belief/BeliefsetTests.cs +++ b/Aplib.Tests/Core/Belief/BeliefsetTests.cs @@ -1,6 +1,10 @@ using Aplib.Core.Belief; namespace Aplib.Tests.Core.Belief; + +/// +/// Describes a set of tests for the class. +/// public class BeliefSetTests { /// From 6dbd151c3da01acfe964b431f05e3acf3e8ef526 Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Sun, 24 Mar 2024 20:53:02 +0100 Subject: [PATCH 15/21] chore: correct test comments --- Aplib.Tests/Core/Belief/BeliefsetTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Aplib.Tests/Core/Belief/BeliefsetTests.cs b/Aplib.Tests/Core/Belief/BeliefsetTests.cs index 941763e0..36cb2928 100644 --- a/Aplib.Tests/Core/Belief/BeliefsetTests.cs +++ b/Aplib.Tests/Core/Belief/BeliefsetTests.cs @@ -30,7 +30,7 @@ public void UpdateBeliefs_PublicBeliefFields_UpdatesAllBeliefs() /// /// Given a BeliefSet instance with multiple public property beliefs, /// When UpdateBeliefs is called, - /// Then all beliefs are updated. + /// Then no beliefs are updated. /// [Fact] public void UpdateBeliefs_PublicBeliefProperties_DoesNotUpdateAnyBeliefs() @@ -50,7 +50,7 @@ public void UpdateBeliefs_PublicBeliefProperties_DoesNotUpdateAnyBeliefs() /// /// Given a BeliefSet instance with multiple private field beliefs, /// When UpdateBeliefs is called, - /// Then all beliefs are updated. + /// Then no beliefs are updated. /// [Fact] public void UpdateBeliefs_PrivateBeliefFields_DoesNotUpdateAnyBeliefs() From 82aafb8cab7320736f8b1ade870f0605a9a7090e Mon Sep 17 00:00:00 2001 From: Jens Steenmetz <35835627+JensSteenmetz@users.noreply.github.com> Date: Sun, 24 Mar 2024 20:57:58 +0100 Subject: [PATCH 16/21] chore: rename Beliefset.cs to BeliefSet.cs --- Aplib.Core/Belief/{Beliefset.cs => BeliefSet.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Aplib.Core/Belief/{Beliefset.cs => BeliefSet.cs} (100%) diff --git a/Aplib.Core/Belief/Beliefset.cs b/Aplib.Core/Belief/BeliefSet.cs similarity index 100% rename from Aplib.Core/Belief/Beliefset.cs rename to Aplib.Core/Belief/BeliefSet.cs From 611d874f498cc431583c70cbe51efb1800b2ceeb Mon Sep 17 00:00:00 2001 From: Jens Steenmetz <35835627+JensSteenmetz@users.noreply.github.com> Date: Sun, 24 Mar 2024 20:58:39 +0100 Subject: [PATCH 17/21] chore: rename BeliefsetTests.cs to BeliefSetTests.cs --- Aplib.Tests/Core/Belief/{BeliefsetTests.cs => BeliefSetTests.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Aplib.Tests/Core/Belief/{BeliefsetTests.cs => BeliefSetTests.cs} (100%) diff --git a/Aplib.Tests/Core/Belief/BeliefsetTests.cs b/Aplib.Tests/Core/Belief/BeliefSetTests.cs similarity index 100% rename from Aplib.Tests/Core/Belief/BeliefsetTests.cs rename to Aplib.Tests/Core/Belief/BeliefSetTests.cs From ab98d40045148775da1d5ff2a102faf20f22a81e Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Mon, 25 Mar 2024 16:05:22 +0100 Subject: [PATCH 18/21] chore: remove Believe folder --- Aplib.Core/Believe/BeliefSet.cs | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 Aplib.Core/Believe/BeliefSet.cs diff --git a/Aplib.Core/Believe/BeliefSet.cs b/Aplib.Core/Believe/BeliefSet.cs deleted file mode 100644 index 2fc254f5..00000000 --- a/Aplib.Core/Believe/BeliefSet.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Aplib.Core.Believe -{ - /// - /// This is a summary - /// - public class BeliefSet - { - /// - /// This is merely here to allow some unit test - /// - public string State = "kaas"; - } -} From 24b63da4df73750cabed4f7b6105e866d3f99c79 Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Mon, 25 Mar 2024 16:58:39 +0100 Subject: [PATCH 19/21] fix: use actual BeliefSet in Goals --- .../Desire/Goals/CommonHeuristicFunctions.cs | 2 +- Aplib.Core/Desire/Goals/Goal.cs | 2 +- Aplib.Tests/Desire/GoalTests.cs | 43 ++++++++++++++++--- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/Aplib.Core/Desire/Goals/CommonHeuristicFunctions.cs b/Aplib.Core/Desire/Goals/CommonHeuristicFunctions.cs index f203a3cb..17479bc1 100644 --- a/Aplib.Core/Desire/Goals/CommonHeuristicFunctions.cs +++ b/Aplib.Core/Desire/Goals/CommonHeuristicFunctions.cs @@ -1,4 +1,4 @@ -using Aplib.Core.Believe; +using Aplib.Core.Belief; using System; namespace Aplib.Core.Desire.Goals diff --git a/Aplib.Core/Desire/Goals/Goal.cs b/Aplib.Core/Desire/Goals/Goal.cs index 75510b59..0fca6422 100644 --- a/Aplib.Core/Desire/Goals/Goal.cs +++ b/Aplib.Core/Desire/Goals/Goal.cs @@ -1,4 +1,4 @@ -using Aplib.Core.Believe; +using Aplib.Core.Belief; using Aplib.Core.Intent.Tactics; using System; diff --git a/Aplib.Tests/Desire/GoalTests.cs b/Aplib.Tests/Desire/GoalTests.cs index 24b87d6f..a59b4d70 100644 --- a/Aplib.Tests/Desire/GoalTests.cs +++ b/Aplib.Tests/Desire/GoalTests.cs @@ -1,4 +1,4 @@ -using Aplib.Core.Believe; +using Aplib.Core.Belief; using Aplib.Core.Desire.Goals; using Aplib.Core.Intent.Tactics; using Aplib.Tests.Stubs.Desire; @@ -61,7 +61,7 @@ public void Goal_WhenConstructed_DidNotIterateYet() public void Goal_WhenReached_ReturnsAsCompleted() { // Arrange - BeliefSet beliefSet = new(); + MyBeliefSet beliefSet = new(); Goal.HeuristicFunction heuristicFunction = CommonHeuristicFunctions.Completed(); // Act @@ -81,7 +81,7 @@ public void Goal_WhenReached_ReturnsAsCompleted() public void Goal_WhenNotReached_DoesNotReturnAsCompleted() { // Arrange - BeliefSet beliefSet = new(); + MyBeliefSet beliefSet = new(); Goal.HeuristicFunction heuristicFunction = CommonHeuristicFunctions.Uncompleted(); // Act @@ -101,15 +101,14 @@ public void Goal_WhenNotReached_DoesNotReturnAsCompleted() public void Goal_WhereEvaluationIsPerformed_DoesNotInfluenceBelieveSet() { // Arrange - BeliefSet beliefSet = new(); + MyBeliefSet beliefSet = new(); // Act - string currentBelieveSetState = beliefSet.State; Goal goal = new TestGoalBuilder().Build(); _ = goal.IsCompleted(beliefSet); // Assert - beliefSet.State.Should().Be(currentBelieveSetState); + beliefSet.MyBelief.Updated.Should().Be(false); } /// @@ -135,11 +134,41 @@ public void GoalConstructor_WhereHeuristicFunctionTypeDiffers_HasEqualBehaviour( Goal goalNonBoolean = new(tactic, heuristicFunctionNonBoolean, name, description); // Act - BeliefSet beliefSet = new(); + MyBeliefSet beliefSet = new(); bool goalBooleanEvaluation = goalBoolean.IsCompleted(beliefSet); bool goalNonBooleanEvaluation = goalNonBoolean.IsCompleted(beliefSet); // Assert goalBooleanEvaluation.Should().Be(goalNonBooleanEvaluation); } + + /// + /// A test belief set that contains two public simple beliefs. + /// + private class MyBeliefSet : BeliefSet + { + /// + /// Belief that sets Updated to true when UpdateBelief is called. + /// + public SimpleBelief MyBelief = new(); + } + + /// + /// A simple belief that can be used to test whether has been called. + /// + private class SimpleBelief : IBelief + { + /// + /// Stores whether has been called. + /// + public bool Updated { get; private set; } = false; + + /// + /// Sets to true. + /// + public void UpdateBelief() + { + Updated = true; + } + } } From adfe5c78a90434184f90bb667531cd92868dd665 Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Mon, 25 Mar 2024 22:48:33 +0100 Subject: [PATCH 20/21] feat: add IBeliefSet --- Aplib.Core/Belief/BeliefSet.cs | 7 +++++-- Aplib.Core/Belief/IBeliefSet.cs | 13 +++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 Aplib.Core/Belief/IBeliefSet.cs diff --git a/Aplib.Core/Belief/BeliefSet.cs b/Aplib.Core/Belief/BeliefSet.cs index fd97a0cf..d3f40863 100644 --- a/Aplib.Core/Belief/BeliefSet.cs +++ b/Aplib.Core/Belief/BeliefSet.cs @@ -7,8 +7,11 @@ namespace Aplib.Core.Belief /// All public fields of type that are defined in the inheriting class /// are automatically updated when calling . /// - public abstract class BeliefSet + public abstract class BeliefSet : IBeliefSet { + /// + /// An array storing all public fields of type that are defined in the inheriting class. + /// private readonly IBelief[] _beliefs; /// @@ -26,7 +29,7 @@ protected BeliefSet() } /// - /// Updates all objects of type that are defined as public fields in . + /// Updates all objects of type that are defined as public fields in the inheriting class. /// public void UpdateBeliefs() { diff --git a/Aplib.Core/Belief/IBeliefSet.cs b/Aplib.Core/Belief/IBeliefSet.cs new file mode 100644 index 00000000..f7268571 --- /dev/null +++ b/Aplib.Core/Belief/IBeliefSet.cs @@ -0,0 +1,13 @@ +namespace Aplib.Core.Belief +{ + /// + /// A belief set defines beliefs for an agent. + /// + public interface IBeliefSet + { + /// + /// Updates all beliefs in the belief set. + /// + void UpdateBeliefs(); + } +} From 22e49867336f70db705b9888e42288b29e51c51b Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Mon, 25 Mar 2024 23:07:29 +0100 Subject: [PATCH 21/21] chore: rename believe to belief in comments --- Aplib.Tests/Desire/GoalTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Aplib.Tests/Desire/GoalTests.cs b/Aplib.Tests/Desire/GoalTests.cs index a59b4d70..09522ed2 100644 --- a/Aplib.Tests/Desire/GoalTests.cs +++ b/Aplib.Tests/Desire/GoalTests.cs @@ -93,9 +93,9 @@ public void Goal_WhenNotReached_DoesNotReturnAsCompleted() } /// - /// Given a valid goal and believe, + /// Given a valid goal and belief, /// when the goal's heuristic function is evaluated, - /// the believeset is not altered + /// the beliefset is not altered /// [Fact] public void Goal_WhereEvaluationIsPerformed_DoesNotInfluenceBelieveSet()