From 0d1fc779bff1840a62abf1bb657b1d845cc70320 Mon Sep 17 00:00:00 2001 From: Sven Carrillo Castillo Date: Sun, 17 Sep 2023 13:43:33 +0200 Subject: [PATCH] work on Tests --- src/main/java/lib/gecom/agent/GeAgent.java | 46 +++++++++++++------ .../java/lib/gecom/agent/GeAgentFactory.java | 1 - src/main/java/lib/gecom/agent/GeFSM.java | 11 +++-- .../lib/gecom/agent/GeAgentFactoryTest.java | 2 + .../java/lib/gecom/agent/GeAgentTest.java | 36 ++++++++++++--- src/test/java/lib/gecom/agent/GeFSMTest.java | 7 +++ 6 files changed, 77 insertions(+), 26 deletions(-) create mode 100644 src/test/java/lib/gecom/agent/GeFSMTest.java diff --git a/src/main/java/lib/gecom/agent/GeAgent.java b/src/main/java/lib/gecom/agent/GeAgent.java index efba2322..d2915d54 100644 --- a/src/main/java/lib/gecom/agent/GeAgent.java +++ b/src/main/java/lib/gecom/agent/GeAgent.java @@ -17,10 +17,8 @@ public class GeAgent { @NonNull private final List possibleActions = new ArrayList<>(); - @NonNull private final PriorityQueue goals = new PriorityQueue<>(); - @NonNull private final HashMap agentsBelieves = new HashMap<>(); @NonNull @@ -33,26 +31,48 @@ public class GeAgent { @Nullable @Setter(AccessLevel.PACKAGE) private GeFSM fsm; - @Nullable private GeAction currentAction; - @Nullable private GeGoal currentGoal; - private boolean isExecutingAction = false; + private boolean isExecutingAction = false; // @TODO -> von FSM abfragen! + private boolean wasStarted = false; + private boolean wasStopped = false; -// GeAgent(@NonNull final GePlanner planner, @NonNull final GeFSM fsm) { -// this.planner = planner; -// this.fsm = fsm; -// } + public boolean wasStarted() { + return wasStarted; + } + + public boolean wasStopped() { + return wasStopped; + } - // Agents seem to implement these methods (Unity; maybe this will be useful for us later) - public void start() { + public boolean isRunning() throws IllegalStateException { + checkPreconditions(); + return fsm.isRunning(); + } + + private void checkPreconditions() throws IllegalStateException { + if (fsm == null) { + throw new IllegalStateException("FSM is not set"); + } + if (planner == null) { + throw new IllegalStateException("Planner is not set"); + } + } + + public void start() throws IllegalStateException { + checkPreconditions(); + wasStarted = true; fsm.start(); - // This is where the agent is initially set up. - // This can include initializing variables or setting up components. + } + + public void stop() throws IllegalStateException { + checkPreconditions(); + wasStopped = true; + fsm.stop(); } public void update() { diff --git a/src/main/java/lib/gecom/agent/GeAgentFactory.java b/src/main/java/lib/gecom/agent/GeAgentFactory.java index 58a20d67..f033f7fc 100644 --- a/src/main/java/lib/gecom/agent/GeAgentFactory.java +++ b/src/main/java/lib/gecom/agent/GeAgentFactory.java @@ -25,7 +25,6 @@ public static GeAgent createAgent(@NonNull final GePlanner planner) { ); fsm.getStates().addAll(states); agent.setFsm(fsm); - agent.start(); return agent; } diff --git a/src/main/java/lib/gecom/agent/GeFSM.java b/src/main/java/lib/gecom/agent/GeFSM.java index ce6c37cb..e9091a9b 100644 --- a/src/main/java/lib/gecom/agent/GeFSM.java +++ b/src/main/java/lib/gecom/agent/GeFSM.java @@ -25,10 +25,10 @@ public class GeFSM { private FSMState currentState; @Getter - private boolean hasStarted = false; + private boolean running = false; @Getter - private boolean hasStopped = false; + private boolean halted = false; public GeFSM(@NonNull final GeAgent agent) { this.agent = agent; @@ -51,7 +51,7 @@ public void start() throws IllegalStateException { ((Startable) currentState).start(); currentState.enter(); - hasStarted = true; + running = true; } } @@ -64,7 +64,8 @@ public void stop() throws IllegalStateException { if (currentState instanceof Stoppable) { currentState.exit(); ((Stoppable) currentState).stop(); - hasStopped = true; + running = false; + halted = true; } } else { final List stoppableCandidates = states.stream() @@ -80,7 +81,7 @@ public void stop() throws IllegalStateException { currentState = stopableState; currentState.exit(); ((Stoppable) currentState).stop(); - hasStopped = true; + halted = true; } } } diff --git a/src/test/java/lib/gecom/agent/GeAgentFactoryTest.java b/src/test/java/lib/gecom/agent/GeAgentFactoryTest.java index 0a956da9..38952886 100644 --- a/src/test/java/lib/gecom/agent/GeAgentFactoryTest.java +++ b/src/test/java/lib/gecom/agent/GeAgentFactoryTest.java @@ -18,6 +18,8 @@ void createAgent_simpleTest() { // Assert assertNotNull(geAgent); + assertFalse(geAgent.wasStarted()); + assertFalse(geAgent.wasStopped()); } } \ No newline at end of file diff --git a/src/test/java/lib/gecom/agent/GeAgentTest.java b/src/test/java/lib/gecom/agent/GeAgentTest.java index 871a9a5b..06f419d5 100644 --- a/src/test/java/lib/gecom/agent/GeAgentTest.java +++ b/src/test/java/lib/gecom/agent/GeAgentTest.java @@ -1,19 +1,37 @@ package lib.gecom.agent; import lib.gecom.TestAction; -import lib.gecom.action.GeAction; import lib.gecom.plan.GePlanner; import lib.gecom.stuff.GeGoal; import org.junit.jupiter.api.Test; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; class GeAgentTest { @Test - public void test() { + public void simpleStartStopTest() { + // Arrange + final GePlanner gePlanner = new GePlanner(); + final GeAgent agentToTest = GeAgentFactory.createAgent(gePlanner); + + // Act && Assert + assertFalse(agentToTest.wasStarted()); + assertFalse(agentToTest.wasStopped()); + agentToTest.start(); + assertTrue(agentToTest.wasStarted()); + + // Act && Assert + assertTrue(agentToTest.wasStarted()); + assertFalse(agentToTest.wasStopped()); + agentToTest.stop(); + assertTrue(agentToTest.wasStarted()); + assertTrue(agentToTest.wasStopped()); + } + + + @Test + public void testAgentToEat_withRecognizingHungry_generatePlan_andPerformPlan() { // Arrange final TestAction eat = new TestAction("eat"); eat.getPreconditions().put("hungry", 1); @@ -29,13 +47,17 @@ public void test() { agentToTest.getAgentsBelieves().put("hungry", 1); agentToTest.getGoals().add(getFull); - // Act + // Act && Assert agentToTest.start(); + + // Act && Assert agentToTest.update(); + // Act && Assert + agentToTest.stop(); + // Assert // assertEquals(0, agentToTest.getAgentsBelieves().get("hungry")); - // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< } } \ No newline at end of file diff --git a/src/test/java/lib/gecom/agent/GeFSMTest.java b/src/test/java/lib/gecom/agent/GeFSMTest.java new file mode 100644 index 00000000..587a00fc --- /dev/null +++ b/src/test/java/lib/gecom/agent/GeFSMTest.java @@ -0,0 +1,7 @@ +package lib.gecom.agent; + +import static org.junit.jupiter.api.Assertions.*; + +class GeFSMTest { + +} \ No newline at end of file