/** * * * <ul> * <li>Go to passive state if the ball is outside of movement range * <li>Go to Defending state if the team doesnt have the ball and the agent doesnt have the ball * <li>Go to Support state if the agent doesn't have the ball but the team does have the ball * </ul> * * @param stateMachine State Machine to update. * @param model Model containing the current game state. */ @Override public void updateState(StateMachine stateMachine, EnvironmentModel model) { if (!model.ballInMovementRange() && !model.agentHasBall()) { stateMachine.changeState(StateMachine.PASSIVE_STATE, model); return; } if (!model.teamHasBall() && !model.agentHasBall()) { stateMachine.changeState(StateMachine.DEFENDING_STATE, model); return; } if (!model.agentHasBall() && model.teamHasBall() && !model.isPlayerGoalKeeper()) { stateMachine.changeState(StateMachine.SUPPORT_STATE, model); } }
/** Test the Change State operation. */ @Test public void testChangeState() { StateVertex state2 = new StateVertex("state2", "<table><div>state2</div></table>"); /** Can not change index because not added. */ assertFalse(sm.changeState(state2)); assertNotSame(sm.getCurrentState(), state2); /** Add index. */ Eventable c = new Eventable(new Identification(How.xpath, "/bla"), EventType.click); assertTrue(sm.updateAndCheckIfClone(c, state2, dummyBrowser, new CrawlSession(dummyPool))); /** Name is correctly changed */ assertEquals("State name changed correctly", "state1", state2.getName()); /** Current index is the new index */ assertEquals(sm.getCurrentState(), state2); /** Change back. */ assertTrue(sm.changeState(index)); assertEquals(sm.getCurrentState(), index); }
/** Test the Clone state behaviour. */ @Test public void testCloneState() { // state2.equals(state3) StateVertex state2 = new StateVertex("state2", "<table><div>state2</div></table>"); StateVertex state3 = new StateVertex("state3", "<table><div>state2</div></table>"); /** Can not change to state2 because not inserted yet. */ assertFalse(sm.changeState(state2)); assertNotSame(sm.getCurrentState(), state2); Eventable c = new Eventable(new Identification(How.xpath, "/bla"), EventType.click); assertTrue(sm.updateAndCheckIfClone(c, state2, dummyBrowser, new CrawlSession(dummyPool))); /** Name is correctly changed */ assertEquals("State name changed correctly", "state1", state2.getName()); // can not change to state2 because we are already in state2 assertFalse(sm.changeState(state2)); assertSame(sm.getCurrentState(), state2); // state2.equals(state3) assertEquals("state2 equals state3", state2, state3); // state2 != state3 because other objects. assertNotSame("state2 != state3", state2, state3); Eventable c2 = new Eventable(new Identification(How.xpath, "/bla2"), EventType.click); // False because its CLONE! assertFalse(sm.updateAndCheckIfClone(c2, state3, dummyBrowser, new CrawlSession(dummyPool))); // state2.equals(state3) assertEquals("state2 equals state3", state2, state3); // state2 == sm.getCurrentState() because changed in update. assertSame("state2 == state3", state2, sm.getCurrentState()); /** Name is correctly changed */ assertEquals("State name changed correctly", "state1", sm.getCurrentState().getName()); }
/** Test the Rewind Operation. */ @Test public void testRewind() { // state2.equals(state3) StateVertex state2 = new StateVertex("state2", "<table><div>state2</div></table>"); StateVertex state3 = new StateVertex("state3", "<table><div>state2</div></table>"); StateVertex state4 = new StateVertex("state4", "<table><div>state4</div></table>"); /** Can not change to state2 because not inserted yet. */ assertFalse(sm.changeState(state2)); assertNotSame(sm.getCurrentState(), state2); Eventable c = new Eventable(new Identification(How.xpath, "/bla"), EventType.click); assertTrue(sm.updateAndCheckIfClone(c, state2, dummyBrowser, new CrawlSession(dummyPool))); /** Name is correctly changed */ assertEquals("State name changed correctly", "state1", state2.getName()); // can not change to state2 because we are already in state2 assertFalse(sm.changeState(state2)); assertSame(sm.getCurrentState(), state2); // state2.equals(state3) assertEquals("state2 equals state3", state2, state3); // !state2.equals(state4) assertFalse("state2 not equals state4", state2.equals(state4)); Eventable c2 = new Eventable(new Identification(How.xpath, "/bla2"), EventType.click); // False because its CLONE! assertFalse(sm.updateAndCheckIfClone(c2, state3, dummyBrowser, new CrawlSession(dummyPool))); Eventable c3 = new Eventable(new Identification(How.xpath, "/bla2"), EventType.click); // True because its not yet known assertTrue(sm.updateAndCheckIfClone(c3, state4, dummyBrowser, new CrawlSession(dummyPool))); sm.rewind(); assertEquals("CurrentState == index", index, sm.getCurrentState()); // Now we can go from index -> state2 assertTrue(sm.changeState(state2)); // Now we can go from state2 -> state2 assertTrue(sm.changeState(state2)); // Now we can go from state2 -> state4 assertTrue(sm.changeState(state4)); sm.rewind(); assertEquals("CurrentState == index", index, sm.getCurrentState()); // Now we can not go from index -> state4 assertFalse(sm.changeState(state4)); }