Example #1
0
  void EndUses::addEndUse(double value, const EndUseFuelType& fuelType, const EndUseCategoryType& category, const std::string& subCategory)
  {
    std::string units = this->getUnitsForFuelType(fuelType);

    boost::optional<Attribute> fuelTypeAttribute = m_attribute.findChildByName(fuelType.valueName());
    if(!fuelTypeAttribute){
      std::vector<Attribute> fuelTypeAttributes = m_attribute.valueAsAttributeVector();
      fuelTypeAttributes.push_back(Attribute(fuelType.valueName(), std::vector<Attribute>()));
      std::sort(fuelTypeAttributes.begin(), fuelTypeAttributes.end(), FuelTypeAttributeSorter());
      m_attribute.setValue(QVariant::fromValue(fuelTypeAttributes));
      fuelTypeAttribute = m_attribute.findChildByName(fuelType.valueName());
    }
    OS_ASSERT(fuelTypeAttribute);

    boost::optional<Attribute> categoryAttribute = fuelTypeAttribute->findChildByName(category.valueName());
    if(!categoryAttribute){
      std::vector<Attribute> categoryAttributes = fuelTypeAttribute->valueAsAttributeVector();
      categoryAttributes.push_back(Attribute(category.valueName(), std::vector<Attribute>()));
      std::sort(categoryAttributes.begin(), categoryAttributes.end(), CategoryAttributeSorter());
      fuelTypeAttribute->setValue(QVariant::fromValue(categoryAttributes));
      categoryAttribute = fuelTypeAttribute->findChildByName(category.valueName());
    }
    OS_ASSERT(categoryAttribute);

    bool found = false;
    std::vector<Attribute> subCategories = categoryAttribute->valueAsAttributeVector();
    std::vector<Attribute> newSubCategories = subCategories;
    for(unsigned i = 0; i < subCategories.size(); ++i){
      if (subCategories[i].name() == subCategory){
        OS_ASSERT(!found);
        newSubCategories[i] = Attribute(subCategory, subCategories[i].valueAsDouble() + value, subCategories[i].units());
        found = true;
      }
    }

    if (!found){
      newSubCategories.push_back(Attribute(subCategory, value, units));
      std::sort(newSubCategories.begin(), newSubCategories.end(), SubCategoryAttributeSorter());
    }

    categoryAttribute->setValue(QVariant::fromValue(newSubCategories));
  }
Example #2
0
  double EndUses::getEndUse(const EndUseFuelType& fuelType, const EndUseCategoryType& category) const
  {
    double result = 0;

    boost::optional<Attribute> fuelTypeAttribute = m_attribute.findChildByName(fuelType.valueName());
    if(fuelTypeAttribute){
      boost::optional<Attribute> categoryAttribute = fuelTypeAttribute->findChildByName(category.valueName());
      if(categoryAttribute){
        std::vector<Attribute> subCategories = categoryAttribute->valueAsAttributeVector();
        for(unsigned i = 0; i < subCategories.size(); ++i){
          result += subCategories[i].valueAsDouble();
        }
      }
    }

    return result;
  }