| 139 | } |
| 140 | |
| 141 | void EndUses::addEndUse(double value, const EndUseFuelType& fuelType, const EndUseCategoryType& category, const std::string& subCategory) { |
| 142 | std::string units = this->getUnitsForFuelType(fuelType); |
| 143 | |
| 144 | // Try to find an existing fuelType attribute |
| 145 | boost::optional<Attribute> fuelTypeAttribute = m_attribute.findChildByName(fuelType.valueName()); |
| 146 | if (!fuelTypeAttribute) { |
| 147 | // If you can't, then add a new Attribute to the std::vector of Attribute |
| 148 | std::vector<Attribute> fuelTypeAttributes = m_attribute.valueAsAttributeVector(); |
| 149 | fuelTypeAttributes.push_back(Attribute(fuelType.valueName(), std::vector<Attribute>())); |
| 150 | std::sort(fuelTypeAttributes.begin(), fuelTypeAttributes.end(), FuelTypeAttributeSorter()); |
| 151 | m_attribute.setValue(fuelTypeAttributes); |
| 152 | fuelTypeAttribute = m_attribute.findChildByName(fuelType.valueName()); |
| 153 | } |
| 154 | OS_ASSERT(fuelTypeAttribute); |
| 155 | |
| 156 | // Within the FuelTypeAttribute, we look if the EndUseCategory actually exists |
| 157 | boost::optional<Attribute> categoryAttribute = fuelTypeAttribute->findChildByName(category.valueName()); |
| 158 | if (!categoryAttribute) { |
| 159 | // Otherwise we add it |
| 160 | std::vector<Attribute> categoryAttributes = fuelTypeAttribute->valueAsAttributeVector(); |
| 161 | categoryAttributes.push_back(Attribute(category.valueName(), std::vector<Attribute>())); |
| 162 | std::sort(categoryAttributes.begin(), categoryAttributes.end(), CategoryAttributeSorter()); |
| 163 | fuelTypeAttribute->setValue(categoryAttributes); |
| 164 | categoryAttribute = fuelTypeAttribute->findChildByName(category.valueName()); |
| 165 | } |
| 166 | OS_ASSERT(categoryAttribute); |
| 167 | |
| 168 | // Within that category, if we find an existing SubCategory, then we add our value that category |
| 169 | bool found = false; |
| 170 | std::vector<Attribute> subCategories = categoryAttribute->valueAsAttributeVector(); |
| 171 | std::vector<Attribute> newSubCategories = subCategories; |
| 172 | for (unsigned i = 0; i < subCategories.size(); ++i) { |
| 173 | if (subCategories[i].name() == subCategory) { |
| 174 | OS_ASSERT(!found); |
| 175 | newSubCategories[i] = Attribute(subCategory, subCategories[i].valueAsDouble() + value, subCategories[i].units()); |
| 176 | found = true; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // If we didn't find the proper Subcategory, we create it |
| 181 | if (!found) { |
| 182 | newSubCategories.push_back(Attribute(subCategory, value, units)); |
| 183 | std::sort(newSubCategories.begin(), newSubCategories.end(), SubCategoryAttributeSorter()); |
| 184 | } |
| 185 | |
| 186 | categoryAttribute->setValue(newSubCategories); |
| 187 | } |
| 188 | |
| 189 | double EndUses::getEndUse(const EndUseFuelType& fuelType, const EndUseCategoryType& category, const std::string& subCategory) const { |
| 190 | double result = 0; |