otsdaq  v2_05_02_indev
IterateTable_table.cc
1 #include "otsdaq/ConfigurationInterface/ConfigurationManager.h"
2 #include "otsdaq/Macros/TablePluginMacros.h"
3 #include "otsdaq/TablePlugins/IterateTable.h"
4 
5 #include <iostream>
6 #include <string>
7 
8 using namespace ots;
9 
10 // instantiate static members
11 
12 const std::string IterateTable::COMMAND_BEGIN_LABEL = "BEGIN_LABEL";
13 const std::string IterateTable::COMMAND_CHOOSE_FSM = "CHOOSE_FSM";
14 const std::string IterateTable::COMMAND_CONFIGURE_ACTIVE_GROUP = "CONFIGURE_ACTIVE_GROUP";
15 const std::string IterateTable::COMMAND_CONFIGURE_ALIAS = "CONFIGURE_ALIAS";
16 const std::string IterateTable::COMMAND_CONFIGURE_GROUP = "CONFIGURE_GROUP";
17 const std::string IterateTable::COMMAND_EXECUTE_FE_MACRO = "EXECUTE_FE_MACRO";
18 const std::string IterateTable::COMMAND_EXECUTE_MACRO = "EXECUTE_MACRO";
19 const std::string IterateTable::COMMAND_MODIFY_ACTIVE_GROUP = "MODIFY_ACTIVE_GROUP";
20 const std::string IterateTable::COMMAND_REPEAT_LABEL = "REPEAT_LABEL";
21 const std::string IterateTable::COMMAND_RUN = "RUN";
22 
23 const std::string IterateTable::ITERATE_TABLE = "IterateTable";
24 const std::string IterateTable::PLAN_TABLE = "IterationPlanTable";
25 const std::string IterateTable::TARGET_TABLE = "IterationTargetTable";
26 
27 const std::map<std::string, std::string> IterateTable::commandToTableMap_ = IterateTable::createCommandToTableMap();
28 
29 IterateTable::PlanTableColumns IterateTable::planTableCols_;
30 IterateTable::IterateTableColumns IterateTable::iterateTableCols_;
31 
32 IterateTable::CommandBeginLabelParams IterateTable::commandBeginLabelParams_;
33 IterateTable::CommandConfigureActiveParams IterateTable::commandConfigureActiveParams_;
34 IterateTable::CommandConfigureAliasParams IterateTable::commandConfigureAliasParams_;
35 IterateTable::CommandConfigureGroupParams IterateTable::commandConfigureGroupParams_;
36 IterateTable::CommandExecuteMacroParams IterateTable::commandExecuteMacroParams_;
37 IterateTable::CommandModifyActiveParams IterateTable::commandModifyActiveParams_;
38 IterateTable::CommandRepeatLabelParams IterateTable::commandRepeatLabelParams_;
39 IterateTable::CommandRunParams IterateTable::commandRunParams_;
40 IterateTable::CommandChooseFSMParams IterateTable::commandChooseFSMParams_;
41 
42 IterateTable::TargetParams IterateTable::targetParams_;
43 IterateTable::TargetTableColumns IterateTable::targetCols_;
44 IterateTable::CommandTargetColumns IterateTable::commandTargetCols_;
45 
46 IterateTable::MacroDimLoopTableColumns IterateTable::macroDimLoopCols_;
47 IterateTable::MacroParamTableColumns IterateTable::macroParamCols_;
48 
49 //==============================================================================
50 IterateTable::IterateTable(void) : TableBase(IterateTable::ITERATE_TABLE) {}
51 
52 //==============================================================================
53 IterateTable::~IterateTable(void) {}
54 
55 //==============================================================================
56 void IterateTable::init(ConfigurationManager* /*configManager*/)
57 {
58  // do something to validate or refactor table
59  // __COUT__ << "*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*" << std::endl;
60  // __COUT__ << configManager->__SELF_NODE__ << std::endl;
61 
62  /*
63  std::string value;
64  auto childrenMap = configManager->__SELF_NODE__.getChildren();
65  for(auto& childPair : childrenMap)
66  {
67  // do something for each row in table
68  //__COUT__ << childPair.first << std::endl;
69  // __COUT__ << childPair.second.getNode(colNames_.colColumnName_) <<
70  // std::endl; childPair.second.getNode(colNames_.colColumnName_
71  // ).getValue(value);
72  }
73  */
74 }
75 
76 //==============================================================================
77 std::vector<IterateTable::Command> IterateTable::getPlanCommands(ConfigurationManager* configManager, const std::string& plan) const
78 {
79  __COUT__ << configManager->__SELF_NODE__ << std::endl;
80 
81  ConfigurationTree planNode = configManager->__SELF_NODE__.getNode(plan);
82 
83  if(!planNode.getNode(IterateTable::planTableCols_.Status_).getValue<bool>())
84  {
85  __SS__ << "Error! Attempt to access disabled plan (Status=FALSE)." << std::endl;
86  __COUT_ERR__ << ss.str();
87  __SS_THROW__;
88  }
89 
90  std::vector<IterateTable::Command> commands;
91 
92  auto commandChildren = planNode.getNode(IterateTable::iterateTableCols_.PlanLink_).getChildren();
93 
94  for(auto& commandChild : commandChildren)
95  {
96  __COUT__ << "Command \t" << commandChild.first << std::endl;
97 
98  __COUT__ << "\t\tStatus \t" << commandChild.second.getNode(IterateTable::planTableCols_.Status_) << std::endl;
99 
100  __COUT__ << "\t\tType \t" << commandChild.second.getNode(IterateTable::planTableCols_.CommandType_) << std::endl;
101 
102  if(!commandChild.second.getNode(IterateTable::planTableCols_.Status_).getValue<bool>())
103  continue; // skip disabled commands
104 
105  commands.push_back(IterateTable::Command());
106  commands.back().type_ = commandChild.second.getNode(IterateTable::planTableCols_.CommandType_).getValue<std::string>();
107 
108  if(commandChild.second.getNode(IterateTable::planTableCols_.CommandLink_).isDisconnected())
109  continue; // skip if no command parameters
110 
111  auto commandSpecificFields = commandChild.second.getNode(IterateTable::planTableCols_.CommandLink_).getChildren();
112 
113  for(unsigned int i = 0; i < commandSpecificFields.size() - 3; ++i) // ignore last three columns
114  {
115  // NOTE -- that links turn into one field with value LinkID/GroupID unless
116  // specially handled
117 
118  __COUT__ << "\t\tParameter \t" << commandSpecificFields[i].first << " = \t" << commandSpecificFields[i].second << std::endl;
119 
120  if(commandSpecificFields[i].first == IterateTable::commandTargetCols_.TargetsLink_)
121  {
122  __COUT__ << "Extracting targets..." << __E__;
123  auto targets = commandSpecificFields[i].second.getChildren();
124 
125  __COUTV__(targets.size());
126 
127  for(auto& target : targets)
128  {
129  __COUT__ << "\t\t\tTarget \t" << target.first << __E__;
130 
131  ConfigurationTree targetNode = target.second.getNode(IterateTable::targetCols_.TargetLink_);
132  if(targetNode.isDisconnected())
133  {
134  __COUT_ERR__ << "Disconnected target!?" << __E__;
135  continue;
136  }
137 
138  __COUT__ << "\t\t = \t"
139  << "Table:" << targetNode.getTableName() << " UID:" << targetNode.getUIDAsString() << std::endl;
140  commands.back().addTarget();
141  commands.back().targets_.back().table_ = targetNode.getTableName();
142  commands.back().targets_.back().UID_ = targetNode.getUIDAsString();
143  }
144  }
145  else if(commandSpecificFields[i].first == IterateTable::commandExecuteMacroParams_.MacroParameterLink_)
146  {
147  // get Macro parameters, place them in params_
148  __COUT__ << "Extracting macro parameters..." << __E__;
149 
150  // need to extract input arguments
151  // by dimension (by priority)
152  //
153  // two vector by dimension <map of params>
154  // one vector for long and for double
155  //
156  // map of params :=
157  // name => {
158  // <long/double current value>
159  // <long/double init value>
160  // <long/double step size>
161  // }
162 
163  auto dimensionalLoops = commandSpecificFields[i].second.getChildren(
164  std::map<std::string /*relative-path*/, std::string /*value*/>() /*no filter*/, true /*by Priority*/);
165 
166  __COUTV__(dimensionalLoops.size());
167 
168  // inputs:
169  // - inputArgs: dimensional semi-colon-separated,
170  // comma separated: dimension iterations and arguments
171  //(colon-separated name/value/stepsize sets)
172  std::string argStr = "";
173  // inputArgsStr = "3;3,myOtherArg:5:2"; //example
174 
175  // std::string name, value;
176  unsigned long numberOfIterations;
177  bool firstDimension = true;
178 
179  for(auto& dimensionalLoop : dimensionalLoops)
180  {
181  __COUT__ << "\t\t\tDimensionalLoop \t" << dimensionalLoop.first << __E__;
182 
183  numberOfIterations = dimensionalLoop.second.getNode(IterateTable::macroDimLoopCols_.NumberOfIterations_).getValue<unsigned long>();
184 
185  __COUTV__(numberOfIterations);
186 
187  if(numberOfIterations == 0)
188  {
189  __SS__ << "Illegal number of iterations value of '" << numberOfIterations << ".' Must be a positive integer!" << __E__;
190  __SS_THROW__;
191  }
192 
193  // at this point add dimension parameter with value numberOfIterations
194 
195  if(!firstDimension)
196  argStr += ";";
197  firstDimension = false;
198  argStr += std::to_string(numberOfIterations);
199 
200  auto paramLinkNode = dimensionalLoop.second.getNode(IterateTable::macroDimLoopCols_.ParamLink_);
201 
202  if(paramLinkNode.isDisconnected())
203  {
204  __COUT__ << "Disconnected parameter link, so no parameters for "
205  "this dimension."
206  << __E__;
207  continue;
208  }
209 
210  auto macroParams = paramLinkNode.getChildren();
211 
212  __COUTV__(macroParams.size());
213 
214  for(auto& macroParam : macroParams)
215  {
216  __COUT__ << "\t\t\tParam \t" << macroParam.first << __E__;
217 
218  // add parameter name:value:step
219 
220  argStr += ",";
221  argStr += macroParam.second.getNode(IterateTable::macroParamCols_.Name_).getValue<std::string>();
222  argStr += ":";
223  argStr += macroParam.second.getNode(IterateTable::macroParamCols_.Value_).getValue<std::string>();
224  argStr += ":";
225  argStr += macroParam.second.getNode(IterateTable::macroParamCols_.Step_).getValue<std::string>();
226 
227  } // end parameter loop
228  } // end dimension loop
229 
230  // Macro argument string is done
231  __COUTV__(argStr);
232 
233  // assume no conflict with fixed parameters in map
234  // because of prepend
235  // IterateTable::commandExecuteMacroParams_.MacroParameterPrepend_
236  //+
237  commands.back().params_.emplace(
238  std::pair<std::string /*param name*/, std::string /*param value*/>(IterateTable::commandExecuteMacroParams_.MacroArgumentString_, argStr));
239  }
240  else // all other non-special fields
241  {
242  if( // bool type, convert to 1 or 0
243  commandSpecificFields[i].second.isValueBoolType())
244  commands.back().params_.emplace(std::pair<std::string /*param name*/, std::string /*param value*/>(
245  commandSpecificFields[i].first, commandSpecificFields[i].second.getValue<bool>() ? "1" : "0"));
246  else if( // number data type, get raw value string (note: does not do
247  // math or variable substitution)
248  commandSpecificFields[i].second.isValueNumberDataType())
249  commands.back().params_.emplace(std::pair<std::string /*param name*/, std::string /*param value*/>(
250  commandSpecificFields[i].first, commandSpecificFields[i].second.getValueAsString()));
251  else
252  commands.back().params_.emplace(std::pair<std::string /*param name*/, std::string /*param value*/>(
253  commandSpecificFields[i].first, commandSpecificFields[i].second.getValue<std::string>()));
254  }
255  }
256  }
257 
258  return commands;
259 }
260 
261 DEFINE_OTS_TABLE(IterateTable)