HOPS
HOPS class reference
MHO_OperatorBuilder.hh
Go to the documentation of this file.
1 #ifndef MHO_OperatorBuilder_HH__
2 #define MHO_OperatorBuilder_HH__
3 
4 #include <string>
5 #include <utility>
6 #include <vector>
7 
8 #include "MHO_ContainerStore.hh"
9 #include "MHO_FringeData.hh"
10 #include "MHO_JSONHeaderWrapper.hh"
11 #include "MHO_Message.hh"
12 #include "MHO_Operator.hh"
13 #include "MHO_OperatorToolbox.hh"
14 #include "MHO_ParameterStore.hh"
15 
16 namespace hops
17 {
18 
28 {
29 
30  public:
32  : fOperatorToolbox(toolbox), fFringeData(fdata), fContainerStore(fdata->GetContainerStore()),
33  fParameterStore(fdata->GetParameterStore()){};
34 
36  MHO_ParameterStore* pstore = nullptr)
37  : fOperatorToolbox(toolbox), fFringeData(nullptr), //ptr to fringe data object not provided
38  fContainerStore(cstore), fParameterStore(pstore){};
39 
40  virtual ~MHO_OperatorBuilder(){}; //delegate memory management to toolbox
41 
47  void SetToolbox(MHO_OperatorToolbox* toolbox) { fOperatorToolbox = toolbox; }
48 
54  void SetFringeData(MHO_FringeData* fdata) { fFringeData = fdata; }
55 
62 
69 
70  //json config for this operator (parsed from the control file and format directives)
76  void SetFormat(const mho_json& format) { fFormat = format; } //operator format
77 
83  void SetConditions(const mho_json& cond) { fConditions = cond; } //conditional statements
84 
90  void SetAttributes(const mho_json& attr) { fAttributes = attr; } //configuration parameters
91 
98  virtual bool Build() = 0;
99 
100  protected:
108  virtual bool IsConfigurationOk()
109  {
110  TODO_FIXME_MSG("TODO FIXME -- improve checks on operator attributes in IsConfigurationOk)")
111  bool ok = true;
112  //for compound statements, check that the fields are present
113  if(fFormat["statement_type"] == "operator")
114  {
115  if(fFormat.contains("type") && fFormat["type"].get< std::string >() == "compound")
116  {
117 
118  for(auto it = fFormat["fields"].begin(); it != fFormat["fields"].end(); it++)
119  {
120  if(!fAttributes["value"].contains(*it))
121  {
122  msg_error("initialization", "missing attribute called " << *it << " in parameters of operator: "
123  << fFormat["name"].get< std::string >()
124  << ", will not build " << eom);
125  return false;
126  }
127  }
128  }
129  }
130 
131  return ok;
132  }
133 
134  //constructed operator gets stashed here
136 
137  //fringe data object (holds both container/parameter store, along with plot data)
139 
140  //data container and parameters stores
143 
144  // Returns all station identifiers named in the 'if station X' condition tokens.
145  // Handles OR/AND compound conditions by collecting every 'station <X>' pair.
146  // Returns {"??"} (wildcard) when no station keyword is present.
147  std::vector< std::string > ExtractAllStationIdentifiers() const
148  {
149  std::vector< std::string > ids;
150  if(fConditions.is_null() || !fConditions.contains("value"))
151  {
152  return {"??"};
153  }
154  auto tokens = fConditions["value"].get< std::vector< std::string > >();
155  for(auto it = tokens.begin(); it != tokens.end(); ++it)
156  {
157  if(*it == "station")
158  {
159  ++it;
160  if(it != tokens.end())
161  {
162  ids.push_back(*it);
163  }
164  }
165  }
166  return ids.empty() ? std::vector< std::string >{"??"} : ids;
167  }
168 
169  // Checks whether a single station identifier (from condition tokens) matches
170  // a specific station role (reference or remote). Handles 1-char mk4 ids,
171  // 2-char station codes, and wildcards (?/??).
172  // role should be "ref" or "rem".
173  bool StationMatchesRole(const std::string& station_id, const std::string& role) const
174  {
175  if(fParameterStore == nullptr)
176  {
177  return true;
178  }
179 
180  std::string role_mk4 = fParameterStore->GetAs< std::string >(std::string("/") + role + "_station/mk4id");
181  std::string role_code = fParameterStore->GetAs< std::string >(std::string("/") + role + "_station/site_id");
182 
183  if(station_id == "??" || station_id == "?")
184  {
185  return true;
186  }
187  if(station_id.size() == 1)
188  {
189  return (station_id == role_mk4);
190  }
191  // 2-char or longer - compare against 2-char station code
192  return (station_id == role_code);
193  }
194 
195  // Checks whether a single station identifier (from condition tokens) matches
196  // either the reference or remote station of the current baseline.
197  // Handles 1-char mk4 ids, 2-char station codes, and wildcards (?/??).
198  bool StationMatchesCurrentBaseline(const std::string& station_id) const
199  {
200  return StationMatchesRole(station_id, "ref") || StationMatchesRole(station_id, "rem");
201  }
202 
203  // Filters the station identifiers extracted from the condition to only those
204  // that actually match the current baseline's ref/rem stations. This prevents
205  // a condition like "if station X or station Y" from producing an operator
206  // bound to both X and Y when only X is on the current baseline.
207  // Returns {"??"} when no station keyword is present (apply to all).
208  std::vector< std::string > GetMatchingStationIdentifiers() const
209  {
210  auto all_ids = ExtractAllStationIdentifiers();
211  std::vector< std::string > matching;
212  for(const auto& id : all_ids)
213  {
215  {
216  matching.push_back(id);
217  }
218  }
219  return matching.empty() ? std::vector< std::string >{"??"} : matching;
220  }
221 
222  //provided for the configuration of the operator that is to be built
223  mho_json fFormat; //optional
224  mho_json fConditions; //optional
225  mho_json fAttributes; //required
226 };
227 
228 } // namespace hops
229 
230 #endif
nlohmann::json mho_json
Definition: MHO_JSONHeaderWrapper.hh:5
#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_json fConditions
Definition: MHO_OperatorBuilder.hh:224
virtual ~MHO_OperatorBuilder()
Definition: MHO_OperatorBuilder.hh:40
void SetConditions(const mho_json &cond)
Setter for applicability conditions.
Definition: MHO_OperatorBuilder.hh:83
void SetFormat(const mho_json &format)
Setter for format.
Definition: MHO_OperatorBuilder.hh:76
void SetParameterStore(MHO_ParameterStore *pstore)
Setter for parameter store.
Definition: MHO_OperatorBuilder.hh:61
bool StationMatchesRole(const std::string &station_id, const std::string &role) const
Definition: MHO_OperatorBuilder.hh:173
MHO_ParameterStore * fParameterStore
Definition: MHO_OperatorBuilder.hh:142
MHO_OperatorToolbox * fOperatorToolbox
Definition: MHO_OperatorBuilder.hh:135
MHO_ContainerStore * fContainerStore
Definition: MHO_OperatorBuilder.hh:141
void SetToolbox(MHO_OperatorToolbox *toolbox)
Setter for toolbox.
Definition: MHO_OperatorBuilder.hh:47
MHO_OperatorBuilder(MHO_OperatorToolbox *toolbox, MHO_FringeData *fdata)
Definition: MHO_OperatorBuilder.hh:31
void SetAttributes(const mho_json &attr)
Setter for attributes.
Definition: MHO_OperatorBuilder.hh:90
virtual bool IsConfigurationOk()
Function IsConfigurationOk provided for derived class to validate fAttributes against fFormat and/or ...
Definition: MHO_OperatorBuilder.hh:108
mho_json fFormat
Definition: MHO_OperatorBuilder.hh:223
MHO_OperatorBuilder(MHO_OperatorToolbox *toolbox, MHO_ContainerStore *cstore=nullptr, MHO_ParameterStore *pstore=nullptr)
Definition: MHO_OperatorBuilder.hh:35
MHO_FringeData * fFringeData
Definition: MHO_OperatorBuilder.hh:138
void SetFringeData(MHO_FringeData *fdata)
Setter for fringe data.
Definition: MHO_OperatorBuilder.hh:54
virtual bool Build()=0
Builds the object and passes it to toolbox if successful, otherwise returns false.
void SetContainerStore(MHO_ContainerStore *cstore)
Setter for container store.
Definition: MHO_OperatorBuilder.hh:68
std::vector< std::string > GetMatchingStationIdentifiers() const
Definition: MHO_OperatorBuilder.hh:208
bool StationMatchesCurrentBaseline(const std::string &station_id) const
Definition: MHO_OperatorBuilder.hh:198
std::vector< std::string > ExtractAllStationIdentifiers() const
Definition: MHO_OperatorBuilder.hh:147
mho_json fAttributes
Definition: MHO_OperatorBuilder.hh:225
Class MHO_OperatorToolbox.
Definition: MHO_OperatorToolbox.hh:28
Class MHO_ParameterStore.
Definition: MHO_ParameterStore.hh:52
XValueType GetAs(const std::string &value_path) const
Function IsPresent.
Definition: MHO_ParameterStore.hh:292
Definition: MHO_AdhocFlagging.hh:18
struct token_struct * tokens
Definition: parse_control_file.c:26