Skip to content

Commit

Permalink
Merge pull request #13 from MattSScott/server-rename-and-testing
Browse files Browse the repository at this point in the history
Server package rename and testing
  • Loading branch information
MattSScott authored Sep 21, 2023
2 parents e34922f + f30a313 commit 9055b98
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 37 deletions.
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/MattSScott/basePlatformSOMAS/pkg/main/server"
baseserver "github.com/MattSScott/basePlatformSOMAS/pkg/main/BaseServer"
"github.com/MattSScott/basePlatformSOMAS/pkg/testing/testAgents/baseExtendedAgent"
helloagent "github.com/MattSScott/basePlatformSOMAS/pkg/testing/testAgents/helloAgent"
worldagent "github.com/MattSScott/basePlatformSOMAS/pkg/testing/testAgents/worldAgent"
Expand All @@ -10,10 +10,10 @@ import (

func main() {

m := make([]server.AgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent], 5)
m := make([]baseserver.AgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent], 2)

m[0] = server.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](helloagent.GetHelloAgent, 3)
m[1] = server.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](worldagent.GetWorldAgent, 2)
m[0] = baseserver.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](helloagent.GetHelloAgent, 3)
m[1] = baseserver.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](worldagent.GetWorldAgent, 2)

iterations := 1
ts := testserver.New(m, iterations)
Expand Down
4 changes: 2 additions & 2 deletions pkg/main/BaseAgent/agentInterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
type IAgent[T any] interface {
// composes messaging passing capabilities
message.IAgentMessenger[T]
// allows agent to update their internal state
UpdateAgentInternalState()
// returns the unique ID of an agent
GetID() uuid.UUID
// allows agent to update their internal state
UpdateAgentInternalState()
}
17 changes: 4 additions & 13 deletions pkg/main/BaseAgent/baseAgent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,22 @@ import (
)

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

func (ba *BaseAgent[T]) GetID() uuid.UUID {
return ba.id
}

func (ba *BaseAgent[T]) UpdateAgentInternalState() {
}
func (ba *BaseAgent[T]) UpdateAgentInternalState() {}

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

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

func (ba *BaseAgent[T]) GetNetwork() []T {
return ba.network
}
func (ba *BaseAgent[T]) GetAllMessages(listOfAgents []T) []message.IMessage[T] {

func (ba *BaseAgent[T]) SetNetwork(newNetwork []T) {
ba.network = newNetwork
return []message.IMessage[T]{}
}
58 changes: 58 additions & 0 deletions pkg/main/BaseAgent/baseAgent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package baseagent_test

import (
"testing"

baseagent "github.com/MattSScott/basePlatformSOMAS/pkg/main/BaseAgent"
"github.com/google/uuid"
)

type IBaseAgent interface {
baseagent.IAgent[IBaseAgent]
}

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

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

type AgentWithState struct {
*baseagent.BaseAgent[*AgentWithState]
state int
}

func (aws *AgentWithState) UpdateAgentInternalState() {
aws.state += 1
}

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

if ag.state != 0 {
t.Error("Additional agent field not correctly instantiated")
}

ag.UpdateAgentInternalState()

if ag.state != 1 {
t.Error("Agent state not correctly updated")
}
}

