Skip to content

Commit

Permalink
restoring UU tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wooshrow committed Jul 20, 2023
1 parent b9dd889 commit 4a8495f
Show file tree
Hide file tree
Showing 14 changed files with 10,875 additions and 38 deletions.
5 changes: 3 additions & 2 deletions JvmClient/src/jvmMain/java/uuspaceagent/UUSeAgentState.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ WorldEntity agentAdditionalInfo(CharacterObservation obs) {
return agentWE ;
}

//@Override
public void updateState() {
@Override
public void updateState(String agentId) {

super.updateState(agentId);

// get the new WOM. Currently it does not include agent's extended properties, so we add them
// explicitly here:
WorldModel newWom = env().observe() ;
//System.out.println(">>>-- agent pos as received from SE:" + newWom.position);
// HACK: SE gives generated-id to the agent, replace that:
newWom.agentId = this.agentId ;
// HACK: because wom that comes from SE has its wom.elements read-only :|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import spaceEngineers.controller.SpaceEngineersTestContext;
import spaceEngineers.iv4xr.goal.TacticLib;
import spaceEngineers.model.*;
import uuspaceagent.TestUtils;

public class Test_SimpleInteractionsWithSE {

Expand Down Expand Up @@ -58,21 +59,28 @@ public void example_of_deploying_a_goal_to_SE() throws InterruptedException {

// set a goal ... does not matter what. This one cannot be solved.
GoalStructure G = goal("dummy").toSolve((SeAgentState S) -> false)
.withTactic(tactics.moveForward(1f))
.withTactic(tactics.moveForward())
.lift()
;
testAgent.setGoal(G) ;

int i = 0 ;
while(i<200) {
testAgent.update();
console("*** " + i + " " + myAgentState.wom.agentId + " @" + myAgentState.wom.position) ;
console("*** " + i + " "
+ myAgentState.worldmodel.agentId
+ " @" + myAgentState.worldmodel.position) ;
if(i==2) {
// debug here...
// System.out.println(">> debug") ;
}
Thread.sleep(10);
i++ ;
}
try {
controllerWrapper.close();
}
catch(Exception e) { }
}

double angle(double x, double z) {
Expand Down Expand Up @@ -121,12 +129,13 @@ public void test_env_interactions() throws InterruptedException {

int i = 0 ;
while(i<20) {
obs = theEnv.moveForward(5f) ;
//obs = theEnv.moveForward(5f) ;
obs = theEnv.moveForward() ;
if (i<20) {
theEnv.getController().getCharacter().moveAndRotate(new Vec3F(0,0,0), new Vec2F(0,20),0) ;
theEnv.getController().getCharacter().moveAndRotate(new Vec3F(0,0,0), new Vec2F(0,20),0,1) ;
}
else {
theEnv.getController().getCharacter().moveAndRotate(new Vec3F(0,0,-36), new Vec2F(0,0),0) ;
theEnv.getController().getCharacter().moveAndRotate(new Vec3F(0,0,-36), new Vec2F(0,0),0,1) ;
}
obs = theEnv.observe() ;
primObs = theEnv.getController().getObserver().observe() ;
Expand Down Expand Up @@ -172,6 +181,10 @@ public void test_env_interactions() throws InterruptedException {
}
i++ ;
}
try {
controllerWrapper.close();
}
catch(Exception e) { }

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
public class Coba_moveToward_and_yTurnToward {

void moveTo(UUSeAgentState state, Vec3 destination) {
state.updateState();
state.updateState(state.agentId);
for (int k = 0 ; k<40; k++) {
UUTacticLib.moveToward(state, destination,100) ;
state.updateState();
state.updateState(state.agentId);
float distance = Vec3.sub(destination,state.wom.position).length() ;
console(">>> dist to dest: " + distance);
if(distance <= 0.5) {
Expand All @@ -30,11 +30,11 @@ public void test_2DMoveTo1() throws InterruptedException {

var state = loadSE("myworld-3").snd;

state.updateState();
state.updateState(state.agentId);

state.updateState();
state.updateState(state.agentId);
state.navgrid.enableFlying = true ;
state.updateState();
state.updateState(state.agentId);

WorldEntity agentInfo = state.wom.elements.get(state.agentId);
console("** Agent's info: " + PrintInfos.showWorldEntity(agentInfo));
Expand All @@ -43,12 +43,12 @@ public void test_2DMoveTo1() throws InterruptedException {

//Vec3 destination = new Vec3(11.5f,-1.5f,60f) ;
console("#### to z+");
state.updateState();
state.updateState(state.agentId);
Vec3 destination = new Vec3(9.54f,-5f,60f) ;
moveTo(state, destination) ;

console("#### Moving to z-:");
state.updateState();
state.updateState(state.agentId);
destination = new Vec3(9.54f,-5f,54.2f) ;
moveTo(state, destination) ;
}
Expand All @@ -58,24 +58,24 @@ public void test_2DMoveTo2() throws InterruptedException {

var state = loadSE("myworld-3").snd;

state.updateState();
state.updateState(state.agentId);

state.updateState();
state.updateState(state.agentId);
state.navgrid.enableFlying = true ;
state.updateState();
state.updateState(state.agentId);

WorldEntity agentInfo = state.wom.elements.get(state.agentId);
console("** Agent's info: " + PrintInfos.showWorldEntity(agentInfo));

// agent se0 @<9.549925,-5.0025005,54.149185>, hdir:<-0.0064151124,1.6736684E-4,0.99997944>, vdir:<-3.9837923E-5,1.0,-1.6762585E-4>, health:1.0, jet:true

console("#### to x+");
state.updateState();
state.updateState(state.agentId);
Vec3 destination = new Vec3(10.2f,-5f,54.14f) ;
moveTo(state, destination) ;

console("#### Moving to z-:");
state.updateState();
state.updateState(state.agentId);
destination = new Vec3(10.2f,-5f,53.14f) ;
moveTo(state, destination) ;
}
Expand All @@ -85,24 +85,24 @@ public void test_YTurn() throws InterruptedException {

var state = loadSE("myworld-3").snd;

state.updateState();
state.updateState(state.agentId);

state.updateState();
state.updateState(state.agentId);
state.navgrid.enableFlying = true ;
state.updateState();
state.updateState(state.agentId);

WorldEntity agentInfo = state.wom.elements.get(state.agentId);
console("** Agent's info: " + PrintInfos.showWorldEntity(agentInfo));

// agent se0 @<9.549925,-5.0025005,54.149185>, hdir:<-0.0064151124,1.6736684E-4,0.99997944>, vdir:<-3.9837923E-5,1.0,-1.6762585E-4>, health:1.0, jet:true

console("#### turning to left +x");
state.updateState();
state.updateState(state.agentId);
Vec3 destination = new Vec3(12f,-5f,54.15f) ;
UUTacticLib.yTurnTowardACT(state,destination,0.99f,200) ;

console("#### turning to left -x");
state.updateState();
state.updateState(state.agentId);
destination = new Vec3(0f,-5f,54.15f) ;
UUTacticLib.yTurnTowardACT(state,destination,0.99f,200) ;

Expand Down
11 changes: 11 additions & 0 deletions JvmClient/src/jvmTest/java/uuspaceagent/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import spaceEngineers.controller.*;
import spaceEngineers.model.ToolbarLocation;

import java.io.IOException;

public class TestUtils {

public static void console(String str) {
Expand Down Expand Up @@ -49,4 +51,13 @@ public static Pair<TestAgent, UUSeAgentState> loadSE(String worldName) {

return new Pair<>(testAgent,myAgentState) ;
}

public static void closeConnectionToSE(UUSeAgentState state){
try {
state.env().getController().close();
}
catch (Exception e) {
// swallow...
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package uuspaceagent;

import eu.iv4xr.framework.mainConcepts.TestAgent;
import eu.iv4xr.framework.spatial.Vec3;
import nl.uu.cs.aplib.mainConcepts.GoalStructure;
import org.junit.jupiter.api.Test;
//import spaceEngineers.transport.SocketReaderWriterKt;

import static org.junit.jupiter.api.Assertions.assertTrue;
//import static spaceEngineers.transport.SocketReaderWriterKt.closeIfCloseable;
import static uuspaceagent.PrintInfos.showWOMAgent;
import static uuspaceagent.TestUtils.console;
import static uuspaceagent.TestUtils.loadSE;

/**
* This tests goals to do basic straight-line 2D move and turning.
*/
public class Test_BasicMoveAndTurnGoals {

public UUSeAgentState test_Goal(GoalStructure G) throws InterruptedException {
console("*** start test...") ;
var agentAndState = loadSE("myworld-3") ;
TestAgent agent = agentAndState.fst ;
UUSeAgentState state = agentAndState.snd ;
Thread.sleep(1000);
state.updateState(state.agentId);

// agent start location should be around: <10.119276,-5.0025,55.681934>
// orientationForward: <-0.043967947,-2.0614608E-4,0.9990329> ... so looking towards z-axis
console(showWOMAgent(state.wom));

agent.setGoal(G) ;

int turn= 0 ;
while(G.getStatus().inProgress()) {
console(">> [" + turn + "] " + showWOMAgent(state.wom));
agent.update();
//Thread.sleep(50);
turn++ ;
if (turn >= 1400) break ;
}
//closeIfCloseable(state.env().getController());
TestUtils.closeConnectionToSE(state);
return state ;
}

/**
* Test a simple goal to turn the agent 90 degree to the right.
*/
@Test
public void test_turningRight() throws InterruptedException {
// agent start location should be around: <10.119276,-5.0025,55.681934>
// orientationForward: <-0.043967947,-2.0614608E-4,0.9990329> ... so looking towards z-axis

Vec3 dest = new Vec3(9,-5,55.68f) ;
var G = UUGoalLib.face2DToward(null,dest) ;
test_Goal(G) ;
G.printGoalStructureStatus();
assertTrue(G.getStatus().success());
}

/**
* Test a simple goal to turn the agent 90 degree to the left.
*/
@Test
public void test_turningLeft() throws InterruptedException {
// agent start location should be around: <9.549925,-5.0025005,54.149185>
// orientationForward: <-0.043967947,-2.0614608E-4,0.9990329> ... so looking towards z-axis

Vec3 dest = new Vec3(11.5f,-5,54.149f) ;
var G = UUGoalLib.face2DToward(null,dest) ;
test_Goal(G) ;
G.printGoalStructureStatus();
assertTrue(G.getStatus().success());
}

/**
* Test strafing to the right.
*/
@Test
public void test_StrafeRight() throws InterruptedException {
// agent start location should be around: <10.119276,-5.0025,55.681934>
// orientationForward: <-0.043967947,-2.0614608E-4,0.9990329> ... so looking towards z-axis

Vec3 dest = new Vec3(8,-5,55.68f) ;
var G = UUGoalLib.veryclose2DTo(null,dest) ;
test_Goal(G) ;
G.printGoalStructureStatus();
assertTrue(G.getStatus().success());
}

/**
* Test strafing to left.
*/
@Test
public void test_StrafeLeft() throws InterruptedException {
// agent start location should be around: <10.119276,-5.0025,55.681934>
// orientationForward: <-0.043967947,-2.0614608E-4,0.9990329> ... so looking towards z-axis

Vec3 dest = new Vec3(12,-5,55.68f) ;
var G = UUGoalLib.veryclose2DTo(null,dest) ;
test_Goal(G) ;
G.printGoalStructureStatus();
assertTrue(G.getStatus().success());
}

/**
* Test move straight forward, mixed with a bit strafing (the target is diagonal,
* and we don't turn the agent).
*/
@Test
public void test_Forward() throws InterruptedException {
// agent start location should be around: <10.119276,-5.0025,55.681934>
// orientationForward: <-0.043967947,-2.0614608E-4,0.9990329> ... so looking towards z-axis

Vec3 dest = new Vec3(8,-5,60f) ;
var G = UUGoalLib.veryclose2DTo(null,dest) ;
test_Goal(G) ;
G.printGoalStructureStatus();
assertTrue(G.getStatus().success());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void test1() throws InterruptedException {
var agent = agentAndState.fst ;
var state = agentAndState.snd ;
Thread.sleep(1000);
state.updateState();
state.updateState(state.agentId);

WorldEntity survivalKit = SEBlockFunctions.findClosestBlock(state.wom,"SurvivalKitLarge", 10) ;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void test_obstacles_membership() throws InterruptedException {
UUSeAgentState state = agentAndState.snd ;
Thread.sleep(1000);
// do a single update, and check that we if we have the structures:
state.updateState();
state.updateState(state.agentId);

assertTrue(state.navgrid.allObstacleIDs.size() > 0 ) ;
console(showWOMElements(state.wom)) ;
Expand All @@ -60,11 +60,7 @@ public void test_obstacles_membership() throws InterruptedException {
.anyMatch(id -> findWorldEntity(state.wom,id).properties.get("blockType").equals("LargeBlockBatteryBlock")));

//SocketReaderWriterKt.closeIfCloseable(state.env().getController());
Closeable cs = state.env().getController() ;
try {
cs.close() ;
} catch (Exception e) {
}
TestUtils.closeConnectionToSE(state);
}

/**
Expand All @@ -79,8 +75,8 @@ Pair<UUSeAgentState,List<DPos3>> test_pathfinder(UUSeAgentState state, Vec3 dest
state = agentAndState.snd ;
Thread.sleep(1000);
state.navgrid.enableFlying = enable3D ;
state.updateState();
state.updateState();
state.updateState(state.agentId);
state.updateState(state.agentId);

}

Expand All @@ -94,11 +90,8 @@ Pair<UUSeAgentState,List<DPos3>> test_pathfinder(UUSeAgentState state, Vec3 dest
var sqDesitnation = state.navgrid.gridProjectedLocation(destination) ;
List<DPos3> path = state.pathfinder2D.findPath(state.navgrid,sqAgent,sqDesitnation) ;
//SocketReaderWriterKt.closeIfCloseable(state.env().getController());
Closeable cs = state.env().getController() ;
try {
cs.close() ;
} catch (Exception e) {
}
TestUtils.closeConnectionToSE(state);

return new Pair<>(state,path) ;
}

Expand Down
Loading

0 comments on commit 4a8495f

Please sign in to comment.