HOPS
HOPS class reference
MHO_PythonOperatorBuilder.hh
Go to the documentation of this file.
1 #ifndef MHO_PythonOperatorBuilder_HH__
2 #define MHO_PythonOperatorBuilder_HH__
3 
4 #include <memory>
5 
6 #include "MHO_OperatorBuilder.hh"
8 
9 namespace hops
10 {
11 
21 {
22  public:
24 
26  MHO_ParameterStore* pstore = nullptr)
27  : MHO_OperatorBuilder(toolbox, cstore, pstore){};
28 
30 
31  virtual bool Build() override
32  {
33  if(IsConfigurationOk())
34  {
35  std::unique_ptr< MHO_PyGenericOperator > op(new MHO_PyGenericOperator());
36 
37  //pass the data container and parameter stores to the python operator
38  // op->SetParameterStore(this->fParameterStore);
39  // op->SetContainerStore(this->fContainerStore);
40  op->SetFringeData(this->fFringeData);
41  op->SetOperatorToolbox(this->fOperatorToolbox);
42 
43  //retrieve pass the module/function name info from the control file
44  std::string op_name = this->fFormat["name"].get< std::string >();
45  std::string op_category = this->fFormat["operator_category"].get< std::string >();
46  std::string module_path = fAttributes["value"]["module_path"].get< std::string >();
47  std::string function_name = fAttributes["value"]["function_name"].get< std::string >();
48  double priority = this->fFormat["priority"].get< double >();
49  if(fAttributes["value"].contains("override_priority"))
50  {
51  double override_priority = fAttributes["value"]["override_priority"].get< double >();
52  msg_debug("initialization", "python operator: " << op_name << " priority overridden from " << priority
53  << " to " << override_priority << eom);
54  priority = override_priority;
55  }
56 
57  op->SetPriority(priority);
58  std::string full_name = module_path + ":" + function_name;
59  op->SetName(full_name);
60  op->SetModulePath(module_path);
61  op->SetFunctionName(function_name);
62 
63  //TODO handle naming scheme for multiple python operators (should they have a name parameter?)
64  bool replace_duplicates = false;
65  this->fOperatorToolbox->AddOperator(std::move(op), full_name, op_category, replace_duplicates);
66  return true;
67  }
68  else
69  {
70  msg_error("initialization", "cannot build python operator." << eom);
71  return false;
72  }
73  }
74 
75  virtual bool IsConfigurationOk() override
76  {
77  TODO_FIXME_MSG("TODO FIXME -- improve checks on operator attributes in IsConfigurationOk)")
78  bool ok = true;
79  //for compound statements, check that the fields are present
80  if(fFormat["statement_type"] == "operator")
81  {
82  if(fFormat.contains("type") && fFormat["type"].get< std::string >() == "compound")
83  {
84  for(auto it = fFormat["fields"].begin(); it != fFormat["fields"].end(); it++)
85  {
86  //check if the field is optional
87  //for python operators, 'override_priority' is optional,
88  //and the default value is provided by the format
89  //if it is option, the field name will be preceeded by a '!'
90  //control file example:
91  // python_calibration example4 set_pc_phase_offset_y 3.1
92 
93  bool is_required = true;
94  std::string field_name = (*it).get< std::string >();
95  //std::cout<<"py op builder, field name = "<<field_name<<std::endl;
96  if(!field_name.empty() && field_name[0] == '!')
97  {
98  is_required = false;
99  }
100  if(!fAttributes["value"].contains(*it) && is_required)
101  {
102  msg_error("initialization", "missing attribute called " << *it << " in parameters of operator: "
103  << fFormat["name"].get< std::string >()
104  << ", will not build " << eom);
105  return false;
106  }
107  }
108  }
109  }
110 
111  return ok;
112  }
113 
114  private:
115 };
116 
117 } // namespace hops
118 
119 #endif
#define msg_debug(xKEY, xCONTENT)
Definition: MHO_Message.hh:291
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:238
#define TODO_FIXME_MSG(x)
Definition: MHO_Message.hh:35
Class MHO_ContainerStore.
Definition: MHO_ContainerStore.hh:32
Class MHO_FringeData.
Definition: MHO_FringeData.hh:30
Abtract base class for a builder object (creates an operator for later use)
Definition: MHO_OperatorBuilder.hh:28
MHO_OperatorToolbox * fOperatorToolbox
Definition: MHO_OperatorBuilder.hh:135
mho_json fFormat
Definition: MHO_OperatorBuilder.hh:223
MHO_FringeData * fFringeData
Definition: MHO_OperatorBuilder.hh:138
mho_json fAttributes
Definition: MHO_OperatorBuilder.hh:225
Class MHO_OperatorToolbox.
Definition: MHO_OperatorToolbox.hh:28
void AddOperator(std::unique_ptr< MHO_Operator > op, const std::string &name, const std::string &category, bool replace_duplicate=true)
Adds an operator to the toolbox with optional replacement if duplicate name exists.
Definition: MHO_OperatorToolbox.hh:49
Class MHO_ParameterStore.
Definition: MHO_ParameterStore.hh:52
this class allows a user to inject a python function of the form: func(fringe_data_interface) into th...
Definition: MHO_PyGenericOperator.hh:35
Definition: MHO_PythonOperatorBuilder.hh:21
virtual ~MHO_PythonOperatorBuilder()
Definition: MHO_PythonOperatorBuilder.hh:29
virtual bool IsConfigurationOk() override
Function IsConfigurationOk provided for derived class to validate fAttributes against fFormat and/or ...
Definition: MHO_PythonOperatorBuilder.hh:75
virtual bool Build() override
Builds the object and passes it to toolbox if successful, otherwise returns false.
Definition: MHO_PythonOperatorBuilder.hh:31
MHO_PythonOperatorBuilder(MHO_OperatorToolbox *toolbox, MHO_FringeData *fdata)
Definition: MHO_PythonOperatorBuilder.hh:23
MHO_PythonOperatorBuilder(MHO_OperatorToolbox *toolbox, MHO_ContainerStore *cstore=nullptr, MHO_ParameterStore *pstore=nullptr)
Definition: MHO_PythonOperatorBuilder.hh:25
Definition: MHO_AdhocFlagging.hh:18