Skip to content

Commit

Permalink
work on Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svencc committed Sep 17, 2023
1 parent a4ae992 commit 0d1fc77
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 26 deletions.
46 changes: 33 additions & 13 deletions src/main/java/lib/gecom/agent/GeAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ public class GeAgent {

@NonNull
private final List<GeAction> possibleActions = new ArrayList<>();

@NonNull
private final PriorityQueue<GeGoal> goals = new PriorityQueue<>();

@NonNull
private final HashMap<String, Integer> agentsBelieves = new HashMap<>();
@NonNull
Expand All @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/lib/gecom/agent/GeAgentFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public static GeAgent createAgent(@NonNull final GePlanner planner) {
);
fsm.getStates().addAll(states);
agent.setFsm(fsm);
agent.start();

return agent;
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/lib/gecom/agent/GeFSM.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,7 +51,7 @@ public void start() throws IllegalStateException {

((Startable) currentState).start();
currentState.enter();
hasStarted = true;
running = true;
}
}

Expand All @@ -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<FSMState> stoppableCandidates = states.stream()
Expand All @@ -80,7 +81,7 @@ public void stop() throws IllegalStateException {
currentState = stopableState;
currentState.exit();
((Stoppable) currentState).stop();
hasStopped = true;
halted = true;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/lib/gecom/agent/GeAgentFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ void createAgent_simpleTest() {

// Assert
assertNotNull(geAgent);
assertFalse(geAgent.wasStarted());
assertFalse(geAgent.wasStopped());
}

}
36 changes: 29 additions & 7 deletions src/test/java/lib/gecom/agent/GeAgentTest.java
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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"));
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}

}
7 changes: 7 additions & 0 deletions src/test/java/lib/gecom/agent/GeFSMTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package lib.gecom.agent;

import static org.junit.jupiter.api.Assertions.*;

class GeFSMTest {

}

0 comments on commit 0d1fc77

Please sign in to comment.