MATSIM
MutableScenario.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * StatelessScenarioImpl
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 package org.matsim.core.scenario;
21 
22 import java.util.HashMap;
23 import java.util.Map;
24 
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 import org.matsim.api.core.v01.Scenario;
30 import org.matsim.core.config.Config;
31 import org.matsim.core.network.*;
37 import org.matsim.lanes.Lanes;
38 import org.matsim.lanes.LanesUtils;
43 
44 
49 public final class MutableScenario implements Scenario, Lockable {
50  private static final Logger log = LogManager.getLogger(MutableScenario.class);
51 
52  private boolean locked = false ;
53 
54  private final Map<String, Object> elements = new HashMap<>();
55 
56  //mandatory attributes
57  private final Config config;
58  private Network network;
61 
62  //non-mandatory attributes
64  private Lanes lanes = null;
67 
68  private Vehicles vehicles ;
69 
70  MutableScenario(Config config) {
71  this.config = config;
72  this.network = NetworkUtils.createNetwork(this.config);
73  this.population = PopulationUtils.createPopulation(this.config, this.network);
74  this.facilities = new ActivityFacilitiesImpl();
75  this.households = new HouseholdsImpl();
76  this.lanes = LanesUtils.createLanesContainer();
77  this.vehicles = VehicleUtils.createVehiclesContainer();
78  this.transitVehicles = VehicleUtils.createVehiclesContainer();
79  this.transitSchedule = new TransitScheduleFactoryImpl().createTransitSchedule();
80 
81  this.config.network().setLocked();
82  }
83 
84  @Override
86  return this.facilities;
87  }
88 
89  @Override
90  public final Network getNetwork() {
91  return this.network;
92  }
93 
94  @Override
95  public final Population getPopulation() {
96  return this.population;
97  }
98 
99  @Override
100  public final Config getConfig() {
101  // yy should throw an exception if null. kai, based on https://matsim.atlassian.net/browse/MATSIM-301 , may'15
102  return this.config;
103  }
104 
105  public final void setNetwork(Network network) {
106  testForLocked();
107  this.network = network;
108  }
109 
110  public final void setPopulation(Population population) {
111  testForLocked();
112  this.population = population;
113  }
114 
115  // NOTE: Thibaut is not so happy about just returning null, since someone may code something like
116  // Vehicles vehicles = scenario.getVehicles() ; // returning null
117  // ... // lots of code
118  // ... vehicles.getVehicles() ... // throwing a null pointer exception
119  // That is, in such a situation the null pointer exception is thrown much later than when the null pointer was obtained.
120  // He rather wants to have this fail when it is called.
121  // I (kn) am a bit sceptic if it makes sense to establish such a convention when most other people use "if ... == null" use as the
122  // official dialect to check if something is there (since this would cause an exception with Thibaut's approach). kai, feb'15
123 
124  @Override
125  public final Households getHouseholds() {
126  return this.households;
127  }
128 
129  @Override
130  public Lanes getLanes() {
131  return this.lanes;
132  }
133 
134  @Override
135  public final Vehicles getTransitVehicles() {
136  return this.transitVehicles;
137  }
138 
139  @Override
140  final public Vehicles getVehicles() {
141  return this.vehicles;
142  }
143 
144 
145 
146  @Override
148  return this.transitSchedule;
149  }
150 
151  @Override
152  public final void addScenarioElement(
153  final String name,
154  final Object o) {
155  // Once the "removal" operation is locked, you cannot add under the same name. kai, sep'14
156  if ( o == null ) throw new NullPointerException( name );
157  final Object former = elements.put( name , o );
158  if ( former != null ) {
159  throw new IllegalStateException( former+" is already associated with name "+name+" when adding "+o );
160  }
161  }
162 
163  public final Object removeScenarioElement(final String name) {
164  testForLocked();
165  return elements.remove( name );
166  }
167 
168  @Override
169  public final Object getScenarioElement(final String name) {
170  // yy should throw an exception if null. kai, based on https://matsim.atlassian.net/browse/MATSIM-301 , may'15
171  return elements.get( name );
172  }
173 
174  @Override
175  public final void setLocked() {
176  this.locked = true ;
177  }
178 
179  private void testForLocked() {
180  if ( locked ) {
181  throw new RuntimeException( "Scenario is locked; too late to do this. See comments in code.") ;
182  /* The decision is roughly as follows:
183  * - It is ok to set network, population, etc. in the Scenario during initial demand generation.
184  * - It is NOT ok to do this once the services is running.
185  * - But we do not want to make a defensive copy of the whole thing at services startup.
186  * - We also want to be able to plug alternative Scenario implementations into the services.
187  * - But then the services only gets the "Scenario" not the "MutableScenario", so it does not have to worry about setNetwork and the like
188  * since it does not exist in the published interface.
189  * kai, sep'14
190  */
191  }
192  }
193 
194  public final void setActivityFacilities( ActivityFacilities facilities ) {
195  testForLocked() ;
196  this.facilities = facilities ;
197  }
198  public final void setHouseholds( Households households ) {
199  testForLocked() ;
200  this.households = households ;
201  }
202  public final void setTransitSchedule( TransitSchedule schedule ) {
203  testForLocked();
204  this.transitSchedule = schedule ;
205  }
206  public final void setTransitVehicles( Vehicles vehicles ) {
207  testForLocked();
208  this.transitVehicles = vehicles ;
209  }
210  public final void setLanes( Lanes lanes ) {
211  testForLocked();
212  this.lanes = lanes ;
213  }
214 
215 }
final Object getScenarioElement(final String name)
final ActivityFacilities getActivityFacilities()
final void addScenarioElement(final String name, final Object o)
final NetworkConfigGroup network()
Definition: Config.java:411
final void setHouseholds(Households households)
final void setPopulation(Population population)
final Object removeScenarioElement(final String name)
final Map< String, Object > elements
static Lanes createLanesContainer()
Definition: LanesUtils.java:39
final void setTransitSchedule(TransitSchedule schedule)
static Population createPopulation(Config config)
final void setNetwork(Network network)
final void setTransitVehicles(Vehicles vehicles)
final void setActivityFacilities(ActivityFacilities facilities)
static Vehicles createVehiclesContainer()