diff --git a/src/main/java/org/matsim/freight/logistics/events/HandlingInHubStartedEventHandler.java b/src/main/java/org/matsim/freight/logistics/events/HandlingInHubStartedEventHandler.java new file mode 100644 index 00000000..22f7b148 --- /dev/null +++ b/src/main/java/org/matsim/freight/logistics/events/HandlingInHubStartedEventHandler.java @@ -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); +} diff --git a/src/main/java/org/matsim/freight/logistics/events/HandlingInHubStartsEvent.java b/src/main/java/org/matsim/freight/logistics/events/HandlingInHubStartsEvent.java index 8432c277..9da8ebac 100644 --- a/src/main/java/org/matsim/freight/logistics/events/HandlingInHubStartsEvent.java +++ b/src/main/java/org/matsim/freight/logistics/events/HandlingInHubStartsEvent.java @@ -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; @@ -67,4 +70,13 @@ public Map getAttributes() { return attr; } + public static HandlingInHubStartsEvent convert (GenericEvent event) { + Map attributes = event.getAttributes(); + double time = Double.parseDouble(attributes.get(ATTRIBUTE_TIME)); + Id linkId = Id.createLinkId(attributes.get(ATTRIBUTE_LINK)); + Id 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); + } } diff --git a/src/main/java/org/matsim/freight/logistics/events/LspEventsReader.java b/src/main/java/org/matsim/freight/logistics/events/LspEventsReader.java new file mode 100644 index 00000000..93df8197 --- /dev/null +++ b/src/main/java/org/matsim/freight/logistics/events/LspEventsReader.java @@ -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 createCustomEventMappers() { + Map 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; + } + +} diff --git a/src/test/java/org/matsim/freight/logistics/events/LspEventsReaderTest.java b/src/test/java/org/matsim/freight/logistics/events/LspEventsReaderTest.java new file mode 100644 index 00000000..dffa57c5 --- /dev/null +++ b/src/test/java/org/matsim/freight/logistics/events/LspEventsReaderTest.java @@ -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 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 handledEvents = new ArrayList<>(); + + @Override public void handleEvent(HandlingInHubStartsEvent event) { + handledEvents.add(event); + } + + @Override public void handleEvent(CarrierTourEndEvent event) { + handledEvents.add(event); + } + } +} \ No newline at end of file