src/utilities/bcl/BCLMeasure.hpp
| 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 | #ifndef UTILITIES_BCL_BCLMEASURE_HPP | ||
| 7 | #define UTILITIES_BCL_BCLMEASURE_HPP | ||
| 8 | |||
| 9 | #include "BCLXML.hpp" | ||
| 10 | #include "BCLEnums.hpp" | ||
| 11 | #include "../core/Optional.hpp" | ||
| 12 | #include "../core/Path.hpp" | ||
| 13 | #include "../core/Deprecated.hpp" | ||
| 14 | #include "../data/Attribute.hpp" | ||
| 15 | #include "../UtilitiesAPI.hpp" | ||
| 16 | |||
| 17 | #include <vector> | ||
| 18 | |||
| 19 | namespace Json { | ||
| 20 | class Value; | ||
| 21 | } | ||
| 22 | |||
| 23 | namespace openstudio { | ||
| 24 | |||
| 25 | class FileReferenceType; | ||
| 26 | |||
| 27 | /** BCLMeasure is a class for managing the contents of a BCL Measure directory including the xml description file. | ||
| 28 | **/ | ||
| 29 | class UTILITIES_API BCLMeasure | ||
| 30 | { | ||
| 31 | public: | ||
| 32 | /** @name Constructors */ | ||
| 33 | //@{ | ||
| 34 | |||
| 35 | /// Constructor for a new measure with required data | ||
| 36 | /// Will throw exception unless dir is a directory that does not exist or an empty directory. | ||
| 37 | /// A template measure will be created, including a blank user script. | ||
| 38 | BCLMeasure(const std::string& name, const std::string& className, const openstudio::path& dir, const std::string& taxonomyTag, | ||
| 39 | MeasureType measureType, const std::string& description, const std::string& modelerDescription, | ||
| 40 | MeasureLanguage measureLanguage = MeasureLanguage::Ruby); | ||
| 41 | |||
| 42 | /// Constructor for downloaded measures, path is to directory containing "measure.xml" file | ||
| 43 | /// Will throw exception if directory does not exist or does not represent a valid measure. | ||
| 44 | BCLMeasure(const openstudio::path& dir); | ||
| 45 | |||
| 46 | /// Convert a measure name to a valid Ruby class name | ||
| 47 | static std::string makeClassName(const std::string& name); | ||
| 48 | |||
| 49 | /// Try to load a BCLMeasure from a directory containing a "measure.xml" file. | ||
| 50 | static boost::optional<BCLMeasure> load(const openstudio::path& dir); | ||
| 51 | |||
| 52 | /// Suggested values for "Intended Software Tool" attribute | ||
| 53 | static std::vector<std::string> suggestedIntendedSoftwareTools(); | ||
| 54 | |||
| 55 | /// Suggested values for "Intended Use Case" attribute | ||
| 56 | static std::vector<std::string> suggestedIntendedUseCases(); | ||
| 57 | |||
| 58 | /// Suggested values for the first level taxonomy tag | ||
| 59 | // DLM: we should move the TID code out of BuildingComponentDialog into BCLComponent | ||
| 60 | static std::vector<std::string> suggestedFirstLevelTaxonomyTerms(); | ||
| 61 | |||
| 62 | /// Suggested values for the second level taxonomy tag | ||
| 63 | static std::vector<std::string> suggestedSecondLevelTaxonomyTerms(const std::string& firstLevelTaxonomyTerm); | ||
| 64 | |||
| 65 | static bool isIgnoredFileName(const std::string& fileName); | ||
| 66 | |||
| 67 | static bool isApprovedFile(const openstudio::path& absoluteFilePath, const openstudio::path& measureDir); | ||
| 68 | |||
| 69 | //@} | ||
| 70 | /** @name Destructor */ | ||
| 71 | //@{ | ||
| 72 | |||
| 73 | /// Virtual destructor | ||
| 74 | 1608 | virtual ~BCLMeasure() = default; | |
| 75 | |||
| 76 | //@} | ||
| 77 | /** @name Getters */ | ||
| 78 | //@{ | ||
| 79 | |||
| 80 | openstudio::path directory() const; | ||
| 81 | |||
| 82 | boost::optional<std::string> error() const; | ||
| 83 | |||
| 84 | std::string uid() const; | ||
| 85 | |||
| 86 | UUID uuid() const; | ||
| 87 | |||
| 88 | std::string versionId() const; | ||
| 89 | |||
| 90 | UUID versionUUID() const; | ||
| 91 | |||
| 92 | boost::optional<DateTime> versionModified() const; | ||
| 93 | |||
| 94 | std::string xmlChecksum() const; | ||
| 95 | |||
| 96 | // string representation of the underlying BCLXML | ||
| 97 | std::string xmlString() const; | ||
| 98 | |||
| 99 | std::string name() const; | ||
| 100 | |||
| 101 | std::string displayName() const; | ||
| 102 | |||
| 103 | std::string className() const; | ||
| 104 | |||
| 105 | std::string description() const; | ||
| 106 | |||
| 107 | std::string modelerDescription() const; | ||
| 108 | |||
| 109 | std::vector<BCLMeasureArgument> arguments() const; | ||
| 110 | |||
| 111 | std::vector<BCLMeasureOutput> outputs() const; | ||
| 112 | |||
| 113 | // ETH: provenance? | ||
| 114 | // DLM: we can do this later | ||
| 115 | |||
| 116 | std::vector<std::string> tags() const; | ||
| 117 | |||
| 118 | std::vector<Attribute> attributes() const; | ||
| 119 | |||
| 120 | std::vector<BCLFileReference> files() const; | ||
| 121 | |||
| 122 | //@} | ||
| 123 | /** @name Setters */ | ||
| 124 | //@{ | ||
| 125 | |||
| 126 | void setError(const std::string& error); | ||
| 127 | |||
| 128 | void resetError(); | ||
| 129 | |||
| 130 | void setName(const std::string& name); | ||
| 131 | |||
| 132 | void setDisplayName(const std::string& displayName); | ||
| 133 | |||
| 134 | void setClassName(const std::string& className); | ||
| 135 | |||
| 136 | void setDescription(const std::string& description); | ||
| 137 | |||
| 138 | void setModelerDescription(const std::string& modelerDescription); | ||
| 139 | |||
| 140 | void setArguments(const std::vector<BCLMeasureArgument>& arguments); | ||
| 141 | |||
| 142 | void setOutputs(const std::vector<BCLMeasureOutput>& outputs); | ||
| 143 | |||
| 144 | //@} | ||
| 145 | /** @name Operators */ | ||
| 146 | //@{ | ||
| 147 | |||
| 148 | /// Returns the single taxonomy tag allowed | ||
| 149 | std::string taxonomyTag() const; | ||
| 150 | |||
| 151 | // DLM: might want to deprecate this as this is set manually in XML | ||
| 152 | /// Sets the single taxonomy tag allowed | ||
| 153 | void setTaxonomyTag(const std::string& taxonomyTag); | ||
| 154 | |||
| 155 | /// Returns value of the "Measure Type" attribute | ||
| 156 | MeasureType measureType() const; | ||
| 157 | |||
| 158 | MeasureLanguage measureLanguage() const; | ||
| 159 | |||
| 160 | /// Sets value of the "Measure Type" attribute | ||
| 161 | void setMeasureType(const MeasureType& measureType); | ||
| 162 | |||
| 163 | void setMeasureLanguage(const MeasureLanguage& measureLanguage); | ||
| 164 | |||
| 165 | /// Returns values of any "Intended Software Tool" attributes | ||
| 166 | std::vector<std::string> intendedSoftwareTools() const; | ||
| 167 | |||
| 168 | /// Returns values of any "Intended Use Case" attributes | ||
| 169 | std::vector<std::string> intendedUseCases() const; | ||
| 170 | |||
| 171 | /** Returns the path of the primary Ruby script if the file exists and is | ||
| 172 | * a regular file. Otherwise returns boost::none. */ | ||
| 173 | boost::optional<openstudio::path> primaryRubyScriptPath() const; | ||
| 174 | |||
| 175 | boost::optional<openstudio::path> primaryPythonScriptPath() const; | ||
| 176 | |||
| 177 | /** Returns the path of the primary script path, based on MeasureLanguage **/ | ||
| 178 | boost::optional<openstudio::path> primaryScriptPath() const; | ||
| 179 | |||
| 180 | /// Returns the input file type for this measure | ||
| 181 | FileReferenceType inputFileType() const; | ||
| 182 | |||
| 183 | /// Returns the output file type for this measure | ||
| 184 | FileReferenceType outputFileType() const; | ||
| 185 | |||
| 186 | /// Clears all files | ||
| 187 | void clearFiles(); | ||
| 188 | |||
| 189 | /// Adds attribute to attribute list | ||
| 190 | void addAttribute(const Attribute& attribute); | ||
| 191 | |||
| 192 | /// get attributes by name | ||
| 193 | std::vector<Attribute> getAttributes(const std::string& name) const; | ||
| 194 | |||
| 195 | /// Removes attribute by name, returns true if attribute was found and removed | ||
| 196 | bool removeAttributes(const std::string& name); | ||
| 197 | |||
| 198 | /// Removes all attributes | ||
| 199 | void clearAttributes(); | ||
| 200 | |||
| 201 | /// Check for all required fields, return true if any are missing | ||
| 202 | /// Does not update the XML | ||
| 203 | bool missingRequiredFields() const; | ||
| 204 | |||
| 205 | /// Check for updates to files, will increment versionID and return true | ||
| 206 | /// if any files have changed, been added, or removed from the measure | ||
| 207 | /// The measure must still be saved to disk to preserve the new versionID | ||
| 208 | /// Typical usage is 1) checkForUpdatesFiles, 2) update content using embedded ruby interpreter, 3) checkForUpdatesXML | ||
| 209 | bool checkForUpdatesFiles(); | ||
| 210 | |||
| 211 | /// Check for updates to the xml, will increment versionID and xmlChecksum then return true | ||
| 212 | /// if any xml fields (other than uid, version id, or xml checksum) have changed | ||
| 213 | /// The xml file must still be saved to disk to preserve the new versionID | ||
| 214 | bool checkForUpdatesXML(); | ||
| 215 | |||
| 216 | /// Equality test - Compares the uids and versionIds of two separate measures | ||
| 217 | bool operator==(const BCLMeasure& other) const; | ||
| 218 | |||
| 219 | bool operator!=(const BCLMeasure& other) const; | ||
| 220 | |||
| 221 | /// Less than | ||
| 222 | bool operator<(const BCLMeasure& other) const; | ||
| 223 | |||
| 224 | /// Save the measure.xml back to the directory | ||
| 225 | bool save() const; | ||
| 226 | |||
| 227 | /// Clone the measure from the original location to the new location | ||
| 228 | /// newDir is a directory which either does not exist or is empty | ||
| 229 | /// If the copy is successful a new BCLMeasure is returned | ||
| 230 | boost::optional<BCLMeasure> clone(const openstudio::path& newDir) const; | ||
| 231 | |||
| 232 | /// Change the UID for this measure | ||
| 233 | void changeUID(); | ||
| 234 | |||
| 235 | /// Change the Version ID for this measure | ||
| 236 | void incrementVersionId(); | ||
| 237 | |||
| 238 | /// Update's the ruby measure script to have this new information | ||
| 239 | // cppcheck-suppress functionConst | ||
| 240 | bool updateMeasureScript(const MeasureType& oldMeasureType, const MeasureType& newMeasureType, const MeasureLanguage& oldMeasureLanguage, | ||
| 241 | const MeasureLanguage& newMeasureLanguage, const std::string& oldClassName, const std::string& newClassName, | ||
| 242 | const std::string& name, const std::string& description, const std::string& modelerDescription); | ||
| 243 | |||
| 244 | /// Update's the ruby measure tests to have this new information | ||
| 245 | // cppcheck-suppress functionConst | ||
| 246 | bool updateMeasureTests(const std::string& oldClassName, const std::string& newClassName); | ||
| 247 | |||
| 248 | /// get all measures in an input directory | ||
| 249 | static std::vector<BCLMeasure> getMeasuresInDir(const openstudio::path& dir); | ||
| 250 | |||
| 251 | Json::Value toJSON() const; | ||
| 252 | std::string toJSONString() const; | ||
| 253 | |||
| 254 | //@} | ||
| 255 | private: | ||
| 256 | // configure logging | ||
| 257 | − | REGISTER_LOGGER("utilities.bcl.BCLMeasure"); | |
| 258 | |||
| 259 | static void createDirectory(const openstudio::path& dir); | ||
| 260 | |||
| 261 | std::string computeXMLChecksum() const; | ||
| 262 | |||
| 263 | openstudio::path m_directory; | ||
| 264 | BCLXML m_bclXML; | ||
| 265 | }; | ||
| 266 | |||
| 267 | using OptionalBCLMeasure = boost::optional<BCLMeasure>; | ||
| 268 | |||
| 269 | using BCLMeasureVector = std::vector<BCLMeasure>; | ||
| 270 | |||
| 271 | } // namespace openstudio | ||
| 272 | |||
| 273 | #endif // UTILITIES_BCL_BCLMEASURE_HPP | ||
| 274 |