system.cpp
Go to the documentation of this file.
00001 #include "mrta/allocation.h"
00002 #include "mrta/problem.h"
00003 #include "mrta/robot.h"
00004 #include "mrta/robot_monitor.h"
00005 #include "mrta/task.h"
00006 #include "mrta/system.h"
00007 #include "rqt_mrta/config/application/rqt_mrta_application.h"
00008 #include "rqt_mrta/config/architecture/rqt_mrta_architecture.h"
00009 #include "utilities/exception.h"
00010 #include "utilities/message_subscriber_registry.h"
00011 
00012 namespace mrta
00013 {
00014 System::System(QObject* parent, ApplicationConfig* application_config,
00015                ArchitectureConfig* architecture_config,
00016                utilities::MessageSubscriberRegistry* registry)
00017     : QObject(parent), application_config_(application_config),
00018       architecture_config_(architecture_config),
00019       problem_(new Problem(this,
00020                            application_config->getApplication()->getName(),
00021                            architecture_config))
00022 {
00023   if (!application_config_)
00024   {
00025     throw utilities::Exception(
00026         "The application configuration must not be null.");
00027   }
00028   if (!architecture_config_)
00029   {
00030     throw utilities::Exception(
00031         "The architecture configuration must not be null.");
00032   }
00033   monitors_.append(new RobotMonitor(
00034       this, registry,
00035       architecture_config->getArchitecture()->getRobots()->getBusyRobots()->getTopic(),
00036       Robot::Busy));
00037   monitors_.append(new RobotMonitor(
00038       this, registry,
00039       architecture_config->getArchitecture()->getRobots()->getIdleRobots()->getTopic(),
00040       Robot::Idle));
00041   connect(problem_, SIGNAL(taskStateChanged(const QString&, int)), this,
00042           SIGNAL(changed()));
00043   connect(problem_, SIGNAL(taskStateChanged(const QString&, int)), this,
00044           SIGNAL(taskStateChanged(const QString&, int)));
00045   connect(problem_, SIGNAL(allocationStateChanged(const QString&, int)), this,
00046           SIGNAL(changed()));
00047   connect(problem_, SIGNAL(allocationStateChanged(const QString&, int)), this,
00048           SIGNAL(allocationStateChanged(const QString&, int)));
00049   rqt_mrta::config::application::Robots* configs =
00050       application_config_->getApplication()->getRobots();
00051   for (size_t index(0); index < configs->count(); index++)
00052   {
00053     addRobot(configs->getRobot(index));
00054   }
00055 }
00056 
00057 System::~System()
00058 {
00059   ROS_INFO_STREAM("[~System] before");
00060   application_config_ = NULL;
00061   architecture_config_ = NULL;
00062   for (size_t index(0); index < monitors_.count(); index++)
00063   {
00064     if (monitors_[index])
00065     {
00066       delete monitors_[index];
00067       monitors_[index] = NULL;
00068     }
00069   }
00070   monitors_.clear();
00071   if (problem_)
00072   {
00073     delete problem_;
00074     problem_ = NULL;
00075   }
00076   for (RobotMap::iterator it(robots_.begin()); it != robots_.end(); it++)
00077   {
00078     if (it.value())
00079     {
00080       delete it.value();
00081       robots_[it.key()] = NULL;
00082     }
00083   }
00084   robots_.clear();
00085   ROS_INFO_STREAM("[~System] after");
00086 }
00087 
00088 Robot* System::getRobot(const QString& id)
00089 {
00090   return robots_.contains(id) ? robots_[id] : NULL;
00091 }
00092 
00093 Task* System::getTask(const QString& id) { return problem_->getTask(id); }
00094 
00095 Allocation* System::getAllocation(const QString& id)
00096 {
00097   return problem_->getAllocation(id);
00098 }
00099 
00100 QList<Robot*> System::getRobots() const { return robots_.values(); }
00101 
00102 QList<Task*> System::getTasks() const { return problem_->getTasks(); }
00103 
00104 QList<Allocation*> System::getAllocations() const
00105 {
00106   return problem_->getAllocations();
00107 }
00108 
00109 void System::setRegistry(utilities::MessageSubscriberRegistry* registry)
00110 {
00111   for (size_t index(0); index < monitors_.count(); index++)
00112   {
00113     monitors_[index]->setRegistry(registry);
00114   }
00115 }
00116 
00117 Robot* System::addRobot(RobotConfig* config)
00118 {
00119   if (robots_.contains(config->getId()))
00120   {
00121     return robots_[config->getId()];
00122   }
00123   Robot* robot = new Robot(this, config);
00124   robots_[robot->getId()] = robot;
00125   emit added(robot->getId());
00126   emit changed();
00127 }
00128 
00129 void System::robotStateChanged(int state)
00130 {
00131   Robot* robot = static_cast<Robot*>(sender());
00132   if (robot)
00133   {
00134     emit robotStateChanged(robot->getId(), state);
00135     emit changed();
00136   }
00137 }
00138 }


rqt_mrta
Author(s): Adriano Henrique Rossette Leite
autogenerated on Thu Jun 6 2019 18:50:52