OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
ModelState.h
Go to the documentation of this file.
1#pragma once
2#pragma warning(disable : 4251)
3
4#include <map>
5#include <string>
6#include <list>
9
10#include <mongocxx/cursor.hpp>
11#include <bsoncxx/document/view.hpp>
12#include <bsoncxx/types.hpp>
13#include <bsoncxx/builder/basic/array.hpp>
14
15class __declspec(dllexport) ModelStateEntity
16{
17public:
18 typedef unsigned long long EntityID;
19 typedef long long EntityVersion;
20
21 enum tEntityType { TOPOLOGY, DATA };
22
23 ModelStateEntity() : m_entityVersion(0), m_parentEntityID(0), m_entityType(tEntityType::DATA) {};
24 ~ModelStateEntity() {};
25
26 void setParentEntityID(EntityID id) { m_parentEntityID = id; };
27 void setVersion(EntityVersion version) { m_entityVersion = version; };
28 void setEntityType(tEntityType type) { m_entityType = type; };
29
30 EntityID getParentEntityID(void) { return m_parentEntityID; };
31 EntityVersion getEntityVersion(void) { return m_entityVersion; };
32 tEntityType getEntityType(void) { return m_entityType; };
33
34private:
35 EntityVersion m_entityVersion;
36 EntityID m_parentEntityID;
37 tEntityType m_entityType;
38};
39
40class __declspec(dllexport) ModelState
41{
42public:
43 ModelState() = delete;
44 ModelState(unsigned int sessionID, unsigned int serviceID);
45 ~ModelState();
46
47 // ###########################################################################################################################################################################################################################################################################################################################
48
49 // Gerneral
50
51 // Reset the model state to an empty project
52 void reset(void);
53
54 // Check whether the model state has been modified since the last save
55 bool isModified(void) const { return m_stateModified; };
56
57 // Open a project, load the version grap and the currently active version
58 bool openProject(void);
59
60 // ###########################################################################################################################################################################################################################################################################################################################
61
62 // Entity handling
63
64 // Create and return a new entity ID (this will increate the maximum entity ID and mark the model state as modified
65 unsigned long long createEntityUID(void);
66
67 // Load a model state with a particular version
68 bool loadModelState(const std::string& _version);
69
70 // Store an entity to the data base (it will be automatically determine whetehr the entity is new or modified)
71 void storeEntity(ModelStateEntity::EntityID entityID, ModelStateEntity::EntityID parentEntityID, ModelStateEntity::EntityVersion entityVersion, ModelStateEntity::tEntityType entityType);
72
73 // Add new entity to model state
74 void addNewEntity(ModelStateEntity::EntityID entityID, ModelStateEntity::EntityID parentEntityID, ModelStateEntity::EntityVersion entityVersion, ModelStateEntity::tEntityType entityType);
75
76 // Mark entity as modified (new version and parent)
77 void modifyEntity(ModelStateEntity::EntityID entityID, ModelStateEntity::EntityID parentEntityID, ModelStateEntity::EntityVersion entityVersion, ModelStateEntity::tEntityType entityType);
78
79 // Mark entity as modified (new version)
80 void modifyEntityVersion(ModelStateEntity::EntityID entityID, ModelStateEntity::EntityVersion entityVersion);
81
82 // Mark entity as modified (new parent)
83 void modifyEntityParent(ModelStateEntity::EntityID entityID, ModelStateEntity::EntityID parentEntityID);
84
85 // Remove entity from model state
86 void removeEntity(ModelStateEntity::EntityID entityID, bool considerChildren = true);
87
88 // Determine the current modelStateVersion (the last saved one)
89 const std::string& getModelStateVersion(void) const { return m_graphCfg.getActiveVersionName(); };
90
91 // Determine the currently active branch
92 const std::string& getActiveBranch(void) const { return m_graphCfg.getActiveBranchName(); }
93
94 // Save the current modified model state. The version counter is incremented automatically in the last digit (e.g. 1.2.1 -> 1.2.2)
95 bool saveModelState(bool forceSave, bool forceAbsoluteState, const std::string &saveComment);
96
97 // Determine the current version of an entity
98 ModelStateEntity::EntityVersion getCurrentEntityVersion(ModelStateEntity::EntityID entityID);
99
100 // Determine the parent of an entity
101 ModelStateEntity::EntityID getCurrentEntityParent(ModelStateEntity::EntityID entityID);
102
103 // Get a list of all topology entities in the model
104 void getListOfTopologyEntites(std::list<unsigned long long> &topologyEntities);
105
106 // Deactivate the latest model state and reload an earlier state. Returns true if the state could be reverted (needs to have at least one more model state)
107 bool undoLastOperation(void);
108
109 // Re-activate the next inactive state and reload the model state
110 bool redoNextOperation(void);
111
112 // Get the description which was stored for the current model state
113 std::string getCurrentModelStateDescription(void);
114
115 // Get the description which was stored for the next redo model state
116 std::string getRedoModelStateDescription(void);
117
118 // Check whether an undo operation is possible
119 bool canUndo(void);
120
121 // Check whether a redo operation is possible
122 bool canRedo(void);
123
124 // Delete all model entities which were written after the last model state and therefore are not referred to in the data base
125 void removeDanglingModelEntities(void);
126
127 // Load the version graph of the model (needs to be called when the project is opened)
128 void loadVersionGraph(void);
129
130 // Get a list of all model states (version, description);
131 ot::VersionGraphCfg& getVersionGraph(void);
132
133 // Get a list of all model states (version, description);
134 const ot::VersionGraphCfg& getVersionGraph(void) const;
135
136 // Check the database schema version and upgrade, if needed
137 void checkAndUpgradeDataBaseSchema(void);
138
139 // Remove all redo model states and the items belonging to them
140 std::list<std::string> removeRedoModelStates(void);
141
142 // Write the version information to the entity in the data base
143 void updateVersionEntity(const std::string& _version);
144
145 // ###########################################################################################################################################################################################################################################################################################################################
146
147 // Private: Helper
148
149private:
150 // This function loads the model state from a given ModelState document
151 bool loadModelFromDocument(bsoncxx::document::view docView);
152
153 // This function loads an absolute model state from a given ModelState document
154 bool loadAbsoluteState(bsoncxx::document::view docView);
155
156 // Clear the information map containing the list of children for each item
157 void clearChildrenInformation(void);
158
159 // Build the information map containing the list of children for each item
160 void buildChildrenInformation(void);
161
162 // Add an entity to the child list of its parent entity
163 void addEntityToParent(ModelStateEntity::EntityID entityID, ModelStateEntity::EntityID parentID);
164
165 // Remove an entity from the child list of its former parent entity
166 void removeEntityFromParent(ModelStateEntity::EntityID entityID, ModelStateEntity::EntityID parentID);
167
168 // This function loads an incremental (relative) model state from a given ModelState document
169 bool loadIncrementalState(bsoncxx::document::view docView);
170
171 // This function loads a state (as modification of the current state) of the expected type
172 bool loadState(bsoncxx::document::view docView, const std::string &expectedType);
173
174 // This function clears the entire model state information
175 void clearModelState(void);
176
177 // Load the entity data from a state document
178 void loadStateData(bsoncxx::document::view docView);
179
180 // Save the current state as absolute model state
181 bool saveAbsoluteState(const std::string &saveComment);
182
183 // Save the current state as absolute model state with extensions
184 bool saveAbsoluteStateWithExtension(const std::string &saveComment);
185
186 // Save the current state as relative model state
187 bool saveIncrementalState(const std::string &saveComment);
188
189 // Write the main document of an absolute state with extensions
190 bool writeMainDocument(std::map<ModelStateEntity::EntityID, ModelStateEntity> &entitiesLeft, const std::string &saveComment);
191
192 // Write an extension document of an absolute state
193 bool writeExtensionDocument(std::map<ModelStateEntity::EntityID, ModelStateEntity> &entitiesLeft);
194
195 // Helper function to determine non-modelstate entries in a list of results
196 bool getListOfNonModelStateEntities(mongocxx::cursor &cursor, bsoncxx::builder::basic::array &entityArray);
197
198 // Determine whether a given version is part of the currently active branch
199 bool isVersionInActiveBranch(const std::string &version);
200
201 // Determine whether a given version is part of the given branch
202 bool isVersionInBranch(const std::string &version, const std::string &branch);
203
204 // Get the parent branch of the given branch (remove the part after the last .)
205 std::string getParentBranch(const std::string &branch);
206
207 // Activate the branch which contains the given version
208 void activateBranch(const std::string& _version);
209
211 bool hasNextVersion(const std::string& _version);
212
213 // Get the next version in the active branch following the given one
214 std::string getNextVersion(const std::string &version);
215
216 // Get the previous version in the active branch before the given one
217 std::string getPreviousVersion(const std::string &version);
218
219 // Get the description for the given version
220 std::string getVersionDescription(const std::string &version);
221
222 // Remove an item from the version graph
223 void removeVersionGraphItem(const std::string &version);
224
225 // Helper function to determine the version of the last model entity in the data base
226 long long getCurrentModelEntityVersion(void);
227
228 // Determine the last version in the currently active branch
229 std::string getLastVersionInActiveBranch(void);
230
231 // Write the current active branch and model version to the model entity
232 void storeCurrentVersionInModelEntity(void);
233
234 // Delete the given model version (model state) together with all its newly created entities
235 void deleteModelVersion(const std::string &version);
236
237 // Determine all versions which are following the given version (regardless of the branch)
238 void getAllChildVersions(const std::string& _version, std::list<std::string>& _childVersions);
239 void getAllChildVersions(const ot::VersionGraphVersionCfg* _version, std::list<std::string>& _childVersions);
240
241 // Create a new branch and activate it (also update the model entity)
242 void createAndActivateNewBranch(void);
243
244 // Count the number of dots in a version string
245 int countNumberOfDots(const std::string &text);
246
247 // Helper to perform schema upgrade from version 1 to version 2
248 void updateSchema_1_2(void);
249
250 // When we load a relative state, the attribute will hold the version of the last absolute state (base state)
251 std::string m_currentModelBaseStateVersion;
252
253 // Information regarding the entities which are currently part of the model
254 std::map<ModelStateEntity::EntityID, ModelStateEntity> m_entities;
255
256 // Information about the added / modified / removed entities with regard to the last saved model state
257 std::map<ModelStateEntity::EntityID, ModelStateEntity> m_addedOrModifiedEntities;
258 std::map<ModelStateEntity::EntityID, ModelStateEntity> m_removedEntities;
259
260 // Information about entity children
261 std::map<ModelStateEntity::EntityID, std::list<ModelStateEntity::EntityID>> m_entityChildrenList;
262
263 // A flag which indicates whether the model state has been modified compared to the last stored state.
264 bool m_stateModified;
265
266 // The maximum number of array entities per state
267 const size_t m_maxNumberArrayEntitiesPerState;
268
269 // The active branch which is currently stored in the model entity
270 std::string m_activeBranchInModelEntity;
271
272 // The active version which is currently stored in the model entity
273 std::string m_activeVersionInModelEntity;
274
275 // The member for creation of Unique IDs
276 DataStorageAPI::UniqueUIDGenerator* m_uniqueUIDGenerator;
277
278 // The version graph (for each version: version, parentVersion, description).
279 // The version graph needs to be loaded when the project is opened and will then be kept up to date
280 ot::VersionGraphCfg m_graphCfg;
281};
Definition UniqueUIDGenerator.h:8
Definition VersionGraphCfg.h:16
Definition VersionGraphVersionCfg.h:24