MATSIM
EventsFileFingerprintComparator.java
Go to the documentation of this file.
1 package org.matsim.utils.eventsfilecomparison;
2 
3 import it.unimi.dsi.fastutil.objects.Object2IntMap;
4 import org.apache.logging.log4j.LogManager;
5 import org.apache.logging.log4j.Logger;
8 
9 import javax.annotation.Nullable;
10 import java.io.IOException;
11 import java.io.UncheckedIOException;
12 import java.util.Arrays;
13 
17 public final class EventsFileFingerprintComparator {
18 
19  private static final Logger log = LogManager.getLogger(EventsFileComparator.class);
20 
22  }
23 
24 
28  public static FingerprintEventHandler createFingerprintHandler(final String eventsfile, @Nullable String compareFingerprint) {
29 
30  EventFingerprint fp = null;
31  Exception err = null;
32  if (compareFingerprint != null) {
33  try {
34  fp = EventFingerprint.read(compareFingerprint);
35  } catch (Exception e) {
36  log.warn("Could not read compare fingerprint from file: {}", compareFingerprint, e);
37  fp = new EventFingerprint();
38  err = e;
39  }
40  }
41 
43 
45 
46  manager.addHandler(handler);
47 
48  EventsUtils.readEvents(manager, eventsfile);
49 
50  manager.finishProcessing();
51  handler.finishProcessing();
52 
53  // File error overwrite any other error
54  if (err != null) {
55  handler.setComparisonResult(ComparisonResult.FILE_ERROR);
56  handler.setComparisonMessage(err.getMessage());
57  }
58 
59  return handler;
60  }
61 
62  public static ComparisonResult compareFingerprints(final String fp1, final String fp2) {
63 
64  EventFingerprint fingerprint1;
65  EventFingerprint fingerprint2;
66  try {
67  fingerprint1 = EventFingerprint.read(fp1);
68  fingerprint2 = EventFingerprint.read(fp2);
69  } catch (IOException e) {
70  throw new UncheckedIOException(e);
71  }
72 
73  String logMessage = "";
74  //Check if time array size is the same
75  if (fingerprint1.timeArray.size() != fingerprint2.timeArray.size()) {
76  logMessage = "Different number of timesteps";
77  log.warn(logMessage);
79  }
80 
81  //Check if both time arrays have the same timesteps
82  if (!Arrays.equals(fingerprint1.timeArray.toFloatArray(), fingerprint2.timeArray.toFloatArray())) {
83  logMessage = "Different timesteps";
84  log.warn(logMessage);
86  }
87 
88 
89  //Check which event type counts are different among 2 fingerprints
90  boolean countDiffers = false;
91  for (Object2IntMap.Entry<String> entry1 : fingerprint1.eventTypeCounter.object2IntEntrySet()) {
92  String key = entry1.getKey();
93  int count1 = entry1.getIntValue();
94  int count2 = fingerprint2.eventTypeCounter.getInt(key);
95  if (count1 != count2) {
96  countDiffers = true;
97 
98  if (!logMessage.isEmpty())
99  logMessage += "\n";
100 
101  logMessage += "Count for event type '%s' differs: %d != %d".formatted(key, count1, count2);
102  }
103  }
104  if (countDiffers) {
105  log.warn(logMessage);
107  }
108 
109 
110  //Check if total hash is the same
111  byte[] hash1 = fingerprint1.hash;
112  byte[] hash2 = fingerprint2.hash;
113  if (!Arrays.equals(hash1, hash2)) {
114 
115  logMessage = String.format("Difference occurred hash codes hash of first file is %s, hash of second is %s", Arrays.toString(hash1), Arrays.toString(hash2));
116 
117  log.warn(logMessage);
119  }
120 
122 
123  }
124 }
static void readEvents(EventsManager events, String filename)
void addHandler(final EventHandler handler)
static EventsManager createEventsManager()
static ComparisonResult compareFingerprints(final String fp1, final String fp2)
static FingerprintEventHandler createFingerprintHandler(final String eventsfile, @Nullable String compareFingerprint)
static EventFingerprint read(String fingerprintPath)