This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from matsim-vsp/kmt_create_HubHandlingEvents2
Add Reader for HandlingInHubStartsEvent ...
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/main/java/org/matsim/freight/logistics/events/HandlingInHubStartedEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/org/matsim/freight/logistics/events/LspEventsReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/test/java/org/matsim/freight/logistics/events/LspEventsReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |