Skip to content

Commit

Permalink
removed networking from base agent
Browse files Browse the repository at this point in the history
  • Loading branch information
MattSScott committed Sep 21, 2023
1 parent a5dfd62 commit f30a313
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 58 deletions.
11 changes: 0 additions & 11 deletions pkg/main/BaseAgent/agentInterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,9 @@ import (
"github.com/google/uuid"
)

type IAgentNetworking[T any] interface {
// returns the full agent network map
GetNetwork() map[uuid.UUID]T
// adds an agent object to the network
AddAgentToNetwork(agent T)
// removes an agent object from the network
RemoveAgentFromNetwork(agent T)
}

type IAgent[T any] interface {
// composes messaging passing capabilities
message.IAgentMessenger[T]
// handles network operations
IAgentNetworking[T]
// returns the unique ID of an agent
GetID() uuid.UUID
// allows agent to update their internal state
Expand Down
25 changes: 2 additions & 23 deletions pkg/main/BaseAgent/baseAgent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
)

type BaseAgent[T IAgent[T]] struct {
id uuid.UUID
network map[uuid.UUID]T
id uuid.UUID
}

func (ba *BaseAgent[T]) GetID() uuid.UUID {
Expand All @@ -18,31 +17,11 @@ func (ba *BaseAgent[T]) UpdateAgentInternalState() {}

func NewAgent[T IAgent[T]]() *BaseAgent[T] {
return &BaseAgent[T]{
id: uuid.New(),
network: make(map[uuid.UUID]T),
}
}

func DefaultAgent[T IAgent[T]]() IAgent[T] {
return &BaseAgent[T]{
id: uuid.New(),
network: make(map[uuid.UUID]T),
id: uuid.New(),
}
}

func (ba *BaseAgent[T]) GetAllMessages(listOfAgents []T) []message.IMessage[T] {

return []message.IMessage[T]{}
}

func (ba *BaseAgent[T]) GetNetwork() map[uuid.UUID]T {
return ba.network
}

func (ba *BaseAgent[T]) AddAgentToNetwork(agent T) {
ba.network[agent.GetID()] = agent
}

func (ba *BaseAgent[T]) RemoveAgentFromNetwork(agent T) {
delete(ba.network, agent.GetID())
}
25 changes: 1 addition & 24 deletions pkg/main/BaseAgent/baseAgent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,14 @@ type IBaseAgent interface {
baseagent.IAgent[IBaseAgent]
}

func TestAgentIDOperations(t *testing.T) {
func TestAgentIdOperations(t *testing.T) {
baseAgent := baseagent.NewAgent[IBaseAgent]()

if baseAgent.GetID() == uuid.Nil {
t.Error("Agent not instantiated with valid ID")
}
}

func TestAddToNetwork(t *testing.T) {
agent1 := baseagent.NewAgent[IBaseAgent]()
agent2 := baseagent.NewAgent[IBaseAgent]()

agent1.AddAgentToNetwork(agent2)

if len(agent1.GetNetwork()) != 1 {
t.Error("Agent not correctly added to map")
}
}

func TestRemoveFromNetwork(t *testing.T) {
agent1 := baseagent.NewAgent[IBaseAgent]()
agent2 := baseagent.NewAgent[IBaseAgent]()

agent1.AddAgentToNetwork(agent2)
agent1.RemoveAgentFromNetwork(agent2)

if len(agent1.GetNetwork()) != 0 {
t.Error("Agent not correctly removed from map")
}
}

type AgentWithState struct {
*baseagent.BaseAgent[*AgentWithState]
state int
Expand Down
10 changes: 10 additions & 0 deletions pkg/testing/testAgents/baseExtendedAgent/baseExtendedAgent.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ func (ag *BaseExtendedAgent) GetPhrase() string {
return ag.phrase
}

func (ag *BaseExtendedAgent) HandleGreetingMessage(GreetingMessage) {}

func GetAgent(phrase string) *BaseExtendedAgent {
return &BaseExtendedAgent{
BaseAgent: baseAgent.NewAgent[IExtendedAgent](),
phrase: phrase,
}

}

func GetDefaultAgent(phrase string) IExtendedAgent {
return &BaseExtendedAgent{
BaseAgent: baseAgent.NewAgent[IExtendedAgent](),
phrase: phrase,
}

}

0 comments on commit f30a313

Please sign in to comment.