diff --git a/pkg/main/BaseAgent/baseAgent.go b/pkg/main/BaseAgent/baseAgent.go index 6e0195b..457ee80 100644 --- a/pkg/main/BaseAgent/baseAgent.go +++ b/pkg/main/BaseAgent/baseAgent.go @@ -15,7 +15,7 @@ func (ba *BaseAgent[T]) GetID() uuid.UUID { func (ba *BaseAgent[T]) UpdateAgentInternalState() {} -func NewAgent[T IAgent[T]]() *BaseAgent[T] { +func NewBaseAgent[T IAgent[T]]() *BaseAgent[T] { return &BaseAgent[T]{ id: uuid.New(), } diff --git a/pkg/main/BaseAgent/baseAgent_test.go b/pkg/main/BaseAgent/baseAgent_test.go index 6e3a0d8..6766a9e 100644 --- a/pkg/main/BaseAgent/baseAgent_test.go +++ b/pkg/main/BaseAgent/baseAgent_test.go @@ -12,7 +12,7 @@ type IBaseAgent interface { } func TestAgentIdOperations(t *testing.T) { - baseAgent := baseagent.NewAgent[IBaseAgent]() + baseAgent := baseagent.NewBaseAgent[IBaseAgent]() if baseAgent.GetID() == uuid.Nil { t.Error("Agent not instantiated with valid ID") @@ -30,7 +30,7 @@ func (aws *AgentWithState) UpdateAgentInternalState() { func TestUpdateAgentInternalState(t *testing.T) { ag := AgentWithState{ - baseagent.NewAgent[*AgentWithState](), + baseagent.NewBaseAgent[*AgentWithState](), 0, } @@ -47,7 +47,7 @@ func TestUpdateAgentInternalState(t *testing.T) { func TestMessageRetrieval(t *testing.T) { - agent := baseagent.NewAgent[IBaseAgent]() + agent := baseagent.NewBaseAgent[IBaseAgent]() messages := agent.GetAllMessages([]IBaseAgent{agent}) diff --git a/pkg/main/BaseServer/baseServer.go b/pkg/main/BaseServer/baseServer.go index 17b27af..7c7361b 100644 --- a/pkg/main/BaseServer/baseServer.go +++ b/pkg/main/BaseServer/baseServer.go @@ -8,38 +8,38 @@ import ( ) type BaseServer[T baseagent.IAgent[T]] struct { - numTurns int - agents map[uuid.UUID]T + agentMap map[uuid.UUID]T + iterations int } -func (bs *BaseServer[T]) GetAgents() map[uuid.UUID]T { - return bs.agents +func (bs *BaseServer[T]) GetAgentMap() map[uuid.UUID]T { + return bs.agentMap } func (bs *BaseServer[T]) AddAgent(agentToAdd T) { - bs.agents[agentToAdd.GetID()] = agentToAdd + bs.agentMap[agentToAdd.GetID()] = agentToAdd } func (bs *BaseServer[T]) RemoveAgent(agentToAdd T) { - delete(bs.agents, agentToAdd.GetID()) + delete(bs.agentMap, agentToAdd.GetID()) } -func (bs *BaseServer[T]) GetNumTurns() int { - return bs.numTurns +func (bs *BaseServer[T]) GetIterations() int { + return bs.iterations } func (bs *BaseServer[T]) RunGameLoop() { - for _, agent := range bs.agents { - fmt.Printf("Agent %s updating state \n", agent.GetID()) + for id, agent := range bs.agentMap { + fmt.Printf("Agent %s updating state \n", id) agent.UpdateAgentInternalState() } } func (bs *BaseServer[T]) Start() { - fmt.Printf("Server initialised with %d agents \n", len(bs.agents)) + fmt.Printf("Server initialised with %d agents \n", len(bs.agentMap)) fmt.Print("\n") //LOOPS - for i := 0; i < bs.numTurns; i++ { + for i := 0; i < bs.iterations; i++ { fmt.Printf("Game Loop %d running... \n \n", i) fmt.Printf("Main game loop running... \n \n") bs.RunGameLoop() @@ -68,10 +68,10 @@ func MakeAgentGeneratorCountPair[T baseagent.IAgent[T]](generatorFunction AgentG func (bs *BaseServer[T]) GenerateAgentArrayFromMap() []T { - agentMapToArray := make([]T, len(bs.agents)) + agentMapToArray := make([]T, len(bs.agentMap)) i := 0 - for _, ag := range bs.agents { + for _, ag := range bs.agentMap { agentMapToArray[i] = ag i++ } @@ -82,7 +82,7 @@ func (bs *BaseServer[T]) RunMessagingSession() { agentArray := bs.GenerateAgentArrayFromMap() - for _, agent := range bs.agents { + for _, agent := range bs.agentMap { allMessages := agent.GetAllMessages(agentArray) for _, msg := range allMessages { recipients := msg.GetRecipients() @@ -108,11 +108,11 @@ func (bs *BaseServer[T]) initialiseAgents(m []AgentGeneratorCountPair[T]) { } // generate a server instance based on a mapping function and number of iterations -func CreateServer[T baseagent.IAgent[T]](mapper []AgentGeneratorCountPair[T], iterations int) *BaseServer[T] { +func CreateServer[T baseagent.IAgent[T]](generatorArray []AgentGeneratorCountPair[T], iterations int) *BaseServer[T] { serv := &BaseServer[T]{ - agents: make(map[uuid.UUID]T), - numTurns: iterations, + agentMap: make(map[uuid.UUID]T), + iterations: iterations, } - serv.initialiseAgents(mapper) + serv.initialiseAgents(generatorArray) return serv } diff --git a/pkg/main/BaseServer/baseServer_test.go b/pkg/main/BaseServer/baseServer_test.go index b52eb63..13d78b5 100644 --- a/pkg/main/BaseServer/baseServer_test.go +++ b/pkg/main/BaseServer/baseServer_test.go @@ -18,7 +18,7 @@ type TestBaseAgent struct { func NewTestBaseAgent() ITestBaseAgent { return &TestBaseAgent{ - baseagent.NewAgent[ITestBaseAgent](), + baseagent.NewBaseAgent[ITestBaseAgent](), } } @@ -28,20 +28,20 @@ func TestAgentsCorrectlyInstantiated(t *testing.T) { server := baseserver.CreateServer[ITestBaseAgent](m, 1) - if len(server.GetAgents()) != 3 { + if len(server.GetAgentMap()) != 3 { t.Error("Incorrect number of agents added to server") } } -func TestNumTurnsInServer(t *testing.T) { +func TestNumIterationsInServer(t *testing.T) { m := make([]baseserver.AgentGeneratorCountPair[ITestBaseAgent], 1) m[0] = baseserver.MakeAgentGeneratorCountPair[ITestBaseAgent](NewTestBaseAgent, 3) server := baseserver.CreateServer[ITestBaseAgent](m, 1) - if server.GetNumTurns() != 1 { - t.Error("Incorrect number of turns instantiated") + if server.GetIterations() != 1 { + t.Error("Incorrect number of iterations instantiated") } } @@ -77,11 +77,11 @@ func TestAddAgent(t *testing.T) { baseServer := baseserver.CreateServer[ITestBaseAgent]([]baseserver.AgentGeneratorCountPair[ITestBaseAgent]{}, 1) - agent1 := baseagent.NewAgent[ITestBaseAgent]() + agent1 := baseagent.NewBaseAgent[ITestBaseAgent]() baseServer.AddAgent(agent1) - if len(baseServer.GetAgents()) != 1 { + if len(baseServer.GetAgentMap()) != 1 { t.Error("Agent not correctly added to map") } } @@ -90,12 +90,12 @@ func TestRemoveAgent(t *testing.T) { baseServer := baseserver.CreateServer[ITestBaseAgent]([]baseserver.AgentGeneratorCountPair[ITestBaseAgent]{}, 1) - agent1 := baseagent.NewAgent[ITestBaseAgent]() + agent1 := baseagent.NewBaseAgent[ITestBaseAgent]() baseServer.AddAgent(agent1) baseServer.RemoveAgent(agent1) - if len(baseServer.GetAgents()) != 0 { + if len(baseServer.GetAgentMap()) != 0 { t.Error("Agent not correctly removed from map") } } @@ -103,10 +103,10 @@ func TestRemoveAgent(t *testing.T) { func TestFullAgentHashmap(t *testing.T) { baseServer := baseserver.CreateServer[ITestBaseAgent]([]baseserver.AgentGeneratorCountPair[ITestBaseAgent]{}, 1) for i := 0; i < 5; i++ { - baseServer.AddAgent(baseagent.NewAgent[ITestBaseAgent]()) + baseServer.AddAgent(baseagent.NewBaseAgent[ITestBaseAgent]()) } - for id, agent := range baseServer.GetAgents() { + for id, agent := range baseServer.GetAgentMap() { if agent.GetID() != id { t.Error("Server agent hashmap key doesn't match object") } diff --git a/pkg/main/BaseServer/serverInterface.go b/pkg/main/BaseServer/serverInterface.go index af91734..548db88 100644 --- a/pkg/main/BaseServer/serverInterface.go +++ b/pkg/main/BaseServer/serverInterface.go @@ -7,7 +7,7 @@ import ( type IAgentOperations[T baseagent.IAgent[T]] interface { // gives access to the agents in the simulator - GetAgents() map[uuid.UUID]T + GetAgentMap() map[uuid.UUID]T // adds an agent to the server AddAgent(agentToAdd T) // removes an agent from the server @@ -17,12 +17,12 @@ type IAgentOperations[T baseagent.IAgent[T]] interface { } type IServer[T baseagent.IAgent[T]] interface { + // gives operations for adding/removing agents from the simulator + IAgentOperations[T] + // gives access to number of iteration in simulator + GetIterations() int // the set of functions defining how a 'game loop' should run RunGameLoop() // begins simulator Start() - // gives operations for adding/removing agents from the simulator - IAgentOperations[T] - // gives access to number of iteration in simulator - GetNumTurns() int } diff --git a/pkg/testing/testAgents/baseExtendedAgent/baseExtendedAgent.go b/pkg/testing/testAgents/baseExtendedAgent/baseExtendedAgent.go index 4be9a33..b887557 100644 --- a/pkg/testing/testAgents/baseExtendedAgent/baseExtendedAgent.go +++ b/pkg/testing/testAgents/baseExtendedAgent/baseExtendedAgent.go @@ -25,17 +25,18 @@ func (ag *BaseExtendedAgent) GetPhrase() string { func (ag *BaseExtendedAgent) HandleGreetingMessage(GreetingMessage) {} -func GetAgent(phrase string) *BaseExtendedAgent { +func GetBaseAgent(phrase string) *BaseExtendedAgent { return &BaseExtendedAgent{ - BaseAgent: baseAgent.NewAgent[IExtendedAgent](), + BaseAgent: baseAgent.NewBaseAgent[IExtendedAgent](), phrase: phrase, } } -func GetDefaultAgent(phrase string) IExtendedAgent { +//returns a default implementation of IExtendedAgent +func GetNewIExtendedAgent(phrase string) IExtendedAgent { return &BaseExtendedAgent{ - BaseAgent: baseAgent.NewAgent[IExtendedAgent](), + BaseAgent: baseAgent.NewBaseAgent[IExtendedAgent](), phrase: phrase, } diff --git a/pkg/testing/testAgents/baseExtendedAgent/baseExtendedAgent_test.go b/pkg/testing/testAgents/baseExtendedAgent/baseExtendedAgent_test.go new file mode 100644 index 0000000..2a0e8e5 --- /dev/null +++ b/pkg/testing/testAgents/baseExtendedAgent/baseExtendedAgent_test.go @@ -0,0 +1,24 @@ +package baseExtendedAgent_test + +import ( + "testing" + + baseExtendedAgent "github.com/MattSScott/basePlatformSOMAS/pkg/testing/testAgents/baseExtendedAgent" + "github.com/google/uuid" + + +) + +func TestGetNewIExtendedAgent(t *testing.T) { + iagent := baseExtendedAgent.GetNewIExtendedAgent("teststring") + phrase := iagent.GetPhrase() + id := iagent.GetID() + + if phrase != "teststring"{ + t.Error("Unsuccessful default implementation of IExtendedAgent due to string mismatch") + } + + if id == uuid.Nil { + t.Error("Unsuccessful default implementation of IExtendedAgent due to Nil ID") + } +} \ No newline at end of file diff --git a/pkg/testing/testAgents/helloAgent/helloAgent.go b/pkg/testing/testAgents/helloAgent/helloAgent.go index 1e64bbd..1a20374 100644 --- a/pkg/testing/testAgents/helloAgent/helloAgent.go +++ b/pkg/testing/testAgents/helloAgent/helloAgent.go @@ -34,6 +34,6 @@ func (ha *HelloAgent) HandleGreetingMessage(msg baseExtendedAgent.GreetingMessag func GetHelloAgent() baseExtendedAgent.IExtendedAgent { return &HelloAgent{ - BaseExtendedAgent: baseExtendedAgent.GetAgent("hello"), + BaseExtendedAgent: baseExtendedAgent.GetBaseAgent("hello"), } } diff --git a/pkg/testing/testAgents/worldAgent/worldAgent.go b/pkg/testing/testAgents/worldAgent/worldAgent.go index 01e1d69..b3c9d0a 100644 --- a/pkg/testing/testAgents/worldAgent/worldAgent.go +++ b/pkg/testing/testAgents/worldAgent/worldAgent.go @@ -34,6 +34,6 @@ func (wa *WorldAgent) HandleGreetingMessage(msg baseExtendedAgent.GreetingMessag func GetWorldAgent() baseExtendedAgent.IExtendedAgent { return &WorldAgent{ - BaseExtendedAgent: baseExtendedAgent.GetAgent("wello"), + BaseExtendedAgent: baseExtendedAgent.GetBaseAgent("wello"), } } diff --git a/pkg/testing/testServer/server_test.go b/pkg/testing/testServer/server_test.go index 07f220d..09cc0f4 100644 --- a/pkg/testing/testServer/server_test.go +++ b/pkg/testing/testServer/server_test.go @@ -21,7 +21,7 @@ func TestInheritedServer(t *testing.T) { floors := 3 ts := testserver.New(m, floors) - if len(ts.GetAgents()) != 5 { + if len(ts.GetAgentMap()) != 5 { t.Error("Agents not properly instantiated") } @@ -43,11 +43,11 @@ func TestServerInterfaceComposition(t *testing.T) { // server can also be declared as type interface var server testserver.IExtendedServer = testserver.New(m, 1) - if len(server.GetAgents()) != 2 { + if len(server.GetAgentMap()) != 2 { t.Error("Agents not properly instantiated") } - for _, agent := range server.GetAgents() { + for _, agent := range server.GetAgentMap() { if agent.GetID() == uuid.Nil { t.Error("Agent types not correctly instantiated") } diff --git a/pkg/testing/testServer/testServer.go b/pkg/testing/testServer/testServer.go index 1063b6b..bf88c3f 100644 --- a/pkg/testing/testServer/testServer.go +++ b/pkg/testing/testServer/testServer.go @@ -26,7 +26,7 @@ func New(mapper []baseserver.AgentGeneratorCountPair[baseExtendedAgent.IExtended } func (s *TestServer) RunAdditionalPhase() { - for _, agent := range s.GetAgents() { + for _, agent := range s.GetAgentMap() { agent.GetID() } } @@ -42,7 +42,7 @@ func (s *TestServer) RunGameLoop() { s.BaseServer.RunGameLoop() // able to call methods from the parametrised subclass too - for _, agent := range s.GetAgents() { + for _, agent := range s.GetAgentMap() { agent.GetPhrase() }