Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #179 from matsim-vsp/kmt_create_HubHandlingEvents2
Browse files Browse the repository at this point in the history
Add Reader for HandlingInHubStartsEvent ...
  • Loading branch information
kt86 authored Nov 20, 2023
2 parents 5ad174b + 9a1a55d commit 7fe9fe0
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.matsim.freight.logistics.events;

import org.matsim.core.events.handler.EventHandler;

/**
* @author Kai Martins-Turner (kturner)
*/
public interface HandlingInHubStartedEventHandler extends EventHandler {

void handleEvent (HandlingInHubStartsEvent event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@


import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.events.GenericEvent;
import org.matsim.api.core.v01.network.Link;
import org.matsim.freight.logistics.LSPResource;
import org.matsim.freight.logistics.shipment.LSPShipment;

import java.util.Map;

import static org.matsim.freight.logistics.events.LspEventAttributes.ATTRIBUTE_EXP_HANDLING_DURATION;
import static org.matsim.freight.logistics.events.LspEventAttributes.ATTRIBUTE_HUB_ID;

Expand Down Expand Up @@ -67,4 +70,13 @@ public Map<String, String> getAttributes() {
return attr;
}

public static HandlingInHubStartsEvent convert (GenericEvent event) {
Map<String, String> attributes = event.getAttributes();
double time = Double.parseDouble(attributes.get(ATTRIBUTE_TIME));
Id<Link> linkId = Id.createLinkId(attributes.get(ATTRIBUTE_LINK));
Id<LSPShipment> lspSipmentId = Id.create(attributes.get(ATTRIBUTE_LSP_SHIPMENT_ID), LSPShipment.class);
var hubId = Id.create(attributes.get(ATTRIBUTE_HUB_ID), LSPResource.class);
double expHandlingDuration = Double.parseDouble(attributes.get(ATTRIBUTE_EXP_HANDLING_DURATION));
return new HandlingInHubStartsEvent(time, linkId, lspSipmentId, hubId, expHandlingDuration);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* *********************************************************************** *
* project: org.matsim.*
* *********************************************************************** *
* *
* copyright : (C) by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* ***********************************************************************
*
*/

package org.matsim.freight.logistics.events;

import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.events.MatsimEventsReader;
import org.matsim.freight.carriers.events.CarrierEventsReaders;

import java.util.Map;
import java.util.TreeMap;

/**
* Creates an {@link MatsimEventsReader} that also handles the {@link org.matsim.freight.logistics.LSP} specific events.
* @author kturner (Kai Martins-Turner)
*/
public class LspEventsReader {

public static Map<String, MatsimEventsReader.CustomEventMapper> createCustomEventMappers() {
Map<String, MatsimEventsReader.CustomEventMapper> map = new TreeMap<>(CarrierEventsReaders.createCustomEventMappers());// also get all the Carrier-related EventMapper
map.put(HandlingInHubStartsEvent.EVENT_TYPE, HandlingInHubStartsEvent::convert);
return map;
}

public static MatsimEventsReader createEventsReader(EventsManager eventsManager) {
MatsimEventsReader reader = new MatsimEventsReader(eventsManager);
createCustomEventMappers().forEach(reader::addCustomEventMapper);
return reader;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.matsim.freight.logistics.events;

import org.junit.Assert;
import org.junit.Test;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.events.Event;
import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.config.groups.ControllerConfigGroup;
import org.matsim.core.events.EventsUtils;
import org.matsim.core.events.algorithms.EventWriterXML;
import org.matsim.freight.carriers.Carrier;
import org.matsim.freight.carriers.Tour;
import org.matsim.freight.carriers.events.CarrierTourEndEvent;
import org.matsim.freight.carriers.events.eventhandler.CarrierTourEndEventHandler;
import org.matsim.freight.logistics.LSPResource;
import org.matsim.freight.logistics.shipment.LSPShipment;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
* @author Kai Martins-Turner (kturner)
*/
public class LspEventsReaderTest {

private final List<Event> lspEvents = List.of(
new HandlingInHubStartsEvent(110.0, Id.createLinkId("TestLink1"), Id.create("shipment1", LSPShipment.class), Id.create("Hub1", LSPResource.class), 42.0),
new HandlingInHubStartsEvent(142.0, Id.createLinkId("TestLink2"), Id.create("shipment2", LSPShipment.class), Id.create("Hub2", LSPResource.class), 13.0),

//Check if also some of the regular CarrierEvents get read correctly -> Load their mapping in the LspEventsReaderMapping via createCustomEventMappers() works
new CarrierTourEndEvent(500, Id.create("c1", Carrier.class), Id.createLinkId("TestLinkC1"), Id.createVehicleId("myVehicle"), Id.create("myCarrierTour", Tour.class))
);

@Test
public void testReader() {
var outputStream = new ByteArrayOutputStream();
EventWriterXML writer = new EventWriterXML(outputStream);
lspEvents.forEach(writer::handleEvent);
writer.closeFile();

EventsManager eventsManager = EventsUtils.createEventsManager();
TestEventHandler handler = new TestEventHandler();
eventsManager.addHandler(handler);
eventsManager.initProcessing();
LspEventsReader.createEventsReader(eventsManager)
.readStream(new ByteArrayInputStream(outputStream.toByteArray()),
ControllerConfigGroup.EventsFileFormat.xml);
eventsManager.finishProcessing();

Assert.assertArrayEquals(lspEvents.toArray(), handler.handledEvents.toArray());
}

private static class TestEventHandler
implements HandlingInHubStartedEventHandler, CarrierTourEndEventHandler {
private final List<Event> handledEvents = new ArrayList<>();

@Override public void handleEvent(HandlingInHubStartsEvent event) {
handledEvents.add(event);
}

@Override public void handleEvent(CarrierTourEndEvent event) {
handledEvents.add(event);
}
}
}

0 comments on commit 7fe9fe0

Please sign in to comment.