Fawkes API  Fawkes Development Version
SkillerDebugInterface.cpp
1 
2 /***************************************************************************
3  * SkillerDebugInterface.cpp - Fawkes BlackBoard Interface - SkillerDebugInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2008 Tim Niemueller
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <interfaces/SkillerDebugInterface.h>
25 
26 #include <core/exceptions/software.h>
27 
28 #include <map>
29 #include <string>
30 #include <cstring>
31 #include <cstdlib>
32 
33 namespace fawkes {
34 
35 /** @class SkillerDebugInterface <interfaces/SkillerDebugInterface.h>
36  * SkillerDebugInterface Fawkes BlackBoard Interface.
37  *
38  This interface provides internal skiller data that should allow for
39  easier debugging of skills and the skiller in general. The most notable
40  feature is a graph representation in the dot language of the available
41  skills (and highlighting for the currently active skill).
42 
43  * @ingroup FawkesInterfaces
44  */
45 
46 
47 
48 /** Constructor */
49 SkillerDebugInterface::SkillerDebugInterface() : Interface()
50 {
51  data_size = sizeof(SkillerDebugInterface_data_t);
52  data_ptr = malloc(data_size);
53  data = (SkillerDebugInterface_data_t *)data_ptr;
54  data_ts = (interface_data_ts_t *)data_ptr;
55  memset(data_ptr, 0, data_size);
56  enum_map_GraphDirectionEnum[(int)GD_TOP_BOTTOM] = "GD_TOP_BOTTOM";
57  enum_map_GraphDirectionEnum[(int)GD_BOTTOM_TOP] = "GD_BOTTOM_TOP";
58  enum_map_GraphDirectionEnum[(int)GD_LEFT_RIGHT] = "GD_LEFT_RIGHT";
59  enum_map_GraphDirectionEnum[(int)GD_RIGHT_LEFT] = "GD_RIGHT_LEFT";
60  add_fieldinfo(IFT_STRING, "graph_fsm", 32, data->graph_fsm);
61  add_fieldinfo(IFT_STRING, "graph", 8192, data->graph);
62  add_fieldinfo(IFT_ENUM, "graph_dir", 1, &data->graph_dir, "GraphDirectionEnum", &enum_map_GraphDirectionEnum);
63  add_fieldinfo(IFT_BOOL, "graph_colored", 1, &data->graph_colored);
64  add_messageinfo("SetGraphMessage");
65  add_messageinfo("SetGraphDirectionMessage");
66  add_messageinfo("SetGraphColoredMessage");
67  unsigned char tmp_hash[] = {0xcf, 0x3d, 0x2f, 0xf8, 0x80, 0x6e, 0x8f, 0xf4, 0x81, 0xa6, 0x7f, 0xd9, 0xb0, 0x29, 0xfc, 0x62};
68  set_hash(tmp_hash);
69 }
70 
71 /** Destructor */
72 SkillerDebugInterface::~SkillerDebugInterface()
73 {
74  free(data_ptr);
75 }
76 /** Convert GraphDirectionEnum constant to string.
77  * @param value value to convert to string
78  * @return constant value as string.
79  */
80 const char *
81 SkillerDebugInterface::tostring_GraphDirectionEnum(GraphDirectionEnum value) const
82 {
83  switch (value) {
84  case GD_TOP_BOTTOM: return "GD_TOP_BOTTOM";
85  case GD_BOTTOM_TOP: return "GD_BOTTOM_TOP";
86  case GD_LEFT_RIGHT: return "GD_LEFT_RIGHT";
87  case GD_RIGHT_LEFT: return "GD_RIGHT_LEFT";
88  default: return "UNKNOWN";
89  }
90 }
91 /* Methods */
92 /** Get graph_fsm value.
93  *
94  The finite state machine (FSM) the current graph has been updated for.
95 
96  * @return graph_fsm value
97  */
98 char *
99 SkillerDebugInterface::graph_fsm() const
100 {
101  return data->graph_fsm;
102 }
103 
104 /** Get maximum length of graph_fsm value.
105  * @return length of graph_fsm value, can be length of the array or number of
106  * maximum number of characters for a string
107  */
108 size_t
109 SkillerDebugInterface::maxlenof_graph_fsm() const
110 {
111  return 32;
112 }
113 
114 /** Set graph_fsm value.
115  *
116  The finite state machine (FSM) the current graph has been updated for.
117 
118  * @param new_graph_fsm new graph_fsm value
119  */
120 void
121 SkillerDebugInterface::set_graph_fsm(const char * new_graph_fsm)
122 {
123  set_field(data->graph_fsm, new_graph_fsm);
124 }
125 
126 /** Get graph value.
127  *
128  The selected graph in a dot string representation.
129 
130  * @return graph value
131  */
132 char *
133 SkillerDebugInterface::graph() const
134 {
135  return data->graph;
136 }
137 
138 /** Get maximum length of graph value.
139  * @return length of graph value, can be length of the array or number of
140  * maximum number of characters for a string
141  */
142 size_t
143 SkillerDebugInterface::maxlenof_graph() const
144 {
145  return 8192;
146 }
147 
148 /** Set graph value.
149  *
150  The selected graph in a dot string representation.
151 
152  * @param new_graph new graph value
153  */
154 void
155 SkillerDebugInterface::set_graph(const char * new_graph)
156 {
157  set_field(data->graph, new_graph);
158 }
159 
160 /** Get graph_dir value.
161  *
162  Primary direction of current graph.
163 
164  * @return graph_dir value
165  */
167 SkillerDebugInterface::graph_dir() const
168 {
169  return (SkillerDebugInterface::GraphDirectionEnum)data->graph_dir;
170 }
171 
172 /** Get maximum length of graph_dir value.
173  * @return length of graph_dir value, can be length of the array or number of
174  * maximum number of characters for a string
175  */
176 size_t
177 SkillerDebugInterface::maxlenof_graph_dir() const
178 {
179  return 1;
180 }
181 
182 /** Set graph_dir value.
183  *
184  Primary direction of current graph.
185 
186  * @param new_graph_dir new graph_dir value
187  */
188 void
189 SkillerDebugInterface::set_graph_dir(const GraphDirectionEnum new_graph_dir)
190 {
191  set_field(data->graph_dir, new_graph_dir);
192 }
193 
194 /** Get graph_colored value.
195  *
196  True if the graph is colored, false otherwise.
197 
198  * @return graph_colored value
199  */
200 bool
201 SkillerDebugInterface::is_graph_colored() const
202 {
203  return data->graph_colored;
204 }
205 
206 /** Get maximum length of graph_colored value.
207  * @return length of graph_colored value, can be length of the array or number of
208  * maximum number of characters for a string
209  */
210 size_t
211 SkillerDebugInterface::maxlenof_graph_colored() const
212 {
213  return 1;
214 }
215 
216 /** Set graph_colored value.
217  *
218  True if the graph is colored, false otherwise.
219 
220  * @param new_graph_colored new graph_colored value
221  */
222 void
223 SkillerDebugInterface::set_graph_colored(const bool new_graph_colored)
224 {
225  set_field(data->graph_colored, new_graph_colored);
226 }
227 
228 /* =========== message create =========== */
229 Message *
230 SkillerDebugInterface::create_message(const char *type) const
231 {
232  if ( strncmp("SetGraphMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
233  return new SetGraphMessage();
234  } else if ( strncmp("SetGraphDirectionMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
235  return new SetGraphDirectionMessage();
236  } else if ( strncmp("SetGraphColoredMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
237  return new SetGraphColoredMessage();
238  } else {
239  throw UnknownTypeException("The given type '%s' does not match any known "
240  "message type for this interface type.", type);
241  }
242 }
243 
244 
245 /** Copy values from other interface.
246  * @param other other interface to copy values from
247  */
248 void
249 SkillerDebugInterface::copy_values(const Interface *other)
250 {
251  const SkillerDebugInterface *oi = dynamic_cast<const SkillerDebugInterface *>(other);
252  if (oi == NULL) {
253  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
254  type(), other->type());
255  }
256  memcpy(data, oi->data, sizeof(SkillerDebugInterface_data_t));
257 }
258 
259 const char *
260 SkillerDebugInterface::enum_tostring(const char *enumtype, int val) const
261 {
262  if (strcmp(enumtype, "GraphDirectionEnum") == 0) {
263  return tostring_GraphDirectionEnum((GraphDirectionEnum)val);
264  }
265  throw UnknownTypeException("Unknown enum type %s", enumtype);
266 }
267 
268 /* =========== messages =========== */
269 /** @class SkillerDebugInterface::SetGraphMessage <interfaces/SkillerDebugInterface.h>
270  * SetGraphMessage Fawkes BlackBoard Interface Message.
271  *
272 
273  */
274 
275 
276 /** Constructor with initial values.
277  * @param ini_graph_fsm initial value for graph_fsm
278  */
279 SkillerDebugInterface::SetGraphMessage::SetGraphMessage(const char * ini_graph_fsm) : Message("SetGraphMessage")
280 {
281  data_size = sizeof(SetGraphMessage_data_t);
282  data_ptr = malloc(data_size);
283  memset(data_ptr, 0, data_size);
284  data = (SetGraphMessage_data_t *)data_ptr;
286  strncpy(data->graph_fsm, ini_graph_fsm, 32-1);
287  data->graph_fsm[32-1] = 0;
288  enum_map_GraphDirectionEnum[(int)GD_TOP_BOTTOM] = "GD_TOP_BOTTOM";
289  enum_map_GraphDirectionEnum[(int)GD_BOTTOM_TOP] = "GD_BOTTOM_TOP";
290  enum_map_GraphDirectionEnum[(int)GD_LEFT_RIGHT] = "GD_LEFT_RIGHT";
291  enum_map_GraphDirectionEnum[(int)GD_RIGHT_LEFT] = "GD_RIGHT_LEFT";
292  add_fieldinfo(IFT_STRING, "graph_fsm", 32, data->graph_fsm);
293 }
294 /** Constructor */
296 {
297  data_size = sizeof(SetGraphMessage_data_t);
298  data_ptr = malloc(data_size);
299  memset(data_ptr, 0, data_size);
300  data = (SetGraphMessage_data_t *)data_ptr;
302  enum_map_GraphDirectionEnum[(int)GD_TOP_BOTTOM] = "GD_TOP_BOTTOM";
303  enum_map_GraphDirectionEnum[(int)GD_BOTTOM_TOP] = "GD_BOTTOM_TOP";
304  enum_map_GraphDirectionEnum[(int)GD_LEFT_RIGHT] = "GD_LEFT_RIGHT";
305  enum_map_GraphDirectionEnum[(int)GD_RIGHT_LEFT] = "GD_RIGHT_LEFT";
306  add_fieldinfo(IFT_STRING, "graph_fsm", 32, data->graph_fsm);
307 }
308 
309 /** Destructor */
311 {
312  free(data_ptr);
313 }
314 
315 /** Copy constructor.
316  * @param m message to copy from
317  */
319 {
320  data_size = m->data_size;
321  data_ptr = malloc(data_size);
322  memcpy(data_ptr, m->data_ptr, data_size);
323  data = (SetGraphMessage_data_t *)data_ptr;
325 }
326 
327 /* Methods */
328 /** Get graph_fsm value.
329  *
330  The finite state machine (FSM) the current graph has been updated for.
331 
332  * @return graph_fsm value
333  */
334 char *
336 {
337  return data->graph_fsm;
338 }
339 
340 /** Get maximum length of graph_fsm value.
341  * @return length of graph_fsm value, can be length of the array or number of
342  * maximum number of characters for a string
343  */
344 size_t
346 {
347  return 32;
348 }
349 
350 /** Set graph_fsm value.
351  *
352  The finite state machine (FSM) the current graph has been updated for.
353 
354  * @param new_graph_fsm new graph_fsm value
355  */
356 void
358 {
359  set_field(data->graph_fsm, new_graph_fsm);
360 }
361 
362 /** Clone this message.
363  * Produces a message of the same type as this message and copies the
364  * data to the new message.
365  * @return clone of this message
366  */
367 Message *
369 {
371 }
372 /** @class SkillerDebugInterface::SetGraphDirectionMessage <interfaces/SkillerDebugInterface.h>
373  * SetGraphDirectionMessage Fawkes BlackBoard Interface Message.
374  *
375 
376  */
377 
378 
379 /** Constructor with initial values.
380  * @param ini_graph_dir initial value for graph_dir
381  */
383 {
384  data_size = sizeof(SetGraphDirectionMessage_data_t);
385  data_ptr = malloc(data_size);
386  memset(data_ptr, 0, data_size);
387  data = (SetGraphDirectionMessage_data_t *)data_ptr;
389  data->graph_dir = ini_graph_dir;
390  enum_map_GraphDirectionEnum[(int)GD_TOP_BOTTOM] = "GD_TOP_BOTTOM";
391  enum_map_GraphDirectionEnum[(int)GD_BOTTOM_TOP] = "GD_BOTTOM_TOP";
392  enum_map_GraphDirectionEnum[(int)GD_LEFT_RIGHT] = "GD_LEFT_RIGHT";
393  enum_map_GraphDirectionEnum[(int)GD_RIGHT_LEFT] = "GD_RIGHT_LEFT";
394  add_fieldinfo(IFT_ENUM, "graph_dir", 1, &data->graph_dir, "GraphDirectionEnum", &enum_map_GraphDirectionEnum);
395 }
396 /** Constructor */
398 {
399  data_size = sizeof(SetGraphDirectionMessage_data_t);
400  data_ptr = malloc(data_size);
401  memset(data_ptr, 0, data_size);
402  data = (SetGraphDirectionMessage_data_t *)data_ptr;
404  enum_map_GraphDirectionEnum[(int)GD_TOP_BOTTOM] = "GD_TOP_BOTTOM";
405  enum_map_GraphDirectionEnum[(int)GD_BOTTOM_TOP] = "GD_BOTTOM_TOP";
406  enum_map_GraphDirectionEnum[(int)GD_LEFT_RIGHT] = "GD_LEFT_RIGHT";
407  enum_map_GraphDirectionEnum[(int)GD_RIGHT_LEFT] = "GD_RIGHT_LEFT";
408  add_fieldinfo(IFT_ENUM, "graph_dir", 1, &data->graph_dir, "GraphDirectionEnum", &enum_map_GraphDirectionEnum);
409 }
410 
411 /** Destructor */
413 {
414  free(data_ptr);
415 }
416 
417 /** Copy constructor.
418  * @param m message to copy from
419  */
421 {
422  data_size = m->data_size;
423  data_ptr = malloc(data_size);
424  memcpy(data_ptr, m->data_ptr, data_size);
425  data = (SetGraphDirectionMessage_data_t *)data_ptr;
427 }
428 
429 /* Methods */
430 /** Get graph_dir value.
431  *
432  Primary direction of current graph.
433 
434  * @return graph_dir value
435  */
438 {
439  return (SkillerDebugInterface::GraphDirectionEnum)data->graph_dir;
440 }
441 
442 /** Get maximum length of graph_dir value.
443  * @return length of graph_dir value, can be length of the array or number of
444  * maximum number of characters for a string
445  */
446 size_t
448 {
449  return 1;
450 }
451 
452 /** Set graph_dir value.
453  *
454  Primary direction of current graph.
455 
456  * @param new_graph_dir new graph_dir value
457  */
458 void
460 {
461  set_field(data->graph_dir, new_graph_dir);
462 }
463 
464 /** Clone this message.
465  * Produces a message of the same type as this message and copies the
466  * data to the new message.
467  * @return clone of this message
468  */
469 Message *
471 {
473 }
474 /** @class SkillerDebugInterface::SetGraphColoredMessage <interfaces/SkillerDebugInterface.h>
475  * SetGraphColoredMessage Fawkes BlackBoard Interface Message.
476  *
477 
478  */
479 
480 
481 /** Constructor with initial values.
482  * @param ini_graph_colored initial value for graph_colored
483  */
484 SkillerDebugInterface::SetGraphColoredMessage::SetGraphColoredMessage(const bool ini_graph_colored) : Message("SetGraphColoredMessage")
485 {
486  data_size = sizeof(SetGraphColoredMessage_data_t);
487  data_ptr = malloc(data_size);
488  memset(data_ptr, 0, data_size);
489  data = (SetGraphColoredMessage_data_t *)data_ptr;
491  data->graph_colored = ini_graph_colored;
492  enum_map_GraphDirectionEnum[(int)GD_TOP_BOTTOM] = "GD_TOP_BOTTOM";
493  enum_map_GraphDirectionEnum[(int)GD_BOTTOM_TOP] = "GD_BOTTOM_TOP";
494  enum_map_GraphDirectionEnum[(int)GD_LEFT_RIGHT] = "GD_LEFT_RIGHT";
495  enum_map_GraphDirectionEnum[(int)GD_RIGHT_LEFT] = "GD_RIGHT_LEFT";
496  add_fieldinfo(IFT_BOOL, "graph_colored", 1, &data->graph_colored);
497 }
498 /** Constructor */
500 {
501  data_size = sizeof(SetGraphColoredMessage_data_t);
502  data_ptr = malloc(data_size);
503  memset(data_ptr, 0, data_size);
504  data = (SetGraphColoredMessage_data_t *)data_ptr;
506  enum_map_GraphDirectionEnum[(int)GD_TOP_BOTTOM] = "GD_TOP_BOTTOM";
507  enum_map_GraphDirectionEnum[(int)GD_BOTTOM_TOP] = "GD_BOTTOM_TOP";
508  enum_map_GraphDirectionEnum[(int)GD_LEFT_RIGHT] = "GD_LEFT_RIGHT";
509  enum_map_GraphDirectionEnum[(int)GD_RIGHT_LEFT] = "GD_RIGHT_LEFT";
510  add_fieldinfo(IFT_BOOL, "graph_colored", 1, &data->graph_colored);
511 }
512 
513 /** Destructor */
515 {
516  free(data_ptr);
517 }
518 
519 /** Copy constructor.
520  * @param m message to copy from
521  */
523 {
524  data_size = m->data_size;
525  data_ptr = malloc(data_size);
526  memcpy(data_ptr, m->data_ptr, data_size);
527  data = (SetGraphColoredMessage_data_t *)data_ptr;
529 }
530 
531 /* Methods */
532 /** Get graph_colored value.
533  *
534  True if the graph is colored, false otherwise.
535 
536  * @return graph_colored value
537  */
538 bool
540 {
541  return data->graph_colored;
542 }
543 
544 /** Get maximum length of graph_colored value.
545  * @return length of graph_colored value, can be length of the array or number of
546  * maximum number of characters for a string
547  */
548 size_t
550 {
551  return 1;
552 }
553 
554 /** Set graph_colored value.
555  *
556  True if the graph is colored, false otherwise.
557 
558  * @param new_graph_colored new graph_colored value
559  */
560 void
562 {
563  set_field(data->graph_colored, new_graph_colored);
564 }
565 
566 /** Clone this message.
567  * Produces a message of the same type as this message and copies the
568  * data to the new message.
569  * @return clone of this message
570  */
571 Message *
573 {
575 }
576 /** Check if message is valid and can be enqueued.
577  * @param message Message to check
578  * @return true if the message is valid, false otherwise.
579  */
580 bool
582 {
583  const SetGraphMessage *m0 = dynamic_cast<const SetGraphMessage *>(message);
584  if ( m0 != NULL ) {
585  return true;
586  }
587  const SetGraphDirectionMessage *m1 = dynamic_cast<const SetGraphDirectionMessage *>(message);
588  if ( m1 != NULL ) {
589  return true;
590  }
591  const SetGraphColoredMessage *m2 = dynamic_cast<const SetGraphColoredMessage *>(message);
592  if ( m2 != NULL ) {
593  return true;
594  }
595  return false;
596 }
597 
598 /// @cond INTERNALS
599 EXPORT_INTERFACE(SkillerDebugInterface)
600 /// @endcond
601 
602 
603 } // end namespace fawkes
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
const char * type() const
Get type of interface.
Definition: interface.cpp:652
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:244
void set_field(FieldT &field, DataT &data)
Set a field, set data_changed to true and update data_changed accordingly.
Definition: interface.h:304
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
void add_fieldinfo(interface_fieldtype_t type, const char *name, size_t length, void *value, const char *enumtype=0, const interface_enum_map_t *enum_map=0)
Add an entry to the info list.
Definition: message.cpp:435
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:146
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:156
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:147
SetGraphColoredMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_graph_colored() const
Get maximum length of graph_colored value.
void set_graph_colored(const bool new_graph_colored)
Set graph_colored value.
virtual Message * clone() const
Clone this message.
SetGraphDirectionMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_graph_dir() const
Get maximum length of graph_dir value.
GraphDirectionEnum graph_dir() const
Get graph_dir value.
void set_graph_dir(const GraphDirectionEnum new_graph_dir)
Set graph_dir value.
virtual Message * clone() const
Clone this message.
SetGraphMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_graph_fsm() const
Get maximum length of graph_fsm value.
void set_graph_fsm(const char *new_graph_fsm)
Set graph_fsm value.
virtual Message * clone() const
Clone this message.
SkillerDebugInterface Fawkes BlackBoard Interface.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
GraphDirectionEnum
Primary direction of the graph.
Fawkes library namespace.
@ IFT_STRING
string field
Definition: types.h:48
@ IFT_BOOL
boolean field
Definition: types.h:37
@ IFT_ENUM
field with interface specific enum type
Definition: types.h:50
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:152