src/utilities/idf/Workspace_Impl.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_WORKSPACE_IMPL_HPP | ||
| 7 | #define UTILITIES_IDF_WORKSPACE_IMPL_HPP | ||
| 8 | |||
| 9 | #include <utilities/UtilitiesAPI.hpp> | ||
| 10 | |||
| 11 | #include <utilities/idf/WorkspaceObject_Impl.hpp> | ||
| 12 | #include <utilities/idf/WorkspaceObjectOrder.hpp> | ||
| 13 | #include <utilities/idf/ValidityEnums.hpp> | ||
| 14 | #include <utilities/idf/ObjectPointer.hpp> | ||
| 15 | |||
| 16 | #include <utilities/idd/IddFileAndFactoryWrapper.hpp> | ||
| 17 | #include <nano/nano_signal_slot.hpp> // Signal-Slot replacement | ||
| 18 | |||
| 19 | #include <boost/functional/hash.hpp> | ||
| 20 | |||
| 21 | #include <utilities/core/Logger.hpp> | ||
| 22 | |||
| 23 | #include <string> | ||
| 24 | #include <ostream> | ||
| 25 | #include <vector> | ||
| 26 | #include <set> | ||
| 27 | #include <map> | ||
| 28 | #include <unordered_map> | ||
| 29 | |||
| 30 | namespace openstudio { | ||
| 31 | |||
| 32 | // forward declarations | ||
| 33 | class IdfFile; | ||
| 34 | class VersionString; | ||
| 35 | |||
| 36 | // private namespace | ||
| 37 | namespace detail { | ||
| 38 | |||
| 39 | /** Implementation of Workspace. Maintains object handles and relationships. Locks down | ||
| 40 | * relationship fields in its IdfObjects if possible. */ | ||
| 41 | class UTILITIES_API Workspace_Impl | ||
| 42 | : public std::enable_shared_from_this<Workspace_Impl> | ||
| 43 | , public Nano::Observer | ||
| 44 | { | ||
| 45 | public: | ||
| 46 | /** @name Constructors and Destructors */ | ||
| 47 | //@{ | ||
| 48 | |||
| 49 | /** Default constructor. Provides access to entire IddFactory, and does not add any objects. */ | ||
| 50 | Workspace_Impl(StrictnessLevel level, IddFileType iddFileType); | ||
| 51 | |||
| 52 | /** Construct from IdfFile. Creates independent IdfObjects, and locks down fields that function | ||
| 53 | * as pointers (\object-list type). Inherits IddFile from idfFile. Aims for StrictnessLevel. | ||
| 54 | * Either sets StrictnessLevel lower if can't get to level, or removes all objects depending | ||
| 55 | * on boolean. */ | ||
| 56 | Workspace_Impl(const IdfFile& idfFile, StrictnessLevel level = StrictnessLevel::Minimal); | ||
| 57 | |||
| 58 | /** Copy constructor makes unconnected copy of all data. Assigning new handles to the new objects | ||
| 59 | * is optional. */ | ||
| 60 | Workspace_Impl(const Workspace_Impl& other, bool keepHandles = false); | ||
| 61 | |||
| 62 | /** Subset copy constructor makes unconnected copy of all data, only keeping objects in handles. */ | ||
| 63 | Workspace_Impl(const Workspace_Impl& other, const std::vector<Handle>& hs, bool keepHandles = false, | ||
| 64 | StrictnessLevel level = StrictnessLevel::Draft); | ||
| 65 | |||
| 66 | /** Create a deep copy (clone) of all data in this Workspace and return the result in a new | ||
| 67 | * Workspace object. If keepHandles, then new handles will not be assigned to the cloned objects. | ||
| 68 | * This feature should be used with care, as reuse of unique object identifiers could lead to | ||
| 69 | * changing data in the wrong Workspace. */ | ||
| 70 | virtual Workspace clone(bool keepHandles = false) const; | ||
| 71 | |||
| 72 | /** Clone just the objects referenced by handles into a new Workspace. All non-object data is | ||
| 73 | * also cloned. If keepHandles, then new handles will not be assigned to the cloned objects. | ||
| 74 | * Virtual implementation, and similar usage to clone. */ | ||
| 75 | virtual Workspace cloneSubset(const std::vector<Handle>& handles, bool keepHandles = false, StrictnessLevel level = StrictnessLevel::Draft) const; | ||
| 76 | |||
| 77 | /** Swaps underlying data between this workspace and other. */ | ||
| 78 | virtual void swap(Workspace& other); | ||
| 79 | |||
| 80 | 20275 | virtual ~Workspace_Impl() = default; | |
| 81 | |||
| 82 | //@} | ||
| 83 | /** @name Type Casting */ | ||
| 84 | //@{ | ||
| 85 | |||
| 86 | template <typename T> | ||
| 87 | 21339 | T getWorkspace() { | |
| 88 |
2/2✓ Branch 1 taken 21339 times.
✓ Branch 6 taken 21339 times.
|
21339 | T result(std::dynamic_pointer_cast<typename T::ImplType>(std::const_pointer_cast<Workspace_Impl>(shared_from_this()))); |
| 89 | 21339 | return result; | |
| 90 | } | ||
| 91 | |||
| 92 | template <typename T> | ||
| 93 | 369 | T getWorkspace() const { | |
| 94 |
2/4openstudio::Workspace openstudio::detail::Workspace_Impl::getWorkspace<openstudio::Workspace>() const:
✓ Branch 1 taken 369 times.
✓ Branch 6 taken 369 times.
openstudio::model::Component openstudio::detail::Workspace_Impl::getWorkspace<openstudio::model::Component>() const:
✗ Branch 1 not taken.
✗ Branch 6 not taken.
|
369 | T result(std::dynamic_pointer_cast<typename T::ImplType>(std::const_pointer_cast<Workspace_Impl>(shared_from_this()))); |
| 95 | 369 | return result; | |
| 96 | } | ||
| 97 | |||
| 98 | Workspace workspace(); | ||
| 99 | |||
| 100 | Workspace workspace() const; | ||
| 101 | |||
| 102 | //@} | ||
| 103 | /** @name Getters */ | ||
| 104 | //@{ | ||
| 105 | |||
| 106 | /// Get the StrictnessLevel this Workspace is currently operating under | ||
| 107 | StrictnessLevel strictnessLevel() const; | ||
| 108 | |||
| 109 | /** Get the version of this model, as determined by the IDD. */ | ||
| 110 | VersionString version() const; | ||
| 111 | |||
| 112 | /// get iddFile | ||
| 113 | IddFile iddFile() const; | ||
| 114 | |||
| 115 | /** Get the \link IddFile IddFile's\endlink IddFileType. */ | ||
| 116 | IddFileType iddFileType() const; | ||
| 117 | |||
| 118 | /** Returns the IddObject if .isInIddFile(type). Otherwise return value evaluates to false. */ | ||
| 119 | boost::optional<IddObject> getIddObject(const IddObjectType& type) const; | ||
| 120 | |||
| 121 | /** True if has an operable name field. */ | ||
| 122 | boost::optional<std::string> name(const Handle& handle) const; | ||
| 123 | |||
| 124 | /** Get object from its handle. */ | ||
| 125 | boost::optional<WorkspaceObject> getObject(const Handle& handle) const; | ||
| 126 | |||
| 127 | /** Get all objects in this workspace. The returned objects' data is shared with the workspace. | ||
| 128 | * If sorted, then the objects are returned in the preferred order. */ | ||
| 129 | std::vector<WorkspaceObject> objects(bool sorted = false) const; | ||
| 130 | |||
| 131 | /** Returns all objects, including the versionObject. Protected in public class. */ | ||
| 132 | std::vector<WorkspaceObject> allObjects() const; | ||
| 133 | |||
| 134 | /** Get all handles. If sorted, then the handles are returned in the preferred order. */ | ||
| 135 | std::vector<Handle> handles(bool sorted = false) const; | ||
| 136 | |||
| 137 | /** Get all the objects in the current workspace that can have a URL entry, that is, they have a | ||
| 138 | * URL IddField in their iddObject(). */ | ||
| 139 | std::vector<WorkspaceObject> objectsWithURLFields() const; | ||
| 140 | |||
| 141 | /** Get the version object in this Workspace, if it exists. */ | ||
| 142 | boost::optional<WorkspaceObject> versionObject() const; | ||
| 143 | |||
| 144 | /** Get objects from their handles. */ | ||
| 145 | std::vector<WorkspaceObject> getObjects(const std::vector<Handle>& handles) const; | ||
| 146 | |||
| 147 | /** Returns all objects named name (case insensitive). If exactMatch == false, will return | ||
| 148 | * all objects with name or name plus an integer suffix. */ | ||
| 149 | std::vector<WorkspaceObject> getObjectsByName(const std::string& name, bool exactMatch = true) const; | ||
| 150 | |||
| 151 | /// get all idf objects by type (e.g. Zone) | ||
| 152 | std::vector<WorkspaceObject> getObjectsByType(IddObjectType objectType) const; | ||
| 153 | |||
| 154 | /// get all idf objects by full idd type | ||
| 155 | std::vector<WorkspaceObject> getObjectsByType(const IddObject& objectType) const; | ||
| 156 | |||
| 157 | /** Returns the first object found of type objectType and named name (case insensitive, | ||
| 158 | * exact match). */ | ||
| 159 | boost::optional<WorkspaceObject> getObjectByTypeAndName(IddObjectType objectType, const std::string& name) const; | ||
| 160 | |||
| 161 | /** Returns all objects named name or name plus an integer suffix (case insensitive). */ | ||
| 162 | std::vector<WorkspaceObject> getObjectsByTypeAndName(IddObjectType objectType, const std::string& name) const; | ||
| 163 | |||
| 164 | /// get all objects by reference name (e.g. ZoneNames) | ||
| 165 | std::vector<WorkspaceObject> getObjectsByReference(const std::string& referenceName) const; | ||
| 166 | |||
| 167 | /// get all objects by reference name (e.g. ZoneNames) | ||
| 168 | std::vector<WorkspaceObject> getObjectsByReference(const std::vector<std::string>& referenceNames) const; | ||
| 169 | |||
| 170 | /** Returns the first object found that is in at least one of the reference lists in | ||
| 171 | * referenceNames and named name (case insensitive, but exact match). Does not look for | ||
| 172 | * conflicts. */ | ||
| 173 | boost::optional<WorkspaceObject> getObjectByNameAndReference(const std::string& name, const std::vector<std::string>& referenceNames) const; | ||
| 174 | |||
| 175 | /** Returns true if fast naming is enabled. */ | ||
| 176 | bool fastNaming() const; | ||
| 177 | |||
| 178 | //@} | ||
| 179 | /** @name Setters */ | ||
| 180 | //@{ | ||
| 181 | |||
| 182 | /** Set the strictness level. If moving to a more strict environment, may return false | ||
| 183 | * to indicate that the collection does not satisfy those requirements. */ | ||
| 184 | virtual bool setStrictnessLevel(StrictnessLevel level); | ||
| 185 | |||
| 186 | // Helper function to start the process of adding an object to the workspace. | ||
| 187 | virtual std::shared_ptr<WorkspaceObject_Impl> createObject(const IdfObject& object, bool keepHandle); | ||
| 188 | |||
| 189 | // Helper function to start the process of adding a cloned object to the workspace. | ||
| 190 | virtual std::shared_ptr<WorkspaceObject_Impl> createObject(const std::shared_ptr<WorkspaceObject_Impl>& originalObjectImplPtr, bool keepHandle); | ||
| 191 | |||
| 192 | virtual std::vector<WorkspaceObject> addObjects(std::vector<std::shared_ptr<WorkspaceObject_Impl>>& objectImplPtrs, bool checkNames); | ||
| 193 | |||
| 194 | virtual std::vector<WorkspaceObject> addObjects(std::vector<std::shared_ptr<WorkspaceObject_Impl>>& objectImplPtrs, | ||
| 195 | const std::vector<UHPointer>& pointersIntoWorkspace = UHPointerVector(), | ||
| 196 | const std::vector<HUPointer>& pointersFromWorkspace = HUPointerVector(), bool driverMethod = true, | ||
| 197 | bool expectToLosePointers = false, bool checkNames = true); | ||
| 198 | |||
| 199 | /** Adds objectImplPtrs to the Workspace. As clones, the pointer handles may be incorrect. This | ||
| 200 | * is fixed by applying oldNewHandleMap to the pointer data. If this is a wholeCollectionClone, | ||
| 201 | * then the map is applied to the directOrder (if it exists) as well, otherwise, the new | ||
| 202 | * objects' handles are pushed onto the directOrder. */ | ||
| 203 | virtual std::vector<WorkspaceObject> addClones(std::vector<std::shared_ptr<WorkspaceObject_Impl>>& objectImplPtrs, | ||
| 204 | const HandleMap& oldNewHandleMap, bool collectionClone, | ||
| 205 | const std::vector<UHPointer>& pointersIntoWorkspace = UHPointerVector(), | ||
| 206 | const std::vector<HUPointer>& pointersFromWorkspace = HUPointerVector(), bool driverMethod = true); | ||
| 207 | |||
| 208 | /** Add object to Workspace. */ | ||
| 209 | virtual boost::optional<WorkspaceObject> addObject(const IdfObject& idfObject); | ||
| 210 | |||
| 211 | /** Equivalent to inserting a Workspace with only idfObject added to it. If successful | ||
| 212 | * (equivalent object found, or object successfully added), new object will be returned. | ||
| 213 | * Otherwise, return value will be false (boost::none). */ | ||
| 214 | virtual boost::optional<WorkspaceObject> insertObject(const IdfObject& idfObject); | ||
| 215 | |||
| 216 | /** Add objects to Workspace, if possible. Use this function to add multiple objects at once | ||
| 217 | * to avoid losing pointer (name reference) information. If successful, returned vector is of | ||
| 218 | * the same size, and is in the same order, as idfObjects. Otherwise, return value will be | ||
| 219 | * .empty(). */ | ||
| 220 | virtual std::vector<WorkspaceObject> addObjects(const std::vector<IdfObject>& idfObjects, bool checkNames = true); | ||
| 221 | |||
| 222 | /** Insert idfObjects into Workspace, if possible. Looks for equivalent objects first, then | ||
| 223 | * adds if necessary. If successful, new and equivalent objects will be returned in same order | ||
| 224 | * as idfObjects. Otherwise, return value will be .empty(). Equivalence is determined by | ||
| 225 | * creating a working Workspace with idfObjects as its contents, then looking for equivalence | ||
| 226 | * between the resulting WorkspaceObjects and this Workspace's objects. */ | ||
| 227 | virtual std::vector<WorkspaceObject> insertObjects(const std::vector<IdfObject>& idfObjects); | ||
| 228 | |||
| 229 | /** Adds objectToAdd and inserts objectsToInsert into Workspace, if possible. If successful, | ||
| 230 | * returned vector is of the same size, and is in the same order, as the concatenation of | ||
| 231 | * objectsToAdd and objectsToInsert. Otherwise, return value will be .empty(). */ | ||
| 232 | virtual std::vector<WorkspaceObject> addAndInsertObjects(const std::vector<IdfObject>& objectsToAdd, | ||
| 233 | const std::vector<IdfObject>& objectsToInsert); | ||
| 234 | |||
| 235 | /** Add objects to this Workspace, if possible. All objects are assumed to be from the same | ||
| 236 | * workspace, possibly this one. All data is cloned. Upon successful completion, the returned | ||
| 237 | * vector is of the same size, and is in the same order, as objects. Pointers are handled as | ||
| 238 | * follows: | ||
| 239 | * | ||
| 240 | * \li objects[i] -> objects[j] becomes result[i] -> result[j] | ||
| 241 | * \li objects[i] -> external object becomes NULL or result[i] -> external object depending on | ||
| 242 | * whether objects[0].workspace() == *this. | ||
| 243 | * \li external object -> objects[i] is not in any way duplicated in result */ | ||
| 244 | virtual std::vector<WorkspaceObject> addObjects(const std::vector<WorkspaceObject>& objects, bool checkNames = true); | ||
| 245 | |||
| 246 | /** Insert objects into this Workspace, if possible. All objects are assumed to be from the same | ||
| 247 | * workspace, possibly this one. Data is only cloned if no equivalent object is located in this | ||
| 248 | * Workspace. Equivalence is defined as dataFieldsEqual and managedObjectListsNonConflicting. | ||
| 249 | * (If objects[0].workspace() == *this, this method call is trivial and objects is returned | ||
| 250 | * directly.) Upon successful completion, the returned vector is of the same size, and is in the | ||
| 251 | * same order, as objects. Pointers are handled as follows: | ||
| 252 | * | ||
| 253 | * \li objects[i] -> objects[j] becomes result[i] -> result[j] | ||
| 254 | * \li objects[i] -> external object becomes NULL or result[i] -> external object depending on | ||
| 255 | * whether objects[0].workspace() == *this. | ||
| 256 | * \li external object -> objects[i] only becomes external object -> result[i] if | ||
| 257 | * objects[0].workspace() == *this */ | ||
| 258 | virtual std::vector<WorkspaceObject> insertObjects(const std::vector<WorkspaceObject>& objects); | ||
| 259 | |||
| 260 | /** Add objectsToAdd and insert objectToInsert into this Workspace. All objects from both vectors | ||
| 261 | * are assumed to be from the same workspace, possibly this one. All relationships between the | ||
| 262 | * objects in both vectors (within and between vectors) are retained. Returns the resulting | ||
| 263 | * objects in the order implied by concatenating objectsToAdd and objectsToInsert. A new object | ||
| 264 | * is always created for each object in objectsToAdd; equivalent objects (dataFieldsEqual and | ||
| 265 | * managedObjectListsNonConflicting) may be returned for objectsToInsert. */ | ||
| 266 | virtual std::vector<WorkspaceObject> addAndInsertObjects(const std::vector<WorkspaceObject>& objectsToAdd, | ||
| 267 | const std::vector<WorkspaceObject>& objectsToInsert); | ||
| 268 | |||
| 269 | /** Add objectsToAdd and insert objectsToInsert into this Workspace. This method differs from the | ||
| 270 | * basic addAndInsertObjects for WorkspaceObjects in that the objectsToInsert are grouped. | ||
| 271 | * Equivalent objects are evaluated as a whole for each group (sub-vector) of objectsToInsert. | ||
| 272 | * That is, if no equivalent is found for any one object in a given group, the whole group is | ||
| 273 | * added to the workspace. */ | ||
| 274 | virtual std::vector<WorkspaceObject> addAndInsertObjects(const std::vector<WorkspaceObject>& objectsToAdd, | ||
| 275 | const std::vector<std::vector<WorkspaceObject>>& objectsToInsert); | ||
| 276 | |||
| 277 | /** Swap currentObject and newObject. To proceed, the objects must be compatible, that is, | ||
| 278 | * all source fields pointing to currentObject must be reset-able to newObject, and (if | ||
| 279 | * keepTargets) newObject must be able to point to the same target objects as currentObject. | ||
| 280 | * If successful, the return value is true, and the data in currentObject and newObject will | ||
| 281 | * have been swapped. Otherwise, the return value evaluates to false. */ | ||
| 282 | virtual bool swap(WorkspaceObject& currentObject, IdfObject& newObject, bool keepTargets = false); | ||
| 283 | |||
| 284 | /** Remove object from Workspace with the expectation that it will be destructed. | ||
| 285 | * This function removes only the object specified by handle it is not overridden | ||
| 286 | * to "do the right thing" for ModelObject. | ||
| 287 | */ | ||
| 288 | virtual bool removeObject(const Handle& handle); | ||
| 289 | |||
| 290 | /** Remove objects from Workspace with the expectation that they will be destructed. | ||
| 291 | * This function removes only the objects specified by handles it is not overridden | ||
| 292 | * to "do the right thing" for ModelObjects. | ||
| 293 | */ | ||
| 294 | virtual bool removeObjects(const std::vector<Handle>& handles); | ||
| 295 | |||
| 296 | /** Add forwarded references. Field index of sourceObject is an object list field that also | ||
| 297 | * defines references. Make sure targetHandle is listed under those reference lists. */ | ||
| 298 | void forwardReferences(const Handle& sourceHandle, unsigned index, const Handle& targetHandle); | ||
| 299 | |||
| 300 | /** Remove forwarded references. Field index of sourceObject is an object list field that also | ||
| 301 | * defines references. That field did point to targetObject. If no other source places | ||
| 302 | * targetObject in those reference lists, remove the association. */ | ||
| 303 | void removeForwardedReferences(const Handle& sourceHandle, unsigned index, const WorkspaceObject& targetObject); | ||
| 304 | |||
| 305 | /** Setting fast naming to true reduces the time taken to create names by using a UUID as the name. | ||
| 306 | * This UUID is not the same as the object's handle. | ||
| 307 | */ | ||
| 308 | void setFastNaming(bool fastNaming); | ||
| 309 | |||
| 310 | /** Resolve name conflicts within other, and between this workspace and other by renaming objects | ||
| 311 | * in other. */ | ||
| 312 | bool resolvePotentialNameConflicts(Workspace& other); | ||
| 313 | |||
| 314 | //@} | ||
| 315 | /** @name Object Order */ | ||
| 316 | //@{ | ||
| 317 | |||
| 318 | /** Access the IdfOrderObject used by the workspace to provide arbitrary order of objects. */ | ||
| 319 | WorkspaceObjectOrder order(); | ||
| 320 | |||
| 321 | /** Access the IdfOrderObject used by the workspace to provide arbitrary order of objects. */ | ||
| 322 | const WorkspaceObjectOrder order() const; | ||
| 323 | |||
| 324 | /** Sort the objects. */ | ||
| 325 | std::vector<WorkspaceObject> sort(const std::vector<WorkspaceObject>& objects) const; | ||
| 326 | |||
| 327 | /** Sort the handles. */ | ||
| 328 | std::vector<Handle> sort(const std::vector<Handle>& handles) const; | ||
| 329 | |||
| 330 | //@} | ||
| 331 | /** @name Queries */ | ||
| 332 | //@{ | ||
| 333 | |||
| 334 | /** Return the total number of objects in the workspace. */ | ||
| 335 | unsigned numObjects() const; | ||
| 336 | |||
| 337 | /** Return the total number of objects (including version objects) in the workspace. */ | ||
| 338 | unsigned numAllObjects() const; | ||
| 339 | |||
| 340 | /** Return the number of objects of IddObjectType type in the workspace. */ | ||
| 341 | unsigned numObjectsOfType(IddObjectType type) const; | ||
| 342 | |||
| 343 | /** Return the number of objects by full IddObject type. */ | ||
| 344 | unsigned numObjectsOfType(const IddObject& objectType) const; | ||
| 345 | |||
| 346 | /** True if handle corresponds to an object in this workspace. */ | ||
| 347 | bool isMember(const Handle& handle) const; | ||
| 348 | |||
| 349 | /** True if an \\object-list field referencing the given names can point to this object. */ | ||
| 350 | bool canBeTarget(const Handle& handle, const std::set<std::string>& referenceListNames) const; | ||
| 351 | |||
| 352 | /** True if the IddObject of type is in iddFile(). */ | ||
| 353 | bool isInIddFile(IddObjectType type) const; | ||
| 354 | |||
| 355 | /** Returns the next name in the pattern 'name << " " << int'. If fillIn, will use unused integer | ||
| 356 | * values smaller than the largest one. */ | ||
| 357 | std::string nextName(const std::string& name, bool fillIn) const; | ||
| 358 | |||
| 359 | /** Returns the next name in the pattern 'iddObject().name() << " " << int' for iddObjectType. | ||
| 360 | * If fillIn, will use unused integer values smaller than the largest one. */ | ||
| 361 | std::string nextName(const IddObjectType& iddObjectType, bool fillIn) const; | ||
| 362 | |||
| 363 | /** Returns true if this Workspace is valid at strictnessLevel(). */ | ||
| 364 | bool isValid() const; | ||
| 365 | |||
| 366 | /** Returns true if this Workspace is valid, that is, has no errors at or below level. */ | ||
| 367 | bool isValid(StrictnessLevel level) const; | ||
| 368 | |||
| 369 | /** Returns a ValidityReport for this Workspace containing all errors at or below | ||
| 370 | * strictnessLevel(). */ | ||
| 371 | virtual ValidityReport validityReport() const; | ||
| 372 | |||
| 373 | /** Returns a ValidityReport for this Workspace containing all errors at or below level. */ | ||
| 374 | virtual ValidityReport validityReport(StrictnessLevel level) const; | ||
| 375 | |||
| 376 | /** Returns an IdfObject based on the Version IddObject appropriate for this Workspace. No | ||
| 377 | * public interface. Used in constructing Workspaces. */ | ||
| 378 | IdfObject versionObjectToAdd() const; | ||
| 379 | |||
| 380 | //@} | ||
| 381 | /** @name Serialization and File Management*/ | ||
| 382 | //@{ | ||
| 383 | |||
| 384 | /** Save Workspace to path. Will construct parent folder, but no further up the chain. Will | ||
| 385 | * only overwrite an existing file if overwrite==true. If no extension is provided will use | ||
| 386 | * .idf or modelFileExtension() depending on the underlying IddFileType. */ | ||
| 387 | virtual bool save(const openstudio::path& p, bool overwrite = false); | ||
| 388 | |||
| 389 | /** Creates an IdfFile from the collection, naming objects if necessary. To print out IDF text, | ||
| 390 | * use this method, then IdfFile.print(ostream). */ | ||
| 391 | IdfFile toIdfFile(); | ||
| 392 | |||
| 393 | /// Locates and updates urls in the workspace | ||
| 394 | //std::vector<std::pair<openstudio::Url, openstudio::path> > locateUrls(const std::vector<URLSearchPath> &t_paths, bool t_create_relative_paths, | ||
| 395 | // const openstudio::path &t_infile, const openstudio::path &t_locationForRemoteUrls = openstudio::path()); | ||
| 396 | |||
| 397 | //@} | ||
| 398 | |||
| 399 | //@} | ||
| 400 | /** @name Nano Signals */ | ||
| 401 | //@{ | ||
| 402 | |||
| 403 | /// range for progress to take | ||
| 404 | // void progressRange(int min, int max) const; | ||
| 405 | mutable Nano::Signal<void(int, int)> progressRange; | ||
| 406 | |||
| 407 | /// report on progress when loading workspace, creating objects, etc | ||
| 408 | // void progressValue(int value) const; | ||
| 409 | mutable Nano::Signal<void(int)> progressValue; | ||
| 410 | |||
| 411 | /// report caption describing what we are currently doing | ||
| 412 | // void progressCaption(const QString& caption) const; | ||
| 413 | mutable Nano::Signal<void(const std::string&)> progressCaption; | ||
| 414 | |||
| 415 | /** Emitted on any change to this Workspace and its contents. */ | ||
| 416 | // void onChange() const; | ||
| 417 | mutable Nano::Signal<void()> onChange; | ||
| 418 | |||
| 419 | /** Send an object being deleted from the workspace. OS_ASSERT(!object.initialized()) | ||
| 420 | * should pass, as should OS_ASSERT(object.handle().isNull()). */ | ||
| 421 | // void removeWorkspaceObject(const WorkspaceObject& object, const openstudio::IddObjectType& iddObjectType, const openstudio::UUID& handle) const; | ||
| 422 | mutable Nano::Signal<void(const WorkspaceObject&, const openstudio::IddObjectType&, const openstudio::UUID&)> removeWorkspaceObject; | ||
| 423 | |||
| 424 | // DLM: deprecate this version | ||
| 425 | // void removeWorkspaceObjectPtr(std::shared_ptr<openstudio::detail::WorkspaceObject_Impl>, const openstudio::IddObjectType& iddObjectType, const openstudio::UUID& handle) const; | ||
| 426 | mutable Nano::Signal<void(std::shared_ptr<openstudio::detail::WorkspaceObject_Impl>, const openstudio::IddObjectType&, const openstudio::UUID&)> | ||
| 427 | removeWorkspaceObjectPtr; | ||
| 428 | |||
| 429 | /** Sends an object just added to the Workspace. */ | ||
| 430 | // void addWorkspaceObject(const WorkspaceObject& object, const openstudio::IddObjectType& iddObjectType, const openstudio::UUID& handle) const; | ||
| 431 | mutable Nano::Signal<void(const WorkspaceObject&, const openstudio::IddObjectType&, const openstudio::UUID&)> addWorkspaceObject; | ||
| 432 | |||
| 433 | // DLM: deprecate this version | ||
| 434 | // void addWorkspaceObjectPtr(std::shared_ptr<openstudio::detail::WorkspaceObject_Impl>, const openstudio::IddObjectType& iddObjectType, const openstudio::UUID& handle) const; | ||
| 435 | mutable Nano::Signal<void(std::shared_ptr<openstudio::detail::WorkspaceObject_Impl>, const openstudio::IddObjectType&, const openstudio::UUID&)> | ||
| 436 | addWorkspaceObjectPtr; | ||
| 437 | //@} | ||
| 438 | |||
| 439 | //public slots: | ||
| 440 | |||
| 441 | void change(); | ||
| 442 | |||
| 443 | protected: | ||
| 444 | // helper for non-virtual part of clone implementation | ||
| 445 | void createAndAddClonedObjects(const std::shared_ptr<Workspace_Impl>& thisImpl, std::shared_ptr<Workspace_Impl> cloneImpl, | ||
| 446 | bool keepHandles) const; | ||
| 447 | |||
| 448 | // helper for non-virtual part of clone subset implementation | ||
| 449 | void createAndAddSubsetClonedObjects(const std::shared_ptr<Workspace_Impl>& thisImpl, std::shared_ptr<Workspace_Impl> cloneImpl, | ||
| 450 | const std::vector<Handle>& handles, bool keepHandles) const; | ||
| 451 | |||
| 452 | private: | ||
| 453 | // DATA | ||
| 454 | |||
| 455 | StrictnessLevel m_strictnessLevel; // level of validity to be maintained by collection | ||
| 456 | std::string m_header; // header for the IdfFile | ||
| 457 | IddFileAndFactoryWrapper m_iddFileAndFactoryWrapper; // IDD file to be used for validity checking | ||
| 458 | bool m_fastNaming; | ||
| 459 | |||
| 460 | using WorkspaceObjectMap = std::unordered_map<Handle, std::shared_ptr<WorkspaceObject_Impl>, boost::hash<boost::uuids::uuid>>; | ||
| 461 | WorkspaceObjectMap m_workspaceObjectMap; | ||
| 462 | |||
| 463 | // object for ordering objects in the collection. | ||
| 464 | WorkspaceObjectOrder m_workspaceObjectOrder; | ||
| 465 | |||
| 466 | // map of IddObjectType to set of objects identified by UUID | ||
| 467 | using IddObjectTypeMap = std::map<IddObjectType, WorkspaceObjectMap>; | ||
| 468 | IddObjectTypeMap m_iddObjectTypeMap; | ||
| 469 | |||
| 470 | // map of reference to set of objects identified by UUID | ||
| 471 | using IdfReferencesMap = std::unordered_map<std::string, WorkspaceObjectMap>; // , IstringCompare | ||
| 472 | IdfReferencesMap m_idfReferencesMap; | ||
| 473 | |||
| 474 | // data object for undos | ||
| 475 | struct SavedWorkspaceObject | ||
| 476 | { | ||
| 477 | Handle handle; | ||
| 478 | std::shared_ptr<WorkspaceObject_Impl> objectImplPtr; | ||
| 479 | OptionalUnsigned orderIndex; | ||
| 480 | 9916 | SavedWorkspaceObject(const Handle& h, const std::shared_ptr<WorkspaceObject_Impl>& o) : handle(h), objectImplPtr(o) {} | |
| 481 | }; | ||
| 482 | using OptionalSavedWorkspaceObject = boost::optional<SavedWorkspaceObject>; | ||
| 483 | using SavedWorkspaceObjectVector = std::vector<SavedWorkspaceObject>; | ||
| 484 | |||
| 485 | // GETTERS | ||
| 486 | |||
| 487 | // Change over from a HandleSet to a std::vector<Handle>. | ||
| 488 | std::vector<Handle> handles(const std::set<Handle>& handles, bool sorted = false) const; | ||
| 489 | |||
| 490 | /** Checks if objectName without any suffix integers matches supplied base name. */ | ||
| 491 | bool baseNamesMatch(const std::string& baseName, const std::string& objectName) const; | ||
| 492 | |||
| 493 | /** Returns optional suffix integer from objectName. */ | ||
| 494 | std::tuple<boost::optional<int>, std::string> getNameSuffix(const std::string& objectName) const; | ||
| 495 | |||
| 496 | /** Returns objectName in with any suffix integers removed. */ | ||
| 497 | std::string getBaseName(const std::string& objectName) const; | ||
| 498 | |||
| 499 | boost::optional<WorkspaceObject> getEquivalentObject(const IdfObject& other) const; | ||
| 500 | |||
| 501 | // SETTERS | ||
| 502 | |||
| 503 | // Replace m_iddFactoryWrapper if workspace remains valid. | ||
| 504 | bool setIddFile(const IddFileAndFactoryWrapper& iddFileAndFileWrapper); | ||
| 505 | |||
| 506 | // Helper function to start the process of adding an object to the workspace. | ||
| 507 | bool nominallyAddObject(std::shared_ptr<WorkspaceObject_Impl>& ptr); | ||
| 508 | |||
| 509 | void insertIntoObjectMap(const Handle& handle, const std::shared_ptr<WorkspaceObject_Impl>& object); | ||
| 510 | |||
| 511 | void insertIntoIddObjectTypeMap(const std::shared_ptr<WorkspaceObject_Impl>& object); | ||
| 512 | |||
| 513 | void insertIntoIdfReferencesMap(const std::shared_ptr<WorkspaceObject_Impl>& object); | ||
| 514 | |||
| 515 | // note default parameter for toIgnore is empty vector | ||
| 516 | bool resolvePotentialNameConflicts(Workspace& other, const std::vector<unsigned>& toIgnore); | ||
| 517 | |||
| 518 | void mergeIdfObjectAfterPotentialNameConflictResolution(IdfObject& mergedObject, const IdfObject& originalObject) const; | ||
| 519 | |||
| 520 | // Adds provided relationships for newly added objects. handles is conversion from unsigned index | ||
| 521 | // to (newly created) handle in workspace. | ||
| 522 | bool addProvidedRelationships(const std::vector<Handle>& handles, const std::vector<UHPointer>& pointersIntoWorkspace, | ||
| 523 | const std::vector<HUPointer>& pointersFromWorkspace); | ||
| 524 | |||
| 525 | std::vector<WorkspaceObject> simpleAddObjects(const std::vector<IdfObject>& objects); | ||
| 526 | |||
| 527 | std::vector<WorkspaceObject> addAndInsertObjects(const std::vector<WorkspaceObject>& allObjects, const std::vector<unsigned>& foundObjectIndices, | ||
| 528 | const std::vector<unsigned>& notFoundObjectIndices, | ||
| 529 | std::vector<WorkspaceObject>& equivalentObjects); | ||
| 530 | |||
| 531 | OptionalSavedWorkspaceObject savedWorkspaceObject(const Handle& handle); | ||
| 532 | |||
| 533 | std::vector<WorkspaceObject> nominallyRemoveObject(const Handle& handle); | ||
| 534 | |||
| 535 | std::vector<std::vector<WorkspaceObject>> nominallyRemoveObjects(const std::vector<Handle>& handles); | ||
| 536 | |||
| 537 | void restoreObject(SavedWorkspaceObject& savedObject); | ||
| 538 | |||
| 539 | void restoreObjects(SavedWorkspaceObjectVector& savedObjects); | ||
| 540 | |||
| 541 | void registerRemovalOfObject(std::shared_ptr<WorkspaceObject_Impl> ptr, const std::vector<WorkspaceObject>& sources, | ||
| 542 | const std::vector<Handle>& removedHandles); | ||
| 543 | |||
| 544 | void registerRemovalOfObjects(std::vector<SavedWorkspaceObject>& savedObjects, const std::vector<std::vector<WorkspaceObject>>& sources, | ||
| 545 | const std::vector<Handle>& removedHandles); | ||
| 546 | |||
| 547 | void registerAdditionOfObject(const WorkspaceObject& object); | ||
| 548 | |||
| 549 | // QUERIES | ||
| 550 | |||
| 551 | /** Returns name with the next available integer suffix. */ | ||
| 552 | std::string constructNextName(const std::string& objectName, const std::vector<WorkspaceObject>& objectsInTheSeries, bool fillIn) const; | ||
| 553 | |||
| 554 | std::vector<std::vector<WorkspaceObject>> nameConflicts(const std::vector<WorkspaceObject>& candidates) const; | ||
| 555 | |||
| 556 | bool potentialNameConflict(const std::string& currentName, const IddObject& iddObject) const; | ||
| 557 | |||
| 558 | // configure logging | ||
| 559 | − | REGISTER_LOGGER("utilities.idf.Workspace"); | |
| 560 | }; | ||
| 561 | |||
| 562 | using Workspace_ImplPtr = std::shared_ptr<Workspace_Impl>; | ||
| 563 | |||
| 564 | } // namespace detail | ||
| 565 | |||
| 566 | } // namespace openstudio | ||
| 567 | |||
| 568 | #endif // UTILITIES_IDF_WORKSPACE_IMPL_HPP | ||
| 569 |