MATSIM
QVehicleImpl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * SimVehicle.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2009 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.mobsim.qsim.qnetsimengine;
22 
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 
27 import org.apache.logging.log4j.LogManager;
28 import org.apache.logging.log4j.Logger;
29 import org.matsim.api.core.v01.Id;
31 import org.matsim.core.gbl.Gbl;
35 import org.matsim.vehicles.Vehicle;
37 
51 public class QVehicleImpl implements QVehicle {
52 
53  private static final Logger log = LogManager.getLogger(QVehicleImpl.class);
54 
55  private static int warnCount = 0;
56 
57  private double linkEnterTime = 0. ;
58  private double earliestLinkExitTime = 0;
59  private DriverAgent driver = null;
60  private Collection<PassengerAgent> passengers = null;
61  private final Id<Vehicle> id;
62  private Link currentLink = null;
63  private final Vehicle vehicle;
64  private final int passengerCapacity;
65 
66  public QVehicleImpl(final Vehicle basicVehicle) {
67  this.id = basicVehicle.getId();
68  this.vehicle = basicVehicle;
69  this.passengers = new ArrayList<>();
70 
71  VehicleCapacity capacity = basicVehicle.getType().getCapacity();
72  if (capacity == null) {
73  this.passengerCapacity = 4;
74  if (warnCount < 10) {
75  log.warn("No VehicleCapacity (= maximum number of passengers) set in Vehicle. "
76  + "Using default value of 4. This is only a problem if you need vehicles with different "
77  + "capacities, e.g. for minibuses.");
78  warnCount++;
79  if ( warnCount == 10 ) {
80  log.warn( Gbl.FUTURE_SUPPRESSED ) ;
81  }
82  }
83  } else {
84  // do *not* subtract one for the driver! Most pt vehicles define the capacity without the driver.
85  // for private cars, think about if we should subtract one from the capacity if the driver is set?
86  // But if we do, change the number of seats of the default vehicle from 4 to 5.
87  this.passengerCapacity = capacity.getSeats() +
88  (capacity.getStandingRoom() == null ? 0 : capacity.getStandingRoom());
89  }
90  }
91 
92  @Override
93  public void setCurrentLink( final Link link ) {
94  this.currentLink = link;
95  }
96  // yy not sure if this needs to be publicly exposed
97 
112 // public double getLinkEnterTime() {
113 // return this.linkEnterTime;
114 // }
115 
123 // public void setLinkEnterTime(final double time) {
124 // this.linkEnterTime = time;
125 // }
126 
127  @Override
128  public double getEarliestLinkExitTime() {
129  return this.earliestLinkExitTime;
130  }
131 
132  @Override
133  public void setEarliestLinkExitTime(final double time) {
134  this.earliestLinkExitTime = time;
135  }
136 
137  @Override
138  public Link getCurrentLink() {
139  return this.currentLink;
140  }
141 
142  @Override
144  if ( this.driver instanceof MobsimDriverAgent ) {
145  return (MobsimDriverAgent) this.driver;
146  } else if ( this.driver==null ) {
147  return null ;
148  } else {
149  throw new RuntimeException( "error (downstream methods need to be made to accept DriverAgent)") ;
150  }
151  }
152  @Override
153  public void setDriver( final DriverAgent driver ) {
154  if (driver != null) {
155  if (this.driver != null && !this.driver.getId().equals(driver.getId())) {
156  throw new RuntimeException( "A driver (" + this.driver.getId() +") " +
157  "is already set in vehicle " + this.getId() + ". " +
158  "Setting agent " + driver.getId().toString() + " is not possible!");
159  }
160  }
161  // TODO: To make this check possible, we would need something like removeDriver().
162 // else {
163 // throw new RuntimeException( "Driver to be set in vehicle " + this.getId() +
164 // " is null!");
165 // }
166 
167  this.driver = driver;
168  }
169 
170  @Override
171  public Id<Vehicle> getId() {
172  return this.id;
173  }
174 
175  @Override
176  public double getSizeInEquivalents() {
177  return vehicle.getType().getPcuEquivalents();
178  }
179 
180  @Override
181  public Vehicle getVehicle() {
182  return this.vehicle;
183  }
184 
185  @Override
186  public String toString() {
187  return "Vehicle Id " + getId() + ", driven by (personId) " + this.driver.getId()
188  + ", on link " + this.currentLink.getId();
189  }
190 
191  public double getMaximumVelocity() {
192  return vehicle.getType().getMaximumVelocity();
193  }
194 
195  @Override
196  public Collection<? extends PassengerAgent> getPassengers() {
197  return Collections.unmodifiableCollection(this.passengers);
198  }
199 
200  @Override
201  public boolean addPassenger(PassengerAgent passenger) {
202  if (this.passengers.size() < this.passengerCapacity) {
203  return this.passengers.add(passenger);
204  }
205  return false;
206  }
207 
208  @Override
209  public boolean removePassenger(PassengerAgent passenger) {
210  return this.passengers.remove(passenger);
211  }
212 
213  @Override
214  public int getPassengerCapacity() {
215  return this.passengerCapacity;
216  }
217  @Override
218  public final double getLinkEnterTime() {
219  return this.linkEnterTime;
220  }
221  @Override
222  public final void setLinkEnterTime( double linkEnterTime ) {
223  // yyyyyy use in code!
224  this.linkEnterTime = linkEnterTime;
225  }
226 }
static final String FUTURE_SUPPRESSED
Definition: Gbl.java:44
boolean removePassenger(PassengerAgent passenger)
final VehicleCapacity getCapacity()
Collection<? extends PassengerAgent > getPassengers()