src/utilities/idf/IdfFile.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_IDF_IDFFILE_HPP | ||
| 7 | #define UTILITIES_IDF_IDFFILE_HPP | ||
| 8 | |||
| 9 | #include "../UtilitiesAPI.hpp" | ||
| 10 | |||
| 11 | #include "../idd/IddFileAndFactoryWrapper.hpp" | ||
| 12 | #include "IdfObject.hpp" | ||
| 13 | |||
| 14 | #include "../core/Path.hpp" | ||
| 15 | |||
| 16 | #include <string> | ||
| 17 | #include <ostream> | ||
| 18 | #include <vector> | ||
| 19 | |||
| 20 | namespace openstudio { | ||
| 21 | |||
| 22 | // FORWARD DECLARATIONS | ||
| 23 | class ProgressBar; | ||
| 24 | class StrictnessLevel; | ||
| 25 | class ValidityReport; | ||
| 26 | class VersionString; | ||
| 27 | namespace detail { | ||
| 28 | class Workspace_Impl; | ||
| 29 | } | ||
| 30 | |||
| 31 | /** IdfFile provides parsing and printing of text files in EnergyPlus Input Data File (IDF) | ||
| 32 | * format. This class can be used for ready-to-simulate EnergyPlus .idf files, OpenStudio .osm | ||
| 33 | * files, and partial idf/osm/osc files. This class expects to be constructed with the | ||
| 34 | * appropriate IddFile. An instance of the IddFileType enumeration class can be used to specify | ||
| 35 | * the latest EnergyPlus or OpenStudio IDD files. Previous versions of these IDD files, or IDD | ||
| 36 | * files of a different origin altogether, can be specified by explicitly calling IddFile::load, | ||
| 37 | * and then using that IddFile instance to load or construct an IdfFile. | ||
| 38 | * | ||
| 39 | * IdfFile is not a shared object. It is composed of an ordered list of \link IdfObject | ||
| 40 | * IdfObjects\endlink, each of which is a shared object. */ | ||
| 41 | class UTILITIES_API IdfFile | ||
| 42 | { | ||
| 43 | public: | ||
| 44 | /** @name Constructors */ | ||
| 45 | //@{ | ||
| 46 | |||
| 47 | /** Default constructor with optional IddFileType argument (default is IddFileType::OpenStudio). | ||
| 48 | * Initializes the file by adding a version IdfObject. */ | ||
| 49 | IdfFile(IddFileType iddFileType = IddFileType("OpenStudio")); | ||
| 50 | |||
| 51 | /** Construct from iddFile. Typically, iddFile is of IddFileType::UserCustom, that is, loaded | ||
| 52 | * from disk. Legacy OpenStudio IDD files back through Version 0.7.0 are available through the | ||
| 53 | * IddFactory. If possible, a version IdfObject is added. */ | ||
| 54 | IdfFile(const IddFile& iddFile); | ||
| 55 | |||
| 56 | //@} | ||
| 57 | /** @name Getters */ | ||
| 58 | // @{ | ||
| 59 | |||
| 60 | /** Returns the file header, that is, the comment block at the top of the file. */ | ||
| 61 | std::string header() const; | ||
| 62 | |||
| 63 | /** Returns the version of this model, as determined by the IddFile header. */ | ||
| 64 | VersionString version() const; | ||
| 65 | |||
| 66 | /** Returns the underlying IddFile (the schema for this IdfFile). */ | ||
| 67 | IddFile iddFile() const; | ||
| 68 | |||
| 69 | /** Returns the underlying IddFile's IddFileType. */ | ||
| 70 | IddFileType iddFileType() const; | ||
| 71 | |||
| 72 | /** Returns the version IdfObject in this file, if it exists and is unique. Typically, the | ||
| 73 | * OS_ASSERT below will pass (but is not a trivial test). | ||
| 74 | * \code | ||
| 75 | * IdfObject vo = versionObject().get(); | ||
| 76 | * OS_ASSERT(VersionString(vo.getString(vo.numFields() - 1,true).get()) == version()); | ||
| 77 | * \endcode */ | ||
| 78 | boost::optional<IdfObject> versionObject() const; | ||
| 79 | |||
| 80 | /** Returns the object at index. */ | ||
| 81 | boost::optional<IdfObject> getObject(unsigned index) const; | ||
| 82 | |||
| 83 | /** Returns the object with .handle() == handle, if such an object exists in this file. */ | ||
| 84 | boost::optional<IdfObject> getObject(const Handle& handle) const; | ||
| 85 | |||
| 86 | /** Returns all the objects in this file, excluding versionObject(). */ | ||
| 87 | std::vector<IdfObject> objects() const; | ||
| 88 | |||
| 89 | /** Returns all objects of objectType (e.g. IddObjectType("Zone"), IddObjectType("OS:Space")) | ||
| 90 | * in this file. */ | ||
| 91 | std::vector<IdfObject> getObjectsByType(IddObjectType objectType) const; | ||
| 92 | |||
| 93 | /** Returns all objects in this file for which .iddObject() == objectType. */ | ||
| 94 | std::vector<IdfObject> getObjectsByType(const IddObject& objectType) const; | ||
| 95 | |||
| 96 | /** Returns all objects named name (case insensitive). */ | ||
| 97 | std::vector<IdfObject> getObjectsByName(const std::string& name) const; | ||
| 98 | |||
| 99 | //@} | ||
| 100 | /** @name Setters */ | ||
| 101 | //@{ | ||
| 102 | |||
| 103 | /** Set the header for this file. Will be printed as a comment block at the top of the file, | ||
| 104 | * before the objects. Automatically prefixes comment characters as needed. */ | ||
| 105 | void setHeader(const std::string& header); | ||
| 106 | |||
| 107 | /** Append object to the end of this file. */ | ||
| 108 | void addObject(const IdfObject& object); | ||
| 109 | |||
| 110 | /** Append objects to the end of this file. */ | ||
| 111 | void addObjects(const std::vector<IdfObject>& objects); | ||
| 112 | |||
| 113 | /** Insert object immediately before the first object in this file whose IddObjectType value | ||
| 114 | * is greater than object's. */ | ||
| 115 | void insertObjectByIddObjectType(const IdfObject& object); | ||
| 116 | |||
| 117 | /** Removes object from this file if it is in this file. Otherwise returns false. */ | ||
| 118 | bool removeObject(const IdfObject& object); | ||
| 119 | |||
| 120 | /** Attempts to remove each element of objects from this file, returning the number of | ||
| 121 | * successfully removed objects. */ | ||
| 122 | int removeObjects(const std::vector<IdfObject>& objects); | ||
| 123 | |||
| 124 | //@} | ||
| 125 | /** @name Queries */ | ||
| 126 | //@{ | ||
| 127 | |||
| 128 | /** Returns true if there is no header and no objects. (A versionObject does not count against | ||
| 129 | * emptiness.) */ | ||
| 130 | bool empty() const; | ||
| 131 | |||
| 132 | /** Returns the number of non-version objects in this file. */ | ||
| 133 | unsigned numObjects() const; | ||
| 134 | |||
| 135 | /** Returns the number of objects of objectType in this file. */ | ||
| 136 | unsigned numObjectsOfType(IddObjectType objectType) const; | ||
| 137 | |||
| 138 | /** Returns the number of objects of objectType in this file. */ | ||
| 139 | unsigned numObjectsOfType(const IddObject& objectType) const; | ||
| 140 | |||
| 141 | /** Returns true if this IdfFile is valid, that is, has no \link DataError DataErrors\endlink | ||
| 142 | * at or below level. */ | ||
| 143 | bool isValid(StrictnessLevel level) const; | ||
| 144 | |||
| 145 | /** Returns a ValidityReport for this IdfFile containing all \link DataError DataErrors\endlink | ||
| 146 | * at or below level. */ | ||
| 147 | ValidityReport validityReport(StrictnessLevel level) const; | ||
| 148 | |||
| 149 | //@} | ||
| 150 | /** @name Serialization */ | ||
| 151 | //@{ | ||
| 152 | |||
| 153 | /** Load an IdfFile from std::istream using the IDD defined by IddFactory and iddFileType, if | ||
| 154 | * possible. */ | ||
| 155 | static boost::optional<IdfFile> load(std::istream& is, const IddFileType& iddFileType, ProgressBar* progressBar = nullptr); | ||
| 156 | |||
| 157 | /** Load an IdfFile from std::istream using iddFile, if possible. */ | ||
| 158 | static boost::optional<IdfFile> load(std::istream& is, const IddFile& iddFile, ProgressBar* progressBar = nullptr); | ||
| 159 | |||
| 160 | /** Load an IdfFile from path using the IddFactory, and choosing iddFileType based on file | ||
| 161 | * extension, if possible. (IddFileType::OpenStudio if extension is modelFileExtension() or | ||
| 162 | * componentFileExtension(), IddFileType::EnergyPlus otherwise.) */ | ||
| 163 | static boost::optional<IdfFile> load(const path& p, ProgressBar* progressBar = nullptr); | ||
| 164 | |||
| 165 | /** Load an IdfFile from path using the IddFactory and iddFileType, if possible. Will attempt to | ||
| 166 | * complete the path by tacking on .osm or .idf as appropriate. */ | ||
| 167 | static boost::optional<IdfFile> load(const path& p, const IddFileType& iddFileType, ProgressBar* progressBar = nullptr); | ||
| 168 | |||
| 169 | /** Load an IdfFile from path using iddFile, if possible. If no file extension is provided, will | ||
| 170 | * try "idf". */ | ||
| 171 | static boost::optional<IdfFile> load(const path& p, const IddFile& iddFile, ProgressBar* progressBar = nullptr); | ||
| 172 | |||
| 173 | /** Quick load method that uses the IddFile::catchallIddFile and stops parsing once a version | ||
| 174 | * identifier is found. Used to determine the appropriate IddFile to use for a full load. */ | ||
| 175 | static boost::optional<VersionString> loadVersionOnly(std::istream& is); | ||
| 176 | |||
| 177 | /** Quick load method that uses the IddFile::catchallIddFile and stops parsing once a version | ||
| 178 | * identifier is found. Used to determine the appropriate IddFile to use for a full load. */ | ||
| 179 | static boost::optional<VersionString> loadVersionOnly(const path& p); | ||
| 180 | |||
| 181 | /** Print this file to std::ostream os. */ | ||
| 182 | std::ostream& print(std::ostream& os) const; | ||
| 183 | |||
| 184 | /** Save this file to path p. Will construct the parent folder if necessary and if its parent | ||
| 185 | * folder already exists. Will only overwrite an existing file if overwrite==true. If no | ||
| 186 | * extension is provided will use modelFileExtension() for files using IddFileType::OpenStudio, | ||
| 187 | * and 'idf' otherwise. Returns true if the save operation is successful; false otherwise. */ | ||
| 188 | bool save(const openstudio::path& p, bool overwrite = false); | ||
| 189 | |||
| 190 | //@} | ||
| 191 | |||
| 192 | protected: | ||
| 193 | friend class detail::Workspace_Impl; | ||
| 194 | |||
| 195 | IddFileAndFactoryWrapper iddFileAndFactoryWrapper() const; | ||
| 196 | void setIddFileAndFactoryWrapper(const IddFileAndFactoryWrapper& iddFileAndFactoryWrapper); | ||
| 197 | |||
| 198 | private: | ||
| 199 | std::string m_header; | ||
| 200 | std::vector<IdfObject> m_objects; | ||
| 201 | std::set<unsigned> m_versionObjectIndices; | ||
| 202 | |||
| 203 | IddFileAndFactoryWrapper m_iddFileAndFactoryWrapper; | ||
| 204 | |||
| 205 | // SETTERS | ||
| 206 | |||
| 207 | void addVersionObject(); | ||
| 208 | |||
| 209 | // QUERIES | ||
| 210 | |||
| 211 | std::vector<IdfObject> m_objectsWithConflictingNames(const std::string& name, bool getAll) const; | ||
| 212 | |||
| 213 | // SERIALIZATION | ||
| 214 | |||
| 215 | /// private load function that uses m_iddFile and m_iddFileType initialized elsewhere | ||
| 216 | bool m_load(std::istream& is, ProgressBar* progressBar = nullptr, bool versionOnly = false); | ||
| 217 | |||
| 218 | // configure logging | ||
| 219 | − | REGISTER_LOGGER("utilities.idf.IdfFile"); | |
| 220 | }; | ||
| 221 | |||
| 222 | /// optional idf file | ||
| 223 | using OptionalIdfFile = boost::optional<IdfFile>; | ||
| 224 | |||
| 225 | /// vector of idf file | ||
| 226 | using IdfFileVector = std::vector<IdfFile>; | ||
| 227 | |||
| 228 | // ostream operator<< | ||
| 229 | UTILITIES_API std::ostream& operator<<(std::ostream& os, const IdfFile& idfFile); | ||
| 230 | |||
| 231 | } // namespace openstudio | ||
| 232 | |||
| 233 | #endif //UTILITIES_IDF_IDFFILE_HPP | ||
| 234 |