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_algorithm.h"
5 #include "teca_shared_object.h"
6 #include "teca_metadata.h"
7 #include "teca_dataset.h"
8 
9 TECA_SHARED_OBJECT_FORWARD_DECL(teca_programmable_algorithm)
10 TECA_SHARED_OBJECT_FORWARD_DECL(teca_threaded_programmable_algorithm)
11 
12 #ifdef SWIG
13 typedef void* report_callback_t;
14 typedef void* request_callback_t;
15 typedef void* execute_callback_t;
16 typedef void* threaded_execute_callback_t;
17 #else
18 /// A callable implementing the report phase of pipeline execution
19 using report_callback_t = std::function<teca_metadata(
20  unsigned int, const std::vector<teca_metadata>&)>;
21 
22 /// A callable implementing the request phase of pipeline execution
23 using request_callback_t = std::function<std::vector<teca_metadata>(
24  unsigned int, const std::vector<teca_metadata> &,
25  const teca_metadata &)>;
26 
27 /// A callable implementing the execute phase of pipeline execution
28 using execute_callback_t = std::function<const_p_teca_dataset(
29  unsigned int, const std::vector<const_p_teca_dataset> &,
30  const teca_metadata &)>;
31 
32 /// A callable implementing the streaming execute phase of pipeline execution
33 using threaded_execute_callback_t = std::function<const_p_teca_dataset(
34  unsigned int, const std::vector<const_p_teca_dataset> &,
35  const teca_metadata &, int)>;
36 #endif
37 
38 /// An algorithm implemented with user provided callbacks.
39 /**
40  * The user can provide a callback for each of the three phases
41  * of pipeline execution. The number of input and output ports
42  * can also be set for filters (1 or more inputs, 1 or more outputs)
43  * sources, (no inputs, 1 or more outputs), or sinks (1 or more
44  * inputs, no outputs).
45  *
46  * 1) report phase. the report callback returns metadata
47  * describing data that can be produced. The report callback
48  * is optional. It's only needed if the algorithm will produce
49  * new data or transform metadata.
50  *
51  * the report callback must be callable with signature:
52  * teca_metadata(unsigned int)
53  *
54  * 2) request phase. the request callback generates a vector
55  * of requests(metadata objects) that inform the upstream of
56  * what data to generate. The request callback is optional.
57  * It's only needed if the algorithm needs data from the
58  * upstream or transform metadata.
59  *
60  * the request callback must be callable with the signature:
61  * std::vector<teca_metadata>(
62  * unsigned int,
63  * const std::vector<teca_metadata> &,
64  * const teca_metadata &)
65  *
66  * 3) execute phase. the execute callback is used to do useful
67  * work on incoming or outgoing data. Examples include
68  * generating new datasets, processing datasets, reading
69  * and writing data to/from disk, and so on. The execute
70  * callback is optional.
71  *
72  * the execute callback must be callable with the signature:
73  * const_p_teca_dataset(
74  * unsigned int, const std::vector<const_p_teca_dataset> &,
75  * const teca_metadata &)
76  *
77  * see also:
78  *
79  * set_number_of_input_connections
80  * set_number_of_output_ports
81  * set_report_callback
82  * set_request_callback
83  * set_execute_callback
84  */
86 {
87 public:
88  TECA_ALGORITHM_STATIC_NEW(teca_programmable_algorithm)
89  TECA_ALGORITHM_DELETE_COPY_ASSIGN(teca_programmable_algorithm)
91 
92  // set/get the class name.
93  virtual int set_name(const std::string &name);
94 
95  const char *get_class_name() const override
96  { return this->class_name; }
97 
98  // set the number of input and outputs
101 
102  // install the default implementation
103  void use_default_report_action();
104  void use_default_request_action();
105  void use_default_execute_action();
106 
107  // set callback that responds to reporting stage
108  // of pipeline execution. the report callback must
109  // be callable with signature:
110  //
111  // teca_metadata (unsigned int,
112  // const std::vector<teca_metadata> &)
113  //
114  // the default implementation forwards downstream
115  TECA_ALGORITHM_CALLBACK_PROPERTY(
116  report_callback_t, report_callback)
117 
118  // set the callback that responds to the requesting
119  // stage of pipeline execution. the request callback
120  // must be callable with the signature:
121  //
122  // std::vector<teca_metadata> (
123  // unsigned int,
124  // const std::vector<teca_metadata> &,
125  // const teca_metadata &)
126  //
127  // the default implementation forwards upstream
128  TECA_ALGORITHM_CALLBACK_PROPERTY(
129  request_callback_t, request_callback)
130 
131  // set the callback that responds to the execution stage
132  // of pipeline execution. the execute callback must be
133  // callable with the signature:
134  //
135  // const_p_teca_dataset (
136  // unsigned int, const std::vector<const_p_teca_dataset> &,
137  // const teca_metadata &)
138  //
139  // the default implementation returns a nullptr
140  TECA_ALGORITHM_CALLBACK_PROPERTY(
141  execute_callback_t, execute_callback)
142 
143 protected:
145 
146 private:
147  teca_metadata get_output_metadata(
148  unsigned int port,
149  const std::vector<teca_metadata> &input_md) override;
150 
151  std::vector<teca_metadata> get_upstream_request(
152  unsigned int port,
153  const std::vector<teca_metadata> &input_md,
154  const teca_metadata &request) override;
155 
156  const_p_teca_dataset execute(
157  unsigned int port,
158  const std::vector<const_p_teca_dataset> &input_data,
159  const teca_metadata &request) override;
160 
161 protected:
162  report_callback_t report_callback;
163  request_callback_t request_callback;
164  execute_callback_t execute_callback;
165  char class_name[64];
166 };
167 
168 #endif
teca_metadata
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:18
teca_programmable_algorithm
An algorithm implemented with user provided callbacks.
Definition: teca_programmable_algorithm.h:85
teca_threaded_programmable_algorithm
An threaded algorithm implemented with user provided callbacks.
Definition: teca_threaded_programmable_algorithm.h:65
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:95
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)