From d955a391ec74891b310295a5d01c5db4e0497f86 Mon Sep 17 00:00:00 2001 From: rakow Date: Wed, 13 Mar 2024 12:14:53 +0100 Subject: [PATCH] handle file errors as separate comparison result --- .../ComparisonResult.java | 2 +- .../EventFingerprint.java | 4 +-- .../EventsFileFingerprintComparator.java | 34 ++++++++++++++++--- .../FingerprintEventHandler.java | 8 +++++ 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/ComparisonResult.java b/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/ComparisonResult.java index c77394fddd4..b5e10fa2d65 100644 --- a/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/ComparisonResult.java +++ b/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/ComparisonResult.java @@ -3,4 +3,4 @@ /** * Result of event file comparison. */ -public enum ComparisonResult {FILES_ARE_EQUAL, DIFFERENT_NUMBER_OF_TIMESTEPS, DIFFERENT_TIMESTEPS, DIFFERENT_EVENT_ATTRIBUTES, MISSING_EVENT, WRONG_EVENT_COUNT} +public enum ComparisonResult {FILES_ARE_EQUAL, DIFFERENT_NUMBER_OF_TIMESTEPS, DIFFERENT_TIMESTEPS, DIFFERENT_EVENT_ATTRIBUTES, MISSING_EVENT, WRONG_EVENT_COUNT, FILE_ERROR} diff --git a/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/EventFingerprint.java b/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/EventFingerprint.java index c2c0a8610a5..74f254f746d 100644 --- a/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/EventFingerprint.java +++ b/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/EventFingerprint.java @@ -91,7 +91,7 @@ public static void write(String filePath, EventFingerprint eventFingerprint) { } } - public static EventFingerprint read(String fingerprintPath) { + public static EventFingerprint read(String fingerprintPath) throws IOException { EventFingerprint eventFingerprint; try (DataInputStream dataInputStream = new DataInputStream(IOUtils.getInputStream(IOUtils.getFileUrl(fingerprintPath)))) { @@ -123,8 +123,6 @@ public static EventFingerprint read(String fingerprintPath) { // Create EventFingerprint object eventFingerprint = new EventFingerprint(timeArray, eventTypeCounter, hash); - } catch (IOException e) { - throw new UncheckedIOException(e); } return eventFingerprint; diff --git a/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/EventsFileFingerprintComparator.java b/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/EventsFileFingerprintComparator.java index ca10bc0bf08..2152758b215 100644 --- a/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/EventsFileFingerprintComparator.java +++ b/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/EventsFileFingerprintComparator.java @@ -7,6 +7,8 @@ import org.matsim.core.events.EventsUtils; import javax.annotation.Nullable; +import java.io.IOException; +import java.io.UncheckedIOException; import java.util.Arrays; /** @@ -25,7 +27,19 @@ private EventsFileFingerprintComparator() { */ public static FingerprintEventHandler createFingerprintHandler(final String eventsfile, @Nullable String compareFingerprint) { - FingerprintEventHandler handler = new FingerprintEventHandler(compareFingerprint != null ? EventFingerprint.read(compareFingerprint) : null); + EventFingerprint fp = null; + Exception err = null; + if (compareFingerprint != null) { + try { + fp = EventFingerprint.read(compareFingerprint); + } catch (Exception e) { + log.warn("Could not read compare fingerprint from file: {}", compareFingerprint, e); + fp = new EventFingerprint(); + err = e; + } + } + + FingerprintEventHandler handler = new FingerprintEventHandler(fp); EventsManager manager = EventsUtils.createEventsManager(); @@ -36,15 +50,27 @@ public static FingerprintEventHandler createFingerprintHandler(final String even manager.finishProcessing(); handler.finishProcessing(); + // File error overwrite any other error + if (err != null) { + handler.setComparisonResult(ComparisonResult.FILE_ERROR); + handler.setComparisonMessage(err.getMessage()); + } + return handler; } public static ComparisonResult compareFingerprints(final String fp1, final String fp2) { - EventFingerprint fingerprint1 = EventFingerprint.read(fp1); - EventFingerprint fingerprint2 = EventFingerprint.read(fp2); + EventFingerprint fingerprint1; + EventFingerprint fingerprint2; + try { + fingerprint1 = EventFingerprint.read(fp1); + fingerprint2 = EventFingerprint.read(fp2); + } catch (IOException e) { + throw new UncheckedIOException(e); + } - String logMessage = ""; + String logMessage = ""; //Check if time array size is the same if (fingerprint1.timeArray.size() != fingerprint2.timeArray.size()) { logMessage = "Different number of timesteps"; diff --git a/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/FingerprintEventHandler.java b/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/FingerprintEventHandler.java index 2469f65759e..8e12c10a53c 100644 --- a/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/FingerprintEventHandler.java +++ b/matsim/src/main/java/org/matsim/utils/eventsfilecomparison/FingerprintEventHandler.java @@ -57,10 +57,18 @@ public ComparisonResult getComparisonResult() { return comparisonResult; } + void setComparisonResult(ComparisonResult comparisonResult) { + this.comparisonResult = comparisonResult; + } + public String getComparisonMessage() { return comparisonMessage; } + void setComparisonMessage(String comparisonMessage) { + this.comparisonMessage = comparisonMessage; + } + @Override public void handleEvent(Event event) {