src/measure/OSMeasureInfoGetter.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 "OSMeasureInfoGetter.hpp" | ||
| 7 | |||
| 8 | #include "../model/Model.hpp" | ||
| 9 | |||
| 10 | #include "../utilities/idf/Workspace.hpp" | ||
| 11 | #include "../utilities/bcl/BCLMeasure.hpp" | ||
| 12 | #include "../utilities/core/StringHelpers.hpp" | ||
| 13 | |||
| 14 | #include <sstream> | ||
| 15 | |||
| 16 | namespace openstudio { | ||
| 17 | namespace measure { | ||
| 18 | |||
| 19 | ✗ | OSMeasureInfo::OSMeasureInfo(const std::string& error) : m_error(error) {} | |
| 20 | |||
| 21 | ✗ | OSMeasureInfo::OSMeasureInfo(const MeasureType& measureType, const std::string& className, const std::string& name, const std::string& description, | |
| 22 | const std::string& taxonomy, const std::string& modelerDescription, const std::vector<OSArgument>& arguments, | ||
| 23 | ✗ | const std::vector<OSOutput>& outputs) | |
| 24 | ✗ | : m_measureType(measureType), | |
| 25 | ✗ | m_className(className), | |
| 26 | ✗ | m_name(name), | |
| 27 | ✗ | m_description(description), | |
| 28 | ✗ | m_taxonomy(taxonomy), | |
| 29 | ✗ | m_modelerDescription(modelerDescription), | |
| 30 | ✗ | m_arguments(arguments), | |
| 31 | ✗ | m_outputs(outputs) {} | |
| 32 | |||
| 33 | ✗ | boost::optional<std::string> OSMeasureInfo::error() const { | |
| 34 | ✗ | return m_error; | |
| 35 | } | ||
| 36 | |||
| 37 | ✗ | MeasureType OSMeasureInfo::measureType() const { | |
| 38 | ✗ | return m_measureType; | |
| 39 | } | ||
| 40 | |||
| 41 | ✗ | std::string OSMeasureInfo::className() const { | |
| 42 | ✗ | return m_className; | |
| 43 | } | ||
| 44 | |||
| 45 | ✗ | std::string OSMeasureInfo::name() const { | |
| 46 | ✗ | return m_name; | |
| 47 | } | ||
| 48 | |||
| 49 | ✗ | std::string OSMeasureInfo::description() const { | |
| 50 | ✗ | return m_description; | |
| 51 | } | ||
| 52 | |||
| 53 | ✗ | std::string OSMeasureInfo::taxonomy() const { | |
| 54 | ✗ | return m_taxonomy; | |
| 55 | } | ||
| 56 | |||
| 57 | ✗ | std::string OSMeasureInfo::modelerDescription() const { | |
| 58 | ✗ | return m_modelerDescription; | |
| 59 | } | ||
| 60 | |||
| 61 | ✗ | std::vector<OSArgument> OSMeasureInfo::arguments() const { | |
| 62 | ✗ | return m_arguments; | |
| 63 | } | ||
| 64 | |||
| 65 | ✗ | std::vector<BCLMeasureArgument> OSMeasureInfo::bclMeasureArguments() const { | |
| 66 | ✗ | unsigned n = m_arguments.size(); | |
| 67 | ✗ | std::vector<BCLMeasureArgument> bclArguments; | |
| 68 | ✗ | bclArguments.reserve(n); | |
| 69 | ✗ | for (const OSArgument& argument : m_arguments) { | |
| 70 | |||
| 71 | ✗ | std::string bclMeasureType = argument.type().valueName(); | |
| 72 | ✗ | if (argument.type() == OSArgumentType::Quantity) { | |
| 73 | ✗ | LOG(Warn, "Mapping deprecated OSArgumentType::Quantity to Double"); | |
| 74 | ✗ | bclMeasureType = "Double"; | |
| 75 | } | ||
| 76 | |||
| 77 | ✗ | boost::optional<std::string> defaultValue; | |
| 78 | ✗ | if (argument.hasDefaultValue()) { | |
| 79 | ✗ | defaultValue = argument.defaultValueAsString(); | |
| 80 | } | ||
| 81 | |||
| 82 | ✗ | boost::optional<std::string> minValue; | |
| 83 | ✗ | boost::optional<std::string> maxValue; | |
| 84 | ✗ | if (argument.hasDomain()) { | |
| 85 | ✗ | if (argument.type() == OSArgumentType::Integer) { | |
| 86 | ✗ | auto domain = argument.domainAsInteger(); | |
| 87 | ✗ | auto& low = domain.front(); | |
| 88 | ✗ | auto& high = domain.back(); | |
| 89 | ✗ | if (low > std::numeric_limits<int>::lowest()) { | |
| 90 | ✗ | minValue = std::to_string(low); | |
| 91 | } | ||
| 92 | ✗ | if (high < std::numeric_limits<int>::max()) { | |
| 93 | ✗ | maxValue = std::to_string(high); | |
| 94 | } | ||
| 95 | ✗ | } else if (argument.type() == OSArgumentType::Double) { | |
| 96 | ✗ | auto domain = argument.domainAsDouble(); | |
| 97 | ✗ | auto& low = domain.front(); | |
| 98 | ✗ | auto& high = domain.back(); | |
| 99 | ✗ | if (low > std::numeric_limits<double>::lowest()) { | |
| 100 | ✗ | minValue = std::to_string(low); | |
| 101 | } | ||
| 102 | ✗ | if (high < std::numeric_limits<double>::max()) { | |
| 103 | ✗ | maxValue = std::to_string(high); | |
| 104 | } | ||
| 105 | ✗ | } | |
| 106 | } | ||
| 107 | |||
| 108 | ✗ | bclArguments.emplace_back(argument.name(), argument.displayName(), argument.description(), bclMeasureType, argument.units(), | |
| 109 | ✗ | argument.required(), argument.modelDependent(), defaultValue, argument.choiceValues(), | |
| 110 | ✗ | argument.choiceValueDisplayNames(), minValue, maxValue); | |
| 111 | ✗ | } | |
| 112 | |||
| 113 | ✗ | return bclArguments; | |
| 114 | ✗ | } | |
| 115 | |||
| 116 | ✗ | std::vector<OSOutput> OSMeasureInfo::outputs() const { | |
| 117 | ✗ | return m_outputs; | |
| 118 | } | ||
| 119 | |||
| 120 | ✗ | bool OSMeasureInfo::update(BCLMeasure& measure) const { | |
| 121 | ✗ | bool result = false; | |
| 122 | |||
| 123 | ✗ | if (m_error) { | |
| 124 | // have error | ||
| 125 | ✗ | if (!measure.error() || measure.error().get() != *m_error) { | |
| 126 | ✗ | result = true; | |
| 127 | ✗ | measure.setError(*m_error); | |
| 128 | } | ||
| 129 | // return here so we don't blow away name or other information from last successful run | ||
| 130 | ✗ | return result; | |
| 131 | } else { | ||
| 132 | // no error | ||
| 133 | ✗ | if (measure.error()) { | |
| 134 | ✗ | result = true; | |
| 135 | ✗ | measure.resetError(); | |
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | // map snake cased class name to bcl measure name | ||
| 140 | ✗ | std::string lowerClassName = toUnderscoreCase(m_className); | |
| 141 | ✗ | if (!lowerClassName.empty() && measure.name() != lowerClassName) { | |
| 142 | ✗ | result = true; | |
| 143 | ✗ | measure.setName(lowerClassName); | |
| 144 | } | ||
| 145 | |||
| 146 | // map name from measure to bcl measure display name | ||
| 147 | ✗ | if (!m_name.empty() && measure.displayName() != m_name) { | |
| 148 | ✗ | result = true; | |
| 149 | ✗ | measure.setDisplayName(m_name); | |
| 150 | } | ||
| 151 | |||
| 152 | ✗ | if (!m_className.empty() && measure.className() != m_className) { | |
| 153 | ✗ | result = true; | |
| 154 | ✗ | measure.setClassName(m_className); | |
| 155 | } | ||
| 156 | |||
| 157 | ✗ | if (!m_description.empty() && measure.description() != m_description) { | |
| 158 | ✗ | result = true; | |
| 159 | ✗ | measure.setDescription(m_description); | |
| 160 | } | ||
| 161 | |||
| 162 | ✗ | if (!m_taxonomy.empty() && measure.taxonomyTag() != m_taxonomy) { | |
| 163 | ✗ | result = true; | |
| 164 | ✗ | measure.setTaxonomyTag(m_taxonomy); | |
| 165 | } | ||
| 166 | |||
| 167 | ✗ | if (!m_modelerDescription.empty() && measure.modelerDescription() != m_modelerDescription) { | |
| 168 | ✗ | result = true; | |
| 169 | ✗ | measure.setModelerDescription(m_modelerDescription); | |
| 170 | } | ||
| 171 | |||
| 172 | ✗ | std::vector<BCLMeasureArgument> bclArguments = bclMeasureArguments(); | |
| 173 | |||
| 174 | ✗ | std::vector<BCLMeasureArgument> otherArguments = measure.arguments(); | |
| 175 | ✗ | if (otherArguments.size() != bclArguments.size()) { | |
| 176 | ✗ | result = true; | |
| 177 | ✗ | measure.setArguments(bclArguments); | |
| 178 | } else { | ||
| 179 | ✗ | for (size_t i = 0; i < bclArguments.size(); ++i) { | |
| 180 | ✗ | if (!(bclArguments[i] == otherArguments[i])) { | |
| 181 | ✗ | result = true; | |
| 182 | ✗ | measure.setArguments(bclArguments); | |
| 183 | ✗ | break; | |
| 184 | } | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | // handle outputs | ||
| 189 | ✗ | auto n = m_outputs.size(); | |
| 190 | ✗ | std::vector<BCLMeasureOutput> bclOutputs; | |
| 191 | ✗ | bclOutputs.reserve(n); | |
| 192 | ✗ | for (const OSOutput& output : m_outputs) { | |
| 193 | ✗ | std::string bclOutputType = output.type().valueName(); | |
| 194 | |||
| 195 | ✗ | bclOutputs.emplace_back(output.name(), output.displayName(), output.shortName(), output.description(), bclOutputType, output.units(), | |
| 196 | ✗ | output.modelDependent()); | |
| 197 | ✗ | } | |
| 198 | |||
| 199 | ✗ | std::vector<BCLMeasureOutput> otherOutputs = measure.outputs(); | |
| 200 | ✗ | if (otherOutputs.size() != n) { | |
| 201 | ✗ | result = true; | |
| 202 | ✗ | measure.setOutputs(bclOutputs); | |
| 203 | } else { | ||
| 204 | ✗ | for (size_t i = 0; i < n; ++i) { | |
| 205 | ✗ | if (!(bclOutputs[i] == otherOutputs[i])) { | |
| 206 | ✗ | result = true; | |
| 207 | ✗ | measure.setOutputs(bclOutputs); | |
| 208 | ✗ | break; | |
| 209 | } | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | ✗ | return result; | |
| 214 | ✗ | } | |
| 215 | |||
| 216 | ✗ | OSMeasureInfo OSMeasureInfoGetter::getInfo(const BCLMeasure& measure, const boost::optional<model::Model>& model, | |
| 217 | const boost::optional<Workspace>& workspace) { | ||
| 218 | ✗ | if (model) { | |
| 219 | ✗ | if (workspace) { | |
| 220 | ✗ | return this->getInfo(measure, *model, *workspace); | |
| 221 | } else { | ||
| 222 | ✗ | return this->getInfo(measure, *model); | |
| 223 | } | ||
| 224 | ✗ | } else if (workspace) { | |
| 225 | ✗ | return this->getInfo(measure, *workspace); | |
| 226 | } else { | ||
| 227 | ✗ | return this->getInfo(measure); | |
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | ✗ | std::string infoExtractorRubyFunction() { | |
| 232 | ✗ | std::stringstream ss; | |
| 233 | ✗ | ss << "def infoExtractor(bclMeasure, optionalModel, optionalWorkspace)" << '\n'; | |
| 234 | ✗ | ss << " # GET THE USER SCRIPT" << '\n'; | |
| 235 | ✗ | ss << " scriptPath = bclMeasure.primaryRubyScriptPath()" << '\n'; | |
| 236 | ✗ | ss << " if scriptPath.empty?" << '\n'; | |
| 237 | ✗ | ss << " raise \"Unable to locate primary Ruby script path for BCLMeasure '\" + " << '\n'; | |
| 238 | ✗ | ss << " bclMeasure.name + \"' located at \" + bclMeasure.directory.to_s + \".\"" << '\n'; | |
| 239 | ✗ | ss << " end" << '\n' << '\n'; | |
| 240 | // Check list of objects in memory before loading the script | ||
| 241 | ✗ | ss << " currentObjects = Hash.new" << '\n'; | |
| 242 | ✗ | ss << " ObjectSpace.each_object(OpenStudio::Measure::OSMeasure) { |obj| currentObjects[obj] = true }" << '\n' << '\n'; | |
| 243 | ✗ | ss << " ObjectSpace.garbage_collect" << '\n'; | |
| 244 | // This line is REQUIRED or the ObjectSpace order will change when garbage collection runs automatically | ||
| 245 | // If ~12 measures are added and garbage collection runs, the following loop to grab the first measure | ||
| 246 | // will get the wrong one and return incorrect arguments | ||
| 247 | ✗ | ss << " ObjectSpace.garbage_collect" << '\n'; | |
| 248 | ✗ | ss << " load scriptPath.get.to_s # need load in case have seen this script before" << '\n'; | |
| 249 | ✗ | ss << '\n'; | |
| 250 | ✗ | ss << " measure = nil" << '\n'; | |
| 251 | ✗ | ss << " type = String.new" << '\n'; | |
| 252 | ✗ | ss << " ObjectSpace.each_object(OpenStudio::Measure::OSMeasure) do |obj|" << '\n'; | |
| 253 | ✗ | ss << " if not currentObjects[obj]" << '\n'; | |
| 254 | ✗ | ss << " if obj.is_a? OpenStudio::Measure::ModelMeasure" << '\n'; | |
| 255 | ✗ | ss << " measure = obj" << '\n'; | |
| 256 | ✗ | ss << " type = \"model\"" << '\n'; | |
| 257 | ✗ | ss << " elsif obj.is_a? OpenStudio::Measure::EnergyPlusMeasure" << '\n'; | |
| 258 | ✗ | ss << " measure = obj" << '\n'; | |
| 259 | ✗ | ss << " type = \"energyplus\"" << '\n'; | |
| 260 | ✗ | ss << " elsif obj.is_a? OpenStudio::Measure::ReportingMeasure" << '\n'; | |
| 261 | ✗ | ss << " measure = obj" << '\n'; | |
| 262 | ✗ | ss << " type = \"report\"" << '\n'; | |
| 263 | ✗ | ss << " end" << '\n'; | |
| 264 | ✗ | ss << " end" << '\n'; | |
| 265 | ✗ | ss << " end" << '\n'; | |
| 266 | ✗ | ss << '\n'; | |
| 267 | ✗ | ss << " if not measure" << '\n'; | |
| 268 | ✗ | ss << " raise \"Unable to extract OpenStudio::Measure::OSMeasure object from \" + " << '\n'; | |
| 269 | ✗ | ss << " scriptPath.get.to_s + \". The script should contain a class that derives \" + " << '\n'; | |
| 270 | ✗ | ss << " \"from OpenStudio::Measure::OSMeasure and should close with a line stating \" + " << '\n'; | |
| 271 | ✗ | ss << " \"the class name followed by .new.registerWithApplication.\"" << '\n'; | |
| 272 | ✗ | ss << " end" << '\n'; | |
| 273 | ✗ | ss << '\n'; | |
| 274 | // The following lines may throw | ||
| 275 | ✗ | ss << " measureType = nil" << '\n'; | |
| 276 | ✗ | ss << " className = measure.class.to_s" << '\n'; | |
| 277 | ✗ | ss << " name = measure.name" << '\n'; | |
| 278 | ✗ | ss << " name = className if name.empty?" << '\n'; | |
| 279 | ✗ | ss << " description = measure.description" << '\n'; | |
| 280 | ✗ | ss << " taxonomy = measure.taxonomy" << '\n'; | |
| 281 | ✗ | ss << " modelerDescription = measure.modeler_description" << '\n'; | |
| 282 | ✗ | ss << " args = OpenStudio::Measure::OSArgumentVector.new" << '\n'; | |
| 283 | ✗ | ss << " outputs = OpenStudio::Measure::OSOutputVector.new" << '\n'; | |
| 284 | ✗ | ss << " model = OpenStudio::Model::Model.new" << '\n'; | |
| 285 | ✗ | ss << " workspace = OpenStudio::Workspace.new(\"Draft\".to_StrictnessLevel," << '\n'; | |
| 286 | ✗ | ss << " \"EnergyPlus\".to_IddFileType)" << '\n'; | |
| 287 | ✗ | ss << " model = optionalModel.get if not optionalModel.empty?" << '\n'; | |
| 288 | ✗ | ss << " workspace = optionalWorkspace.get if not optionalWorkspace.empty?" << '\n'; | |
| 289 | ✗ | ss << " if type == \"report\"" << '\n'; | |
| 290 | ✗ | ss << " measureType = OpenStudio::MeasureType.new(\"ReportingMeasure\")" << '\n'; | |
| 291 | ✗ | ss << " args = measure.arguments(model)" << '\n'; | |
| 292 | ✗ | ss << " outputs = measure.outputs()" << '\n'; | |
| 293 | ✗ | ss << " elsif type == \"model\"" << '\n'; | |
| 294 | ✗ | ss << " measureType = OpenStudio::MeasureType.new(\"ModelMeasure\")" << '\n'; | |
| 295 | ✗ | ss << " args = measure.arguments(model)" << '\n'; | |
| 296 | ✗ | ss << " outputs = measure.outputs()" << '\n'; | |
| 297 | ✗ | ss << " elsif type == \"energyplus\"" << '\n'; | |
| 298 | ✗ | ss << " measureType = OpenStudio::MeasureType.new(\"EnergyPlusMeasure\")" << '\n'; | |
| 299 | ✗ | ss << " args = measure.arguments(workspace)" << '\n'; | |
| 300 | ✗ | ss << " outputs = measure.outputs()" << '\n'; | |
| 301 | ✗ | ss << " end" << '\n'; | |
| 302 | ✗ | ss << '\n'; | |
| 303 | ss << " return OpenStudio::Measure::OSMeasureInfo.new(measureType, className, name, description, taxonomy, modelerDescription, args, outputs)" | ||
| 304 | ✗ | << '\n'; | |
| 305 | ✗ | ss << "end" << '\n'; | |
| 306 | ✗ | return ss.str(); | |
| 307 | ✗ | } | |
| 308 | |||
| 309 | ✗ | void MeasureInfoBinding::setMeasureInfo(OSMeasureInfo& info) {}; | |
| 310 | ✗ | bool MeasureInfoBinding::renderFile(const std::string& readmeInPath) { | |
| 311 | ✗ | return false; | |
| 312 | }; | ||
| 313 | |||
| 314 | } // namespace measure | ||
| 315 | } // namespace openstudio | ||
| 316 |