MATSIM
DefaultLinkSpeedCalculator.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2013 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.qnetsimengine;
21 
22 import com.google.inject.Inject;
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
29 
30 import java.util.ArrayList;
31 import java.util.Collection;
32 
39 public final class DefaultLinkSpeedCalculator implements LinkSpeedCalculator{
40  // I have moved this into the qnetsimengine package so that it can be used without injection inside that package. kai, jun'23
41  private static final Logger log = LogManager.getLogger(DefaultLinkSpeedCalculator.class );
42  private final Collection<LinkSpeedCalculator> calculators = new ArrayList<>();
43 
44  @Inject DefaultLinkSpeedCalculator(){} // so it has to be instantiated by injection from outside package. kai, jun'23
45 
46  @Override public double getMaximumVelocity(QVehicle vehicle, Link link, double time) {
47  double speed = Double.NaN;
48  for ( LinkSpeedCalculator calculator : calculators ) {
49  double tmp = calculator.getMaximumVelocity( vehicle, link, time ) ;
50  if ( !Double.isNaN( tmp ) ) {
51  if ( Double.isNaN( speed ) ){
52  speed = tmp;
53  } else {
54  throw new RuntimeException( "two vehicle speed calculators feel responsible for vehicle; don't know what to do." );
55  // would be possible to have the calculators sorted, i.e. as List, but don't see how this could be configured in an easy way. kai, jun'23
56  }
57  }
58  }
59  if ( !Double.isNaN( speed ) ) {
60  return speed;
61  } else{
62  return Math.min( vehicle.getMaximumVelocity(), link.getFreespeed( time ) );
63  }
64  }
65 
72  this.calculators.add( linkSpeedCalculator );
73  return this;
74  }
75 
76 }