public void finish() { for (Map.Entry<Id<Person>, Activity> entry : activities.entrySet()) { for (ActivityHandler activityHandler : activityHandlers) { activityHandler.handleActivity(new PersonExperiencedActivity(entry.getKey(), entry.getValue())); } } }
@Override synchronized public void handleActivity(PersonExperiencedActivity o) { // Has to be synchronized because the thing which sends Legs and the thing which sends Activities can run // on different threads. Will go away when/if we get a more Actor or Reactive Streams like event infrastructure. Id<Person> agentId = o.getAgentId(); Activity activity = o.getActivity(); Plan plan = agentRecords.get(agentId); if (plan != null) { agentRecords.get(agentId).addActivity(activity); } }
@Test public void testCreateNightActivity() { EventsToActivities testee = new EventsToActivities(); MockActivityHandler ah = new MockActivityHandler(); testee.addActivityHandler(ah); testee.reset(0); testee.handleEvent(new ActivityEndEvent(10.0, Id.create("1", Person.class), Id.create("l1", Link.class), Id.create("l1", ActivityFacility.class), "home")); Assert.assertNotNull(ah.handledActivity); Assert.assertEquals(Time.UNDEFINED_TIME, ah.handledActivity.getActivity().getStartTime(), 1e-8); Assert.assertEquals(10.0, ah.handledActivity.getActivity().getEndTime(), 1e-8); ah.reset(); testee.handleEvent(new ActivityStartEvent(90.0, Id.create("1", Person.class), Id.create("l1", Link.class), Id.create("l1", ActivityFacility.class), "home")); testee.finish(); Assert.assertNotNull(ah.handledActivity); Assert.assertEquals(Time.UNDEFINED_TIME, ah.handledActivity.getActivity().getEndTime(), 1e-8); Assert.assertEquals(90.0, ah.handledActivity.getActivity().getStartTime(), 1e-8); }
@Test public void testDontCreateNightActivityIfNoneIsBeingPerformedWhenSimulationEnds() { EventsToActivities testee = new EventsToActivities(); MockActivityHandler ah = new MockActivityHandler(); testee.addActivityHandler(ah); testee.reset(0); testee.handleEvent(new ActivityEndEvent(10.0, Id.create("1", Person.class), Id.create("l1", Link.class), Id.create("f1", ActivityFacility.class), "home")); Assert.assertNotNull(ah.handledActivity); Assert.assertEquals(Time.UNDEFINED_TIME, ah.handledActivity.getActivity().getStartTime(), 1e-8); Assert.assertEquals(10.0, ah.handledActivity.getActivity().getEndTime(), 1e-8); ah.reset(); testee.finish(); Assert.assertNull(ah.handledActivity); }
@Override synchronized public void handleActivity(PersonExperiencedActivity o) { Id<Person> agentId = o.getAgentId(); Activity activity = o.getActivity(); ScoringFunction scoringFunction = ScoringFunctionsForPopulation.this.getScoringFunctionForAgent(agentId); if (scoringFunction != null) {
@Override public void handleEvent(ActivityEndEvent event) { Activity activity = activities.get(event.getPersonId()); if (activity == null) { Activity firstActivity = PopulationUtils.createActivityFromLinkId(event.getActType(), event.getLinkId()); firstActivity.setFacilityId(event.getFacilityId()); activity = firstActivity; } activity.setEndTime(event.getTime()); for (ActivityHandler activityHandler : activityHandlers) { activityHandler.handleActivity(new PersonExperiencedActivity(event.getPersonId(), activity)); } activities.remove(event.getPersonId()); }
@Test public void testCreatesActivty() { EventsToActivities testee = new EventsToActivities(); MockActivityHandler ah = new MockActivityHandler(); testee.addActivityHandler(ah); testee.reset(0); testee.handleEvent(new ActivityStartEvent(10.0, Id.create("1", Person.class), Id.create("l1", Link.class), Id.create("l1", ActivityFacility.class), "work")); testee.handleEvent(new ActivityEndEvent(30.0, Id.create("1", Person.class), Id.create("l1", Link.class), Id.create("l1", ActivityFacility.class), "work")); Assert.assertNotNull(ah.handledActivity); Assert.assertEquals(10.0, ah.handledActivity.getActivity().getStartTime(), 1e-8); Assert.assertEquals(30.0, ah.handledActivity.getActivity().getEndTime(), 1e-8); }
RecordingScoringFunction rs = (RecordingScoringFunction) s; sf.handleActivity(new PersonExperiencedActivity(personId, pf.createActivityFromCoord("home", new Coord(100, 100)))); Assert.assertEquals(0, rs.tripCounter); sf.handleLeg(new PersonExperiencedLeg(personId, pf.createLeg("walk"))); Assert.assertEquals(0, rs.tripCounter); sf.handleActivity(new PersonExperiencedActivity(personId, pf.createActivityFromCoord("work", new Coord(1000, 100)))); Assert.assertEquals(1, rs.tripCounter); Assert.assertEquals(1, rs.lastTrip.getTripElements().size()); sf.handleActivity(new PersonExperiencedActivity(personId, pf.createActivityFromCoord("pt_interaction", new Coord(1000, 200)))); Assert.assertEquals(1, rs.tripCounter); sf.handleLeg(new PersonExperiencedLeg(personId, pf.createLeg("pt"))); sf.handleActivity(new PersonExperiencedActivity(personId, pf.createActivityFromCoord("pt_interaction", new Coord(1000, 200)))); Assert.assertEquals(1, rs.tripCounter); sf.handleLeg(new PersonExperiencedLeg(personId, pf.createLeg("transit_walk"))); sf.handleActivity(new PersonExperiencedActivity(personId, pf.createActivityFromCoord("leisure", new Coord(1000, 200)))); Assert.assertEquals(2, rs.tripCounter); Assert.assertEquals(5, rs.lastTrip.getTripElements().size());