| 145 | } |
| 146 | |
| 147 | bool ScheduleYear_Impl::addScheduleWeek(const openstudio::Date& untilDate, const ScheduleWeek& scheduleWeek) { |
| 148 | YearDescription yd = this->model().getUniqueModelObject<YearDescription>(); |
| 149 | |
| 150 | if (yd.assumedYear() != untilDate.assumedBaseYear()) { |
| 151 | LOG(Error, "Assumed base year " << untilDate.assumedBaseYear() << " of untilDate does not match this model's assumed base year of " |
| 152 | << yd.assumedYear()); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | std::vector<ScheduleWeek> scheduleWeeks = this->scheduleWeeks(); // these are already sorted |
| 157 | std::vector<openstudio::Date> dates = this->dates(); // these are already sorted |
| 158 | bool inserted = false; |
| 159 | |
| 160 | unsigned N = dates.size(); |
| 161 | OS_ASSERT(scheduleWeeks.size() == N); |
| 162 | |
| 163 | this->clearExtensibleGroups(); |
| 164 | |
| 165 | for (unsigned i = 0; i < N; ++i) { |
| 166 | |
| 167 | if (dates[i] == untilDate) { |
| 168 | |
| 169 | bool doEmit = (i == (N - 1)); |
| 170 | |
| 171 | // push back just this schedule/date pair |
| 172 | std::vector<std::string> groupValues{boost::lexical_cast<std::string>(untilDate.monthOfYear().value()), |
| 173 | boost::lexical_cast<std::string>(untilDate.dayOfMonth()), scheduleWeek.name().get()}; |
| 174 | |
| 175 | auto group = pushExtensibleGroup(groupValues, doEmit); |
| 176 | OS_ASSERT(!group.empty()); |
| 177 | |
| 178 | inserted = true; |
| 179 | |
| 180 | } else { |
| 181 | |
| 182 | // if we need to insert new schedule/date pair here |
| 183 | if ((untilDate < dates[i]) && !inserted) { |
| 184 | |
| 185 | // push back this schedule/date pair |
| 186 | std::vector<std::string> groupValues{boost::lexical_cast<std::string>(untilDate.monthOfYear().value()), |
| 187 | boost::lexical_cast<std::string>(untilDate.dayOfMonth()), scheduleWeek.name().get()}; |
| 188 | |
| 189 | auto group = pushExtensibleGroup(groupValues, false); |
| 190 | OS_ASSERT(!group.empty()); |
| 191 | |
| 192 | inserted = true; |
| 193 | } |
| 194 | |
| 195 | bool doEmit = (i == (N - 1)) && inserted; |
| 196 | |
| 197 | // insert existing schedule/date pair |
| 198 | std::vector<std::string> groupValues{boost::lexical_cast<std::string>(dates[i].monthOfYear().value()), |
| 199 | boost::lexical_cast<std::string>(dates[i].dayOfMonth()), scheduleWeeks[i].name().get()}; |
| 200 | |
| 201 | auto group = pushExtensibleGroup(groupValues, doEmit); |
| 202 | OS_ASSERT(!group.empty()); |
| 203 | } |
| 204 | } |