src/utilities/bcl/BCLXML.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_BCLXML_HPP | ||
| 7 | #define UTILITIES_BCL_BCLXML_HPP | ||
| 8 | |||
| 9 | #include "BCLEnums.hpp" | ||
| 10 | #include "BCLFileReference.hpp" | ||
| 11 | #include "BCLMeasureArgument.hpp" | ||
| 12 | #include "BCLMeasureOutput.hpp" | ||
| 13 | #include "../core/Optional.hpp" | ||
| 14 | #include "../core/Path.hpp" | ||
| 15 | #include "../core/UUID.hpp" | ||
| 16 | #include "../data/Attribute.hpp" | ||
| 17 | #include "../core/Deprecated.hpp" | ||
| 18 | #include "../UtilitiesAPI.hpp" | ||
| 19 | |||
| 20 | #include <compare> | ||
| 21 | #include <vector> | ||
| 22 | |||
| 23 | namespace pugi { | ||
| 24 | class xml_document; | ||
| 25 | } // namespace pugi | ||
| 26 | |||
| 27 | namespace Json { | ||
| 28 | class Value; | ||
| 29 | } | ||
| 30 | |||
| 31 | namespace openstudio { | ||
| 32 | |||
| 33 | class BCLComponent; | ||
| 34 | class BCLMeasure; | ||
| 35 | class DateTime; | ||
| 36 | |||
| 37 | /** BCLXML is a class for accessing the common XML structures of BCLComponent and BCLMeasure. | ||
| 38 | **/ | ||
| 39 | class UTILITIES_API BCLXML | ||
| 40 | { | ||
| 41 | |||
| 42 | public: | ||
| 43 | /** @name Constructors */ | ||
| 44 | //@{ | ||
| 45 | |||
| 46 | /// Constructor for a new XML | ||
| 47 | explicit BCLXML(const BCLXMLType& bclXMLType); | ||
| 48 | |||
| 49 | /// Constructor from file path, will throw exception if file does not exist or is incorrect. | ||
| 50 | explicit BCLXML(const openstudio::path& xmlPath); | ||
| 51 | |||
| 52 | /// Try to load a BCLXML from disk. | ||
| 53 | static boost::optional<BCLXML> load(const openstudio::path& xmlPath); | ||
| 54 | |||
| 55 | /// Escape a string to write to xml | ||
| 56 | static std::string escapeString(const std::string& txt); | ||
| 57 | |||
| 58 | /// Decode a string written in xml | ||
| 59 | static std::string decodeString(const std::string& txt); | ||
| 60 | |||
| 61 | // Returns the component/measure XSD schema version currently in use when writting a new xml. | ||
| 62 | static VersionString currentSchemaVersion(); | ||
| 63 | |||
| 64 | //@} | ||
| 65 | /** @name Destructor */ | ||
| 66 | //@{ | ||
| 67 | |||
| 68 | /// Virtual destructor | ||
| 69 | 2352 | virtual ~BCLXML() = default; | |
| 70 | |||
| 71 | //@} | ||
| 72 | /** @name Getters */ | ||
| 73 | //@{ | ||
| 74 | |||
| 75 | /// Returns path to XML file. | ||
| 76 | openstudio::path path() const; | ||
| 77 | |||
| 78 | /// Returns parent path of XML file. | ||
| 79 | openstudio::path directory() const; | ||
| 80 | |||
| 81 | boost::optional<std::string> error() const; | ||
| 82 | |||
| 83 | std::string uid() const; | ||
| 84 | |||
| 85 | std::string versionId() const; | ||
| 86 | |||
| 87 | boost::optional<DateTime> versionModified() const; | ||
| 88 | |||
| 89 | std::string xmlChecksum() const; | ||
| 90 | |||
| 91 | std::string name() const; | ||
| 92 | |||
| 93 | std::string displayName() const; | ||
| 94 | |||
| 95 | std::string className() const; | ||
| 96 | |||
| 97 | std::string description() const; | ||
| 98 | |||
| 99 | std::string modelerDescription() const; | ||
| 100 | |||
| 101 | std::vector<BCLMeasureArgument> arguments() const; | ||
| 102 | |||
| 103 | std::vector<BCLMeasureOutput> outputs() const; | ||
| 104 | |||
| 105 | std::vector<BCLFileReference> files() const; | ||
| 106 | |||
| 107 | /// Returns references to all files of given type. | ||
| 108 | std::vector<BCLFileReference> files(const std::string& filetype) const; | ||
| 109 | |||
| 110 | std::vector<Attribute> attributes() const; | ||
| 111 | |||
| 112 | /// get attributes by name | ||
| 113 | std::vector<Attribute> getAttributes(const std::string& name) const; | ||
| 114 | |||
| 115 | std::vector<std::string> tags() const; | ||
| 116 | |||
| 117 | //@} | ||
| 118 | /** @name Setters */ | ||
| 119 | //@{ | ||
| 120 | |||
| 121 | void resetXMLChecksum(); | ||
| 122 | |||
| 123 | void setError(const std::string& error); | ||
| 124 | |||
| 125 | void resetError(); | ||
| 126 | |||
| 127 | void setName(const std::string& name); | ||
| 128 | |||
| 129 | void setDisplayName(const std::string& displayName); | ||
| 130 | |||
| 131 | void setClassName(const std::string& className); | ||
| 132 | |||
| 133 | void setDescription(const std::string& description); | ||
| 134 | |||
| 135 | void setModelerDescription(const std::string& modelerDescription); | ||
| 136 | |||
| 137 | void setArguments(const std::vector<BCLMeasureArgument>& arguments); | ||
| 138 | |||
| 139 | void setOutputs(const std::vector<BCLMeasureOutput>& outputs); | ||
| 140 | |||
| 141 | /// adds file to list, file with same full path will be removed | ||
| 142 | void addFile(const BCLFileReference& file); | ||
| 143 | |||
| 144 | /// check if has file reference by full path, returns true if file was found | ||
| 145 | bool hasFile(const openstudio::path& path) const; | ||
| 146 | |||
| 147 | /// remove file reference by full path, returns true if file was found and removed | ||
| 148 | bool removeFile(const openstudio::path& path); | ||
| 149 | |||
| 150 | /// clear all files | ||
| 151 | void clearFiles(); | ||
| 152 | |||
| 153 | /// adds attribute to attribute list | ||
| 154 | void addAttribute(const Attribute& attribute); | ||
| 155 | |||
| 156 | /// removes all attributes with name, returns true if attributes were found and removed | ||
| 157 | bool removeAttributes(const std::string& name); | ||
| 158 | |||
| 159 | /// removes all attributes | ||
| 160 | void clearAttributes(); | ||
| 161 | |||
| 162 | /// adds a tag, existing tag with same name will be removed | ||
| 163 | void addTag(const std::string& tagName); | ||
| 164 | |||
| 165 | /// removes tag by name, returns true if tag was found and removed | ||
| 166 | bool removeTag(const std::string& tagName); | ||
| 167 | |||
| 168 | /// removes all tags | ||
| 169 | void clearTags(); | ||
| 170 | |||
| 171 | //@} | ||
| 172 | /** @name Operators */ | ||
| 173 | //@{ | ||
| 174 | |||
| 175 | /// Save the XML back to the original path, always increments version id. | ||
| 176 | /// TODO: does it? it wouldn't be const if it did | ||
| 177 | bool save() const; | ||
| 178 | |||
| 179 | /// Save the XML to a new path, always increments version id. | ||
| 180 | bool saveAs(const openstudio::path& xmlPath); | ||
| 181 | |||
| 182 | /// Returns a string representation of the XML | ||
| 183 | std::string toString() const; | ||
| 184 | |||
| 185 | void changeUID(); | ||
| 186 | |||
| 187 | void incrementVersionId(); | ||
| 188 | |||
| 189 | /// Check for updates to the xml, will increment versionID and xmlChecksum then return true | ||
| 190 | /// if any xml fields (other than uid, version id, or xml checksum) have changed | ||
| 191 | /// The xml file must still be saved to disk to preserve the new versionID | ||
| 192 | bool checkForUpdatesXML(); | ||
| 193 | |||
| 194 | Json::Value toJSON() const; | ||
| 195 | std::string toJSONString() const; | ||
| 196 | |||
| 197 | //@} | ||
| 198 | protected: | ||
| 199 | /** Prints BCLXML to os. \relates BCLXML */ | ||
| 200 | UTILITIES_API friend std::ostream& operator<<(std::ostream& os, const BCLXML& bclXML); | ||
| 201 | |||
| 202 | private: | ||
| 203 | // configure logging | ||
| 204 | − | REGISTER_LOGGER("utilities.bcl.BCLXML"); | |
| 205 | |||
| 206 | /// Compute the current xml checksum | ||
| 207 | std::string computeXMLChecksum() const; | ||
| 208 | |||
| 209 | pugi::xml_document toXML() const; | ||
| 210 | |||
| 211 | openstudio::path m_path; | ||
| 212 | BCLXMLType m_bclXMLType; | ||
| 213 | boost::optional<std::string> m_error; | ||
| 214 | std::string m_name; | ||
| 215 | std::string m_displayName; | ||
| 216 | std::string m_className; // only for measures | ||
| 217 | std::string m_uid; | ||
| 218 | std::string m_versionId; | ||
| 219 | std::string m_versionModified; | ||
| 220 | std::string m_xmlChecksum; | ||
| 221 | std::string m_description; | ||
| 222 | std::string m_modelerDescription; // only for measures | ||
| 223 | std::vector<BCLMeasureArgument> m_arguments; // only for measures | ||
| 224 | std::vector<BCLMeasureOutput> m_outputs; // only for measures | ||
| 225 | std::vector<BCLFileReference> m_files; | ||
| 226 | std::vector<std::string> m_filetypes; | ||
| 227 | std::vector<Attribute> m_attributes; | ||
| 228 | std::vector<std::string> m_tags; | ||
| 229 | }; | ||
| 230 | |||
| 231 | } // namespace openstudio | ||
| 232 | |||
| 233 | #endif // UTILITIES_BCL_BCLXML_HPP | ||
| 234 |