src/alfalfa/AlfalfaJSON.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "AlfalfaJSON.hpp" | ||
| 7 | #include "AlfalfaJSON_Impl.hpp" | ||
| 8 | |||
| 9 | #include "AlfalfaComponent.hpp" | ||
| 10 | #include "AlfalfaActuator.hpp" | ||
| 11 | #include "AlfalfaConstant.hpp" | ||
| 12 | #include "AlfalfaMeter.hpp" | ||
| 13 | #include "AlfalfaGlobalVariable.hpp" | ||
| 14 | #include "AlfalfaOutputVariable.hpp" | ||
| 15 | |||
| 16 | #include "../utilities/core/Path.hpp" | ||
| 17 | #include "../utilities/core/PathHelpers.hpp" | ||
| 18 | #include "../utilities/core/Logger.hpp" | ||
| 19 | |||
| 20 | #include "../model/ModelObject.hpp" | ||
| 21 | #include "../model/ModelObject_Impl.hpp" | ||
| 22 | #include "../utilities/idf/IdfObject.hpp" | ||
| 23 | #include "../utilities/idd/IddObject.hpp" | ||
| 24 | #include "../utilities/idd/IddEnums.hpp" | ||
| 25 | |||
| 26 | #include <json/json.h> | ||
| 27 | #include <boost/optional.hpp> | ||
| 28 | #include <boost/none.hpp> | ||
| 29 | #include <fmt/format.h> | ||
| 30 | |||
| 31 | #include <vector> | ||
| 32 | #include <fstream> | ||
| 33 | #include <memory> | ||
| 34 | #include <utility> | ||
| 35 | #include <string> | ||
| 36 | |||
| 37 | namespace openstudio { | ||
| 38 | namespace alfalfa { | ||
| 39 | namespace detail { | ||
| 40 | 64 | AlfalfaJSON_Impl::AlfalfaJSON_Impl() = default; | |
| 41 | |||
| 42 | ✗ | AlfalfaJSON_Impl::AlfalfaJSON_Impl(const openstudio::path& p) { | |
| 43 | ✗ | setJSONPath(p); | |
| 44 | ✗ | } | |
| 45 | |||
| 46 | 73 | void AlfalfaJSON_Impl::exposePoint(const AlfalfaPoint& point) { | |
| 47 | 73 | m_points.push_back(point); | |
| 48 | 73 | } | |
| 49 | |||
| 50 | 25 | std::vector<AlfalfaPoint> AlfalfaJSON_Impl::points() const { | |
| 51 | 25 | return m_points; | |
| 52 | } | ||
| 53 | |||
| 54 | 2 | bool AlfalfaJSON_Impl::saveAs(const openstudio::path& path) { | |
| 55 | 2 | m_JSONPath = path; | |
| 56 | 2 | return save(); | |
| 57 | } | ||
| 58 | |||
| 59 | 2 | bool AlfalfaJSON_Impl::save() const { | |
| 60 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (m_JSONPath.empty()) { |
| 61 | ✗ | LOG(Error, "Unable to write Alfalfa JSON as path has not been specified"); | |
| 62 | ✗ | return false; | |
| 63 | } | ||
| 64 |
2/3✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
|
2 | if (makeParentFolder(m_JSONPath)) { |
| 65 |
1/1✓ Branch 1 taken 2 times.
|
2 | const Json::Value root = toJSON(); |
| 66 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
2 | std::ofstream outFile(openstudio::toSystemFilename(m_JSONPath)); |
| 67 | |||
| 68 |
2/3✓ Branch 1 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
|
2 | if (outFile) { |
| 69 | try { | ||
| 70 |
1/1✓ Branch 1 taken 2 times.
|
2 | outFile << root; |
| 71 |
1/1✓ Branch 1 taken 2 times.
|
2 | outFile.close(); |
| 72 | 2 | return true; | |
| 73 | ✗ | } catch (...) { | |
| 74 | ✗ | LOG(Error, "Unable to write file to path '" << toString(m_JSONPath) << "'."); | |
| 75 | ✗ | return false; | |
| 76 | ✗ | } | |
| 77 | } | ||
| 78 | 4 | } | |
| 79 | ✗ | LOG(Error, "Unable to write file to path '" << toString(m_JSONPath) << "', because parent directory " << "could not be created."); | |
| 80 | |||
| 81 | ✗ | return false; | |
| 82 | } | ||
| 83 | |||
| 84 | ✗ | void AlfalfaJSON_Impl::setJSONPath(const openstudio::path& p) { | |
| 85 | ✗ | m_JSONPath = p; | |
| 86 | ✗ | } | |
| 87 | |||
| 88 | 3 | Json::Value AlfalfaJSON_Impl::toJSON() const { | |
| 89 | 3 | Json::Value root; | |
| 90 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 7 taken 50 times.
✓ Branch 8 taken 3 times.
|
53 | for (Json::ArrayIndex i = 0; const auto& point : points()) { |
| 91 | // No guard here as the toJSON call will throw an exception if the id does not exist. | ||
| 92 |
2/2✓ Branch 1 taken 50 times.
✓ Branch 4 taken 50 times.
|
50 | root[i++] = point.toJSON(); |
| 93 | 3 | } | |
| 94 | 3 | return root; | |
| 95 | ✗ | } | |
| 96 | } // namespace detail | ||
| 97 | |||
| 98 | 64 | AlfalfaJSON::AlfalfaJSON() : m_impl(std::make_shared<detail::AlfalfaJSON_Impl>()) {} | |
| 99 | |||
| 100 | ✗ | AlfalfaJSON::AlfalfaJSON(const std::string& s) : m_impl(std::make_shared<detail::AlfalfaJSON_Impl>(s)) {} | |
| 101 | |||
| 102 | ✗ | AlfalfaJSON::AlfalfaJSON(const openstudio::path& p) : m_impl(std::make_shared<detail::AlfalfaJSON_Impl>(p)) {} | |
| 103 | |||
| 104 | 3 | boost::optional<AlfalfaPoint> AlfalfaJSON::exposeConstant(float value, const std::string& display_name) { | |
| 105 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | return exposeFromComponent(AlfalfaConstant{value}, display_name); |
| 106 | } | ||
| 107 | |||
| 108 | 8 | boost::optional<AlfalfaPoint> AlfalfaJSON::exposeMeter(const std::string& meter_name, const std::string& display_name) { | |
| 109 |
3/3✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
|
8 | return exposeFromComponent(AlfalfaMeter{meter_name}, display_name); |
| 110 | } | ||
| 111 | |||
| 112 | 8 | boost::optional<AlfalfaPoint> AlfalfaJSON::exposeActuator(const std::string& component_name, const std::string& component_type, | |
| 113 | const std::string& control_type, const std::string& display_name) { | ||
| 114 |
3/3✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
|
8 | return exposeFromComponent(AlfalfaActuator{component_name, component_type, control_type}, display_name); |
| 115 | } | ||
| 116 | |||
| 117 | 8 | boost::optional<AlfalfaPoint> AlfalfaJSON::exposeOutputVariable(const std::string& variable_key, const std::string& variable_name, | |
| 118 | const std::string& display_name) { | ||
| 119 |
3/3✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
|
8 | return exposeFromComponent(AlfalfaOutputVariable{variable_key, variable_name}, display_name); |
| 120 | } | ||
| 121 | |||
| 122 | 8 | boost::optional<AlfalfaPoint> AlfalfaJSON::exposeGlobalVariable(const std::string& variable_name, const std::string& display_name) { | |
| 123 |
3/3✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
|
8 | return exposeFromComponent(AlfalfaGlobalVariable{variable_name}, display_name); |
| 124 | } | ||
| 125 | |||
| 126 | 73 | void AlfalfaJSON::exposePoint(const AlfalfaPoint& point) { | |
| 127 | 73 | m_impl->exposePoint(point); | |
| 128 | 73 | } | |
| 129 | |||
| 130 | 39 | boost::optional<AlfalfaPoint> AlfalfaJSON::exposeFromObject(const openstudio::IdfObject& idf_object, const std::string& display_name) { | |
| 131 |
2/2✓ Branch 1 taken 39 times.
✓ Branch 4 taken 39 times.
|
39 | const IddObjectType idd_type = idf_object.iddObject().type(); |
| 132 |
1/1✓ Branch 1 taken 39 times.
|
39 | std::string display_name_ = display_name; |
| 133 | |||
| 134 |
2/2✓ Branch 1 taken 15 times.
✓ Branch 2 taken 24 times.
|
39 | if (display_name.empty()) { |
| 135 |
3/3✓ Branch 1 taken 15 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 6 times.
|
30 | if (auto name_ = getName(idf_object)) { |
| 136 |
1/1✓ Branch 1 taken 9 times.
|
9 | display_name_ = std::move(*name_); |
| 137 | 15 | } | |
| 138 | } | ||
| 139 | |||
| 140 | 39 | boost::optional<AlfalfaComponent> component; | |
| 141 | try { | ||
| 142 |
3/3✓ Branch 1 taken 39 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 31 times.
|
39 | if (AlfalfaActuator::acceptsObjectType(idd_type)) { |
| 143 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
|
8 | component = AlfalfaActuator(idf_object); |
| 144 |
3/3✓ Branch 1 taken 31 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 23 times.
|
31 | } else if (AlfalfaMeter::acceptsObjectType(idd_type)) { |
| 145 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
|
8 | component = AlfalfaMeter(idf_object); |
| 146 |
3/3✓ Branch 1 taken 23 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 7 times.
|
23 | } else if (AlfalfaOutputVariable::acceptsObjectType(idd_type)) { |
| 147 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
|
16 | component = AlfalfaOutputVariable(idf_object); |
| 148 |
2/3✓ Branch 1 taken 7 times.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
|
7 | } else if (AlfalfaGlobalVariable::acceptsObjectType(idd_type)) { |
| 149 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
|
7 | component = AlfalfaGlobalVariable(idf_object); |
| 150 | } else { | ||
| 151 | ✗ | LOG(Error, "Unable to create Alfalfa Point from Object of type " + idd_type.valueDescription()); | |
| 152 | ✗ | return boost::none; | |
| 153 | } | ||
| 154 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | } catch (std::runtime_error& error) { |
| 155 |
5/5✓ Branch 1 taken 9 times.
✓ Branch 5 taken 9 times.
✓ Branch 8 taken 9 times.
✓ Branch 11 taken 9 times.
✓ Branch 14 taken 9 times.
|
9 | LOG(Error, error.what()); |
| 156 | 9 | return boost::none; | |
| 157 | 9 | } | |
| 158 |
2/2✓ Branch 1 taken 30 times.
✓ Branch 4 taken 30 times.
|
30 | return exposeFromComponent(*component, display_name_); |
| 159 | 39 | } | |
| 160 | |||
| 161 | 71 | boost::optional<AlfalfaPoint> AlfalfaJSON::exposeFromComponent(const AlfalfaComponent& component, const std::string& display_name) { | |
| 162 |
1/1✓ Branch 1 taken 71 times.
|
71 | std::string display_name_ = display_name; |
| 163 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 67 times.
|
71 | if (display_name.empty()) { |
| 164 |
1/1✓ Branch 1 taken 4 times.
|
4 | display_name_ = component.deriveName(); |
| 165 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if (display_name_.empty()) { |
| 166 | ✗ | LOG(Error, fmt::format("{} points must be provided with a display name", component.typeName())); | |
| 167 | ✗ | return boost::none; | |
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 |
1/1✓ Branch 1 taken 71 times.
|
71 | AlfalfaPoint point(display_name_); |
| 172 |
3/3✓ Branch 1 taken 71 times.
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 39 times.
|
71 | if (component.canInput()) { |
| 173 |
1/1✓ Branch 1 taken 32 times.
|
32 | point.setInput(component); |
| 174 | } | ||
| 175 |
2/3✓ Branch 1 taken 71 times.
✓ Branch 3 taken 71 times.
✗ Branch 4 not taken.
|
71 | if (component.canOutput()) { |
| 176 |
1/1✓ Branch 1 taken 71 times.
|
71 | point.setOutput(component); |
| 177 | } | ||
| 178 |
1/1✓ Branch 1 taken 71 times.
|
71 | exposePoint(point); |
| 179 |
1/1✓ Branch 2 taken 71 times.
|
71 | return {std::move(point)}; |
| 180 | 71 | } | |
| 181 | |||
| 182 | 15 | boost::optional<std::string> AlfalfaJSON::getName(const openstudio::IdfObject& idf_object) { | |
| 183 |
3/3✓ Branch 1 taken 15 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 6 times.
|
30 | if (auto mo_ = idf_object.optionalCast<openstudio::model::ModelObject>()) { |
| 184 |
4/4✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 4 times.
|
18 | if (boost::optional<std::string> display_name = mo_->displayName()) { |
| 185 | 5 | return display_name; | |
| 186 | 9 | } | |
| 187 | 15 | } | |
| 188 |
3/3✓ Branch 1 taken 10 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 3 times.
|
20 | if (auto name_ = idf_object.name()) { |
| 189 |
3/3✓ Branch 1 taken 7 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 3 times.
|
7 | if (!name_->empty()) { |
| 190 | 4 | return name_; | |
| 191 | } | ||
| 192 | 10 | } | |
| 193 | 6 | return boost::none; | |
| 194 | } | ||
| 195 | |||
| 196 | 2 | bool AlfalfaJSON::saveAs(const openstudio::path& p) { | |
| 197 | 2 | return m_impl->saveAs(p); | |
| 198 | } | ||
| 199 | |||
| 200 | ✗ | bool AlfalfaJSON::save() const { | |
| 201 | ✗ | return m_impl->save(); | |
| 202 | } | ||
| 203 | |||
| 204 | 1 | Json::Value AlfalfaJSON::toJSON() const { | |
| 205 | 1 | return m_impl->toJSON(); | |
| 206 | } | ||
| 207 | |||
| 208 | 22 | std::vector<AlfalfaPoint> AlfalfaJSON::points() const { | |
| 209 | 22 | return m_impl->points(); | |
| 210 | } | ||
| 211 | |||
| 212 | } // namespace alfalfa | ||
| 213 | } // namespace openstudio | ||
| 214 |