SkillerDebugInterface.cpp

00001 
00002 /***************************************************************************
00003  *  SkillerDebugInterface.cpp - Fawkes BlackBoard Interface - SkillerDebugInterface
00004  *
00005  *  Templated created:   Thu Oct 12 10:49:19 2006
00006  *  Copyright  2008  Tim Niemueller
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <interfaces/SkillerDebugInterface.h>
00025 
00026 #include <core/exceptions/software.h>
00027 
00028 #include <cstring>
00029 #include <cstdlib>
00030 
00031 namespace fawkes {
00032 
00033 /** @class SkillerDebugInterface <interfaces/SkillerDebugInterface.h>
00034  * SkillerDebugInterface Fawkes BlackBoard Interface.
00035  * 
00036       This interface provides internal skiller data that should allow for
00037       easier debugging of skills and the skiller in general. The most notable
00038       feature is a graph representation in the dot language of the available
00039       skills (and highlighting for the currently active skill).
00040     
00041  * @ingroup FawkesInterfaces
00042  */
00043 
00044 
00045 
00046 /** Constructor */
00047 SkillerDebugInterface::SkillerDebugInterface() : Interface()
00048 {
00049   data_size = sizeof(SkillerDebugInterface_data_t);
00050   data_ptr  = malloc(data_size);
00051   data      = (SkillerDebugInterface_data_t *)data_ptr;
00052   data_ts   = (interface_data_ts_t *)data_ptr;
00053   memset(data_ptr, 0, data_size);
00054   add_fieldinfo(IFT_STRING, "graph_fsm", 32, data->graph_fsm);
00055   add_fieldinfo(IFT_STRING, "graph", 8192, data->graph);
00056   add_fieldinfo(IFT_ENUM, "graph_dir", 1, &data->graph_dir, "GraphDirectionEnum");
00057   add_fieldinfo(IFT_BOOL, "graph_colored", 1, &data->graph_colored);
00058   add_messageinfo("SetGraphMessage");
00059   add_messageinfo("SetGraphDirectionMessage");
00060   add_messageinfo("SetGraphColoredMessage");
00061   unsigned char tmp_hash[] = {0xcf, 0x3d, 0x2f, 0xf8, 0x80, 0x6e, 0x8f, 0xf4, 0x81, 0xa6, 0x7f, 0xd9, 0xb0, 0x29, 0xfc, 0x62};
00062   set_hash(tmp_hash);
00063 }
00064 
00065 /** Destructor */
00066 SkillerDebugInterface::~SkillerDebugInterface()
00067 {
00068   free(data_ptr);
00069 }
00070 /** Convert GraphDirectionEnum constant to string.
00071  * @param value value to convert to string
00072  * @return constant value as string.
00073  */
00074 const char *
00075 SkillerDebugInterface::tostring_GraphDirectionEnum(GraphDirectionEnum value) const
00076 {
00077   switch (value) {
00078   case GD_TOP_BOTTOM: return "GD_TOP_BOTTOM";
00079   case GD_BOTTOM_TOP: return "GD_BOTTOM_TOP";
00080   case GD_LEFT_RIGHT: return "GD_LEFT_RIGHT";
00081   case GD_RIGHT_LEFT: return "GD_RIGHT_LEFT";
00082   default: return "UNKNOWN";
00083   }
00084 }
00085 /* Methods */
00086 /** Get graph_fsm value.
00087  * 
00088       The finite state machine (FSM) the current graph has been updated for.
00089     
00090  * @return graph_fsm value
00091  */
00092 char *
00093 SkillerDebugInterface::graph_fsm() const
00094 {
00095   return data->graph_fsm;
00096 }
00097 
00098 /** Get maximum length of graph_fsm value.
00099  * @return length of graph_fsm value, can be length of the array or number of 
00100  * maximum number of characters for a string
00101  */
00102 size_t
00103 SkillerDebugInterface::maxlenof_graph_fsm() const
00104 {
00105   return 32;
00106 }
00107 
00108 /** Set graph_fsm value.
00109  * 
00110       The finite state machine (FSM) the current graph has been updated for.
00111     
00112  * @param new_graph_fsm new graph_fsm value
00113  */
00114 void
00115 SkillerDebugInterface::set_graph_fsm(const char * new_graph_fsm)
00116 {
00117   strncpy(data->graph_fsm, new_graph_fsm, sizeof(data->graph_fsm));
00118   data_changed = true;
00119 }
00120 
00121 /** Get graph value.
00122  * 
00123       The selected graph in a dot string representation.
00124     
00125  * @return graph value
00126  */
00127 char *
00128 SkillerDebugInterface::graph() const
00129 {
00130   return data->graph;
00131 }
00132 
00133 /** Get maximum length of graph value.
00134  * @return length of graph value, can be length of the array or number of 
00135  * maximum number of characters for a string
00136  */
00137 size_t
00138 SkillerDebugInterface::maxlenof_graph() const
00139 {
00140   return 8192;
00141 }
00142 
00143 /** Set graph value.
00144  * 
00145       The selected graph in a dot string representation.
00146     
00147  * @param new_graph new graph value
00148  */
00149 void
00150 SkillerDebugInterface::set_graph(const char * new_graph)
00151 {
00152   strncpy(data->graph, new_graph, sizeof(data->graph));
00153   data_changed = true;
00154 }
00155 
00156 /** Get graph_dir value.
00157  * 
00158       Primary direction of current graph.
00159     
00160  * @return graph_dir value
00161  */
00162 SkillerDebugInterface::GraphDirectionEnum
00163 SkillerDebugInterface::graph_dir() const
00164 {
00165   return data->graph_dir;
00166 }
00167 
00168 /** Get maximum length of graph_dir value.
00169  * @return length of graph_dir value, can be length of the array or number of 
00170  * maximum number of characters for a string
00171  */
00172 size_t
00173 SkillerDebugInterface::maxlenof_graph_dir() const
00174 {
00175   return 1;
00176 }
00177 
00178 /** Set graph_dir value.
00179  * 
00180       Primary direction of current graph.
00181     
00182  * @param new_graph_dir new graph_dir value
00183  */
00184 void
00185 SkillerDebugInterface::set_graph_dir(const GraphDirectionEnum new_graph_dir)
00186 {
00187   data->graph_dir = new_graph_dir;
00188   data_changed = true;
00189 }
00190 
00191 /** Get graph_colored value.
00192  * 
00193       True if the graph is colored, false otherwise.
00194     
00195  * @return graph_colored value
00196  */
00197 bool
00198 SkillerDebugInterface::is_graph_colored() const
00199 {
00200   return data->graph_colored;
00201 }
00202 
00203 /** Get maximum length of graph_colored value.
00204  * @return length of graph_colored value, can be length of the array or number of 
00205  * maximum number of characters for a string
00206  */
00207 size_t
00208 SkillerDebugInterface::maxlenof_graph_colored() const
00209 {
00210   return 1;
00211 }
00212 
00213 /** Set graph_colored value.
00214  * 
00215       True if the graph is colored, false otherwise.
00216     
00217  * @param new_graph_colored new graph_colored value
00218  */
00219 void
00220 SkillerDebugInterface::set_graph_colored(const bool new_graph_colored)
00221 {
00222   data->graph_colored = new_graph_colored;
00223   data_changed = true;
00224 }
00225 
00226 /* =========== message create =========== */
00227 Message *
00228 SkillerDebugInterface::create_message(const char *type) const
00229 {
00230   if ( strncmp("SetGraphMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00231     return new SetGraphMessage();
00232   } else if ( strncmp("SetGraphDirectionMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00233     return new SetGraphDirectionMessage();
00234   } else if ( strncmp("SetGraphColoredMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00235     return new SetGraphColoredMessage();
00236   } else {
00237     throw UnknownTypeException("The given type '%s' does not match any known "
00238                                "message type for this interface type.", type);
00239   }
00240 }
00241 
00242 
00243 /** Copy values from other interface.
00244  * @param other other interface to copy values from
00245  */
00246 void
00247 SkillerDebugInterface::copy_values(const Interface *other)
00248 {
00249   const SkillerDebugInterface *oi = dynamic_cast<const SkillerDebugInterface *>(other);
00250   if (oi == NULL) {
00251     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00252                                 type(), other->type());
00253   }
00254   memcpy(data, oi->data, sizeof(SkillerDebugInterface_data_t));
00255 }
00256 
00257 const char *
00258 SkillerDebugInterface::enum_tostring(const char *enumtype, int val) const
00259 {
00260   if (strcmp(enumtype, "GraphDirectionEnum") == 0) {
00261     return tostring_GraphDirectionEnum((GraphDirectionEnum)val);
00262   }
00263   throw UnknownTypeException("Unknown enum type %s", enumtype);
00264 }
00265 
00266 /* =========== messages =========== */
00267 /** @class SkillerDebugInterface::SetGraphMessage <interfaces/SkillerDebugInterface.h>
00268  * SetGraphMessage Fawkes BlackBoard Interface Message.
00269  * 
00270     
00271  */
00272 
00273 
00274 /** Constructor with initial values.
00275  * @param ini_graph_fsm initial value for graph_fsm
00276  */
00277 SkillerDebugInterface::SetGraphMessage::SetGraphMessage(const char * ini_graph_fsm) : Message("SetGraphMessage")
00278 {
00279   data_size = sizeof(SetGraphMessage_data_t);
00280   data_ptr  = malloc(data_size);
00281   memset(data_ptr, 0, data_size);
00282   data      = (SetGraphMessage_data_t *)data_ptr;
00283   data_ts   = (message_data_ts_t *)data_ptr;
00284   strncpy(data->graph_fsm, ini_graph_fsm, 32);
00285   add_fieldinfo(IFT_STRING, "graph_fsm", 32, data->graph_fsm);
00286 }
00287 /** Constructor */
00288 SkillerDebugInterface::SetGraphMessage::SetGraphMessage() : Message("SetGraphMessage")
00289 {
00290   data_size = sizeof(SetGraphMessage_data_t);
00291   data_ptr  = malloc(data_size);
00292   memset(data_ptr, 0, data_size);
00293   data      = (SetGraphMessage_data_t *)data_ptr;
00294   data_ts   = (message_data_ts_t *)data_ptr;
00295   add_fieldinfo(IFT_STRING, "graph_fsm", 32, data->graph_fsm);
00296 }
00297 
00298 /** Destructor */
00299 SkillerDebugInterface::SetGraphMessage::~SetGraphMessage()
00300 {
00301   free(data_ptr);
00302 }
00303 
00304 /** Copy constructor.
00305  * @param m message to copy from
00306  */
00307 SkillerDebugInterface::SetGraphMessage::SetGraphMessage(const SetGraphMessage *m) : Message("SetGraphMessage")
00308 {
00309   data_size = m->data_size;
00310   data_ptr  = malloc(data_size);
00311   memcpy(data_ptr, m->data_ptr, data_size);
00312   data      = (SetGraphMessage_data_t *)data_ptr;
00313   data_ts   = (message_data_ts_t *)data_ptr;
00314 }
00315 
00316 /* Methods */
00317 /** Get graph_fsm value.
00318  * 
00319       The finite state machine (FSM) the current graph has been updated for.
00320     
00321  * @return graph_fsm value
00322  */
00323 char *
00324 SkillerDebugInterface::SetGraphMessage::graph_fsm() const
00325 {
00326   return data->graph_fsm;
00327 }
00328 
00329 /** Get maximum length of graph_fsm value.
00330  * @return length of graph_fsm value, can be length of the array or number of 
00331  * maximum number of characters for a string
00332  */
00333 size_t
00334 SkillerDebugInterface::SetGraphMessage::maxlenof_graph_fsm() const
00335 {
00336   return 32;
00337 }
00338 
00339 /** Set graph_fsm value.
00340  * 
00341       The finite state machine (FSM) the current graph has been updated for.
00342     
00343  * @param new_graph_fsm new graph_fsm value
00344  */
00345 void
00346 SkillerDebugInterface::SetGraphMessage::set_graph_fsm(const char * new_graph_fsm)
00347 {
00348   strncpy(data->graph_fsm, new_graph_fsm, sizeof(data->graph_fsm));
00349 }
00350 
00351 /** Clone this message.
00352  * Produces a message of the same type as this message and copies the
00353  * data to the new message.
00354  * @return clone of this message
00355  */
00356 Message *
00357 SkillerDebugInterface::SetGraphMessage::clone() const
00358 {
00359   return new SkillerDebugInterface::SetGraphMessage(this);
00360 }
00361 /** @class SkillerDebugInterface::SetGraphDirectionMessage <interfaces/SkillerDebugInterface.h>
00362  * SetGraphDirectionMessage Fawkes BlackBoard Interface Message.
00363  * 
00364     
00365  */
00366 
00367 
00368 /** Constructor with initial values.
00369  * @param ini_graph_dir initial value for graph_dir
00370  */
00371 SkillerDebugInterface::SetGraphDirectionMessage::SetGraphDirectionMessage(const GraphDirectionEnum ini_graph_dir) : Message("SetGraphDirectionMessage")
00372 {
00373   data_size = sizeof(SetGraphDirectionMessage_data_t);
00374   data_ptr  = malloc(data_size);
00375   memset(data_ptr, 0, data_size);
00376   data      = (SetGraphDirectionMessage_data_t *)data_ptr;
00377   data_ts   = (message_data_ts_t *)data_ptr;
00378   data->graph_dir = ini_graph_dir;
00379   add_fieldinfo(IFT_ENUM, "graph_dir", 1, &data->graph_dir, "GraphDirectionEnum");
00380 }
00381 /** Constructor */
00382 SkillerDebugInterface::SetGraphDirectionMessage::SetGraphDirectionMessage() : Message("SetGraphDirectionMessage")
00383 {
00384   data_size = sizeof(SetGraphDirectionMessage_data_t);
00385   data_ptr  = malloc(data_size);
00386   memset(data_ptr, 0, data_size);
00387   data      = (SetGraphDirectionMessage_data_t *)data_ptr;
00388   data_ts   = (message_data_ts_t *)data_ptr;
00389   add_fieldinfo(IFT_ENUM, "graph_dir", 1, &data->graph_dir, "GraphDirectionEnum");
00390 }
00391 
00392 /** Destructor */
00393 SkillerDebugInterface::SetGraphDirectionMessage::~SetGraphDirectionMessage()
00394 {
00395   free(data_ptr);
00396 }
00397 
00398 /** Copy constructor.
00399  * @param m message to copy from
00400  */
00401 SkillerDebugInterface::SetGraphDirectionMessage::SetGraphDirectionMessage(const SetGraphDirectionMessage *m) : Message("SetGraphDirectionMessage")
00402 {
00403   data_size = m->data_size;
00404   data_ptr  = malloc(data_size);
00405   memcpy(data_ptr, m->data_ptr, data_size);
00406   data      = (SetGraphDirectionMessage_data_t *)data_ptr;
00407   data_ts   = (message_data_ts_t *)data_ptr;
00408 }
00409 
00410 /* Methods */
00411 /** Get graph_dir value.
00412  * 
00413       Primary direction of current graph.
00414     
00415  * @return graph_dir value
00416  */
00417 SkillerDebugInterface::GraphDirectionEnum
00418 SkillerDebugInterface::SetGraphDirectionMessage::graph_dir() const
00419 {
00420   return data->graph_dir;
00421 }
00422 
00423 /** Get maximum length of graph_dir value.
00424  * @return length of graph_dir value, can be length of the array or number of 
00425  * maximum number of characters for a string
00426  */
00427 size_t
00428 SkillerDebugInterface::SetGraphDirectionMessage::maxlenof_graph_dir() const
00429 {
00430   return 1;
00431 }
00432 
00433 /** Set graph_dir value.
00434  * 
00435       Primary direction of current graph.
00436     
00437  * @param new_graph_dir new graph_dir value
00438  */
00439 void
00440 SkillerDebugInterface::SetGraphDirectionMessage::set_graph_dir(const GraphDirectionEnum new_graph_dir)
00441 {
00442   data->graph_dir = new_graph_dir;
00443 }
00444 
00445 /** Clone this message.
00446  * Produces a message of the same type as this message and copies the
00447  * data to the new message.
00448  * @return clone of this message
00449  */
00450 Message *
00451 SkillerDebugInterface::SetGraphDirectionMessage::clone() const
00452 {
00453   return new SkillerDebugInterface::SetGraphDirectionMessage(this);
00454 }
00455 /** @class SkillerDebugInterface::SetGraphColoredMessage <interfaces/SkillerDebugInterface.h>
00456  * SetGraphColoredMessage Fawkes BlackBoard Interface Message.
00457  * 
00458     
00459  */
00460 
00461 
00462 /** Constructor with initial values.
00463  * @param ini_graph_colored initial value for graph_colored
00464  */
00465 SkillerDebugInterface::SetGraphColoredMessage::SetGraphColoredMessage(const bool ini_graph_colored) : Message("SetGraphColoredMessage")
00466 {
00467   data_size = sizeof(SetGraphColoredMessage_data_t);
00468   data_ptr  = malloc(data_size);
00469   memset(data_ptr, 0, data_size);
00470   data      = (SetGraphColoredMessage_data_t *)data_ptr;
00471   data_ts   = (message_data_ts_t *)data_ptr;
00472   data->graph_colored = ini_graph_colored;
00473   add_fieldinfo(IFT_BOOL, "graph_colored", 1, &data->graph_colored);
00474 }
00475 /** Constructor */
00476 SkillerDebugInterface::SetGraphColoredMessage::SetGraphColoredMessage() : Message("SetGraphColoredMessage")
00477 {
00478   data_size = sizeof(SetGraphColoredMessage_data_t);
00479   data_ptr  = malloc(data_size);
00480   memset(data_ptr, 0, data_size);
00481   data      = (SetGraphColoredMessage_data_t *)data_ptr;
00482   data_ts   = (message_data_ts_t *)data_ptr;
00483   add_fieldinfo(IFT_BOOL, "graph_colored", 1, &data->graph_colored);
00484 }
00485 
00486 /** Destructor */
00487 SkillerDebugInterface::SetGraphColoredMessage::~SetGraphColoredMessage()
00488 {
00489   free(data_ptr);
00490 }
00491 
00492 /** Copy constructor.
00493  * @param m message to copy from
00494  */
00495 SkillerDebugInterface::SetGraphColoredMessage::SetGraphColoredMessage(const SetGraphColoredMessage *m) : Message("SetGraphColoredMessage")
00496 {
00497   data_size = m->data_size;
00498   data_ptr  = malloc(data_size);
00499   memcpy(data_ptr, m->data_ptr, data_size);
00500   data      = (SetGraphColoredMessage_data_t *)data_ptr;
00501   data_ts   = (message_data_ts_t *)data_ptr;
00502 }
00503 
00504 /* Methods */
00505 /** Get graph_colored value.
00506  * 
00507       True if the graph is colored, false otherwise.
00508     
00509  * @return graph_colored value
00510  */
00511 bool
00512 SkillerDebugInterface::SetGraphColoredMessage::is_graph_colored() const
00513 {
00514   return data->graph_colored;
00515 }
00516 
00517 /** Get maximum length of graph_colored value.
00518  * @return length of graph_colored value, can be length of the array or number of 
00519  * maximum number of characters for a string
00520  */
00521 size_t
00522 SkillerDebugInterface::SetGraphColoredMessage::maxlenof_graph_colored() const
00523 {
00524   return 1;
00525 }
00526 
00527 /** Set graph_colored value.
00528  * 
00529       True if the graph is colored, false otherwise.
00530     
00531  * @param new_graph_colored new graph_colored value
00532  */
00533 void
00534 SkillerDebugInterface::SetGraphColoredMessage::set_graph_colored(const bool new_graph_colored)
00535 {
00536   data->graph_colored = new_graph_colored;
00537 }
00538 
00539 /** Clone this message.
00540  * Produces a message of the same type as this message and copies the
00541  * data to the new message.
00542  * @return clone of this message
00543  */
00544 Message *
00545 SkillerDebugInterface::SetGraphColoredMessage::clone() const
00546 {
00547   return new SkillerDebugInterface::SetGraphColoredMessage(this);
00548 }
00549 /** Check if message is valid and can be enqueued.
00550  * @param message Message to check
00551  * @return true if the message is valid, false otherwise.
00552  */
00553 bool
00554 SkillerDebugInterface::message_valid(const Message *message) const
00555 {
00556   const SetGraphMessage *m0 = dynamic_cast<const SetGraphMessage *>(message);
00557   if ( m0 != NULL ) {
00558     return true;
00559   }
00560   const SetGraphDirectionMessage *m1 = dynamic_cast<const SetGraphDirectionMessage *>(message);
00561   if ( m1 != NULL ) {
00562     return true;
00563   }
00564   const SetGraphColoredMessage *m2 = dynamic_cast<const SetGraphColoredMessage *>(message);
00565   if ( m2 != NULL ) {
00566     return true;
00567   }
00568   return false;
00569 }
00570 
00571 /// @cond INTERNALS
00572 EXPORT_INTERFACE(SkillerDebugInterface)
00573 /// @endcond
00574 
00575 
00576 } // end namespace fawkes