src/workflow/ApplyMeasure.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 "OSWorkflow.hpp" | ||
| 7 | |||
| 8 | #include "Util.hpp" | ||
| 9 | |||
| 10 | #include "../osversion/VersionTranslator.hpp" | ||
| 11 | #include "../measure/OSMeasure.hpp" | ||
| 12 | #include "../measure/ModelMeasure.hpp" | ||
| 13 | #include "../measure/EnergyPlusMeasure.hpp" | ||
| 14 | #include "../measure/ModelicaMeasure.hpp" | ||
| 15 | #include "../measure/ReportingMeasure.hpp" | ||
| 16 | #include "../measure/OSArgument.hpp" | ||
| 17 | #include "../measure/OSRunner.hpp" | ||
| 18 | #include "../model/Model.hpp" | ||
| 19 | #include "../model/Model_Impl.hpp" | ||
| 20 | #include "../utilities/filetypes/WorkflowStep.hpp" | ||
| 21 | #include "../utilities/bcl/BCLMeasure.hpp" | ||
| 22 | #include "../utilities/idf/Workspace.hpp" | ||
| 23 | #include "../utilities/data/Variant.hpp" | ||
| 24 | #include "../utilities/core/Filesystem.hpp" | ||
| 25 | #include "../utilities/core/Logger.hpp" | ||
| 26 | #include "../utilities/filetypes/WorkflowJSON.hpp" | ||
| 27 | #include <boost/filesystem/operations.hpp> | ||
| 28 | |||
| 29 | #include <fmt/format.h> | ||
| 30 | #include <fmt/ranges.h> | ||
| 31 | |||
| 32 | namespace openstudio { | ||
| 33 | |||
| 34 | 51 | bool isStepMarkedSkip(const std::map<std::string, openstudio::Variant>& stepArgs) { | |
| 35 | 51 | bool skip_measure = false; | |
| 36 | |||
| 37 | // handle skip first | ||
| 38 |
10/16✓ Branch 1 taken 11 times.
✓ Branch 2 taken 40 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 11 times.
✓ Branch 11 taken 11 times.
✓ Branch 12 taken 40 times.
✓ Branch 14 taken 11 times.
✓ Branch 15 taken 40 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 51 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
|
73 | if (!stepArgs.empty() && stepArgs.contains("__SKIP__")) { |
| 39 | ✗ | const openstudio::Variant& argumentValue = stepArgs.at("__SKIP__"); | |
| 40 | ✗ | skip_measure = argumentValue.isTrueish(); | |
| 41 | } | ||
| 42 | 51 | return skip_measure; | |
| 43 | } | ||
| 44 | |||
| 45 | 66 | void OSWorkflow::applyMeasures(MeasureType measureType, ApplyMeasureType apply_measure_type) { | |
| 46 | |||
| 47 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
66 | if (m_add_timings && m_detailed_timings) { |
| 48 | ✗ | m_timers->newTimer(fmt::format("{}:apply_measures", measureType.valueName()), 1); | |
| 49 | } | ||
| 50 | |||
| 51 |
7/7✓ Branch 1 taken 66 times.
✓ Branch 4 taken 66 times.
✓ Branch 7 taken 66 times.
✓ Branch 10 taken 66 times.
✓ Branch 14 taken 66 times.
✓ Branch 17 taken 66 times.
✓ Branch 20 taken 66 times.
|
66 | LOG(Debug, "Finding measures of type " << measureType.valueName()); |
| 52 | |||
| 53 | // We actually want the order of the steps preserved, so this won't cut it | ||
| 54 | // const auto modelSteps = workflowJSON.getMeasureSteps(stepType); | ||
| 55 | |||
| 56 |
3/3✓ Branch 1 taken 66 times.
✓ Branch 8 taken 51 times.
✓ Branch 9 taken 64 times.
|
115 | for (const auto& stepAndIndex : workflowJSON.getMeasureStepsWithIndex(measureType)) { |
| 57 | 51 | auto stepIndex = stepAndIndex.first; | |
| 58 | 51 | auto step = stepAndIndex.second; | |
| 59 |
6/6✓ Branch 1 taken 51 times.
✓ Branch 4 taken 51 times.
✓ Branch 7 taken 51 times.
✓ Branch 10 taken 51 times.
✓ Branch 13 taken 51 times.
✓ Branch 16 taken 51 times.
|
51 | LOG(Debug, "Running step " << stepIndex); |
| 60 | |||
| 61 |
1/1✓ Branch 1 taken 51 times.
|
51 | const auto measureDirName = step.measureDirName(); |
| 62 | |||
| 63 |
1/1✓ Branch 1 taken 51 times.
|
51 | auto stepArgs = step.arguments(); |
| 64 |
1/1✓ Branch 1 taken 51 times.
|
51 | const bool skip_measure = isStepMarkedSkip(stepArgs); |
| 65 |
4/7✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 51 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 51 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 51 times.
|
51 | if (skip_measure || runner.halted()) { |
| 66 | ✗ | if (apply_measure_type == ApplyMeasureType::Regular) { | |
| 67 | ✗ | if (runner.halted()) { | |
| 68 | ✗ | LOG(Info, fmt::format("Skipping measure '{}' because simulation halted", measureDirName)); | |
| 69 | } else { | ||
| 70 | ✗ | LOG(Info, fmt::format("Skipping measure '{}'", measureDirName)); | |
| 71 | // required to update current step | ||
| 72 | ✗ | runner.prepareForMeasureRun(); | |
| 73 | |||
| 74 | ✗ | WorkflowStepResult result = runner.result(); | |
| 75 | ✗ | runner.incrementStep(); | |
| 76 | ✗ | const auto measureDirPath_ = workflowJSON.findMeasure(measureDirName); | |
| 77 | ✗ | if (measureDirPath_) { | |
| 78 | ✗ | BCLMeasure bclMeasure(measureDirPath_.get()); | |
| 79 | ✗ | workflow::util::addResultMeasureInfo(result, bclMeasure); | |
| 80 | ✗ | const std::string className = bclMeasure.className(); | |
| 81 | ✗ | const auto measureName = step.name().value_or(className); | |
| 82 | ✗ | output_attributes[measureName]["applicable"] = openstudio::Variant(false); | |
| 83 | ✗ | } else { | |
| 84 | ✗ | LOG(Warn, "Could not find measure '" << measureDirName << "', but it's marked as skipped anyways so ignoring."); | |
| 85 | ✗ | output_attributes[step.name().value_or(measureDirName)]["applicable"] = openstudio::Variant(false); | |
| 86 | } | ||
| 87 | ✗ | result.setStepResult(StepResult::Skip); | |
| 88 | ✗ | } | |
| 89 | } | ||
| 90 | ✗ | continue; | |
| 91 | ✗ | } | |
| 92 | |||
| 93 |
2/3✓ Branch 1 taken 51 times.
✓ Branch 3 taken 51 times.
✗ Branch 4 not taken.
|
51 | if (model.numObjects() > 0) { |
| 94 |
1/1✓ Branch 1 taken 51 times.
|
51 | runner.setLastOpenStudioModel(model); |
| 95 | } | ||
| 96 |
2/2✓ Branch 1 taken 29 times.
✓ Branch 2 taken 22 times.
|
102 | if (workspace_) { |
| 97 |
2/2✓ Branch 1 taken 29 times.
✓ Branch 4 taken 29 times.
|
29 | runner.setLastEnergyPlusWorkspace(workspace_.get()); |
| 98 | } | ||
| 99 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 2 taken 43 times.
|
51 | if (!sqlPath.empty()) { |
| 100 |
1/1✓ Branch 1 taken 8 times.
|
8 | runner.setLastEnergyPlusSqlFilePath(sqlPath); |
| 101 | } | ||
| 102 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
|
102 | if (m_lastModelicaResultPath) { |
| 103 | ✗ | runner.setLastModelicaResultPath(*m_lastModelicaResultPath); | |
| 104 | } | ||
| 105 | |||
| 106 |
1/1✓ Branch 1 taken 51 times.
|
51 | updateLastWeatherFileFromModel(); |
| 107 | |||
| 108 |
3/4✓ Branch 1 taken 51 times.
✓ Branch 4 taken 51 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 51 times.
|
51 | if (openstudio::filesystem::path(measureDirName).is_absolute()) { |
| 109 | ✗ | LOG(Warn, "measure_dir_name should not be a full path. It should be a relative path to the measure directory or the name of the measure " | |
| 110 | "directory containing the measure.rb / measure.py file."); | ||
| 111 | } | ||
| 112 | |||
| 113 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
51 | if (m_add_timings && m_detailed_timings) { |
| 114 | ✗ | m_timers->newTimer(fmt::format("Measure::{}", measureDirName), 2); | |
| 115 | } | ||
| 116 |
1/1✓ Branch 1 taken 51 times.
|
51 | openstudio::filesystem::path curDirPath = boost::filesystem::current_path(); |
| 117 | |||
| 118 | 51 | auto ensureBlock = [this, &curDirPath](bool stepHasThrown) { | |
| 119 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
51 | if (m_add_timings && m_detailed_timings) { |
| 120 | ✗ | m_timers->tockCurrentTimer(); | |
| 121 | ✗ | if (stepHasThrown) { | |
| 122 | ✗ | m_timers->tockCurrentTimer(); | |
| 123 | } | ||
| 124 | } | ||
| 125 | 51 | boost::filesystem::current_path(curDirPath); | |
| 126 | 102 | }; | |
| 127 | |||
| 128 |
2/2✓ Branch 1 taken 51 times.
✓ Branch 4 taken 51 times.
|
153 | auto thisRunDir = workflowJSON.absoluteRunDir() / openstudio::toPath(fmt::format("{:03}_{}", stepIndex, measureDirName)); |
| 129 |
7/7✓ Branch 1 taken 51 times.
✓ Branch 4 taken 51 times.
✓ Branch 7 taken 51 times.
✓ Branch 10 taken 51 times.
✓ Branch 13 taken 51 times.
✓ Branch 16 taken 51 times.
✓ Branch 19 taken 51 times.
|
51 | LOG(Debug, "Creating run directory for measure in '" << thisRunDir << "'"); |
| 130 |
1/1✓ Branch 1 taken 51 times.
|
51 | openstudio::filesystem::create_directory(thisRunDir); |
| 131 |
1/1✓ Branch 1 taken 51 times.
|
51 | boost::filesystem::current_path(thisRunDir); |
| 132 | |||
| 133 |
1/1✓ Branch 1 taken 51 times.
|
51 | const auto measureDirPath_ = workflowJSON.findMeasure(measureDirName); |
| 134 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
|
51 | if (!measureDirPath_) { |
| 135 | ✗ | LOG_AND_THROW("Could not find measure '" << measureDirName << "'."); | |
| 136 | continue; | ||
| 137 | } | ||
| 138 |
2/2✓ Branch 1 taken 51 times.
✓ Branch 4 taken 51 times.
|
51 | BCLMeasure bclMeasure(measureDirPath_.get()); |
| 139 | |||
| 140 |
1/1✓ Branch 1 taken 51 times.
|
51 | const auto scriptPath_ = bclMeasure.primaryScriptPath(); |
| 141 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
|
51 | if (!scriptPath_) { |
| 142 | ✗ | LOG(Warn, "Could not find primaryScriptPath '" << measureDirName << "'"); | |
| 143 | ✗ | continue; | |
| 144 | ✗ | } | |
| 145 |
7/7✓ Branch 1 taken 51 times.
✓ Branch 4 taken 51 times.
✓ Branch 7 taken 51 times.
✓ Branch 11 taken 51 times.
✓ Branch 16 taken 51 times.
✓ Branch 19 taken 51 times.
✓ Branch 22 taken 51 times.
|
102 | LOG(Debug, fmt::format("Found {} at primaryScriptPath: '{}'", measureDirName, openstudio::toString(scriptPath_.get()))); |
| 146 |
1/1✓ Branch 1 taken 51 times.
|
51 | const std::string className = bclMeasure.className(); |
| 147 |
1/1✓ Branch 1 taken 51 times.
|
51 | const auto measureType = bclMeasure.measureType(); |
| 148 |
1/1✓ Branch 1 taken 51 times.
|
51 | const MeasureLanguage measureLanguage = bclMeasure.measureLanguage(); |
| 149 | |||
| 150 | // TODO: will add a Logger later | ||
| 151 |
6/6✓ Branch 1 taken 51 times.
✓ Branch 4 taken 51 times.
✓ Branch 7 taken 51 times.
✓ Branch 10 taken 51 times.
✓ Branch 13 taken 51 times.
✓ Branch 16 taken 51 times.
|
51 | LOG(Debug, "Class Name: " << className); |
| 152 |
7/7✓ Branch 1 taken 51 times.
✓ Branch 4 taken 51 times.
✓ Branch 7 taken 51 times.
✓ Branch 10 taken 51 times.
✓ Branch 13 taken 51 times.
✓ Branch 16 taken 51 times.
✓ Branch 19 taken 51 times.
|
51 | LOG(Debug, "Measure Script Path: " << scriptPath_.get()); |
| 153 |
8/8✓ Branch 1 taken 51 times.
✓ Branch 4 taken 51 times.
✓ Branch 7 taken 51 times.
✓ Branch 10 taken 51 times.
✓ Branch 13 taken 51 times.
✓ Branch 17 taken 51 times.
✓ Branch 20 taken 51 times.
✓ Branch 23 taken 51 times.
|
51 | LOG(Debug, "Measure Type: " << bclMeasure.measureType().valueName()); |
| 154 |
7/7✓ Branch 1 taken 51 times.
✓ Branch 4 taken 51 times.
✓ Branch 7 taken 51 times.
✓ Branch 10 taken 51 times.
✓ Branch 14 taken 51 times.
✓ Branch 17 taken 51 times.
✓ Branch 20 taken 51 times.
|
51 | LOG(Debug, "Measure Language: " << measureLanguage.valueName()); |
| 155 | |||
| 156 | 51 | auto getArguments = [this, &measureType, &stepArgs](openstudio::measure::OSMeasure* measurePtr) -> measure::OSArgumentMap { | |
| 157 |
8/8✓ Branch 1 taken 51 times.
✓ Branch 4 taken 51 times.
✓ Branch 7 taken 51 times.
✓ Branch 10 taken 51 times.
✓ Branch 13 taken 51 times.
✓ Branch 17 taken 51 times.
✓ Branch 20 taken 51 times.
✓ Branch 23 taken 51 times.
|
51 | LOG(Debug, "measure->name()= '" << measurePtr->name() << "'"); |
| 158 | |||
| 159 | 51 | measure::OSArgumentMap argumentMap; | |
| 160 | |||
| 161 | // logger.debug "Iterating over arguments for workflow item '#{measure_dir_name}'" | ||
| 162 |
7/7✓ Branch 1 taken 51 times.
✓ Branch 4 taken 51 times.
✓ Branch 8 taken 51 times.
✓ Branch 11 taken 51 times.
✓ Branch 14 taken 51 times.
✓ Branch 17 taken 51 times.
✓ Branch 20 taken 51 times.
|
51 | LOG(Debug, "Current step has " << stepArgs.size() << " arguments"); |
| 163 | |||
| 164 | // Initialize arguments which may be model dependent, don't allow arguments method access to real model in case it changes something | ||
| 165 | 51 | std::vector<measure::OSArgument> arguments; | |
| 166 | |||
| 167 |
3/3✓ Branch 1 taken 51 times.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 37 times.
|
51 | if (measureType == MeasureType::ModelMeasure) { |
| 168 | // For computing arguments | ||
| 169 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
|
14 | auto modelClone = model.clone(true).cast<model::Model>(); |
| 170 |
1/1✓ Branch 1 taken 14 times.
|
14 | arguments = static_cast<openstudio::measure::ModelMeasure*>(measurePtr)->arguments(modelClone); // NOLINT |
| 171 |
3/3✓ Branch 2 taken 37 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 30 times.
|
51 | } else if (measureType == MeasureType::EnergyPlusMeasure) { |
| 172 |
3/3✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
✓ Branch 7 taken 7 times.
|
7 | auto workspaceClone = workspace_->clone(true).cast<openstudio::Workspace>(); |
| 173 |
1/1✓ Branch 1 taken 7 times.
|
7 | arguments = static_cast<openstudio::measure::EnergyPlusMeasure*>(measurePtr)->arguments(workspaceClone); // NOLINT |
| 174 |
2/3✓ Branch 2 taken 30 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 30 times.
|
37 | } else if (measureType == MeasureType::ModelicaMeasure) { |
| 175 | ✗ | auto workspaceClone = workspace_->clone(true).cast<openstudio::Workspace>(); | |
| 176 | ✗ | auto modelClone = model.clone(true).cast<model::Model>(); | |
| 177 | ✗ | arguments = static_cast<openstudio::measure::ModelicaMeasure*>(measurePtr)->arguments(modelClone, workspaceClone); // NOLINT | |
| 178 |
2/3✓ Branch 3 taken 30 times.
✓ Branch 6 taken 30 times.
✗ Branch 7 not taken.
|
30 | } else if (measureType == MeasureType::ReportingMeasure) { |
| 179 |
2/2✓ Branch 1 taken 30 times.
✓ Branch 4 taken 30 times.
|
30 | auto modelClone = model.clone(true).cast<model::Model>(); |
| 180 |
1/1✓ Branch 1 taken 30 times.
|
30 | arguments = static_cast<openstudio::measure::ReportingMeasure*>(measurePtr)->arguments(modelClone); // NOLINT |
| 181 | 30 | } | |
| 182 | |||
| 183 |
2/2✓ Branch 1 taken 27 times.
✓ Branch 2 taken 24 times.
|
51 | if (!arguments.empty()) { |
| 184 |
2/2✓ Branch 4 taken 33 times.
✓ Branch 5 taken 27 times.
|
60 | for (const auto& argument : arguments) { |
| 185 | // LOG(Debug, "Argument: " << argument); | ||
| 186 |
3/3✓ Branch 1 taken 33 times.
✓ Branch 4 taken 33 times.
✓ Branch 7 taken 33 times.
|
33 | argumentMap[argument.name()] = argument.clone(); |
| 187 | } | ||
| 188 | |||
| 189 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 2 taken 16 times.
|
27 | if (!stepArgs.empty()) { |
| 190 | // TODO: I've copied workflow-gem here, but it's wrong. It's trying to issue a warning if an argument is not set, so it should loop on | ||
| 191 | // argumentMap instead... | ||
| 192 |
2/2✓ Branch 7 taken 11 times.
✓ Branch 8 taken 11 times.
|
22 | for (auto const& [argumentName, argumentValue] : stepArgs) { |
| 193 |
2/3✓ Branch 1 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
|
11 | if (argumentName == "__SKIP__") { |
| 194 | ✗ | continue; | |
| 195 | } | ||
| 196 |
1/1✓ Branch 1 taken 11 times.
|
11 | applyArguments(argumentMap, argumentName, argumentValue); |
| 197 | } | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | 51 | std::map<std::string, std::string> formattedArgMap; | |
| 202 |
2/2✓ Branch 2 taken 51 times.
✓ Branch 7 taken 51 times.
|
51 | std::transform(argumentMap.begin(), argumentMap.end(), std::inserter(formattedArgMap, formattedArgMap.begin()), |
| 203 |
2/2✓ Branch 1 taken 33 times.
✓ Branch 4 taken 33 times.
|
33 | [](const auto& p) { return std::make_pair(p.first, p.second.printValue(true)); }); |
| 204 |
5/5✓ Branch 1 taken 51 times.
✓ Branch 5 taken 51 times.
✓ Branch 9 taken 51 times.
✓ Branch 12 taken 51 times.
✓ Branch 15 taken 51 times.
|
102 | LOG(Debug, fmt::format("argumentMap={}", formattedArgMap)); |
| 205 | 102 | return argumentMap; | |
| 206 | 51 | }; | |
| 207 | |||
| 208 | 51 | ScriptEngineInstance* thisEngine = nullptr; | |
| 209 |
3/3✓ Branch 1 taken 51 times.
✓ Branch 4 taken 37 times.
✓ Branch 5 taken 14 times.
|
51 | if (measureLanguage == MeasureLanguage::Ruby) { |
| 210 | // TODO: probably need to do path formatting properly for windows | ||
| 211 | #if USE_RUBY_ENGINE | ||
| 212 | 37 | thisEngine = &rubyEngine; | |
| 213 | #else | ||
| 214 | ensureBlock(true); | ||
| 215 | throw std::runtime_error("Cannot run a Ruby measure when RubyEngine isn't enabled"); | ||
| 216 | #endif | ||
| 217 |
2/3✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
|
14 | } else if (measureLanguage == MeasureLanguage::Python) { |
| 218 | #if USE_PYTHON_ENGINE | ||
| 219 | 14 | thisEngine = &pythonEngine; | |
| 220 | #else | ||
| 221 | ensureBlock(true); | ||
| 222 | throw std::runtime_error("Cannot run a Python measure when PythonEngine isn't enabled"); | ||
| 223 | #endif | ||
| 224 | } | ||
| 225 | |||
| 226 |
3/3✓ Branch 1 taken 51 times.
✓ Branch 6 taken 51 times.
✓ Branch 9 taken 51 times.
|
51 | ScriptObject measureScriptObject = (*thisEngine)->loadMeasure(*scriptPath_, className); |
| 227 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
|
51 | if (measureScriptObject.empty()) { |
| 228 | ✗ | ensureBlock(true); | |
| 229 | ✗ | throw std::runtime_error(fmt::format("Failed to load measure '{}' from '{}'\n", className, openstudio::toString(scriptPath_.get()))); | |
| 230 | } | ||
| 231 | |||
| 232 | // This pointer will only be valid for as long as the above PythonMeasure is in scope | ||
| 233 | // After that, dereferencing the measure pointer will crash the program | ||
| 234 |
2/2✓ Branch 1 taken 51 times.
✓ Branch 5 taken 51 times.
|
51 | auto* measurePtr = (*thisEngine)->getAs<openstudio::measure::OSMeasure*>(measureScriptObject); |
| 235 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
|
51 | if (!measurePtr) { |
| 236 | ✗ | ensureBlock(true); | |
| 237 | ✗ | throw std::runtime_error(fmt::format("Could not load measure at '{}'", openstudio::toString(scriptPath_.get()))); | |
| 238 | } | ||
| 239 | |||
| 240 | // Patch measure if needed | ||
| 241 | 51 | bool was_patched = false; | |
| 242 |
3/3✓ Branch 1 taken 51 times.
✓ Branch 4 taken 30 times.
✓ Branch 5 taken 21 times.
|
51 | if (measureType == MeasureType::ReportingMeasure) { |
| 243 |
2/2✓ Branch 1 taken 30 times.
✓ Branch 6 taken 30 times.
|
30 | const int numArgs = (*thisEngine)->numberOfArguments(measureScriptObject, "arguments"); |
| 244 | // fmt::print("numArgs={}\n", numArgs); | ||
| 245 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 29 times.
|
30 | if (numArgs == 0) { |
| 246 |
2/3✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
|
1 | if (measureLanguage == MeasureLanguage::Ruby) { |
| 247 | auto msg = fmt::format("Reporting Measure at '{}' is using the old format where the 'arguments' method does not take model. " | ||
| 248 | " Please consider updating this to `def arguments(model)`.", | ||
| 249 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
2 | scriptPath_->generic_string()); |
| 250 |
5/5✓ 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.
|
1 | LOG(Warn, msg); |
| 251 | auto patchArgumentsCmd = fmt::format(R"ruby( | ||
| 252 | module {0}Extensions | ||
| 253 | def arguments(model=nil) | ||
| 254 | super() | ||
| 255 | end | ||
| 256 | end | ||
| 257 | |||
| 258 | class {0} | ||
| 259 | prepend {0}Extensions | ||
| 260 | end | ||
| 261 | )ruby", | ||
| 262 | 1 | className); | |
| 263 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 6 taken 1 time.
|
1 | rubyEngine->exec(patchArgumentsCmd); |
| 264 | 1 | was_patched = true; | |
| 265 | 1 | } else { | |
| 266 | auto msg = fmt::format("Wrong number of parameters for method `arguments` in reporting_measure '{}' from '{}'\n", className, | ||
| 267 | ✗ | scriptPath_->generic_string()); | |
| 268 | ✗ | ensureBlock(true); | |
| 269 | ✗ | throw std::runtime_error(msg); | |
| 270 | ✗ | } | |
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 |
1/1✓ Branch 1 taken 51 times.
|
51 | const auto argmap = getArguments(measurePtr); |
| 275 | |||
| 276 | // TODO: this doesn't protect! it'll crash if a wrong method is used in the ruby measure for eg | ||
| 277 | try { | ||
| 278 |
3/3✓ Branch 1 taken 51 times.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 37 times.
|
51 | if (measureType == MeasureType::ModelMeasure) { |
| 279 |
1/1✓ Branch 1 taken 14 times.
|
14 | static_cast<openstudio::measure::ModelMeasure*>(measurePtr)->run(model, runner, argmap); |
| 280 |
3/3✓ Branch 1 taken 37 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 30 times.
|
37 | } else if (measureType == MeasureType::EnergyPlusMeasure) { |
| 281 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
|
7 | static_cast<openstudio::measure::EnergyPlusMeasure*>(measurePtr)->run(workspace_.get(), runner, argmap); |
| 282 |
2/3✓ Branch 1 taken 30 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 30 times.
|
30 | } else if (measureType == MeasureType::ModelicaMeasure) { |
| 283 | ✗ | static_cast<openstudio::measure::ModelicaMeasure*>(measurePtr)->run(modelicaFile.get(), model, workspace_.get(), runner, argmap); | |
| 284 |
2/3✓ Branch 1 taken 30 times.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
|
30 | } else if (measureType == MeasureType::ReportingMeasure) { |
| 285 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 22 times.
|
30 | if (apply_measure_type == ApplyMeasureType::ModelOutputRequests) { |
| 286 |
4/4✓ Branch 1 taken 8 times.
✓ Branch 6 taken 8 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 6 times.
|
8 | if ((*thisEngine)->hasMethod(measureScriptObject, "modelOutputRequests")) { |
| 287 |
1/1✓ Branch 1 taken 2 times.
|
2 | auto n = model.numObjects(); |
| 288 |
7/7✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 16 taken 2 times.
✓ Branch 19 taken 2 times.
|
2 | LOG(Debug, "Calling measure.modelOutputRequests for '" << measureDirName << "'"); |
| 289 |
1/1✓ Branch 1 taken 2 times.
|
2 | static_cast<openstudio::measure::ReportingMeasure*>(measurePtr)->modelOutputRequests(model, runner, argmap); |
| 290 |
10/10✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 16 taken 2 times.
✓ Branch 19 taken 2 times.
✓ Branch 22 taken 2 times.
✓ Branch 25 taken 2 times.
✓ Branch 28 taken 2 times.
|
2 | LOG(Debug, "Finished measure.modelOutputRequests for '" << measureDirName << "', " << (model.numObjects() - n) << " objects added"); |
| 291 | } else { | ||
| 292 |
7/7✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 13 taken 6 times.
✓ Branch 16 taken 6 times.
✓ Branch 19 taken 6 times.
|
6 | LOG(Debug, "Reporting Measure '" << measureDirName << "' does not have a modelOutputRequests method"); |
| 293 | } | ||
| 294 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 14 times.
|
22 | } else if (apply_measure_type == ApplyMeasureType::EnergyPlusOutputRequest) { |
| 295 |
7/7✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
✓ Branch 10 taken 8 times.
✓ Branch 13 taken 8 times.
✓ Branch 16 taken 8 times.
✓ Branch 19 taken 8 times.
|
8 | LOG(Debug, "Calling measure.energyPlusOutputRequests for '" << measureDirName << "'"); |
| 296 | |||
| 297 | 8 | std::vector<IdfObject> idfObjects; | |
| 298 |
1/1✓ Branch 1 taken 8 times.
|
8 | idfObjects = static_cast<openstudio::measure::ReportingMeasure*>(measurePtr)->energyPlusOutputRequests(runner, argmap); |
| 299 | |||
| 300 | 8 | int num_added = 0; | |
| 301 |
2/2✓ Branch 5 taken 4 times.
✓ Branch 6 taken 8 times.
|
12 | for (auto& idfObject : idfObjects) { |
| 302 |
4/4✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 2 times.
|
4 | if (openstudio::workflow::util::addEnergyPlusOutputRequest(workspace_.get(), idfObject)) { |
| 303 | 2 | ++num_added; | |
| 304 | } | ||
| 305 | } | ||
| 306 |
9/9✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
✓ Branch 10 taken 8 times.
✓ Branch 13 taken 8 times.
✓ Branch 16 taken 8 times.
✓ Branch 19 taken 8 times.
✓ Branch 22 taken 8 times.
✓ Branch 25 taken 8 times.
|
8 | LOG(Debug, "Finished measure.energyPlusOutputRequests for '" << measureDirName << "', " << num_added << " output requests added"); |
| 307 | 8 | } else { | |
| 308 |
1/1✓ Branch 1 taken 14 times.
|
14 | static_cast<openstudio::measure::ReportingMeasure*>(measurePtr)->run(runner, argmap); |
| 309 | } | ||
| 310 | } | ||
| 311 | ✗ | } catch (const std::exception& e) { | |
| 312 | ✗ | runner.registerError(e.what()); | |
| 313 | ✗ | if (apply_measure_type == ApplyMeasureType::Regular) { | |
| 314 | ✗ | WorkflowStepResult result = runner.result(); | |
| 315 | // incrementStep must be called after run | ||
| 316 | ✗ | runner.incrementStep(); | |
| 317 | ✗ | workflow::util::addResultMeasureInfo(result, bclMeasure); | |
| 318 | ✗ | } | |
| 319 | ✗ | ensureBlock(true); | |
| 320 | ✗ | throw std::runtime_error(fmt::format("Runner error: Measure '{}' reported an error with [{}]", scriptPath_->generic_string(), e.what())); | |
| 321 | ✗ | } | |
| 322 | |||
| 323 | // if doing output requests we are done now | ||
| 324 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 16 times.
|
51 | if (apply_measure_type == ApplyMeasureType::Regular) { |
| 325 |
1/1✓ Branch 1 taken 35 times.
|
35 | WorkflowStepResult result = runner.result(); |
| 326 | |||
| 327 | // incrementStep must be called after run | ||
| 328 |
1/1✓ Branch 1 taken 35 times.
|
35 | runner.incrementStep(); |
| 329 |
1/1✓ Branch 1 taken 35 times.
|
35 | workflow::util::addResultMeasureInfo(result, bclMeasure); |
| 330 |
3/3✓ Branch 1 taken 35 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 33 times.
|
35 | if (auto errors = result.stepErrors(); !errors.empty()) { |
| 331 |
1/1✓ Branch 1 taken 2 times.
|
2 | ensureBlock(true); |
| 332 |
2/2✓ Branch 3 taken 2 times.
✓ Branch 7 taken 2 times.
|
6 | throw std::runtime_error(fmt::format("Measure '{}' reported an error with [{}]", measureDirName, fmt::join(errors, "\n"))); |
| 333 | 35 | } | |
| 334 | |||
| 335 |
2/2✓ Branch 1 taken 33 times.
✓ Branch 4 taken 33 times.
|
33 | const auto measureName = step.name().value_or(className); |
| 336 |
1/1✓ Branch 1 taken 33 times.
|
33 | auto& measureAttributes = output_attributes[measureName]; |
| 337 |
3/3✓ Branch 1 taken 33 times.
✓ Branch 7 taken 41 times.
✓ Branch 8 taken 33 times.
|
74 | for (const auto& stepValue : result.stepValues()) { |
| 338 |
3/3✓ Branch 1 taken 41 times.
✓ Branch 4 taken 41 times.
✓ Branch 7 taken 41 times.
|
41 | measureAttributes[stepValue.name()] = stepValue.valueAsVariant(); |
| 339 | 33 | } | |
| 340 |
1/1✓ Branch 1 taken 33 times.
|
33 | auto stepResult_ = result.stepResult(); |
| 341 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
|
33 | if (!stepResult_.has_value()) { |
| 342 | ✗ | LOG_AND_THROW("Step Result not set for '" << scriptPath_->generic_string() << "'"); | |
| 343 | } | ||
| 344 | |||
| 345 | // Add an applicability flag to all the measure results | ||
| 346 |
1/1✓ Branch 1 taken 33 times.
|
33 | const StepResult stepResult = std::move(*stepResult_); |
| 347 |
7/7✓ Branch 1 taken 33 times.
✓ Branch 4 taken 33 times.
✓ Branch 7 taken 33 times.
✓ Branch 10 taken 33 times.
✓ Branch 14 taken 33 times.
✓ Branch 17 taken 33 times.
✓ Branch 20 taken 33 times.
|
33 | LOG(Debug, "Step Result: " << stepResult.valueName()); |
| 348 |
8/9✓ Branch 1 taken 33 times.
✓ Branch 4 taken 29 times.
✓ Branch 5 taken 4 times.
✓ Branch 7 taken 29 times.
✓ Branch 10 taken 29 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 33 times.
✓ Branch 16 taken 33 times.
✓ Branch 19 taken 33 times.
|
99 | measureAttributes["applicable"] = openstudio::Variant(!((stepResult == StepResult::NA) || (stepResult == StepResult::Skip))); |
| 349 | |||
| 350 |
2/3✓ Branch 1 taken 33 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 33 times.
|
33 | if (measureType == MeasureType::ModelicaMeasure) { |
| 351 | try { | ||
| 352 | ✗ | saveModelicaFileSnapshot(thisRunDir); | |
| 353 | ✗ | } catch (const std::exception& e) { | |
| 354 | ✗ | runner.registerError(e.what()); | |
| 355 | ✗ | ensureBlock(true); | |
| 356 | ✗ | throw; | |
| 357 | ✗ | } | |
| 358 | } | ||
| 359 | |||
| 360 |
3/3✓ Branch 1 taken 33 times.
✓ Branch 4 taken 13 times.
✓ Branch 5 taken 20 times.
|
33 | if (measureType == MeasureType::ModelMeasure) { |
| 361 |
1/1✓ Branch 1 taken 13 times.
|
13 | updateLastWeatherFileFromModel(); |
| 362 | } | ||
| 363 | 35 | } | |
| 364 | |||
| 365 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 48 times.
|
49 | if (was_patched) { |
| 366 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 7 taken 1 time.
|
2 | rubyEngine->exec(fmt::format("Object.send(:remove_const, :{}Extensions)", className)); |
| 367 | } | ||
| 368 | |||
| 369 |
1/1✓ Branch 1 taken 49 times.
|
49 | ensureBlock(false); |
| 370 | |||
| 371 | 137 | } // End for each step | |
| 372 | |||
| 373 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
64 | if (m_add_timings && m_detailed_timings) { |
| 374 | ✗ | m_timers->tockCurrentTimer(); | |
| 375 | } | ||
| 376 | 64 | } | |
| 377 | |||
| 378 | } // namespace openstudio | ||
| 379 |