MATSIM
ActivityEngineWithWakeup.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2008 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.core.mobsim.qsim;
21 
22 import java.util.Comparator;
23 import java.util.Map;
24 import java.util.Queue;
25 import java.util.concurrent.PriorityBlockingQueue;
26 
27 import jakarta.inject.Inject;
28 
29 import org.apache.logging.log4j.LogManager;
30 import org.apache.logging.log4j.Logger;
31 import org.matsim.api.core.v01.Id;
39 
40 public final class ActivityEngineWithWakeup implements ActivityEngine {
41  public static final String COMPONENT_NAME = "ActivityEngineWithWakeup";
42  private static final Logger log = LogManager.getLogger(ActivityEngineWithWakeup.class );
45  private final ActivityEngine delegate;
46 
47  private final Queue<AgentEntry> wakeUpList = new PriorityBlockingQueue<>(500,
48  Comparator.comparingDouble((AgentEntry o) -> o.time).thenComparing(o -> o.agent.getId()));
50 
51  @Inject
52  ActivityEngineWithWakeup(EventsManager eventsManager, PreplanningEngine preplanningEngine) {
53  this.delegate = new ActivityEngineDefaultImpl(eventsManager);
54  this.eventsManager = eventsManager;
55  this.preplanningEngine = preplanningEngine;
56  }
57 
58  @Override
59  public void onPrepareSim() {
60  log.warn( "running onPrepareSim");
61  delegate.onPrepareSim();
62  }
63 
64  @Override
65  public void doSimStep(double now) {
66  while (!wakeUpList.isEmpty() && wakeUpList.peek().time <= now) {
67  final AgentEntry entry = wakeUpList.poll();
68  this.eventsManager.processEvent(new AgentWakeupEvent(now, entry.agent.getId()));
69  entry.agentWakeup.executeOnWakeup(entry.agent, now );
70  }
71  delegate.doSimStep(now);
72  }
73 
74  @Override
75  public void afterSim() {
76  delegate.afterSim();
77  }
78 
79  @Override
80  public void setInternalInterface(InternalInterface internalInterface) {
81  this.internalInterface = internalInterface;
82  delegate.setInternalInterface(internalInterface);
83  }
84 
93  @Override
94  public boolean handleActivity(MobsimAgent agent) {
95  double now = this.internalInterface.getMobsim().getSimTimer().getTimeOfDay();
96 
97 // Activity act = (Activity)WithinDayAgentUtils.getCurrentPlanElement(agent);
98  if ( agent instanceof PlanAgent ) {
99  Activity act = (Activity) ((PlanAgent) agent).getCurrentPlanElement();
100  if (!act.getType().contains("interaction")) {
101  wakeUpList.addAll(preplanningEngine.generateWakeups(agent, now));
102  }
103  }
104 
105  return delegate.handleActivity(agent);
106  }
107 
108  public interface AgentWakeup {
109  void executeOnWakeup( MobsimAgent agent, double now );
110  }
111 
120  @Override
121  public void rescheduleActivityEnd(final MobsimAgent agent) {
122  delegate.rescheduleActivityEnd(agent);
123  }
124 
134  static class AgentEntry {
135  AgentEntry( MobsimAgent agent, double time, AgentWakeup agentWakeup ) {
136  // yyyy Let us be careful that the executeOnWakeUp does not become overkill here; if we want something more
137  // general, rather move on a completely general MessageQueue. kai, mar'19
138 
139  this.agent = agent;
140  this.time = time;
141  this.agentWakeup = agentWakeup;
142  }
143 
144  final MobsimAgent agent;
145  final double time;
146  final AgentWakeup agentWakeup;
147  }
148 
149  public final static class AgentWakeupEvent extends Event implements HasPersonId {
150  private final Id<Person> personId;
151 
152  AgentWakeupEvent( double now, Id<Person> personId ) {
153  super(now);
154  this.personId = personId;
155  }
156 
157  @Override
158  public String getEventType() {
159  return "agentWakeup";
160  }
161 
162  @Override
164  return personId;
165  }
166 
167  @Override
168  public Map<String, String> getAttributes() {
169  Map<String, String> attr = super.getAttributes();
170 // attr.put(ATTRIBUTE_PERSON, this.personId.toString()); // already done in superclass. kai, apr'23
171  return attr;
172  }
173  }
174 }
void setInternalInterface(InternalInterface internalInterface)
void setInternalInterface(InternalInterface internalInterface)