MATSIM
EventsToActivities.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * EventsToActivities.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2011 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21  package org.matsim.core.scoring;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 import jakarta.inject.Inject;
26 import org.matsim.api.core.v01.IdMap;
37 
52 
53  public interface ActivityHandler {
55  }
56 
57  private final IdMap<Person, Activity> activities = new IdMap<>(Person.class);
58  private final List<ActivityHandler> activityHandlers = new ArrayList<>();
59 
60  public EventsToActivities() {
61 
62  }
63 
64  @Inject
65  EventsToActivities(ControlerListenerManager controlerListenerManager) {
66  controlerListenerManager.addControlerListener(new AfterMobsimListener() {
67  @Override
68  public void notifyAfterMobsim(AfterMobsimEvent event) {
69  finish();
70  }
71  });
72  }
73 
74  @Override
75  public void handleEvent(ActivityEndEvent event) {
76  Activity activity = this.activities.remove(event.getPersonId());
77  if (activity == null) {
78  Activity firstActivity = PopulationUtils.createActivityFromLinkId(event.getActType(), event.getLinkId());
79  firstActivity.setFacilityId(event.getFacilityId());
80  firstActivity.setCoord(event.getCoord());
81  activity = firstActivity;
82  }
83  activity.setEndTime(event.getTime());
84  PersonExperiencedActivity personExperiencedActivity = new PersonExperiencedActivity(event.getPersonId(), activity);
85  for (ActivityHandler activityHandler : this.activityHandlers) {
86  activityHandler.handleActivity(personExperiencedActivity);
87  }
88  }
89 
90  @Override
91  public void handleEvent(ActivityStartEvent event) {
92  Activity activity = PopulationUtils.createActivityFromLinkId(event.getActType(), event.getLinkId());
93  activity.setFacilityId(event.getFacilityId());
94  activity.setStartTime(event.getTime());
95  activity.setCoord( event.getCoord() );
96 
97  this.activities.put(event.getPersonId(), activity);
98  }
99 
100  @Override
101  public void reset(int iteration) {
102  this.activities.clear();
103  }
104 
105  public void addActivityHandler(ActivityHandler activityHandler) {
106  this.activityHandlers.add(activityHandler);
107  }
108 
109  public void finish() {
110  this.activities.forEach((id, activity) -> {
111  PersonExperiencedActivity personExperiencedActivity = new PersonExperiencedActivity(id, activity);
112  for (ActivityHandler activityHandler : this.activityHandlers) {
113  activityHandler.handleActivity(personExperiencedActivity);
114  }
115  });
116  }
117 
118 }
final IdMap< Person, Activity > activities
void forEach(BiConsumer<? super Id< T >, ? super V > action)
Definition: IdMap.java:194
static Activity createActivityFromLinkId(String type, Id< Link > linkId)
void addActivityHandler(ActivityHandler activityHandler)
void handleEvent(ActivityStartEvent event)
void handleActivity(PersonExperiencedActivity activity)
final List< ActivityHandler > activityHandlers
V put(Id< T > key, V value)
Definition: IdMap.java:141
void addControlerListener(ControlerListener controlerListener)
void setFacilityId(final Id< ActivityFacility > id)
void setEndTime(final double seconds)