Skip to content

Commit

Permalink
handle file errors as separate comparison result
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Mar 13, 2024
1 parent a6ca470 commit d955a39
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Original file line number Diff line number Diff line change
Expand Up @@ -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)))) {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();

Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down

0 comments on commit d955a39

Please sign in to comment.