001/* *********************************************************************** *
002 * project: org.matsim.*
003 * CreateVehiclesForSchedule.java
004 *                                                                         *
005 * *********************************************************************** *
006 *                                                                         *
007 * copyright       : (C) 2009 by the members listed in the COPYING,        *
008 *                   LICENSE and WARRANTY file.                            *
009 * email           : info at matsim dot org                                *
010 *                                                                         *
011 * *********************************************************************** *
012 *                                                                         *
013 *   This program is free software; you can redistribute it and/or modify  *
014 *   it under the terms of the GNU General Public License as published by  *
015 *   the Free Software Foundation; either version 2 of the License, or     *
016 *   (at your option) any later version.                                   *
017 *   See also COPYING, LICENSE and WARRANTY file                           *
018 *                                                                         *
019 * *********************************************************************** */
020
021package org.matsim.pt.utils;
022
023import org.matsim.api.core.v01.Id;
024import org.matsim.pt.transitSchedule.api.Departure;
025import org.matsim.pt.transitSchedule.api.TransitLine;
026import org.matsim.pt.transitSchedule.api.TransitRoute;
027import org.matsim.pt.transitSchedule.api.TransitSchedule;
028import org.matsim.vehicles.Vehicle;
029import org.matsim.vehicles.VehicleCapacity;
030import org.matsim.vehicles.VehicleType;
031import org.matsim.vehicles.Vehicles;
032import org.matsim.vehicles.VehiclesFactory;
033
034/**
035 * Creates a vehicle of type "defaultTransitVehicleType" for each departure.
036 * Useful for tests and demos.
037 *
038 * @author mrieser
039 */
040public class CreateVehiclesForSchedule {
041
042        private final TransitSchedule schedule;
043        private final Vehicles vehicles;
044
045        public CreateVehiclesForSchedule(final TransitSchedule schedule, final Vehicles vehicles) {
046                this.schedule = schedule;
047                this.vehicles = vehicles;
048        }
049
050        public void run() {
051                VehiclesFactory vb = this.vehicles.getFactory();
052                VehicleType vehicleType = vb.createVehicleType(Id.create("defaultTransitVehicleType", VehicleType.class));
053//              VehicleCapacity capacity = new VehicleCapacity();
054                vehicleType.getCapacity().setSeats( 101 );
055                vehicleType.getCapacity().setStandingRoom( 0 );
056//              vehicleType.setCapacity(capacity);
057                this.vehicles.addVehicleType(vehicleType);
058
059                long vehId = 0;
060                for (TransitLine line : this.schedule.getTransitLines().values()) {
061                        for (TransitRoute route : line.getRoutes().values()) {
062                                for (Departure departure : route.getDepartures().values()) {
063                                        Vehicle veh = vb.createVehicle(Id.create("tr_" + Long.toString(vehId++), Vehicle.class), vehicleType);
064                                        this.vehicles.addVehicle(veh);
065                                        departure.setVehicleId(veh.getId());
066                                }
067                        }
068                }
069        }
070}