DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
StreamManipulator.h
Go to the documentation of this file.
1// This file is part of DynExp.
2
9#pragma once
10
11#include "stdafx.h"
12#include "DynExpCore.h"
13#include "PyModules.h"
14#include "../Instruments/InterModuleCommunicator.h"
15
16#include "CommonModuleEvents.h"
17
18namespace DynExpModule
19{
20 class StreamManipulator;
21
25 using PyStreamListType = std::vector<DynExpInstr::PyDataStreamInstrument>;
26
31 {
37
41 std::chrono::time_point<std::chrono::system_clock> LastExecutionTime{};
42
47
52
57 std::string SaveFilename;
58
63 void Reset();
64 };
65
70 {
75 std::chrono::milliseconds MinNextExecutionDelay{};
76
81 std::chrono::milliseconds MaxNextExecutionDelay{};
82
91
96 void Reset();
97 };
98
103 {
104 public:
106 virtual ~StreamManipulatorData() = default;
107
108 auto& GetInputDataStreams() const noexcept { return InputDataStreams; }
109 auto& GetInputDataStreams() noexcept { return InputDataStreams; }
110 auto& GetOutputDataStreams() const noexcept { return OutputDataStreams; }
111 auto& GetOutputDataStreams() noexcept { return OutputDataStreams; }
112 auto& GetCommunicator() noexcept { return Communicator; }
113
114 private:
118 void ResetImpl(dispatch_tag<ModuleDataBase>) override final;
119
124
129 void Init();
130
135
140
146 };
147
152 {
153 public:
158
159 virtual ~StreamManipulatorParams() = default;
160
161 virtual const char* GetParamClassTag() const noexcept override { return "StreamManipulatorParams"; }
162
168 "InputDataStreams", "Input data stream instrument(s)", "Data stream instruments to be used as data sources", DynExpUI::Icons::Instrument, true };
169
175 "OutputDataStreams", "Output data stream instrument(s)", "Data stream instruments to be used as data sinks", DynExpUI::Icons::Instrument, true };
176
203 Param<ParamsConfigDialog::TextType> PythonCodePath = { *this, "PythonCodePath", "Python code path",
204 "Path to a Python file containing the function which writes samples to the output streams based on the input streams' data", true, "", DynExp::TextUsageType::Code };
205
212 "InterModuleCommunicator", "Inter-module communicator", "Inter-module communicator to control this module with", DynExpUI::Icons::Instrument, true };
213
214 private:
219 };
220
225 {
226 public:
229
232
233 private:
234 virtual DynExp::ParamsBasePtrType MakeParams(DynExp::ItemIDType ID, const DynExp::DynExpCore& Core) const override final { return DynExp::MakeParams<StreamManipulatorConfigurator>(ID, Core); }
235 };
236
242 {
248
253
254 public:
258
259 constexpr static auto Name() noexcept { return "Stream Manipulator"; }
260 constexpr static auto Category() noexcept { return "I/O"; }
261
267
268 virtual ~StreamManipulator() = default;
269
270 virtual std::string GetName() const override { return Name(); }
271 virtual std::string GetCategory() const override { return Category(); }
272
277 bool TreatModuleExceptionsAsWarnings() const override { return IsInitialized; }
278
279 std::chrono::milliseconds GetMainLoopDelay() const override final { return std::chrono::milliseconds(1); }
280
281 private:
283
287 void ResetImpl(dispatch_tag<ModuleBase>) override final;
288
298 void Step(Util::SynchronizedPointer<ModuleDataType>& ModuleData, const PyFuncType& StepFunction, bool UpdateLastExecutionTime = true) const;
299
304 void OnInit(DynExp::ModuleInstance* Instance) const override final;
305 void OnExit(DynExp::ModuleInstance* Instance) const override final;
306
313 void OnSetFilename(DynExp::ModuleInstance* Instance, const std::string& Filename) const;
314
319 void OnFinished(DynExp::ModuleInstance* Instance) const;
320
325 void OnStart(DynExp::ModuleInstance* Instance) const;
326
331 void OnStop(DynExp::ModuleInstance* Instance) const;
332
337 void OnTrigger(DynExp::ModuleInstance* Instance) const;
339
345 mutable std::atomic<bool> IsInitialized = false;
346
354
362
367
372
378
382 mutable std::chrono::time_point<std::chrono::system_clock> LastManipulatorPyFuncExecution{};
383 };
384}
Provides common events for inter-module communication. Also refer to DynExp::InterModuleEventBase.
Defines DynExp's core module as an interface between the UI and DynExp objects.
Implementation of mappings between DynExp instruments and corresponding embedded Python modules makin...
Base class for all circular data streams based on Util::circularbuf.
Configurator class for StreamManipulator.
virtual DynExp::ParamsBasePtrType MakeParams(DynExp::ItemIDType ID, const DynExp::DynExpCore &Core) const override final
Override to make derived classes call DynExp::MakeParams with the correct configurator type derived f...
Data class for StreamManipulator.
auto & GetInputDataStreams() const noexcept
Getter for InputDataStreams.
virtual void ResetImpl(dispatch_tag< StreamManipulatorData >)
Refer to DynExp::ModuleDataBase::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of ...
auto & GetInputDataStreams() noexcept
Getter for InputDataStreams.
virtual ~StreamManipulatorData()=default
auto & GetOutputDataStreams() noexcept
Getter for OutputDataStreams.
DynExp::LinkedObjectWrapperContainerList< DynExpInstr::DataStreamInstrument > InputDataStreams
Linked input data stream instruments whose samples are going to be used for calculations.
auto & GetOutputDataStreams() const noexcept
Getter for OutputDataStreams.
void Init()
Called by ResetImpl(dispatch_tag<DynExp::ModuleDataBase>) overridden by this class to initialize the ...
DynExp::LinkedObjectWrapperContainerList< DynExpInstr::DataStreamInstrument > OutputDataStreams
Linked output data stream instruments to write the computed new samples to.
void ResetImpl(dispatch_tag< ModuleDataBase >) override final
Refer to DynExp::ModuleDataBase::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of ...
DynExp::LinkedObjectWrapperContainer< DynExpInstr::InterModuleCommunicator > Communicator
Inter-module communicator instrument to receive events from that are translated to calls to respectiv...
auto & GetCommunicator() noexcept
Getter for Communicator.
Parameter class for StreamManipulator.
ListParam< DynExp::ObjectLink< DynExpInstr::DataStreamInstrument > > InputDataStreams
Parameter for data stream instruments containing input samples. Refer to StreamManipulatorData::Input...
Param< ParamsConfigDialog::TextType > PythonCodePath
Path to the Python file containing the code, which performs the stream manipulation.
StreamManipulatorParams(DynExp::ItemIDType ID, const DynExp::DynExpCore &Core)
Constructs the parameters for a StreamManipulator instance.
virtual const char * GetParamClassTag() const noexcept override
This function is intended to be overridden once in each derived class returning the name of the respe...
Param< DynExp::ObjectLink< DynExpInstr::InterModuleCommunicator > > Communicator
This module listens to the DynExpModule::StartEvent, DynExpModule::StopEvent, and DynExpModule::Trigg...
void ConfigureParamsImpl(dispatch_tag< ModuleParamsBase >) override final
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
ListParam< DynExp::ObjectLink< DynExpInstr::DataStreamInstrument > > OutputDataStreams
Parameter for data stream instruments containing output samples. Refer to StreamManipulatorData::Outp...
virtual ~StreamManipulatorParams()=default
Module to process data stored in data stream instrument(s) with a Python script and to write the resu...
void OnStart(DynExp::ModuleInstance *Instance) const
Called when receiving DynExpModule::StartEvent.
void Step(Util::SynchronizedPointer< ModuleDataType > &ModuleData, const PyFuncType &StepFunction, bool UpdateLastExecutionTime=true) const
Performs a single manipulation step by preparing input data for a call to the Python function on_step...
void OnInit(DynExp::ModuleInstance *Instance) const override final
This event is triggered right before the module thread starts. Override it to lock instruments this m...
size_t NumFailedUpdateAttempts
Counts how often StreamManipulator::ModuleMainLoop() contiguously failed due to an exception of type ...
std::chrono::time_point< std::chrono::system_clock > LastManipulatorPyFuncExecution
Time point when the on_step() Python function was invoked last.
std::atomic< bool > IsInitialized
Indicates whether the module has been fully initialized by finishing OnInit(). The variable is atomic...
void OnTrigger(DynExp::ModuleInstance *Instance) const
Called when receiving DynExpModule::TriggerEvent.
PyFuncType ManipulatorPyFuncStep
Handle to a Python function called for each manipulation step.
PyFuncType ManipulatorPyFuncFinished
Handle to a Python function called for DynExpModule::FinishedEvent.
virtual std::string GetCategory() const override
Returns the category of this Object type.
PyStreamManipulatorOutputData ManipulatorPyFuncOutput
Output data returned from the on_step() and event handler Python functions.
Util::DynExpErrorCodes::DynExpErrorCodes ModuleMainLoop(DynExp::ModuleInstance &Instance) override final
Module main loop. The function is executed periodically by the module thread. Also refer to GetMainLo...
StreamManipulator(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType &&Params)
Constructs a ModuleBase instance.
PyFuncType ManipulatorPyFuncExit
Handle to a Python function called on module termination.
virtual std::string GetName() const override
Returns the name of this Object type.
virtual ~StreamManipulator()=default
void OnFinished(DynExp::ModuleInstance *Instance) const
Called when receiving DynExpModule::FinishedEvent.
static constexpr auto Name() noexcept
Every derived class has to redefine this function.
void ResetImpl(dispatch_tag< ModuleBase >) override final
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
PyFuncType ManipulatorPyFuncStart
Handle to a Python function called for DynExpModule::StartEvent.
static constexpr auto Category() noexcept
Every derived class has to redefine this function.
void OnStop(DynExp::ModuleInstance *Instance) const
Called when receiving DynExpModule::StopEvent.
PyFuncType ManipulatorPyFuncInit
Handle to a Python function called on module initialization.
void OnExit(DynExp::ModuleInstance *Instance) const override final
This event is triggered right before the module thread terminates (not due to an exception,...
PyFuncType ManipulatorPyFuncStop
Handle to a Python function called for DynExpModule::StopEvent.
std::string ManipulatorPyFuncName
Unique name of the Python function all the code of this StreamManipulator instance is declared in....
PyStreamManipulatorInputData ManipulatorPyFuncInput
Input data passed to the on_step() and event handler Python functions.
std::chrono::milliseconds GetMainLoopDelay() const override final
Specifies in which time intervals the module's event queue runs to handle pending events.
void OnSetFilename(DynExp::ModuleInstance *Instance, const std::string &Filename) const
Called when receiving DynExpModule::SetFilenameEvent.
bool TreatModuleExceptionsAsWarnings() const override
Determines whether this module should be terminated if an exception leaves the module's main loop or ...
PyFuncType ManipulatorPyFuncTrigger
Handle to a Python function called for DynExpModule::TriggerEvent.
Util::PyObject< py::object > PyFuncType
Type of a pointer to a Python function bound by pybind11.
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Definition DynExpCore.h:127
This class defines a list of LinkedObjectWrapperContainer instances. The list owns the contained Link...
Definition Object.h:3311
This class holds a pointer (LinkedObjectWrapperPointer) to a LinkedObjectWrapper. Intances of this cl...
Definition Object.h:3168
Base class for modules. Modules implement programs on their own (e.g. measurement protocols or server...
Definition Module.h:428
const std::unique_ptr< ModuleDataType > ModuleData
Module data belonging to this ModuleBase instance.
Definition Module.h:788
ModuleBase(const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
Constructs a ModuleBase instance.
Definition Module.cpp:206
Configurator class for ModuleBase.
Definition Module.h:410
Data structure to contain data which is synchronized in between different threads....
Definition Module.h:173
Refer to ParamsBase::dispatch_tag.
Definition Module.h:191
Defines data for a thread belonging to a ModuleBase instance. Refer to RunnableInstance.
Definition Module.h:840
Parameter class for ModuleBase.
Definition Module.h:373
ModuleParamsBase(ItemIDType ID, const DynExpCore &Core)
Constructs the parameters for a ModuleBase instance.
Definition Module.h:384
const std::thread::id OwnerThreadID
Thread id of the thread which has constructed (and owns) this Object instance.
Definition Object.h:2302
const ParamsBasePtrType Params
Pointer to the parameter class instance belonging to this Object instance.
Definition Object.h:2303
const auto & GetCore() const noexcept
Returns a reference to DynExp's core.
Definition Object.h:1677
const ItemIDType ID
ID of the Object this parameter class instance belongs to.
Definition Object.h:1779
const DynExpCore & Core
Reference to DynExp's core.
Definition Object.h:1780
Tag for function dispatching mechanism within this class used when derived classes are not intended t...
Definition Object.h:349
Pointer to lock a class derived from ISynchronizedPointerLockable for synchronizing between threads....
Definition Util.h:170
DynExp's module namespace contains the implementation of DynExp modules which extend DynExp's core fu...
std::vector< DynExpInstr::PyDataStreamInstrument > PyStreamListType
Type of a list of data stream instruments made available to Python.
constexpr auto Instrument
std::unique_ptr< ParamsBase > ParamsBasePtrType
Alias for a pointer to the parameter system base class ParamsBase.
Definition Object.h:1807
size_t ItemIDType
ID type of objects/items managed by DynExp.
DynExpErrorCodes
DynExp's error codes
Definition Exception.h:22
Accumulates include statements to provide a precompiled header.
Input data type passed to on_step() and event handler Python functions.
PyStreamListType OutputStreams
Vector of data stream instruments to write the resulting output samples to.
PyStreamListType InputStreams
Vector of data stream instruments with input samples to be manipulated.
void Reset()
Resets all member variables of this PyStreamManipulatorInputData instance back to their default value...
DynExp::ItemIDType ModuleID
ID of the module invoking the on_step() or an event handler Python function for stream manipulation.
std::string SaveFilename
Filename and path where the Python functions invoked by the StreamManipulator module can save data....
std::chrono::time_point< std::chrono::system_clock > LastExecutionTime
Time point when the on_step() Python function was invoked last.
Output data type returned from on_step() or event handler Python functions.
void Reset()
Resets all member variables of this PyStreamManipulatorOutputData instance back to their default valu...
std::chrono::milliseconds MinNextExecutionDelay
Time wo wait minimally before the on_step() Python function is called again when each input data stre...
std::chrono::milliseconds MaxNextExecutionDelay
Time to wait maximally before the on_step() Python function is called again when the input data strea...
std::vector< size_t > LastConsumedSampleIDsPerInputStream
Maintaining the order of input data streams in PyStreamManipulatorInputData::InputStreams as passed t...