iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NetworkFileReader.H
Go to the documentation of this file.
1 /*! @file
2  @author Unknown
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 #include <fstream>
35 #include <yaml-cpp/yaml.h>
36 #include <nrt/Core/Debugging/Log.H>
37 namespace nrt
38 {
39  namespace networkfilereader
40  {
41  // ######################################################################
42  struct internalpostingtopicfilter
43  {
44  std::string portname;
45  std::string topicfilter;
46  std::string messagetype;
47  std::string returntype;
48  };
49 
50  // ######################################################################
51  struct internalcheckingtopic
52  {
53  std::string portname;
54  std::string topic;
55  std::string messagetype;
56  std::string returntype;
57  };
58 
59  // ######################################################################
60  struct internalsubscriptiontopic
61  {
62  std::string portname;
63  std::string topic;
64  std::string messagetype;
65  std::string returntype;
66  };
67 
68  // ######################################################################
69  struct postingtopic
70  {
71  std::string portname;
72  std::string topic;
73  };
74 
75  // ######################################################################
76  struct subscriptiontopicfilter
77  {
78  std::string portname;
79  std::string topicfilter;
80  };
81 
82  // ######################################################################
83  struct checkingtopicfilter
84  {
85  std::string portname;
86  std::string topicfilter;
87  };
88 
89  // ######################################################################
90  struct parameter
91  {
92  std::string name;
93  std::string value;
94  };
95 
96  struct position
97  {
98  float x;
99  float y;
100  };
101 
102  // ######################################################################
103  struct module
104  {
105  std::string instancename;
106  std::string logicalpath;
107  std::string bbnick;
108  std::vector<parameter> parameters;
109  std::vector<postingtopic> postingtopics;
110  std::vector<subscriptiontopicfilter> subscriptiontopicfilters;
111  std::vector<checkingtopicfilter> checkingtopicfilters;
112  position pos;
113  };
114 
115  // ######################################################################
116  struct macromoduleinclude
117  {
118  std::string path;
119  std::string name;
120  std::vector<postingtopic> postingtopics;
121  std::vector<subscriptiontopicfilter> subscriptiontopicfilters;
122  std::vector<checkingtopicfilter> checkingtopicfilters;
123  position pos;
124  };
125 
126  // ######################################################################
127  struct macromodule
128  {
129  std::string name;
130  std::vector<module> modules;
131  std::vector<internalpostingtopicfilter> postingtopicfilters;
132  std::vector<internalcheckingtopic> checkingtopics;
133  std::vector<internalsubscriptiontopic> subscriptiontopics;
134  std::vector<macromoduleinclude> macromodules;
135  };
136 
137  // ######################################################################
138  struct networkfile
139  {
140  macromodule mm;
141  };
142 
143  // ######################################################################
144  // ######################################################################
145  // ######################################################################
146  void operator >> (const YAML::Node& node, internalpostingtopicfilter& ipf)
147  {
148  node["topicfilter"] >> ipf.topicfilter;
149  node["messagetype"] >> ipf.messagetype;
150  node["returntype"] >> ipf.returntype;
151  };
152 
153  // ######################################################################
154  void operator >> (const YAML::Node& node, internalcheckingtopic& ict)
155  {
156  node["topic"] >> ict.topic;
157  node["messagetype"] >> ict.messagetype;
158  node["returntype"] >> ict.returntype;
159  };
160 
161  // ######################################################################
162  void operator >> (const YAML::Node& node, internalsubscriptiontopic& ist)
163  {
164  node["topic"] >> ist.topic;
165  node["messagetype"] >> ist.messagetype;
166  node["returntype"] >> ist.returntype;
167  };
168 
169  // ######################################################################
170  void operator >> (const YAML::Node& node, position& p)
171  {
172  node["x"] >> p.x;
173  node["y"] >> p.y;
174  }
175 
176  // ######################################################################
177  void operator >> (const YAML::Node& node, macromoduleinclude& m)
178  {
179  if (node.FindValue("name"))
180  {
181  node["name"] >> m.name;
182  }
183  else
184  {
185  m.name = "";
186  }
187 
188  if (node.FindValue("postingtopics"))
189  {
190  const YAML::Node& postingtopics = node["postingtopics"];
191  for (YAML::Iterator it=postingtopics.begin(); it!=postingtopics.end(); ++it)
192  {
193  postingtopic t;
194  it->begin().first() >> t.portname;
195  it->begin().second() >> t.topic;
196  m.postingtopics.push_back(t);
197  }
198  }
199 
200  if (node.FindValue("subscriptiontopicfilters"))
201  {
202  const YAML::Node& subscriptiontopicfilters = node["subscriptiontopicfilters"];
203  for (YAML::Iterator it=subscriptiontopicfilters.begin();
204  it!=subscriptiontopicfilters.end(); ++it)
205  {
206  subscriptiontopicfilter t;
207  it->begin().first() >> t.portname;
208  it->begin().second() >> t.topicfilter;
209  m.subscriptiontopicfilters.push_back(t);
210  }
211  }
212 
213  if (node.FindValue("checkingtopicfilters"))
214  {
215  const YAML::Node& checkingtopicfilters = node["checkingtopicfilters"];
216  for (YAML::Iterator it=checkingtopicfilters.begin();
217  it!=checkingtopicfilters.end(); ++it)
218  {
219  checkingtopicfilter t;
220  it->begin().first() >> t.portname;
221  it->begin().second() >> t.topicfilter;
222  m.checkingtopicfilters.push_back(t);
223  }
224  }
225 
226  if (node.FindValue("position"))
227  {
228  const YAML::Node& position = node["position"];
229  position >> m.pos;
230  }
231  }
232 
233 
234  // ######################################################################
235  void operator >> (const YAML::Node& node, module& m)
236  {
237  node["instancename"] >> m.instancename;
238  node["logicalpath"] >> m.logicalpath;
239  node["bbnick"] >> m.bbnick;
240 
241  if (node.FindValue("parameters"))
242  {
243  const YAML::Node& parameters = node["parameters"];
244 
245  for (YAML::Iterator it=parameters.begin(); it!=parameters.end(); ++it)
246  {
247  parameter p;
248  it->begin().first() >> p.name;
249  it->begin().second() >> p.value;
250  m.parameters.push_back(p);
251  }
252  }
253 
254  if (node.FindValue("postingtopics"))
255  {
256  const YAML::Node& postingtopics = node["postingtopics"];
257  for (YAML::Iterator it=postingtopics.begin(); it!=postingtopics.end(); ++it)
258  {
259  postingtopic t;
260  it->begin().first() >> t.portname;
261  it->begin().second() >> t.topic;
262  m.postingtopics.push_back(t);
263  }
264  }
265 
266  if (node.FindValue("subscriptiontopicfilters"))
267  {
268  const YAML::Node& subscriptiontopicfilters = node["subscriptiontopicfilters"];
269  for (YAML::Iterator it=subscriptiontopicfilters.begin();
270  it!=subscriptiontopicfilters.end(); ++it)
271  {
272  subscriptiontopicfilter t;
273  it->begin().first() >> t.portname;
274  it->begin().second() >> t.topicfilter;
275  m.subscriptiontopicfilters.push_back(t);
276  }
277  }
278 
279  if (node.FindValue("checkingtopicfilters"))
280  {
281  const YAML::Node& checkingtopicfilters = node["checkingtopicfilters"];
282  for (YAML::Iterator it=checkingtopicfilters.begin();
283  it!=checkingtopicfilters.end(); ++it)
284  {
285  checkingtopicfilter t;
286  it->begin().first() >> t.portname;
287  it->begin().second() >> t.topicfilter;
288  m.checkingtopicfilters.push_back(t);
289  }
290  }
291 
292  if (node.FindValue("position"))
293  {
294  const YAML::Node& position = node["position"];
295  position >> m.pos;
296  }
297  }
298 
299  // ######################################################################
300  void operator >> (const YAML::Node& node, macromodule& mm)
301  {
302  node["name"] >> mm.name;
303 
304  if(node.FindValue("modules"))
305  {
306  const YAML::Node& modules= node["modules"];
307  for(size_t i=0; i<modules.size(); i++)
308  {
309  module mod;
310  modules[i] >> mod;
311  mm.modules.push_back(mod);
312  }
313  }
314 
315  if(node.FindValue("macromodules"))
316  {
317  const YAML::Node& macromodules = node["macromodules"];
318  for(YAML::Iterator it=macromodules.begin(); it != macromodules.end(); ++it)
319  {
320  macromoduleinclude mmi;
321  it->begin().first() >> mmi.path;
322  it->begin().second() >> mmi;
323  mm.macromodules.push_back(mmi);
324  }
325  }
326 
327  if(node.FindValue("subscriptiontopics"))
328  {
329  const YAML::Node& subtopics = node["subscriptiontopics"];
330  for(YAML::Iterator it=subtopics.begin(); it != subtopics.end(); ++it)
331  {
332  internalsubscriptiontopic ist;
333  it->begin().first() >> ist.portname;
334  it->begin().second() >> ist;
335  mm.subscriptiontopics.push_back(ist);
336  }
337  }
338 
339  if(node.FindValue("checkingtopics"))
340  {
341  const YAML::Node& chktopics = node["checkingtopics"];
342  for(YAML::Iterator it=chktopics.begin(); it != chktopics.end(); ++it)
343  {
344  internalcheckingtopic ict;
345  it->begin().first() >> ict.portname;
346  it->begin().second() >> ict;
347  mm.checkingtopics.push_back(ict);
348  }
349  }
350 
351  if(node.FindValue("postingtopicfilters"))
352  {
353  const YAML::Node& postfilters= node["postingtopicfilters"];
354  for(YAML::Iterator it=postfilters.begin(); it != postfilters.end(); ++it)
355  {
356  internalpostingtopicfilter ipf;
357  it->begin().first() >> ipf.portname;
358  it->begin().second() >> ipf;
359  mm.postingtopicfilters.push_back(ipf);
360  }
361  }
362  }
363 
364  // ######################################################################
365  void operator >> (const YAML::Node& node, networkfile& n)
366  {
367  const YAML::Node& mm = node["macromodule"];
368  mm >> n.mm;
369  }
370 
371  } // yamlLoader
372 } // nrt