boost::optional<ScheduleWeek> ScheduleYear_Impl::getScheduleWeek(const openstudio::Date& date) const { YearDescription yd = this->model().getUniqueModelObject<YearDescription>(); if (yd.assumedYear() != date.assumedBaseYear()){ LOG(Error, "Assumed base year " << date.assumedBaseYear() << " of date does not match this model's assumed base year of " << yd.assumedYear()); return boost::none; } std::vector<ScheduleWeek> scheduleWeeks = this->scheduleWeeks(); // these are already sorted std::vector<openstudio::Date> dates = this->dates(); // these are already sorted unsigned N = dates.size(); OS_ASSERT(scheduleWeeks.size() == N); if (N == 0){ return boost::none; } boost::optional<ScheduleWeek> result; for (unsigned i = 0; i < N; ++i){ // want first date which is greater than or equal to the target date if(dates[i] >= date){ result = scheduleWeeks[i]; break; } } return result; }
bool ScheduleYear_Impl::addScheduleWeek(const openstudio::Date& untilDate, const ScheduleWeek& scheduleWeek) { YearDescription yd = this->model().getUniqueModelObject<YearDescription>(); if (yd.assumedYear() != untilDate.assumedBaseYear()){ LOG(Error, "Assumed base year " << untilDate.assumedBaseYear() << " of untilDate does not match this model's assumed base year of " << yd.assumedYear()); return false; } std::vector<ScheduleWeek> scheduleWeeks = this->scheduleWeeks(); // these are already sorted std::vector<openstudio::Date> dates = this->dates(); // these are already sorted bool inserted = false; unsigned N = dates.size(); OS_ASSERT(scheduleWeeks.size() == N); this->clearExtensibleGroups(); for (unsigned i = 0; i < N; ++i){ if (dates[i] == untilDate){ bool doEmit = (i == (N-1)); // push back just this schedule/date pair std::vector<std::string> groupValues; groupValues.push_back(boost::lexical_cast<std::string>(untilDate.monthOfYear().value())); groupValues.push_back(boost::lexical_cast<std::string>(untilDate.dayOfMonth())); groupValues.push_back(scheduleWeek.name().get()); ModelExtensibleGroup group = pushExtensibleGroup(groupValues, doEmit).cast<ModelExtensibleGroup>(); OS_ASSERT(!group.empty()); inserted = true; }else{ // if we need to insert new schedule/date pair here if ((untilDate < dates[i]) && !inserted){ // push back this schedule/date pair std::vector<std::string> groupValues; groupValues.push_back(boost::lexical_cast<std::string>(untilDate.monthOfYear().value())); groupValues.push_back(boost::lexical_cast<std::string>(untilDate.dayOfMonth())); groupValues.push_back(scheduleWeek.name().get()); ModelExtensibleGroup group = pushExtensibleGroup(groupValues, false).cast<ModelExtensibleGroup>(); OS_ASSERT(!group.empty()); inserted = true; } bool doEmit = (i == (N-1)) && inserted; // insert existing schedule/date pair std::vector<std::string> groupValues; groupValues.push_back(boost::lexical_cast<std::string>(dates[i].monthOfYear().value())); groupValues.push_back(boost::lexical_cast<std::string>(dates[i].dayOfMonth())); groupValues.push_back(scheduleWeeks[i].name().get()); ModelExtensibleGroup group = pushExtensibleGroup(groupValues, doEmit).cast<ModelExtensibleGroup>(); OS_ASSERT(!group.empty()); } } if (!inserted){ // push back this schedule/date pair std::vector<std::string> groupValues; groupValues.push_back(boost::lexical_cast<std::string>(untilDate.monthOfYear().value())); groupValues.push_back(boost::lexical_cast<std::string>(untilDate.dayOfMonth())); groupValues.push_back(scheduleWeek.name().get()); ModelExtensibleGroup group = pushExtensibleGroup(groupValues, true).cast<ModelExtensibleGroup>(); OS_ASSERT(!group.empty()); } return true; }