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  // set/get the class name.
94  virtual int set_name(const std::string &name);
95 
96  const char *get_class_name() const override
97  { return this->class_name; }
98 
99  // set the number of input and outputs
102 
103  // install the default implementation
104  void use_default_report_action();
105  void use_default_request_action();
106  void use_default_execute_action();
107 
108  // set callback that responds to reporting stage
109  // of pipeline execution. the report callback must
110  // be callable with signature:
111  //
112  // teca_metadata (unsigned int,
113  // const std::vector<teca_metadata> &)
114  //
115  // the default implementation forwards downstream
116  TECA_ALGORITHM_CALLBACK_PROPERTY(
117  report_callback_t, report_callback)
118 
119  // set the callback that responds to the requesting
120  // stage of pipeline execution. the request callback
121  // must be callable with the signature:
122  //
123  // std::vector<teca_metadata> (
124  // unsigned int,
125  // const std::vector<teca_metadata> &,
126  // const teca_metadata &)
127  //
128  // the default implementation forwards upstream
129  TECA_ALGORITHM_CALLBACK_PROPERTY(
130  request_callback_t, request_callback)
131 
132  // set the callback that responds to the execution stage
133  // of pipeline execution. the execute callback must be
134  // callable with the signature:
135  //
136  // const_p_teca_dataset (
137  // unsigned int, const std::vector<const_p_teca_dataset> &,
138  // const teca_metadata &)
139  //
140  // the default implementation returns a nullptr
141  TECA_ALGORITHM_CALLBACK_PROPERTY(
142  execute_callback_t, execute_callback)
143 
144 protected:
146 
147 private:
149 
151  unsigned int port,
152  const std::vector<teca_metadata> &input_md) override;
153 
154  std::vector<teca_metadata> get_upstream_request(
155  unsigned int port,
156  const std::vector<teca_metadata> &input_md,
157  const teca_metadata &request) override;
158 
159  const_p_teca_dataset execute(
160  unsigned int port,
161  const std::vector<const_p_teca_dataset> &input_data,
162  const teca_metadata &request) override;
163 
164 protected:
165  report_callback_t report_callback;
166  request_callback_t request_callback;
167  execute_callback_t execute_callback;
168  char class_name[64];
169 };
170 
171 #endif
teca_metadata
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:21
teca_algorithm::get_upstream_request
virtual std::vector< teca_metadata > get_upstream_request(unsigned int port, const std::vector< teca_metadata > &input_md, const teca_metadata &request)
teca_algorithm::get_output_metadata
virtual teca_metadata get_output_metadata(unsigned int port, const std::vector< teca_metadata > &input_md)
teca_algorithm::execute
virtual const_p_teca_dataset execute(unsigned int port, const std::vector< const_p_teca_dataset > &input_data, const teca_metadata &request)
teca_programmable_algorithm
An algorithm implemented with user provided callbacks.
Definition: teca_programmable_algorithm.h:86
teca_threaded_programmable_algorithm
An threaded algorithm implemented with user provided callbacks.
Definition: teca_threaded_programmable_algorithm.h:66
teca_shared_object.h
teca_algorithm::set_number_of_input_connections
void set_number_of_input_connections(unsigned int n)
teca_programmable_algorithm::get_class_name
const char * get_class_name() const override
return the name of the class.
Definition: teca_programmable_algorithm.h:96
teca_error::TECA_EXPORT
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
teca_algorithm
The interface to TECA pipeline architecture.
Definition: teca_algorithm.h:237
teca_algorithm::set_number_of_output_ports
void set_number_of_output_ports(unsigned int n)