src/model/PythonPluginInstance.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 "PythonPluginInstance.hpp" | ||
| 7 | #include "PythonPluginInstance_Impl.hpp" | ||
| 8 | |||
| 9 | #include "ExternalFile.hpp" | ||
| 10 | #include "ExternalFile_Impl.hpp" | ||
| 11 | #include "Model.hpp" | ||
| 12 | #include "Model_Impl.hpp" | ||
| 13 | |||
| 14 | #include <utilities/idd/IddFactory.hxx> | ||
| 15 | #include <utilities/idd/IddEnums.hxx> | ||
| 16 | #include <utilities/idd/OS_PythonPlugin_Instance_FieldEnums.hxx> | ||
| 17 | |||
| 18 | #include "../utilities/units/Unit.hpp" | ||
| 19 | #include "../utilities/core/Assert.hpp" | ||
| 20 | |||
| 21 | #include <boost/regex.hpp> | ||
| 22 | |||
| 23 | #include <algorithm> | ||
| 24 | #include <sstream> | ||
| 25 | #include <string> | ||
| 26 | #include <vector> | ||
| 27 | |||
| 28 | namespace openstudio { | ||
| 29 | namespace model { | ||
| 30 | |||
| 31 | namespace detail { | ||
| 32 | |||
| 33 | 11 | PythonPluginInstance_Impl::PythonPluginInstance_Impl(const IdfObject& idfObject, Model_Impl* model, bool keepHandle) | |
| 34 | 11 | : ResourceObject_Impl(idfObject, model, keepHandle) { | |
| 35 |
4/6✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 11 times.
✗ Branch 13 not taken.
|
11 | OS_ASSERT(idfObject.iddObject().type() == PythonPluginInstance::iddObjectType()); |
| 36 | 11 | } | |
| 37 | |||
| 38 | ✗ | PythonPluginInstance_Impl::PythonPluginInstance_Impl(const openstudio::detail::WorkspaceObject_Impl& other, Model_Impl* model, bool keepHandle) | |
| 39 | ✗ | : ResourceObject_Impl(other, model, keepHandle) { | |
| 40 | ✗ | OS_ASSERT(other.iddObject().type() == PythonPluginInstance::iddObjectType()); | |
| 41 | ✗ | } | |
| 42 | |||
| 43 | 7 | PythonPluginInstance_Impl::PythonPluginInstance_Impl(const PythonPluginInstance_Impl& other, Model_Impl* model, bool keepHandle) | |
| 44 | 7 | : ResourceObject_Impl(other, model, keepHandle) {} | |
| 45 | |||
| 46 | ✗ | const std::vector<std::string>& PythonPluginInstance_Impl::outputVariableNames() const { | |
| 47 | ✗ | static const std::vector<std::string> result; | |
| 48 | ✗ | if (result.empty()) { | |
| 49 | } | ||
| 50 | ✗ | return result; | |
| 51 | } | ||
| 52 | |||
| 53 | ✗ | IddObjectType PythonPluginInstance_Impl::iddObjectType() const { | |
| 54 | ✗ | return PythonPluginInstance::iddObjectType(); | |
| 55 | } | ||
| 56 | |||
| 57 | ✗ | std::vector<ResourceObject> PythonPluginInstance_Impl::resources() const { | |
| 58 | ✗ | return {externalFile()}; | |
| 59 | ✗ | } | |
| 60 | |||
| 61 | 28 | ExternalFile PythonPluginInstance_Impl::externalFile() const { | |
| 62 |
2/2✓ Branch 1 taken 28 times.
✓ Branch 4 taken 28 times.
|
28 | auto value = getObject<ModelObject>().getModelObjectTarget<ExternalFile>(OS_PythonPlugin_InstanceFields::ExternalFileName); |
| 63 |
1/3✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 4 not taken.
|
28 | OS_ASSERT(value); |
| 64 |
2/2✓ Branch 1 taken 28 times.
✓ Branch 4 taken 28 times.
|
56 | return value.get(); |
| 65 | 28 | } | |
| 66 | |||
| 67 | 8 | bool PythonPluginInstance_Impl::runDuringWarmupDays() const { | |
| 68 |
1/1✓ Branch 1 taken 8 times.
|
8 | boost::optional<std::string> value = getString(OS_PythonPlugin_InstanceFields::RunDuringWarmupDays, true); |
| 69 |
1/3✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 4 not taken.
|
8 | OS_ASSERT(value); |
| 70 |
3/3✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
|
16 | return openstudio::istringEqual(value.get(), "Yes"); |
| 71 | 8 | } | |
| 72 | |||
| 73 | 3 | bool PythonPluginInstance_Impl::isRunDuringWarmupDaysDefaulted() const { | |
| 74 | 3 | return isEmpty(OS_PythonPlugin_InstanceFields::RunDuringWarmupDays); | |
| 75 | } | ||
| 76 | |||
| 77 | 10 | std::string PythonPluginInstance_Impl::pluginClassName() const { | |
| 78 |
1/1✓ Branch 1 taken 10 times.
|
10 | boost::optional<std::string> value = getString(OS_PythonPlugin_InstanceFields::PluginClassName, true); |
| 79 |
1/3✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 4 not taken.
|
10 | OS_ASSERT(value); |
| 80 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
|
20 | return value.get(); |
| 81 | 10 | } | |
| 82 | |||
| 83 | 4 | bool PythonPluginInstance_Impl::setRunDuringWarmupDays(bool runDuringWarmupDays) { | |
| 84 | 4 | bool result = false; | |
| 85 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (runDuringWarmupDays) { |
| 86 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
6 | result = setString(OS_PythonPlugin_InstanceFields::RunDuringWarmupDays, "Yes"); |
| 87 | } else { | ||
| 88 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
8 | result = setString(OS_PythonPlugin_InstanceFields::RunDuringWarmupDays, "No"); |
| 89 | } | ||
| 90 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | OS_ASSERT(result); |
| 91 | 4 | return result; | |
| 92 | } | ||
| 93 | |||
| 94 | 1 | void PythonPluginInstance_Impl::resetRunDuringWarmupDays() { | |
| 95 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
2 | bool result = setString(OS_PythonPlugin_InstanceFields::RunDuringWarmupDays, ""); |
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1 | OS_ASSERT(result); |
| 97 | 1 | } | |
| 98 | |||
| 99 | 13 | bool PythonPluginInstance_Impl::setPluginClassName(const std::string& pluginClassName) { | |
| 100 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 10 times.
|
13 | if (!findPluginClassNameInFile(pluginClassName)) { |
| 101 | 3 | return false; | |
| 102 | } | ||
| 103 | |||
| 104 | 10 | bool result = setString(OS_PythonPlugin_InstanceFields::PluginClassName, pluginClassName); | |
| 105 | 10 | return result; | |
| 106 | } | ||
| 107 | |||
| 108 | 19 | bool PythonPluginInstance_Impl::findPluginClassNameInFile(const std::string& pluginClassName) const { | |
| 109 |
1/1✓ Branch 1 taken 19 times.
|
19 | auto validClassNames = validPluginClassNamesInFile(); |
| 110 |
1/1✓ Branch 3 taken 19 times.
|
19 | auto it = std::find(validClassNames.cbegin(), validClassNames.cend(), pluginClassName); |
| 111 |
2/2✓ Branch 2 taken 4 times.
✓ Branch 3 taken 15 times.
|
19 | if (it == validClassNames.cend()) { |
| 112 |
1/1✓ Branch 1 taken 4 times.
|
4 | std::stringstream ss; |
| 113 |
1/1✓ Branch 1 taken 4 times.
|
4 | ss << "["; |
| 114 | 4 | bool firstTime = true; | |
| 115 |
2/2✓ Branch 5 taken 8 times.
✓ Branch 6 taken 4 times.
|
12 | for (const auto& className : validClassNames) { |
| 116 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (firstTime) { |
| 117 |
3/3✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
|
4 | ss << "\"" << className << "\""; |
| 118 | 4 | firstTime = false; | |
| 119 | } else { | ||
| 120 |
3/3✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
|
4 | ss << ", \"" << className << "\""; |
| 121 | } | ||
| 122 | } | ||
| 123 |
1/1✓ Branch 1 taken 4 times.
|
4 | ss << "]"; |
| 124 |
8/8✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
✓ Branch 10 taken 4 times.
✓ Branch 13 taken 4 times.
✓ Branch 17 taken 4 times.
✓ Branch 20 taken 4 times.
✓ Branch 23 taken 4 times.
|
4 | LOG(Warn, pluginClassName << " not found. validClassNames=" << ss.str()); |
| 125 | 4 | return false; | |
| 126 | 4 | } | |
| 127 | |||
| 128 | 15 | return true; | |
| 129 | 19 | } | |
| 130 | |||
| 131 | 20 | std::vector<std::string> PythonPluginInstance_Impl::validPluginClassNamesInFile() const { | |
| 132 | 20 | std::vector<std::string> result; | |
| 133 | |||
| 134 | // Regexes are expensive, so static duration makes sense | ||
| 135 |
4/7✓ Branch 0 taken 5 times.
✓ Branch 1 taken 15 times.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 5 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
20 | static const boost::regex re(R"(^class\s+(\w+)\s*\(\s*EnergyPlusPlugin)"); |
| 136 |
1/1✓ Branch 1 taken 20 times.
|
20 | boost::smatch matches; |
| 137 | |||
| 138 |
3/3✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
✓ Branch 7 taken 20 times.
|
20 | auto filePath = openstudio::filesystem::system_complete(externalFile().filePath()); |
| 139 |
1/1✓ Branch 1 taken 20 times.
|
20 | openstudio::filesystem::ifstream ifs(filePath); |
| 140 | 20 | std::string line; | |
| 141 |
4/4✓ Branch 1 taken 3244 times.
✓ Branch 4 taken 3244 times.
✓ Branch 6 taken 3224 times.
✓ Branch 7 taken 20 times.
|
3264 | while (std::getline(ifs, line)) { |
| 142 |
3/3✓ Branch 1 taken 3224 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 3188 times.
|
3224 | if (boost::regex_search(line, matches, re)) { |
| 143 |
3/3✓ Branch 1 taken 36 times.
✓ Branch 4 taken 36 times.
✓ Branch 7 taken 36 times.
|
36 | result.emplace_back(matches[1].first, matches[1].second); |
| 144 | } | ||
| 145 | } | ||
| 146 | 40 | return result; | |
| 147 | 20 | } | |
| 148 | |||
| 149 | } // namespace detail | ||
| 150 | |||
| 151 | 11 | PythonPluginInstance::PythonPluginInstance(const ExternalFile& externalfile, const std::string& pluginClassName) | |
| 152 |
3/3✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
|
11 | : ResourceObject(PythonPluginInstance::iddObjectType(), externalfile.model()) { |
| 153 |
1/3✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
✗ Branch 5 not taken.
|
11 | OS_ASSERT(getImpl<detail::PythonPluginInstance_Impl>()); |
| 154 | |||
| 155 |
1/1✓ Branch 1 taken 11 times.
|
11 | auto filePath = externalfile.filePath(); |
| 156 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
|
11 | bool ok = (toString(filePath.extension()) == ".py"); |
| 157 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 10 times.
|
11 | if (!ok) { |
| 158 |
1/1✓ Branch 1 taken 1 time.
|
1 | remove(); |
| 159 |
17/17✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 25 taken 1 time.
✓ Branch 28 taken 1 time.
✓ Branch 31 taken 1 time.
✓ Branch 34 taken 1 time.
✓ Branch 37 taken 1 time.
✓ Branch 40 taken 1 time.
✓ Branch 43 taken 1 time.
✓ Branch 46 taken 1 time.
✓ Branch 50 taken 1 time.
✓ Branch 53 taken 1 time.
|
1 | LOG_AND_THROW("External file must have a .py extension, got externalfile='" << filePath << "'."); |
| 160 | } | ||
| 161 | |||
| 162 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
|
10 | ok = setPointer(OS_PythonPlugin_InstanceFields::ExternalFileName, externalfile.handle()); |
| 163 |
1/3✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✗ Branch 3 not taken.
|
10 | OS_ASSERT(ok); |
| 164 | |||
| 165 | // This must occur after setPointer externalfile | ||
| 166 |
1/1✓ Branch 1 taken 10 times.
|
10 | ok = setPluginClassName(pluginClassName); |
| 167 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 9 times.
|
10 | if (!ok) { |
| 168 |
1/1✓ Branch 1 taken 1 time.
|
1 | remove(); |
| 169 |
21/21✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 22 taken 1 time.
✓ Branch 25 taken 1 time.
✓ Branch 31 taken 1 time.
✓ Branch 34 taken 1 time.
✓ Branch 37 taken 1 time.
✓ Branch 40 taken 1 time.
✓ Branch 43 taken 1 time.
✓ Branch 46 taken 1 time.
✓ Branch 49 taken 1 time.
✓ Branch 52 taken 1 time.
✓ Branch 55 taken 1 time.
✓ Branch 58 taken 1 time.
✓ Branch 62 taken 1 time.
✓ Branch 65 taken 1 time.
|
1 | LOG_AND_THROW("Cannot find the Plugin Class Name '" << pluginClassName << "' in external file '" << filePath << "'."); |
| 170 | } | ||
| 171 | 13 | } | |
| 172 | |||
| 173 | 7755 | IddObjectType PythonPluginInstance::iddObjectType() { | |
| 174 |
1/1✓ Branch 1 taken 7755 times.
|
7755 | return {IddObjectType::OS_PythonPlugin_Instance}; |
| 175 | } | ||
| 176 | |||
| 177 | 8 | ExternalFile PythonPluginInstance::externalFile() const { | |
| 178 |
1/1✓ Branch 3 taken 8 times.
|
8 | return getImpl<detail::PythonPluginInstance_Impl>()->externalFile(); |
| 179 | } | ||
| 180 | |||
| 181 | 8 | bool PythonPluginInstance::runDuringWarmupDays() const { | |
| 182 |
1/1✓ Branch 3 taken 8 times.
|
8 | return getImpl<detail::PythonPluginInstance_Impl>()->runDuringWarmupDays(); |
| 183 | } | ||
| 184 | |||
| 185 | 3 | bool PythonPluginInstance::isRunDuringWarmupDaysDefaulted() const { | |
| 186 |
1/1✓ Branch 3 taken 3 times.
|
3 | return getImpl<detail::PythonPluginInstance_Impl>()->isRunDuringWarmupDaysDefaulted(); |
| 187 | } | ||
| 188 | |||
| 189 | 10 | std::string PythonPluginInstance::pluginClassName() const { | |
| 190 |
1/1✓ Branch 3 taken 10 times.
|
10 | return getImpl<detail::PythonPluginInstance_Impl>()->pluginClassName(); |
| 191 | } | ||
| 192 | |||
| 193 | 4 | bool PythonPluginInstance::setRunDuringWarmupDays(bool runDuringWarmupDays) { | |
| 194 |
1/1✓ Branch 3 taken 4 times.
|
4 | return getImpl<detail::PythonPluginInstance_Impl>()->setRunDuringWarmupDays(runDuringWarmupDays); |
| 195 | } | ||
| 196 | |||
| 197 | 1 | void PythonPluginInstance::resetRunDuringWarmupDays() { | |
| 198 |
1/1✓ Branch 3 taken 1 time.
|
1 | getImpl<detail::PythonPluginInstance_Impl>()->resetRunDuringWarmupDays(); |
| 199 | 1 | } | |
| 200 | |||
| 201 | 13 | bool PythonPluginInstance::setPluginClassName(const std::string& pluginClassName) { | |
| 202 |
1/1✓ Branch 3 taken 13 times.
|
13 | return getImpl<detail::PythonPluginInstance_Impl>()->setPluginClassName(pluginClassName); |
| 203 | } | ||
| 204 | |||
| 205 | 6 | bool PythonPluginInstance::findPluginClassNameInFile(const std::string& pluginClassName) const { | |
| 206 |
1/1✓ Branch 3 taken 6 times.
|
6 | return getImpl<detail::PythonPluginInstance_Impl>()->findPluginClassNameInFile(pluginClassName); |
| 207 | } | ||
| 208 | |||
| 209 | 1 | std::vector<std::string> PythonPluginInstance::validPluginClassNamesInFile() const { | |
| 210 |
1/1✓ Branch 3 taken 1 time.
|
1 | return getImpl<detail::PythonPluginInstance_Impl>()->validPluginClassNamesInFile(); |
| 211 | } | ||
| 212 | |||
| 213 | /// @cond | ||
| 214 |
1/1✓ Branch 2 taken 15 times.
|
15 | PythonPluginInstance::PythonPluginInstance(std::shared_ptr<detail::PythonPluginInstance_Impl> impl) : ResourceObject(impl) {} |
| 215 | /// @endcond | ||
| 216 | |||
| 217 | } // namespace model | ||
| 218 | } // namespace openstudio | ||
| 219 |