Skip to content

Commit

Permalink
Merge pull request #16 from MattSScott/variable-disambiguation
Browse files Browse the repository at this point in the history
rename `agents` to `agentMap`
  • Loading branch information
ADimoska authored Sep 22, 2023
2 parents d91f9aa + b7aae43 commit d805f8f
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 50 deletions.
2 changes: 1 addition & 1 deletion pkg/main/BaseAgent/baseAgent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/main/BaseAgent/baseAgent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -30,7 +30,7 @@ func (aws *AgentWithState) UpdateAgentInternalState() {

func TestUpdateAgentInternalState(t *testing.T) {
ag := AgentWithState{
baseagent.NewAgent[*AgentWithState](),
baseagent.NewBaseAgent[*AgentWithState](),
0,
}

Expand All @@ -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})

Expand Down
38 changes: 19 additions & 19 deletions pkg/main/BaseServer/baseServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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++
}
Expand All @@ -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()
Expand All @@ -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
}
22 changes: 11 additions & 11 deletions pkg/main/BaseServer/baseServer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type TestBaseAgent struct {

func NewTestBaseAgent() ITestBaseAgent {
return &TestBaseAgent{
baseagent.NewAgent[ITestBaseAgent](),
baseagent.NewBaseAgent[ITestBaseAgent](),
}
}

Expand All @@ -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")
}

}
Expand Down Expand Up @@ -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")
}
}
Expand All @@ -90,23 +90,23 @@ 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")
}
}

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")
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/main/BaseServer/serverInterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
9 changes: 5 additions & 4 deletions pkg/testing/testAgents/baseExtendedAgent/baseExtendedAgent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
24 changes: 24 additions & 0 deletions pkg/testing/testAgents/baseExtendedAgent/baseExtendedAgent_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
2 changes: 1 addition & 1 deletion pkg/testing/testAgents/helloAgent/helloAgent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
2 changes: 1 addition & 1 deletion pkg/testing/testAgents/worldAgent/worldAgent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
6 changes: 3 additions & 3 deletions pkg/testing/testServer/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/testing/testServer/testServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand All @@ -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()

}
Expand Down

0 comments on commit d805f8f

Please sign in to comment.