MATSIM
LinkReplanningMap.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * LinkReplanningMap.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2010 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.withinday.replanning.identifiers.tools;
22 
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.Map.Entry;
26 import java.util.Set;
27 
28 import jakarta.inject.Inject;
29 
30 import org.apache.logging.log4j.LogManager;
31 import org.apache.logging.log4j.Logger;
32 import org.matsim.api.core.v01.Id;
45 
69 
70  private static final Logger log = LogManager.getLogger(LinkReplanningMap.class);
71 
73 
74  /*
75  * EXACT... replanning is scheduled for the current time step (time == replanning time)
76  * RESTRICTED ... available replanning operations are restricted (time > replanning time)
77  * UNRESTRICTED ... replanning operations are not restricted (time <= replanning time)
78  */
79  private enum TimeFilterMode {
80  EXACT, RESTRICTED, UNRESTRICTED
81  }
82 
83  private final Set<Id<Person>> legJustStartedAgents;
84  private double currentTime = 0.0;
85 
86  @Inject
87  public LinkReplanningMap(EarliestLinkExitTimeProvider earliestLinkExitTimeProvider, EventsManager eventsManager) {
88 
89  eventsManager.addHandler(this);
90  log.info("Note that the LinkReplanningMap has to be registered as an EventHandler and a SimulationListener!");
91  this. earliestLinkExitTimeProvider = earliestLinkExitTimeProvider;
92 
93  this.legJustStartedAgents = new HashSet<Id<Person>>();
94  }
95 
96  @Override
97  public void handleEvent(PersonStuckEvent event) {
98  this.legJustStartedAgents.remove(event.getPersonId());
99  }
100 
101  @Override
102  public void handleEvent(ActivityEndEvent event) {
103  checkTime(event.getTime());
104  this.legJustStartedAgents.add(event.getPersonId());
105  }
106 
107  @Override
108  public void handleEvent(ActivityStartEvent event) {
109  this.legJustStartedAgents.remove(event.getPersonId());
110  }
111 
112  @Override
115  }
116 
117  /*
118  * If time > currentTime, then legJustStartedAgents is not up to date
119  * anymore, therefore we clear all entries and update currentTime.
120  */
121  private void checkTime(double time) {
122  if (time > currentTime) {
123  this.currentTime = time;
124  this.legJustStartedAgents.clear();
125  }
126  }
127 
128  @Override
129  public void reset(int iteration) {
130  currentTime = 0.0;
131  this.legJustStartedAgents.clear();
132  }
133 
138  public Set<Id<Person>> getReplanningAgents(final double time) {
139 
140  Set<Id<Person>> set = this.earliestLinkExitTimeProvider.getEarliestLinkExitTimesPerTimeStep(time);
141  if (set != null) return Collections.unmodifiableSet(set);
142  else return new HashSet<>();
143  }
144 
149  public Set<Id<Person>> getUnrestrictedReplanningAgents(final double time) {
150  return this.filterAgents(time, TimeFilterMode.UNRESTRICTED);
151  }
152 
157  public Set<Id<Person>> getRestrictedReplanningAgents(final double time) {
158  return this.filterAgents(time, TimeFilterMode.RESTRICTED);
159  }
160 
161  private Set<Id<Person>> filterAgents(final double time, final TimeFilterMode timeMode) {
162 
163  Set<Id<Person>> set = new HashSet<>();
164 
165  Set<Entry<OptionalTime, Set<Id<Person>>>> entries = this.earliestLinkExitTimeProvider.getEarliestLinkExitTimesPerTimeStep().entrySet();
166 
167  for (Entry<OptionalTime, Set<Id<Person>>> entry : entries) {
168  OptionalTime earliestLinkExitTime = entry.getKey();
169 
170  // check time
171  if (timeMode == TimeFilterMode.RESTRICTED) {
172  if (time <= earliestLinkExitTime.seconds()) continue;
173  } else if (timeMode == TimeFilterMode.UNRESTRICTED) {
174  if (time > earliestLinkExitTime.seconds()) continue;
175  } else {
176  throw new RuntimeException("Unexpected TimeFilterMode was found: " + timeMode.toString());
177  }
178 
179  // non of the checks fails therefore add agents to the set
180  set.addAll(entry.getValue());
181  }
182 
183  return set;
184  }
185 
190  public Set<Id<Person>> getLegPerformingAgents() {
191  return Collections.unmodifiableSet(this.earliestLinkExitTimeProvider.getEarliestLinkExitTimes().keySet());
192  }
193 
198  public Set<Id<Person>> getLegStartedAgents() {
199  return Collections.unmodifiableSet(this.legJustStartedAgents);
200  }
201 }
void addHandler(final EventHandler handler)