ChimeraTK-ApplicationCore  04.01.00
Application.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 #pragma once
4 
6 #include "ConnectionMaker.h"
7 #include "Flags.h"
8 #include "InternalModule.h"
9 #include "Logger.h"
10 #include "Model.h"
11 #include "ModuleGroup.h"
12 #include "TestableMode.h"
13 
14 #ifdef CHIMERATK_APPLICATION_CORE_WITH_PYTHON
15 # include "PythonModuleManager.h"
16 #endif
17 
18 #include <ChimeraTK/ControlSystemAdapter/ApplicationBase.h>
19 #include <ChimeraTK/DeviceBackend.h>
20 
21 #include <atomic>
22 #include <mutex>
23 
24 namespace ChimeraTK {
25 
26  /*********************************************************************************************************************/
27 
28  class Module;
29  class AccessorBase;
30  class VariableNetwork;
31  class TestFacility;
32  class DeviceManager;
33  class ApplicationModule;
34 
35  template<typename UserType>
36  class Accessor;
37  template<typename UserType>
38  class FanOut;
39  template<typename UserType>
41 
42  namespace detail {
43  struct TestableMode;
44  } // namespace detail
45 
46  /*********************************************************************************************************************/
47 
48  class Application : public ApplicationBase, public ModuleGroup {
49  public:
54  explicit Application(const std::string& name);
55 
56  ~Application() override;
57 
58  using ApplicationBase::getName;
59 
67  void shutdown() override;
68 
69  void initialise() override;
70 
71  void optimiseUnmappedVariables(const std::set<std::string>& names) override;
72 
73  void run() override;
74 
79 
83  void generateXML();
84 
88  void generateDOT();
89 
94  static Application& getInstance();
95 
99  static bool hasInstance() { return instance != nullptr; }
100 
112  void enableTestableMode();
113 
118 
123  static void registerThread(const std::string& name);
124 
129 
130  ModuleType getModuleType() const override { return ModuleType::ModuleGroup; }
131 
132  std::string getQualifiedName() const override { return "/" + _name; }
133 
134  std::string getFullDescription() const override { return ""; }
135 
140  _debugMode_variableList.insert(node.getUniqueId());
141  }
142 
149 
154  static void incrementDataLossCounter(const std::string& name);
155 
159  static size_t getAndResetDataLossCounter();
160 
164  boost::shared_ptr<DeviceManager> getDeviceManager(const std::string& aliasOrCDD);
165 
170 
175  VersionNumber getStartVersion() const { return _startVersion; }
178 
179  size_t getCircularNetworkInvalidityCounter(size_t circularNetworkHash) const {
180  return _circularNetworkInvalidityCounters.at(circularNetworkHash);
181  }
182 
183 #ifdef CHIMERATK_APPLICATION_CORE_WITH_PYTHON
184  PythonModuleManager& getPythonModuleManager() {
185  return _pythonModuleManager;
186  }
187 #endif
188 
190  return *_defaultConfigReader;
191  }
192 
193  protected:
194  friend class Module;
195  friend class VariableNetwork;
196  friend class VariableNetworkNode;
199  friend class XMLGeneratorVisitor;
200  friend struct StatusAggregator;
201  friend struct detail::TestableMode;
202  friend class NetworkVisitor;
203  friend class ConnectionMaker;
204 
205  template<typename UserType>
206  friend class Accessor;
207 
208  template<typename UserType>
210 
213 
216 
218  std::list<boost::shared_ptr<InternalModule>> _internalModuleList;
219 
221  std::map<std::string, boost::shared_ptr<DeviceManager>> _deviceManagerMap;
222 
226 
228  bool _initialiseCalled{false};
229 
231  bool _runCalled{false};
232 
235 
238  std::map<size_t, size_t> _pvIdMap;
239 
242 
246  std::unordered_set<const void*> _debugMode_variableList;
247 
249  std::atomic<size_t> _dataLossCounter{0};
250 
252  bool _debugDataLoss{false};
253 
255  std::atomic<LifeCycleState> _lifeCycleState{LifeCycleState::initialisation};
256 
258  VersionNumber _startVersion;
259 
261 
266  std::map<size_t, std::atomic<uint64_t>> _circularNetworkInvalidityCounters;
267 
270  std::map<size_t, std::list<EntityOwner*>> _circularDependencyNetworks;
271 
276  std::shared_ptr<Logger> _logger{Logger::getSharedPtr()};
277 
281 #ifdef CHIMERATK_APPLICATION_CORE_WITH_PYTHON
282  PythonModuleManager _pythonModuleManager;
283 #endif
284 
286  std::shared_ptr<ConfigReader> _configReader;
288 
289  friend class TestFacility; // needs access to testableMode variables
290 
291  template<typename UserType>
292  friend class DebugPrintAccessorDecorator; // needs access to the idMap
293  template<typename UserType>
294  friend class MetaDataPropagatingRegisterDecorator; // needs to access circularNetworkInvalidityCounters
295  friend class ApplicationModule; // needs to access circularNetworkInvalidityCounters
297  friend struct ConfigReader; // needs access to _configReader to replace it eventually
298 
299  VersionNumber getCurrentVersionNumber() const override {
300  throw ChimeraTK::logic_error("getCurrentVersionNumber() called on the application. This is probably "
301  "caused by incorrect ownership of variables/accessors or VariableGroups.");
302  }
303  void setCurrentVersionNumber(VersionNumber) override {
304  throw ChimeraTK::logic_error("setCurrentVersionNumber() called on the application. This is probably "
305  "caused by incorrect ownership of variables/accessors or VariableGroups.");
306  }
307  DataValidity getDataValidity() const override {
308  throw ChimeraTK::logic_error("getDataValidity() called on the application. This is probably "
309  "caused by incorrect ownership of variables/accessors or VariableGroups.");
310  }
311  void incrementDataFaultCounter() override {
312  throw ChimeraTK::logic_error("incrementDataFaultCounter() called on the application. This is probably "
313  "caused by incorrect ownership of variables/accessors or VariableGroups.");
314  }
315  void decrementDataFaultCounter() override {
316  throw ChimeraTK::logic_error("decrementDataFaultCounter() called on the application. This is probably "
317  "caused by incorrect ownership of variables/accessors or VariableGroups.");
318  }
319  std::list<EntityOwner*> getInputModulesRecursively([[maybe_unused]] std::list<EntityOwner*> startList) override {
320  throw ChimeraTK::logic_error("getInputModulesRecursively() called on the application. This is probably "
321  "caused by incorrect ownership of variables/accessors or VariableGroups.");
322  }
323  size_t getCircularNetworkHash() const override {
324  throw ChimeraTK::logic_error("getCircularNetworkHash() called on the application. This is probably "
325  "caused by incorrect ownership of variables/accessors or VariableGroups.");
326  }
327  };
328 
329 } /* namespace ChimeraTK */
ChimeraTK::Application::enableTestableMode
void enableTestableMode()
Enable the testable mode.
Definition: Application.cc:74
ChimeraTK::VariableNetworkNode
Class describing a node of a variable network.
Definition: VariableNetworkNode.h:37
ChimeraTK::Application::_deviceManagerMap
std::map< std::string, boost::shared_ptr< DeviceManager > > _deviceManagerMap
Map of DeviceManagers.
Definition: Application.h:221
ChimeraTK::Application::_defaultConfigReader
ConfigReader * _defaultConfigReader
Definition: Application.h:287
ChimeraTK::Accessor
Definition: Application.h:36
ChimeraTK::Application::registerThread
static void registerThread(const std::string &name)
Register the thread in the application system and give it a name.
Definition: Application.cc:81
ChimeraTK::ConfigReader
Generic module to read an XML config file and provide the defined values as constant variables.
Definition: ConfigReader.h:113
Flags.h
ChimeraTK::Application::optimiseUnmappedVariables
void optimiseUnmappedVariables(const std::set< std::string > &names) override
Definition: Application.cc:123
ChimeraTK::PythonModuleManager
This class loads and unloads the Python modules as specified in the ConfigReader XML file,...
Definition: PythonModuleManager.h:22
ChimeraTK::Application::_initialiseCalled
bool _initialiseCalled
Flag whether initialise() has been called already, to make sure it doesn't get called twice.
Definition: Application.h:228
ChimeraTK::StatusAggregator
The StatusAggregator collects results of multiple StatusMonitor instances and aggregates them into a ...
Definition: StatusAggregator.h:32
InternalModule.h
ChimeraTK::Application::shutdown
void shutdown() override
This will remove the global pointer to the instance and allows creating another instance afterwards.
Definition: Application.cc:207
ChimeraTK::Application::setCurrentVersionNumber
void setCurrentVersionNumber(VersionNumber) override
Set the current version number.
Definition: Application.h:303
ChimeraTK::Application::_dataLossCounter
std::atomic< size_t > _dataLossCounter
Counter for how many write() operations have overwritten unread data.
Definition: Application.h:249
ChimeraTK::Application::_debugDataLoss
bool _debugDataLoss
Flag whether to debug data loss (as counted with the data loss counter).
Definition: Application.h:252
ChimeraTK::Application::_startVersion
VersionNumber _startVersion
Version number used at application start, e.g.
Definition: Application.h:258
ChimeraTK::Application::_lifeCycleState
std::atomic< LifeCycleState > _lifeCycleState
Life-cycle state of the application.
Definition: Application.h:255
ChimeraTK::Application::_model
Model::RootProxy _model
The model of the application.
Definition: Application.h:212
ChimeraTK::Application::debugMakeConnections
void debugMakeConnections()
Enable debug output for the ConnectionMaker.
Definition: Application.h:128
PythonModuleManager.h
ChimeraTK::ModuleGroup
Definition: ModuleGroup.h:16
ChimeraTK::Application::getModel
Model::RootProxy getModel()
Return the root of the application model.
Definition: Application.h:78
ChimeraTK::Application::TestableMode
friend struct detail::TestableMode
Definition: Application.h:201
ChimeraTK::Application::incrementDataLossCounter
static void incrementDataLossCounter(const std::string &name)
Increment counter for how many write() operations have overwritten unread data.
Definition: Application.cc:87
ChimeraTK::Application::generateXML
void generateXML()
Instead of running the application, just initialise it and output the published variables to an XML f...
Definition: Application.cc:244
ChimeraTK::Application::_logger
std::shared_ptr< Logger > _logger
Shared Pointer to central logger instance.
Definition: Application.h:276
ChimeraTK::Application::getQualifiedName
std::string getQualifiedName() const override
Get the fully qualified name of the module instance, i.e.
Definition: Application.h:132
ChimeraTK::Application::getInputModulesRecursively
std::list< EntityOwner * > getInputModulesRecursively([[maybe_unused]] std::list< EntityOwner * > startList) override
Definition: Application.h:319
ChimeraTK::Application::getVersionInfoProvider
ApplicationModule * getVersionInfoProvider()
Definition: Application.h:176
ChimeraTK::Application::_internalModuleList
std::list< boost::shared_ptr< InternalModule > > _internalModuleList
List of InternalModules.
Definition: Application.h:218
ChimeraTK::Application::XMLGeneratorVisitor
friend class XMLGeneratorVisitor
Definition: Application.h:199
detail
Definition: XMLGeneratorVisitor.cc:16
ChimeraTK::ConsumingFanOut
FanOut implementation which acts as a read-only (i.e.
Definition: Application.h:40
ChimeraTK::ConnectionMaker
Definition: ConnectionMaker.h:74
ChimeraTK::Application::enableDebugDataLoss
void enableDebugDataLoss()
Enable debug output for lost data.
Definition: Application.h:148
ModuleGroup.h
ChimeraTK::Application::initialise
void initialise() override
Definition: Application.cc:106
ChimeraTK::Application::_circularDependencyNetworks
std::map< size_t, std::list< EntityOwner * > > _circularDependencyNetworks
The networks of circular dependencies, reachable by their hash, which serves as unique ID.
Definition: Application.h:270
ChimeraTK::Application::getCircularNetworkHash
size_t getCircularNetworkHash() const override
Get the ID of the circular dependency network (0 if none).
Definition: Application.h:323
ChimeraTK::Application::getFullDescription
std::string getFullDescription() const override
Obtain the full description including the full description of the owner.
Definition: Application.h:134
ChimeraTK::Application::run
void run() override
Execute the module.
Definition: Application.cc:134
ChimeraTK::Application::getCircularNetworkInvalidityCounter
size_t getCircularNetworkInvalidityCounter(size_t circularNetworkHash) const
Definition: Application.h:179
ChimeraTK::Application::_circularDependencyDetector
detail::CircularDependencyDetector _circularDependencyDetector
Definition: Application.h:240
ChimeraTK::Application::VariableNetwork
friend class VariableNetwork
Definition: Application.h:195
ChimeraTK::Application::getInstance
static Application & getInstance()
Obtain instance of the application.
Definition: Application.cc:261
Logger.h
ChimeraTK::Application::generateDOT
void generateDOT()
Instead of running the application, just initialise it and output the published variables to a DOT fi...
Definition: Application.cc:254
ChimeraTK::Application::setVersionInfoProvider
void setVersionInfoProvider(ApplicationModule *provider)
Definition: Application.h:177
ChimeraTK::Application::getConfigReader
ConfigReader & getConfigReader()
Definition: Application.h:189
ChimeraTK::Application::getDeviceManager
boost::shared_ptr< DeviceManager > getDeviceManager(const std::string &aliasOrCDD)
Return the DeviceManager for the given alias name or CDD.
Definition: Application.cc:267
ChimeraTK::Application::incrementDataFaultCounter
void incrementDataFaultCounter() override
Set the data validity flag to fault and increment the fault counter.
Definition: Application.h:311
ChimeraTK::EntityOwner::ModuleType
ModuleType
Definition: EntityOwner.h:117
ChimeraTK::Application::VariableNetworkModuleGraphDumpingVisitor
friend class VariableNetworkModuleGraphDumpingVisitor
Definition: Application.h:198
ChimeraTK::ApplicationModule
Definition: ApplicationModule.h:24
ChimeraTK::DebugPrintAccessorDecorator
Decorator of the NDRegisterAccessor which facilitates tests of the application.
Definition: DebugPrintAccessorDecorator.h:17
ChimeraTK::NetworkVisitor
Definition: ConnectionMaker.h:17
ChimeraTK::Application::_configReader
std::shared_ptr< ConfigReader > _configReader
Manager for Python-based ApplicationModules.
Definition: Application.h:286
ChimeraTK::VariableNetworkNode::getUniqueId
const void * getUniqueId() const
Return the unique ID of this node (will change every time the application is started).
Definition: VariableNetworkNode.h:158
ChimeraTK::LifeCycleState
LifeCycleState
Enum to define the life-cycle states of an Application.
Definition: Flags.h:41
Model.h
TestableMode.h
CircularDependencyDetector.h
ChimeraTK::Application::_pvIdMap
std::map< size_t, size_t > _pvIdMap
Map from ProcessArray uniqueId to the variable ID for control system variables.
Definition: Application.h:238
ChimeraTK::ExceptionHandlingDecorator
Decorator of the NDRegisterAccessor which facilitates tests of the application.
Definition: ExceptionHandlingDecorator.h:15
ChimeraTK::Application::CircularDependencyDetector
friend struct detail::CircularDependencyDetector
Definition: Application.h:296
ChimeraTK::Application::_debugMode_variableList
std::unordered_set< const void * > _debugMode_variableList
List of variables for which debug output was requested via enableVariableDebugging().
Definition: Application.h:246
ChimeraTK::TestFacility
Helper class to facilitate tests of applications based on ApplicationCore.
Definition: TestFacility.h:36
ConnectionMaker.h
ChimeraTK::Application::hasInstance
static bool hasInstance()
Check whether an instance of Application currently exists.
Definition: Application.h:99
ChimeraTK::Application::_testableMode
detail::TestableMode _testableMode
Definition: Application.h:241
ChimeraTK::ModuleGroup::Application
friend class Application
Definition: ModuleGroup.h:47
ChimeraTK::Application::_testFacilityRunApplicationCalled
bool _testFacilityRunApplicationCalled
Flag which is set by the TestFacility in runApplication() at the beginning.
Definition: Application.h:225
ChimeraTK::Application::getCurrentVersionNumber
VersionNumber getCurrentVersionNumber() const override
Return the current version number which has been received with the last push-type read operation.
Definition: Application.h:299
ChimeraTK::LifeCycleState::initialisation
@ initialisation
Initialisation phase including ApplicationModule::prepare().
ChimeraTK::Application::_runCalled
bool _runCalled
Flag whether run() has been called already, to make sure it doesn't get called twice.
Definition: Application.h:231
ChimeraTK::Application::_cm
ConnectionMaker _cm
Helper class to create connections.
Definition: Application.h:215
ChimeraTK::Application::enableVariableDebugging
void enableVariableDebugging(const VariableNetworkNode &node)
Enable debug output for a given variable.
Definition: Application.h:139
ChimeraTK::Application::_enableDebugMakeConnections
bool _enableDebugMakeConnections
Flag if debug output is enabled for creation of the variable connections.
Definition: Application.h:234
ChimeraTK::Model::RootProxy
Proxy representing the root of the application model.
Definition: Model.h:210
ChimeraTK::FanOut
Base class for several implementations which distribute values from one feeder to multiple consumers.
Definition: Application.h:38
ChimeraTK::Application::VariableNetworkGraphDumpingVisitor
friend class VariableNetworkGraphDumpingVisitor
Definition: Application.h:197
ChimeraTK::Application::getDataValidity
DataValidity getDataValidity() const override
Return the data validity flag.
Definition: Application.h:307
ChimeraTK::Application::getAndResetDataLossCounter
static size_t getAndResetDataLossCounter()
Return the current value of the data loss counter and (atomically) reset it to 0.
Definition: Application.cc:96
ChimeraTK::Application::_circularNetworkInvalidityCounters
std::map< size_t, std::atomic< uint64_t > > _circularNetworkInvalidityCounters
Map of atomic invalidity counters for each circular dependency network.
Definition: Application.h:266
ChimeraTK::Application::getStartVersion
VersionNumber getStartVersion() const
Return the start version.
Definition: Application.h:175
ChimeraTK::EntityOwner::_name
std::string _name
The name of this instance.
Definition: EntityOwner.h:171
ChimeraTK::Application::getLifeCycleState
LifeCycleState getLifeCycleState() const
Get the current LifeCycleState of the application.
Definition: Application.h:169
ChimeraTK::Application::getModuleType
ModuleType getModuleType() const override
Return the module type of this module, or in case of a VirtualModule the module type this VirtualModu...
Definition: Application.h:130
ChimeraTK::Application::decrementDataFaultCounter
void decrementDataFaultCounter() override
Decrement the fault counter and set the data validity flag to ok if the counter has reached 0.
Definition: Application.h:315
ChimeraTK::MetaDataPropagatingRegisterDecorator
NDRegisterAccessorDecorator which propagates meta data attached to input process variables through th...
Definition: MetaDataPropagatingRegisterDecorator.h:51
ChimeraTK
InvalidityTracer application module.
Definition: spec_dataValidityPropagation.dox:2
ChimeraTK::Application::~Application
~Application() override
Definition: Application.cc:64
ChimeraTK::Application::getTestableMode
detail::TestableMode & getTestableMode()
Get the TestableMode control object of this application.
Definition: Application.h:117
ChimeraTK::EntityOwner::ModuleType::ModuleGroup
@ ModuleGroup
ChimeraTK::Application::_versionInfoProvider
ApplicationModule * _versionInfoProvider
Definition: Application.h:260
ChimeraTK::Module
Base class for ApplicationModule and DeviceModule, to have a common interface for these module types.
Definition: Module.h:21
ChimeraTK::Application
Definition: Application.h:48