All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.kit.ifv.mobitopp.data.local.serialiser.ZoneSerialiser Maven / Gradle / Ivy

Go to download

mobiTopp (http://mobitopp.ifv.kit.edu/) is an agent-based travel demand model developed at the Institute for transport studies at the Karlsruhe Institute of Technology (http://www.ifv.kit.edu/english/index.php). Publications about mobiTopp can be found on the project site (http://mobitopp.ifv.kit.edu/28.php).

The newest version!
package edu.kit.ifv.mobitopp.data.local.serialiser;

import static edu.kit.ifv.mobitopp.util.collections.StreamUtils.warn;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.stream.Stream;

import edu.kit.ifv.mobitopp.data.Zone;
import edu.kit.ifv.mobitopp.data.ZoneRepository;
import edu.kit.ifv.mobitopp.data.areatype.AreaTypeRepository;
import edu.kit.ifv.mobitopp.data.local.ZoneChargingFacility;
import edu.kit.ifv.mobitopp.dataimport.ChargingDataFactory;
import edu.kit.ifv.mobitopp.populationsynthesis.serialiser.CsvSerialiser;
import edu.kit.ifv.mobitopp.simulation.Car;
import edu.kit.ifv.mobitopp.simulation.carsharing.CarSharingDataForZone;
import edu.kit.ifv.mobitopp.simulation.carsharing.CarSharingStation;
import edu.kit.ifv.mobitopp.simulation.carsharing.FreeFloatingCar;
import edu.kit.ifv.mobitopp.simulation.carsharing.FreeFloatingCarSharingOrganization;
import edu.kit.ifv.mobitopp.simulation.carsharing.StationBasedCarSharingCar;
import edu.kit.ifv.mobitopp.simulation.carsharing.StationBasedCarSharingOrganization;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ZoneSerialiser implements Closeable {

	private final ZoneRepository repository;
	private final CsvSerialiser chargingData;
	private final CsvSerialiser zones;
	private final CsvSerialiser stationCompany;
	private final CsvSerialiser freeFloatingCompany;
	private final CsvSerialiser stations;
	private final CsvSerialiser cars;
	private final CsvSerialiser stationCars;
	private final CsvSerialiser freeFloatingCars;

	public ZoneSerialiser(
			ZoneRepository repository, CsvSerialiser chargingData,
			CsvSerialiser zone, CsvSerialiser stationCompany,
			CsvSerialiser freeFloatingCompany,
			CsvSerialiser stations, CsvSerialiser cars,
			CsvSerialiser stationCars,
			CsvSerialiser freeFloatingCars) {
		super();
		this.repository = repository;
		this.chargingData = chargingData;
		this.zones = zone;
		this.stationCompany = stationCompany;
		this.freeFloatingCompany = freeFloatingCompany;
		this.stations = stations;
		this.cars = cars;
		this.stationCars = stationCars;
		this.freeFloatingCars = freeFloatingCars;
	}

	public static ZoneSerialiser in(
			File zoneRepositoryFolder, ChargingDataFactory factory, ZoneRepository repository,
			AreaTypeRepository areaTypeRepository) {
		try {
			return new SerialiserBuilder(zoneRepositoryFolder, repository, factory, areaTypeRepository)
					.build();
			
		} catch (IOException cause) {
			throw warn(new UncheckedIOException(cause), log);
		}
	}

	public void serialise() {
		List zones = repository.getZones();
		writeHeaders();
    serialiseStationBasedOrganizations(zones);
    serialiseFreeFloatingOrganizations(zones);
		zones.stream().forEach(this::serialise);
	}

	private void writeHeaders() {
		chargingData.writeHeader();
		zones.writeHeader();
		stationCompany.writeHeader();
		freeFloatingCompany.writeHeader();
		stations.writeHeader();
		cars.writeHeader();
		stationCars.writeHeader();
		freeFloatingCars.writeHeader();
	}

	private void serialise(Zone zone) {
		serialiseChargingFacilitiesOf(zone);
		serialiseZone(zone);
		serialiseCarSharingOf(zone);
	}

	private void serialiseChargingFacilitiesOf(Zone zone) {
		zone
				.charging()
				.facilities()
				.map(facility -> new ZoneChargingFacility(zone.getId().getMatrixColumn(), facility))
				.forEach(chargingData::write);
	}

	private void serialiseZone(Zone zone) {
		zones.write(zone);
	}

	private void serialiseCarSharingOf(Zone zone) {
		CarSharingDataForZone carSharing = zone.carSharing();
		serialiseStations(carSharing);
		serialiseCars(carSharing);
	}

	private void serialiseStationBasedOrganizations(List zones) {
	  new StationBasedOrganizationSerialiser(stationCompany::write).serialise(zones);
	}

	private void serialiseFreeFloatingOrganizations(List zones) {
	  new FreeFloatingOrganizationSerialiser(freeFloatingCompany::write).serialise(zones);
	}

	private void serialiseStations(CarSharingDataForZone carSharing) {
		carSharing
				.stationBasedCarSharingCompanies()
				.stream()
				.flatMap(StationBasedCarSharingOrganization::stations)
				.forEach(stations::write);
	}

	private void serialiseCars(CarSharingDataForZone carSharing) {
		serialiseConventionalCars(carSharing);
		serialiseStationBasedCars(carSharing);
		serialiseFreeFloatingCars(carSharing);
	}

	private void serialiseConventionalCars(CarSharingDataForZone carSharing) {
		Stream
				.concat(stationBasedCars(carSharing), freeFloatingCars(carSharing).map(c -> c.car))
				.forEach(cars::write);
	}

	private void serialiseStationBasedCars(CarSharingDataForZone carSharing) {
		stationBasedCars(carSharing).forEach(stationCars::write);
	}

	private Stream stationBasedCars(CarSharingDataForZone carSharing) {
		return carSharing
				.stationBasedCarSharingCompanies()
				.stream()
				.flatMap(StationBasedCarSharingOrganization::ownedCars);
	}

	private void serialiseFreeFloatingCars(CarSharingDataForZone carSharing) {
		freeFloatingCars(carSharing).forEach(freeFloatingCars::write);
	}

	private Stream freeFloatingCars(CarSharingDataForZone carSharing) {
		return carSharing
				.freeFloatingCarSharingCompanies()
				.stream()
				.flatMap(FreeFloatingCarSharingOrganization::availableCars);
	}

	@Override
	public void close() throws IOException {
		chargingData.close();
		zones.close();
		stationCompany.close();
		freeFloatingCompany.close();
		stations.close();
		cars.close();
		stationCars.close();
		freeFloatingCars.close();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy