Stride Reference Manual  1.0
Calendar.cpp
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2017, Willem L, Kuylen E, Stijven S & Broeckhove J
14  */
15 
21 #include "Calendar.h"
22 
23 #include "util/InstallDirs.h"
24 
25 #include <boost/property_tree/json_parser.hpp>
26 
27 namespace stride {
28 
29 using namespace std;
30 using namespace boost::filesystem;
31 using namespace stride::util;
32 
33 Calendar::Calendar(const boost::property_tree::ptree& pt_config)
34  : m_day(0) {
35  // Set start date
36  const string start_date {pt_config.get<string>("run.start_date", "2016-01-01")};
37  m_date = boost::gregorian::from_simple_string(start_date);
38 
39  // Set holidays & school holidays
40  initializeHolidays(pt_config);
41 }
42 
44  m_day++;
45  m_date = m_date + boost::gregorian::date_duration(1);
46 }
47 
48 void Calendar::initializeHolidays(const boost::property_tree::ptree& pt_config) {
49  // Load json file
50  boost::property_tree::ptree pt_holidays;
51  {
52  const auto file_name {pt_config.get<string>("run.holidays", "holidays_flanders_2016.json")};
53  const auto file_path {InstallDirs::getDataDir() /= file_name};
54  if (!is_regular_file(file_path)) {
55  throw runtime_error(string(__func__) + "Holidays file " + file_path.string() + " not present.");
56  }
57  read_json(file_path.string(), pt_holidays);
58  }
59 
60  // Read in holidays
61  for (int i = 1; i < 13; i++) {
62  const string month {to_string(i)};
63  const string year {pt_holidays.get<string>("year", "2016")};
64 
65  // read in general holidays
66  const string general_key {"general." + month};
67  for (auto& date: pt_holidays.get_child(general_key)) {
68  const string date_string {year + "-" + month + "-" + date.second.get_value<string>()};
69  const auto new_holiday = boost::gregorian::from_simple_string(date_string);
70  m_holidays.push_back(new_holiday);
71  }
72 
73  // read in school holidays
74  const string school_key {"school." + month};
75  for (auto& date: pt_holidays.get_child(school_key)) {
76  const string date_string {year + "-" + month + "-" + date.second.get_value<string>()};
77  const auto new_holiday = boost::gregorian::from_simple_string(date_string);
78  m_school_holidays.push_back(new_holiday);
79  }
80  }
81 }
82 
83 }
std::vector< boost::gregorian::date > m_school_holidays
Vector of school holidays.
Definition: Calendar.h:78
Interface for install directory queries.
std::vector< boost::gregorian::date > m_holidays
Vector of general holidays.
Definition: Calendar.h:77
Header file for the Calendar class.
Time Dependent Person DataType.
Definition: NoBehaviour.h:17
static boost::filesystem::path getDataDir()
Utility method: get path to the directory for data files.
void initializeHolidays(const boost::property_tree::ptree &pt_config)
Definition: Calendar.cpp:48
Utilities for the project.
Definition: Infector.h:36
void advanceDay()
Advance the internal calendar by one day.
Definition: Calendar.cpp:43
Calendar(const boost::property_tree::ptree &pt_config)
Constructor.
Definition: Calendar.cpp:33
STL namespace.
std::size_t m_day
The current simulation day.
Definition: Calendar.h:75
boost::gregorian::date m_date
The current simulated day.
Definition: Calendar.h:76