Stride Reference Manual  1.0
TravellerScheduleReader.cpp
Go to the documentation of this file.
2 #include "util/InstallDirs.h"
3 
4 #include <fstream>
5 #include <stdexcept>
6 #include <iostream>
7 #include <iostream>
8 
9 using namespace stride;
10 using namespace util;
11 using namespace std;
12 
13 bool stride::util::operator==(const Flight& flight1, const Flight& flight2) {
14  return flight1.m_source_sim == flight2.m_source_sim
15  && flight1.m_destination_sim == flight2.m_destination_sim
16  && flight1.m_amount == flight2.m_amount
17  && flight1.m_duration == flight2.m_duration
18  && flight1.m_day_of_the_week == flight2.m_day_of_the_week;
19 }
20 
22  string complete_filename = (InstallDirs::getDataDir() /= filename).string();
23 
24  // Read the file
25  parseTree(complete_filename);
26 
27  Schedule schedule;
28  auto schedule_config = m_pt.get_child("travel_schedule");
29 
30  // Loop over all flights
31  for (auto it = schedule_config.begin(); it != schedule_config.end(); it++) {
32  // Parse the flight and add it to the schedule
33  Flight new_flight = parseFlight(it->second);
34  schedule[new_flight.m_day_of_the_week].push_back(new_flight);
35  }
36  return schedule;
37 }
38 
39 void TravellerScheduleReader::parseTree(string filename) {
40  // Parse the file
41  try {
42  boost::property_tree::read_xml(filename, m_pt);
43  } catch (...) {
44  throw invalid_argument("In TravellerScheduleReader: Error while parsing.");
45  }
46 }
47 
48 Flight TravellerScheduleReader::parseFlight(boost::property_tree::ptree& node) const {
49  string source_sim = node.get<string>("direction.<xmlattr>.src");
50  string destination_sim = node.get<string>("direction.<xmlattr>.dest");
51 
52  int amount = node.get<int>("<xmlattr>.amount");
53  if (amount < 0) {
54  throw invalid_argument("In TravellerScheduleReader: Invalid amount of travellers.");
55  }
56 
57  int duration = node.get<int>("<xmlattr>.duration");
58  if (duration < 0) {
59  throw invalid_argument("In TravellerScheduleReader: Invalid duration.");
60  }
61 
62  int day_of_the_week = node.get<int>("<xmlattr>.day_of_the_week");
63  if (day_of_the_week < 0 || day_of_the_week > 6) {
64  throw invalid_argument("In TravellerScheduleReader: Invalid day of the week.");
65  }
66 
67  string district = node.get<string>("arrival.<xmlattr>.district");
68  string facility = node.get<string>("arrival.<xmlattr>.facility");
69 
70  return Flight(source_sim, destination_sim, uint(amount), uint(duration), uint(day_of_the_week), district, facility);
71 }
Interface for install directory queries.
Schedule readSchedule(string filename)
Expects a worthy path.
Time Dependent Person DataType.
Definition: NoBehaviour.h:17
static boost::filesystem::path getDataDir()
Utility method: get path to the directory for data files.
array< vector< Flight >, 7 > Schedule
STL namespace.
bool operator==(const GeoCoordinate &coord1, const GeoCoordinate &coord2)
Flight parseFlight(boost::property_tree::ptree &node) const