Antares Simulator
Power System Simulator
updater.hxx
1 /*
2 ** Copyright 2007-2025, RTE (https://www.rte-france.com)
3 ** See AUTHORS.txt
4 ** SPDX-License-Identifier: MPL-2.0
5 ** This file is part of Antares-Simulator,
6 ** Adequacy and Performance assessment for interconnected energy networks.
7 **
8 ** Antares_Simulator is free software: you can redistribute it and/or modify
9 ** it under the terms of the Mozilla Public Licence 2.0 as published by
10 ** the Mozilla Foundation, either version 2 of the License, or
11 ** (at your option) any later version.
12 **
13 ** Antares_Simulator is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** Mozilla Public Licence 2.0 for more details.
17 **
18 ** You should have received a copy of the Mozilla Public Licence 2.0
19 ** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20 */
21 #ifndef __ANTARES_LIBS_STUDY_SCENARIO_BUILDER_UPDATER_HXX__
22 #define __ANTARES_LIBS_STUDY_SCENARIO_BUILDER_UPDATER_HXX__
23 
24 #include <yuni/yuni.h>
25 
26 #include "antares/study/study.h"
27 
28 #include "sets.h"
29 
30 #define SEP IO::Separator
31 
32 namespace Antares
33 {
34 namespace // anonymous
35 {
36 class ScenarioBuilderUpdater final
37 {
38 public:
39  ScenarioBuilderUpdater(Data::Study& study):
40  pStudy(study)
41  {
42  using namespace Yuni;
43  // We can store the INI files in disk because it may not fit in memory
44 
45  if (study.scenarioRules)
46  {
47  study.scenarioRules->inUpdaterMode = true;
48  logs.debug() << "[scenario-builder] updater mode ON";
49 
50  logs.debug()
51  << "[scenario-builder] writing data to a temporary file before structure changes";
52  pTempFile << memory.cacheFolder() << SEP << "antares-scenbld-save-"
53  << memory.processID() << '-' << (size_t)(this) << "-scenariobuilder.tmp";
54  // Dump the memory
55 #ifdef BUILD_UI
56  study.scenarioRules->saveToINIFile(pTempFile);
57 #endif
58  study.scenarioRules->clear();
59  }
60  }
61 
62  ~ScenarioBuilderUpdater()
63  {
64  using namespace Yuni;
65 
66  if (not pTempFile.empty())
67  {
68  if (pStudy.scenarioRules)
69  {
70  logs.debug() << "[scenario-builder] reloading data from a temporary file";
71  pStudy.scenarioRules->loadFromINIFile(pTempFile);
72 
73  pStudy.scenarioRules->inUpdaterMode = false;
74  logs.debug() << "[scenario-builder] updater mode OFF";
75  }
76  // Removing the temporary file
77  IO::File::Delete(pTempFile);
78  }
79  }
80 
81 private:
82  Data::Study& pStudy;
83  Yuni::String pTempFile;
84 
85 }; // class ScenarioBuilderUpdater
86 
87 } // anonymous namespace
88 
89 } // namespace Antares
90 
91 #endif // __ANTARES_LIBS_STUDY_SCENARIO_BUILDER_UPDATER_HXX__
Definition: study.h:57
std::unique_ptr< ScenarioBuilder::Sets > scenarioRules
Rules for building scenarios (can be null)
Definition: study.h:555
const Yuni::String & cacheFolder() const
\nane Cache Folder
Definition: memory.cpp:137
uint64_t processID() const
Get the process ID of the application.
Definition: memory.hxx:26