func TestMessageRetrieval(t *testing.T) {

agent := baseagent.NewAgent[IBaseAgent]()

messages := agent.GetAllMessages([]IBaseAgent{agent})

if len(messages) > 0 {
t.Error("Agent erroneously constructed message")
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server
package baseserver

import (
"fmt"
Expand Down
91 changes: 91 additions & 0 deletions pkg/main/BaseServer/baseServer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package baseserver_test

import (
"testing"

baseagent "github.com/MattSScott/basePlatformSOMAS/pkg/main/BaseAgent"
baseserver "github.com/MattSScott/basePlatformSOMAS/pkg/main/BaseServer"
)

type ITestBaseAgent interface {
baseagent.IAgent[ITestBaseAgent]
}

type TestBaseAgent struct {
*baseagent.BaseAgent[ITestBaseAgent]
}

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

func TestAgentsCorrectlyInstantiated(t *testing.T) {
m := make([]baseserver.AgentGeneratorCountPair[ITestBaseAgent], 1)
m[0] = baseserver.MakeAgentGeneratorCountPair[ITestBaseAgent](NewTestBaseAgent, 3)

server := baseserver.CreateServer[ITestBaseAgent](m, 1)

if len(server.GetAgents()) != 3 {
t.Error("Incorrect number of agents added to server")
}

}

func TestNumTurnsInServer(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")
}

}

type IExtendedTestServer interface {
baseserver.IServer[ITestBaseAgent]
GetAdditionalField() int
}

type ExtendedTestServer struct {
*baseserver.BaseServer[ITestBaseAgent]
testField int
}

func (ets *ExtendedTestServer) GetAdditionalField() int {
return ets.testField
}

func (ets *ExtendedTestServer) RunGameLoop() {

ets.BaseServer.RunGameLoop()
ets.testField += 1
}

func CreateTestServer(mapper []baseserver.AgentGeneratorCountPair[ITestBaseAgent], iters int) IExtendedTestServer {
return &ExtendedTestServer{
BaseServer: baseserver.CreateServer[ITestBaseAgent](mapper, iters),
testField: 0,
}
}

func TestServerGameLoop(t *testing.T) {
m := make([]baseserver.AgentGeneratorCountPair[ITestBaseAgent], 1)
m[0] = baseserver.MakeAgentGeneratorCountPair[ITestBaseAgent](NewTestBaseAgent, 3)

server := CreateTestServer(m, 1)

if server.GetAdditionalField() != 0 {
t.Error("Additional server parameter not correctly instantiated")
}

server.RunGameLoop()

if server.GetAdditionalField() != 1 {
t.Error("Run Game Loop method not successfully overridden")
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server
package baseserver

import baseagent "github.com/MattSScott/basePlatformSOMAS/pkg/main/BaseAgent"

Expand Down
2 changes: 1 addition & 1 deletion pkg/main/messaging/agentMessenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package messaging
// // base interface structure used for message passing - can be composed for more complex message structures
type IAgentMessenger[T any] interface {
// produces a list of messages (of any common interface) that an agent wishes to pass
GetAllMessages([]T) []IMessage[T]
GetAllMessages(listOfAgents []T) []IMessage[T]
}
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,
}

}
20 changes: 10 additions & 10 deletions pkg/testing/testServer/server_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package testserver_test

import (
"github.com/MattSScott/basePlatformSOMAS/pkg/main/server"
baseserver "github.com/MattSScott/basePlatformSOMAS/pkg/main/BaseServer"
"github.com/MattSScott/basePlatformSOMAS/pkg/testing/testAgents/baseExtendedAgent"
helloagent "github.com/MattSScott/basePlatformSOMAS/pkg/testing/testAgents/helloAgent"
worldagent "github.com/MattSScott/basePlatformSOMAS/pkg/testing/testAgents/worldAgent"
Expand All @@ -13,10 +13,10 @@ import (

func TestInheritedServer(t *testing.T) {

m := make([]server.AgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent], 5)
m := make([]baseserver.AgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent], 2)

m[0] = server.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](helloagent.GetHelloAgent, 3)
m[1] = server.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](worldagent.GetWorldAgent, 2)
m[0] = baseserver.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](helloagent.GetHelloAgent, 3)
m[1] = baseserver.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](worldagent.GetWorldAgent, 2)

floors := 3
ts := testserver.New(m, floors)
Expand All @@ -35,10 +35,10 @@ func TestInheritedServer(t *testing.T) {

func TestServerInterfaceComposition(t *testing.T) {

m := make([]server.AgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent], 2)
m := make([]baseserver.AgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent], 2)

m[0] = server.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](helloagent.GetHelloAgent, 1)
m[1] = server.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](worldagent.GetWorldAgent, 1)
m[0] = baseserver.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](helloagent.GetHelloAgent, 1)
m[1] = baseserver.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](worldagent.GetWorldAgent, 1)

// server can also be declared as type interface
var server testserver.IExtendedServer = testserver.New(m, 1)
Expand All @@ -57,10 +57,10 @@ func TestServerInterfaceComposition(t *testing.T) {
}

func TestServerMessagePassing(t *testing.T) {
m := make([]server.AgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent], 5)
m := make([]baseserver.AgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent], 2)

m[0] = server.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](helloagent.GetHelloAgent, 1)
m[1] = server.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](worldagent.GetWorldAgent, 1)
m[0] = baseserver.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](helloagent.GetHelloAgent, 1)
m[1] = baseserver.MakeAgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent](worldagent.GetWorldAgent, 1)

floors := 3
ts := testserver.New(m, floors)
Expand Down
10 changes: 5 additions & 5 deletions pkg/testing/testServer/testServer.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package testserver

import (
baseServer "github.com/MattSScott/basePlatformSOMAS/pkg/main/server"
baseserver "github.com/MattSScott/basePlatformSOMAS/pkg/main/BaseServer"
"github.com/MattSScott/basePlatformSOMAS/pkg/testing/testAgents/baseExtendedAgent"
)

// the base server functionality can be extended with 'environment specific' functions
type IExtendedServer interface {
baseServer.IServer[baseExtendedAgent.IExtendedAgent]
baseserver.IServer[baseExtendedAgent.IExtendedAgent]
RunAdditionalPhase()
}

// composing the 'base server' allows access to the pre-made interface implementations
// new fields can be added to the extended server data structure
type TestServer struct {
*baseServer.BaseServer[baseExtendedAgent.IExtendedAgent]
*baseserver.BaseServer[baseExtendedAgent.IExtendedAgent]
name string
}

func New(mapper []baseServer.AgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent], iterations int) *TestServer {
func New(mapper []baseserver.AgentGeneratorCountPair[baseExtendedAgent.IExtendedAgent], iterations int) *TestServer {
return &TestServer{
BaseServer: baseServer.CreateServer[baseExtendedAgent.IExtendedAgent](mapper, iterations),
BaseServer: baseserver.CreateServer[baseExtendedAgent.IExtendedAgent](mapper, iterations),
name: "TestServer",
}
}
Expand Down

0 comments on commit 9055b98

Please sign in to comment.