MATSIM
Worker.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2010 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.utils.eventsfilecomparison;
21 
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.concurrent.BrokenBarrierException;
29 import java.util.concurrent.CyclicBarrier;
30 
31 import org.apache.logging.log4j.LogManager;
32 import org.apache.logging.log4j.Logger;
38 
39 class Worker extends Thread implements BasicEventHandler{
40 
41  private static final Logger log = LogManager.getLogger(Worker.class);
42 
43  private final EventsManager eventsManager;
44  private final String eFile;
45  private final CyclicBarrier doComparison;
46  private final boolean ignoringCoordinates;
47 
48  private final Map<String,Counter> events = new HashMap<String,Counter>();
49 
50  private volatile double time = -1;
51  private volatile boolean finished = false;
52  private volatile int numEvents = 0;
53 
54  Worker( String eFile1, final CyclicBarrier doComparison, boolean ignoringCoordinates ) {
55  this.eFile = eFile1;
56  this.doComparison = doComparison;
57  this.ignoringCoordinates = ignoringCoordinates;
58 
59  this.eventsManager = new SingleHandlerEventsManager(this);
60 
61  }
62 
63  /*package*/ String getEventsFile() {
64  return this.eFile;
65  }
66 
67  @Override
68  public void run() {
69  try {
70  new MatsimEventsReader(this.eventsManager).readFile(this.eFile);
71  this.finished = true;
72  try {
73  this.doComparison.await();
74 
75  } catch (InterruptedException e1) {
76  throw new ComparatorInterruptedException(e1);
77  } catch (BrokenBarrierException e1) {
78  throw new ComparatorInterruptedException(e1);
79  }
80  } catch (ComparatorInterruptedException e1) {
81 // log.info("events-comparator got interrupted", e1);
82  log.info("events-comparator got interrupted");
83  }
84  }
85 
86  public boolean isFinished() {
87  return this.finished;
88  }
89 
90  public Map<String, Counter> getEventsMap() {
91  return this.events;
92  }
93 
94  public double getCurrentTime() {
95  return this.time;
96  }
97 
98  @Override
99  public void reset(int iteration) {
100  }
101 
102  @Override
103  public void handleEvent(Event event) {
104  if (this.time != event.getTime()) {
105  try {
106  doComparison.await();
107  } catch (InterruptedException e1) {
108  throw new ComparatorInterruptedException(e1);
109  } catch (BrokenBarrierException e1) {
110  throw new ComparatorInterruptedException(e1);
111  }
112 
113  this.events.clear();
114  this.time = event.getTime();
115  this.numEvents = 0;
116  }
117 
118  addEvent(event);
119  }
120 
121  public int getNumEvents() {
122  return this.numEvents;
123  }
124 
125  private void addEvent(Event event) {
126  this.numEvents++;
127  String lexString = toLexicographicSortedString(event);
128  Counter counter = this.events.get(lexString);
129  if (counter == null) {
130  counter = new Counter();
131  this.events.put(lexString, counter);
132  }
133  counter.increment();
134  }
135 
136  private String toLexicographicSortedString(Event event) {
137  List<String> strings = new ArrayList<String>();
138  for (Entry<String, String> e : event.getAttributes().entrySet()) {
139  StringBuilder tmp = new StringBuilder();
140  final String key = e.getKey();
141 
142  // don't look at coordinates if configured as such:
143  if ( ignoringCoordinates ){
144  switch( key ){
145  case Event.ATTRIBUTE_X:
146  case Event.ATTRIBUTE_Y:
147  case Event.ATTRIBUTE_TIME:
148  continue;
149  }
150  }
151 
152  tmp.append( key );
153  tmp.append("=");
154  tmp.append(e.getValue());
155  strings.add(tmp.toString());
156  }
157  Collections.sort(strings);
158  StringBuilder eventStr = new StringBuilder();
159  for (String str : strings) {
160  eventStr.append(" | ");
161  eventStr.append(str);
162  }
163 
164  eventStr.append(" | ") ;
165  return eventStr.toString();
166  }
167 
174  private static class ComparatorInterruptedException extends RuntimeException {
176  super(e);
177  }
178  }
179 
180 }