diff --git a/src/main/java/org/matsim/freight/logistics/LSPControlerListener.java b/src/main/java/org/matsim/freight/logistics/LSPControlerListener.java index 86d0860d..b19a7f2c 100644 --- a/src/main/java/org/matsim/freight/logistics/LSPControlerListener.java +++ b/src/main/java/org/matsim/freight/logistics/LSPControlerListener.java @@ -22,8 +22,11 @@ import jakarta.inject.Inject; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import javax.annotation.Nullable; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.matsim.api.core.v01.Scenario; @@ -43,7 +46,8 @@ import org.matsim.freight.logistics.shipment.LSPShipment; class LSPControlerListener - implements BeforeMobsimListener, + implements StartupListener, + BeforeMobsimListener, AfterMobsimListener, ScoringListener, ReplanningListener, @@ -70,6 +74,47 @@ class LSPControlerListener this.scenario = scenario; } + @Override + public void notifyStartup(StartupEvent event) { + //Ensure that all ressource Ids are only there once. + + checkForUniqueResourceIds(); + + } + +/** +* For later steps, e.g. scoring the Ids of the {@link LSPResource} ids must be unique. + * Otherwise, there are scored several times. + *

+ * For the future we may reduce it to unique {@link LSPResource} ids PER {@link LSP}. + * This means, that the events (also from the carriers) need to have an information obout the LSP it belongs to and that + * in scoring and analysis this must be taken into account. What itself is another source for errors... + * KMT jul'24 +*/ + private void checkForUniqueResourceIds() { + List duplicates = new ArrayList<>(); + Set set = new HashSet<>(); + + LSPs lsps = LSPUtils.getLSPs(scenario); + for (LSP lsp : lsps.getLSPs().values()) { + for (LSPResource lspResource : lsp.getResources()) { + String idString = lspResource.getId().toString(); + if (set.contains(idString)) { + duplicates.add(idString); + } else { + set.add(idString); + } + } + } + + if (!duplicates.isEmpty()) { + log.error("There are non-unique ressource Ids. This must not be! The duplicate ids are: {}.", duplicates.toString()); + log.error("You may also use output_lsp.xml to check were the duplicates are located"); + log.error("Aborting now ..."); + throw new RuntimeException(); + } + } + @Override public void notifyBeforeMobsim(BeforeMobsimEvent event) { LSPs lsps = LSPUtils.getLSPs(scenario); @@ -114,13 +159,13 @@ private void registerSimulationTrackers(HasSimulationTrackers hasSimulationTr hasSimulationTrackers.getSimulationTrackers()) { // ... register them ... if (!registeredHandlers.contains(simulationTracker)) { - log.info("adding eventsHandler: " + simulationTracker); + log.info("adding eventsHandler: {}", simulationTracker); eventsManager.addHandler(simulationTracker); registeredHandlers.add(simulationTracker); matsimServices.addControlerListener(simulationTracker); simulationTracker.setEventsManager(eventsManager); } else if ( addListenerCnt < maxAddListenerCnt ){ - log.warn("not adding eventsHandler since already added: " + simulationTracker); + log.warn("not adding eventsHandler since already added: {}", simulationTracker); addListenerCnt++; if (addListenerCnt == maxAddListenerCnt) { log.warn(Gbl.FUTURE_SUPPRESSED); @@ -205,4 +250,5 @@ public void notifyShutdown(ShutdownEvent event) { new CarrierPlanWriter(CarriersUtils.getCarriers(scenario)) .write(controlerIO.getOutputPath() + "/output_carriers.xml.gz"); } + }