MATSIM
Hermes.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2014 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 package org.matsim.core.mobsim.hermes;
20 
21 import org.apache.logging.log4j.LogManager;
22 import org.apache.logging.log4j.Logger;
23 import org.matsim.api.core.v01.Id;
24 import org.matsim.api.core.v01.Scenario;
29 
30 final class Hermes implements Mobsim {
31 
32  final private static Logger log = LogManager.getLogger(Hermes.class);
33  private Realm realm;
34  private Agent[] agents;
35  private ScenarioImporter scenarioImporter;
36  private final Scenario scenario;
37  private final EventsManager eventsManager;
38 
39  public Hermes(Scenario scenario, EventsManager eventsManager) {
40  this.scenario = scenario;
41  this.eventsManager = eventsManager;
42  }
43 
44  private void importScenario() throws Exception {
45  scenarioImporter = ScenarioImporter.instance(scenario, eventsManager);
46  scenarioImporter.generate();
47  this.realm = scenarioImporter.realm;
48  this.agents = scenarioImporter.hermesAgents;
49  }
50 
51  private void processEvents() {
52  eventsManager.processEvents(realm.getSortedEvents());
53 
54  for (Agent agent : agents) {
55  if (agent != null && !agent.finished() && !agent.isTransitVehicle()) {
56  int matsim_id = scenarioImporter.matsim_id(agent.id(), false);
57  eventsManager.processEvent(
58  new PersonStuckEvent(
59  HermesConfigGroup.SIM_STEPS, Id.get(matsim_id, Person.class), Id.createLinkId("0"), "zero"));
60  }
61  }
62  }
63 
64  @Override
65  public void run() {
66  long time;
67  try {
68  time = System.currentTimeMillis();
69  importScenario();
70  log.info(String.format("Hermes importing scenario took %d ms", System.currentTimeMillis() - time));
71 
72  eventsManager.initProcessing();
73 
74  time = System.currentTimeMillis();
75  realm.run();
76  log.info(String.format(
77  "Hermes took %d ms", System.currentTimeMillis() - time));
78 
79  time = System.currentTimeMillis();
80  processEvents();
81  eventsManager.finishProcessing();
82  log.info(String.format("Hermes MATSim event processing took %d ms", System.currentTimeMillis() - time));
83 
84  // Launch scenario imported in background
85  scenarioImporter.reset();
86  } catch (Exception e) {
87  throw new RuntimeException(e);
88  }
89  }
90 }