MATSIM
PassengerAccessEgressImpl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * PassengerAccessEgressImpl
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2012 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 package org.matsim.core.mobsim.qsim.pt;
21 
22 import java.util.ArrayList;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26 
27 import org.matsim.api.core.v01.Id;
28 import org.matsim.api.core.v01.Scenario;
44 import org.matsim.vehicles.Vehicle;
45 
46 
52 class PassengerAccessEgressImpl implements PassengerAccessEgress {
53 
54  private final InternalInterface internalInterface;
55  private final TransitStopAgentTracker agentTracker;
56  private final boolean isGeneratingDeniedBoardingEvents ;
57  private Set<PTPassengerAgent> agentsDeniedToBoard = null;
58  private Scenario scenario;
59  private EventsManager eventsManager;
60 
61  PassengerAccessEgressImpl(InternalInterface internalInterface, TransitStopAgentTracker agentTracker, Scenario scenario, EventsManager eventsManager) {
62  this.internalInterface = internalInterface;
63  this.agentTracker = agentTracker;
64  this.scenario = scenario;
65  this.eventsManager = eventsManager;
66  this.isGeneratingDeniedBoardingEvents =
67  this.scenario.getConfig().vspExperimental().isGeneratingBoardingDeniedEvents() ;
68  if (this.isGeneratingDeniedBoardingEvents){
69  this.agentsDeniedToBoard = new HashSet<>();
70  }
71  }
72 
76  /*package*/ double calculateStopTimeAndTriggerBoarding(TransitRoute transitRoute, TransitLine transitLine, final TransitVehicle vehicle,
77  final TransitStopFacility stop, List<TransitRouteStop> stopsToCome, final double now) {
78  ArrayList<PTPassengerAgent> passengersLeaving = findPassengersLeaving(vehicle, stop);
79  int freeCapacity = vehicle.getPassengerCapacity() - vehicle.getPassengers().size() + passengersLeaving.size();
80  List<PTPassengerAgent> passengersEntering = findPassengersEntering(transitRoute, transitLine, vehicle, stop, stopsToCome, freeCapacity, now);
81 
82  TransitStopHandler stopHandler = vehicle.getStopHandler();
83  double stopTime = stopHandler.handleTransitStop(stop, now, passengersLeaving, passengersEntering, this, vehicle);
84  if (stopTime == 0.0){ // (de-)boarding is complete when the additional stopTime is 0.0
85  if (this.isGeneratingDeniedBoardingEvents){
86  this.fireBoardingDeniedEvents(vehicle, now);
87  this.agentsDeniedToBoard.clear();
88  }
89  }
90  return stopTime;
91  }
92 
93  private void fireBoardingDeniedEvents(TransitVehicle vehicle, double now){
94  Id<Vehicle> vehicleId = vehicle.getId() ;
95  for (PTPassengerAgent agent : this.agentsDeniedToBoard){
96  Id<Person> agentId = agent.getId() ;
97  this.eventsManager.processEvent(
98  new BoardingDeniedEvent(now, agentId, vehicleId)
99  ) ;
100  }
101  }
102 
103 
104  private List<PTPassengerAgent> findPassengersEntering(TransitRoute transitRoute, TransitLine transitLine, TransitVehicle vehicle,
105  final TransitStopFacility stop, List<TransitRouteStop> stopsToCome, int freeCapacity, double now) {
106  ArrayList<PTPassengerAgent> passengersEntering = new ArrayList<>();
107 
108  if (this.isGeneratingDeniedBoardingEvents) {
109 
110  for (PTPassengerAgent agent : this.agentTracker.getAgentsAtFacility(stop.getId())) {
111  if (agent.getEnterTransitRoute(transitLine, transitRoute, stopsToCome, vehicle)) {
112  if (freeCapacity >= 1) {
113  passengersEntering.add(agent);
114  freeCapacity--;
115  } else {
116  this.agentsDeniedToBoard.add(agent);
117  }
118  }
119  }
120 
121  } else {
122 
123  for (PTPassengerAgent agent : this.agentTracker.getAgentsAtFacility(stop.getId())) {
124  if (freeCapacity == 0) {
125  break;
126  }
127  if (agent.getEnterTransitRoute(transitLine, transitRoute, stopsToCome, vehicle)) {
128  passengersEntering.add(agent);
129  freeCapacity--;
130  }
131  }
132 
133  }
134 
135  return passengersEntering;
136  }
137 
138 
139 
140  private ArrayList<PTPassengerAgent> findPassengersLeaving(TransitVehicle vehicle,
141  final TransitStopFacility stop) {
142  ArrayList<PTPassengerAgent> passengersLeaving = new ArrayList<>();
143  for (PassengerAgent passenger : vehicle.getPassengers()) {
144  if (((PTPassengerAgent) passenger).getExitAtStop(stop)) {
145  passengersLeaving.add((PTPassengerAgent) passenger);
146  }
147  }
148  return passengersLeaving;
149  }
150 
151 
152  @Override
153  public boolean handlePassengerEntering(PTPassengerAgent passenger, MobsimVehicle vehicle, Id<TransitStopFacility> fromStopFacilityId, double time) {
154  boolean handled = vehicle.addPassenger(passenger);
155  if(handled){
156  this.agentTracker.removeAgentFromStop(passenger, fromStopFacilityId);
157  MobsimAgent planAgent = (MobsimAgent) passenger;
158 // if (planAgent instanceof PersonDriverAgentImpl) {
159  Id<Person> agentId = planAgent.getId();
160  Id<Link> linkId = planAgent.getCurrentLinkId();
161  this.internalInterface.unregisterAdditionalAgentOnLink(agentId, linkId) ;
162 // }
163  MobsimDriverAgent agent = (MobsimDriverAgent) passenger;
164  passenger.setVehicle(vehicle);
165  eventsManager.processEvent(new PersonEntersVehicleEvent(time, agent.getId(), vehicle.getVehicle().getId()));
166  }
167  return handled;
168  }
169 
170  @Override
171  public boolean handlePassengerLeaving(PTPassengerAgent passenger, MobsimVehicle vehicle, Id<Link> toLinkId, double time) {
172  boolean handled = vehicle.removePassenger(passenger);
173  if(handled){
174  passenger.setVehicle(null);
175  eventsManager.processEvent(new PersonLeavesVehicleEvent(time, passenger.getId(), vehicle.getVehicle().getId()));
176 
177  // from here on works only if PassengerAgent can be cast into MobsimAgent ... but this is how it was before.
178  // kai, sep'12
179 
180  MobsimAgent agent = (MobsimAgent) passenger ;
181  agent.notifyArrivalOnLinkByNonNetworkMode(toLinkId);
182  agent.endLegAndComputeNextState(time);
183  this.internalInterface.arrangeNextAgentState(agent) ;
184  // (cannot set trEngine to TransitQSimEngine because there are tests where this will not work. kai, dec'11)
185  }
186  return handled;
187  }
188 
189 
190 
191 
192 
193 }