NRP Core  1.4.1
function_manager.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef FUNCTION_MANAGER_H
23 #define FUNCTION_MANAGER_H
24 
28 
29 #include <vector>
30 #include <map>
31 #include <memory>
32 #include <list>
33 #include <boost/python.hpp>
34 
39 {
43  std::string Name;
44 
49 
54 
55  FunctionData() = default;
56  FunctionData(const std::string &name,
58  const datapack_identifiers_set_t &datapackIDs);
59 };
60 
61 
74 {
75  public:
76 
77  using function_datas_t = std::multimap<std::string, FunctionData>;
78  using linked_functions_t = std::pair<FunctionManager::function_datas_t::iterator, FunctionManager::function_datas_t::iterator>;
79  using status_function_results_t = std::tuple<bool, datapacks_vector_t>;
80 
82  FunctionManager(const boost::python::dict &tfGlobals);
83 
89 
91 
93 
98  void setSimulationTime(SimulationTime simulationTime);
99 
103  const SimulationTime & getSimulationTime() const;
104 
109  void setSimulationIteration(unsigned long simulationIteration);
110 
114  unsigned long getSimulationIteration() const;
115 
127  void loadDataPackFunction(const std::string & dataPackFunctionName, const std::string & dataPackFunctionFilename);
128 
138  void loadStatusFunction(const std::string & statusFunctionName, const std::string & statusFunctionFilename);
139 
145  datapacks_vector_t executePreprocessingFunctions(const std::string &engineName, datapacks_set_t dataPacks);
146 
152  datapacks_vector_t executeTransceiverFunctions(const std::string &engineName, datapacks_set_t dataPacks);
153 
161 
162  private:
169  // TODO Why does this return a pointer to pointer?
170  TransceiverDataPackInterfaceSharedPtr *registerNewDataPackFunction(const std::string &linkedEngine, const TransceiverDataPackInterfaceSharedPtr &transceiverFunction);
171 
172  SimulationTime _simulationTime = SimulationTime::zero();
173  unsigned long _simulationIteration = 0;
174  DataPackPassingPolicy _DataPackPassingPolicy;
175 
181  // TODO Why does this return a pointer to pointer?
182  TransceiverDataPackInterfaceSharedPtr *registerNewStatusFunction(const TransceiverDataPackInterfaceSharedPtr &statusFunction);
183 
187  boost::python::dict _globalDict;
188 
192  function_datas_t _dataPackFunctions;
193 
197  std::unique_ptr<FunctionData> _statusFunction = nullptr;
198 
209  function_datas_t::iterator _newDataPackFunctionIt = this->_dataPackFunctions.end();
210 
216  function_datas_t::const_iterator findDataPackFunction(const std::string &name) const;
217 
223  std::vector<std::shared_ptr<DataPackInterface>> runDataPackFunction(const std::string &tfName, datapacks_set_t dataPacks);
224 
230  linked_functions_t getDataPackFunctions(const std::string &engineName);
231 
239  datapacks_vector_t executeDataPackFunctions(const std::string &engineName,
240  datapacks_set_t dataPacks,
241  const bool preprocessing);
242 
243  // Give the function classes access to private methods,
244  // so that they can register themselves in the manager
245 
246  friend class TransceiverFunction;
247  friend class PreprocessingFunction;
248  friend class StatusFunction;
249 };
250 
251 using FunctionManagerSharedPtr = std::shared_ptr<FunctionManager>;
252 using FunctionManagerConstSharedPtr = std::shared_ptr<const FunctionManager>;
253 
254 #endif // FUNCTION_MANAGER_H
255 
256 // EOF
datapacks_vector_t
std::vector< std::shared_ptr< const DataPackInterface > > datapacks_vector_t
Definition: datapack_interface.h:220
datapacks_set_t
std::set< std::shared_ptr< const DataPackInterface >, DataPackPointerComparator > datapacks_set_t
Definition: datapack_interface.h:219
FunctionManager::executeStatusFunction
status_function_results_t executeStatusFunction(datapacks_set_t dataPacks)
Executes Status Function registered loaded by the manager.
Definition: function_manager.cpp:144
FunctionData::Function
TransceiverDataPackInterface::shared_ptr Function
Pointer to the Function.
Definition: function_manager.h:48
FunctionManager::FunctionManager
FunctionManager()
Definition: function_manager.cpp:39
TransceiverFunction
Holds a single transfer function decorator.
Definition: transceiver_function.h:36
DataPackPassingPolicy
DataPackPassingPolicy
Definition: from_engine_datapack.h:28
from_engine_datapack.h
engine_client_interface.h
FunctionManager::setDataPackPassingPolicy
void setDataPackPassingPolicy(DataPackPassingPolicy method)
Definition: function_manager.cpp:411
FunctionManager::status_function_results_t
std::tuple< bool, datapacks_vector_t > status_function_results_t
Definition: function_manager.h:79
FunctionManager::executeTransceiverFunctions
datapacks_vector_t executeTransceiverFunctions(const std::string &engineName, datapacks_set_t dataPacks)
Executes all Transceiver Functions linked to an engine.
Definition: function_manager.cpp:381
FunctionData::FunctionData
FunctionData()=default
datapack_identifiers_set_t
std::set< DataPackIdentifier > datapack_identifiers_set_t
Definition: datapack_interface.h:221
FunctionManager::getDataPackPassingPolicy
DataPackPassingPolicy getDataPackPassingPolicy() const
Definition: function_manager.cpp:406
FunctionManager::executePreprocessingFunctions
datapacks_vector_t executePreprocessingFunctions(const std::string &engineName, datapacks_set_t dataPacks)
Executes all Preprocessing Functions linked to an engine.
Definition: function_manager.cpp:376
PreprocessingFunction
Dummy alias class for TransceiverFunction, mapped to PreprocessingFunction python decorator.
Definition: python_module.cpp:68
FunctionManagerConstSharedPtr
std::shared_ptr< const FunctionManager > FunctionManagerConstSharedPtr
Definition: function_manager.h:252
FunctionData::Name
std::string Name
Name of the Function.
Definition: function_manager.h:43
FunctionManager::linked_functions_t
std::pair< FunctionManager::function_datas_t::iterator, FunctionManager::function_datas_t::iterator > linked_functions_t
Definition: function_manager.h:78
FunctionManagerSharedPtr
std::shared_ptr< FunctionManager > FunctionManagerSharedPtr
Definition: function_manager.h:251
FunctionManager::loadDataPackFunction
void loadDataPackFunction(const std::string &dataPackFunctionName, const std::string &dataPackFunctionFilename)
Loads given DataPack Processing Function.
Definition: function_manager.cpp:271
FunctionManager::getSimulationTime
const SimulationTime & getSimulationTime() const
Returns simulation time stored by the manager.
Definition: function_manager.cpp:391
PtrTemplates< TransceiverDataPackInterface >::shared_ptr
std::shared_ptr< TransceiverDataPackInterface > shared_ptr
Definition: ptr_templates.h:31
FunctionManager::loadStatusFunction
void loadStatusFunction(const std::string &statusFunctionName, const std::string &statusFunctionFilename)
Loads given status funtion.
Definition: function_manager.cpp:226
TransceiverDataPackInterfaceSharedPtr
TransceiverDataPackInterface::shared_ptr TransceiverDataPackInterfaceSharedPtr
Definition: transceiver_datapack_interface.h:134
FunctionManager::function_datas_t
std::multimap< std::string, FunctionData > function_datas_t
Definition: function_manager.h:77
FunctionData
Data associated with a single function.
Definition: function_manager.h:38
FunctionManager::setSimulationTime
void setSimulationTime(SimulationTime simulationTime)
Sets simulation time that will be accessible by all Python Functions.
Definition: function_manager.cpp:386
FunctionManager::setSimulationIteration
void setSimulationIteration(unsigned long simulationIteration)
Sets simulation iteration that will be accessible by all Python Functions.
Definition: function_manager.cpp:396
StatusFunction
Holds a single Python function created with StatusFunction decorator.
Definition: status_function.h:36
transceiver_datapack_interface.h
FunctionManager::getSimulationIteration
unsigned long getSimulationIteration() const
Returns simulation iteration number stored by the manager.
Definition: function_manager.cpp:401
FunctionManager::getRequestedDataPackIDs
datapack_identifiers_set_t getRequestedDataPackIDs() const
Get DataPack IDs requested by TFs.
Definition: function_manager.cpp:61
FunctionData::DataPackIDs
datapack_identifiers_set_t DataPackIDs
DataPacks requested by the Function.
Definition: function_manager.h:53
SimulationTime
std::chrono::nanoseconds SimulationTime
Definition: time_utils.h:31
FunctionManager
Manages all types of Python functions that can be run as part of NRP Core.
Definition: function_manager.h:73