TECA
The Toolkit for Extreme Climate Analysis
teca_programmable_algorithm.h
1 #ifndef teca_programmable_algorithm_h
2 #define teca_programmable_algorithm_h
3 
4 #include "teca_config.h"
5 #include "teca_algorithm.h"
6 #include "teca_shared_object.h"
7 #include "teca_metadata.h"
8 #include "teca_dataset.h"
9 
10 TECA_SHARED_OBJECT_FORWARD_DECL(teca_programmable_algorithm)
11 TECA_SHARED_OBJECT_FORWARD_DECL(teca_threaded_programmable_algorithm)
12 
13 #ifdef SWIG
14 typedef void* report_callback_t;
15 typedef void* request_callback_t;
16 typedef void* execute_callback_t;
17 typedef void* threaded_execute_callback_t;
18 #else
19 /// A callable implementing the report phase of pipeline execution
20 using report_callback_t = std::function<teca_metadata(
21  unsigned int, const std::vector<teca_metadata>&)>;
22 
23 /// A callable implementing the request phase of pipeline execution
24 using request_callback_t = std::function<std::vector<teca_metadata>(
25  unsigned int, const std::vector<teca_metadata> &,
26  const teca_metadata &)>;
27 
28 /// A callable implementing the execute phase of pipeline execution
29 using execute_callback_t = std::function<const_p_teca_dataset(
30  unsigned int, const std::vector<const_p_teca_dataset> &,
31  const teca_metadata &)>;
32 
33 /// A callable implementing the streaming execute phase of pipeline execution
34 using threaded_execute_callback_t = std::function<const_p_teca_dataset(
35  unsigned int, const std::vector<const_p_teca_dataset> &,
36  const teca_metadata &, int)>;
37 #endif
38 
39 /// An algorithm implemented with user provided callbacks.
40 /**
41  * The user can provide a callback for each of the three phases
42  * of pipeline execution. The number of input and output ports
43  * can also be set for filters (1 or more inputs, 1 or more outputs)
44  * sources, (no inputs, 1 or more outputs), or sinks (1 or more
45  * inputs, no outputs).
46  *
47  * 1. report phase. the report callback returns metadata
48  * describing data that can be produced. The report callback
49  * is optional. It's only needed if the algorithm will produce
50  * new data or transform metadata.
51  *
52  * the report callback must be callable with signature:
53  * teca_metadata(unsigned int)
54  *
55  * 2. request phase. the request callback generates a vector
56  * of requests(metadata objects) that inform the upstream of
57  * what data to generate. The request callback is optional.
58  * It's only needed if the algorithm needs data from the
59  * upstream or transform metadata.
60  *
61  * the request callback must be callable with the signature:
62  * std::vector<teca_metadata>(
63  * unsigned int,
64  * const std::vector<teca_metadata> &,
65  * const teca_metadata &)
66  *
67  * 3. execute phase. the execute callback is used to do useful
68  * work on incoming or outgoing data. Examples include
69  * generating new datasets, processing datasets, reading
70  * and writing data to/from disk, and so on. The execute
71  * callback is optional.
72  *
73  * the execute callback must be callable with the signature:
74  * const_p_teca_dataset(
75  * unsigned int, const std::vector<const_p_teca_dataset> &,
76  * const teca_metadata &)
77  *
78  * see also:
79  *
80  * set_number_of_input_connections
81  * set_number_of_output_ports
82  * set_report_callback
83  * set_request_callback
84  * set_execute_callback
85  */
87 {
88 public:
89  TECA_ALGORITHM_STATIC_NEW(teca_programmable_algorithm)
90  TECA_ALGORITHM_DELETE_COPY_ASSIGN(teca_programmable_algorithm)
92 
93  /// @name
94  /** set/get the class name.
95  */
96  /// @{
97  /// set the name differntiating this instance in the pipeline.
98  virtual int set_name(const std::string &name);
99 
100  /// get the name differntiating this instance in the pipeline.
101  const char *get_class_name() const override
102  { return this->class_name; }
103  /// @}
104 
105  /// set the number of input and outputs
108 
109  /// @name default actions
110  /** Default implemenations for the phases of pipeline execution. Note that
111  * these generally do not do all that is needed but may suffice for simple
112  * pipelines.
113  */
114  /// @{
115  /** Install the default implementation for the report phase which forwards
116  * the incoming report downstream.
117  */
119 
120  /** Install the default implementation for the request phase which forwards
121  * the incoming request upstream.
122  */
124 
125  /** Install the default implementation for the execute phase which
126  * returns a nullptr.
127  */
129  /// @}
130 
131  /// @name report_callback
132  /// @{
133  /** Set callback that responds to reporting stage of pipeline execution.
134  * the report callback must be callable with signature:
135  *
136  * ```C++
137  * teca_metadata (unsigned int, const std::vector<teca_metadata> &)
138  * ```
139  */
140  TECA_ALGORITHM_CALLBACK_PROPERTY(report_callback_t, report_callback)
141  /// @}
142 
143  /// @name request_callback
144  /// @{
145  /** Set the callback that responds to the requesting stage of pipeline
146  * execution. the request callback must be callable with the signature:
147  *
148  * ```C++
149  * std::vector<teca_metadata> (unsigned int,
150  * const std::vector<teca_metadata> &, const teca_metadata &)
151  * ```
152  */
153  TECA_ALGORITHM_CALLBACK_PROPERTY(request_callback_t, request_callback)
154  /// @}
155 
156  /// @name execute_callback
157  /// @{
158  /** Set the callback that responds to the execution stage of pipeline
159  * execution. the execute callback must be callable with the signature:
160  *
161  * ```C++
162  * const_p_teca_dataset (
163  * unsigned int, const std::vector<const_p_teca_dataset> &,
164  * const teca_metadata &)
165  * ```
166  */
167  TECA_ALGORITHM_CALLBACK_PROPERTY(execute_callback_t, execute_callback)
168 
169 protected:
171 
172  using teca_algorithm::get_output_metadata;
173 
174  teca_metadata get_output_metadata(
175  unsigned int port,
176  const std::vector<teca_metadata> &input_md) override;
177 
178  std::vector<teca_metadata> get_upstream_request(
179  unsigned int port,
180  const std::vector<teca_metadata> &input_md,
181  const teca_metadata &request) override;
182 
183  const_p_teca_dataset execute(
184  unsigned int port,
185  const std::vector<const_p_teca_dataset> &input_data,
186  const teca_metadata &request) override;
187 
188  report_callback_t report_callback;
189  request_callback_t request_callback;
190  execute_callback_t execute_callback;
191  char class_name[128];
192 };
193 
194 #endif
The interface to TECA pipeline architecture.
Definition: teca_algorithm.h:244
void set_number_of_output_ports(unsigned int n)
void set_number_of_input_connections(unsigned int n)
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:22
An algorithm implemented with user provided callbacks.
Definition: teca_programmable_algorithm.h:87
const char * get_class_name() const override
get the name differntiating this instance in the pipeline.
Definition: teca_programmable_algorithm.h:101
virtual int set_name(const std::string &name)
An threaded algorithm implemented with user provided callbacks.
Definition: teca_threaded_programmable_algorithm.h:67
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.