GAMS  1.4.0
BaseController.h
Go to the documentation of this file.
1 
54 #ifndef _GAMS_BASE_CONTROLLER_H_
55 #define _GAMS_BASE_CONTROLLER_H_
56 
57 #include "ControllerSettings.h"
58 
59 #include "gams/GamsExport.h"
60 #include "gams/variables/Agent.h"
61 #include "gams/variables/Swarm.h"
62 #include "gams/variables/Self.h"
63 #include "gams/variables/Sensor.h"
70 #include "gams/groups/GroupBase.h"
71 
72 #include "madara/knowledge/containers/String.h"
73 #include "madara/knowledge/containers/Vector.h"
74 
75 #ifdef _GAMS_JAVA_
76 #include <jni.h>
77 #endif
78 
79 namespace gams
80 {
81  namespace controllers
82  {
88  {
89  public:
95  BaseController(madara::knowledge::KnowledgeBase & knowledge,
96  const ControllerSettings & settings = ControllerSettings());
97 
101  virtual ~BaseController();
102 
107  virtual int monitor(void);
108 
114  virtual int system_analyze(void);
115 
120  virtual int analyze(void);
121 
126  virtual int plan(void);
127 
132  virtual int execute(void);
133 
140  int run_once(void);
141 
153  int run(double loop_period,
154  double max_runtime = -1,
155  double send_period = -1.0);
156 
167  inline int run_hz(double loop_hz = 0.0,
168  double max_runtime = -1,
169  double send_hz = -1.0)
170  {
171  double loop_rate, send_rate;
172 
173  // check for bad data
174  if(loop_hz <= 0.0)
175  loop_rate = 0.0;
176  // otherwise, set rate to 1s divided by the intended hz
177  else
178  loop_rate = 1.0 / loop_hz;
179 
180  // copy from loop hz if send hz is non-positive
181  if(send_hz <= 0)
182  send_rate = loop_rate;
183  // otherwise, set rate to 1s divided by the intended hz
184  else
185  send_rate = 1.0 / send_hz;
186 
187  return run(loop_rate, max_runtime, send_rate);
188  }
189 
194  int run(void);
195 
201  void init_accent(const std::string & algorithm,
202  const madara::knowledge::KnowledgeMap & args =
203  madara::knowledge::KnowledgeMap());
204 
208  void clear_accents(void);
209 
219  const std::vector <std::string> & aliases,
220  platforms::PlatformFactory * factory);
221 
231  const std::vector <std::string> & aliases,
232  algorithms::AlgorithmFactory * factory);
233 
239  void init_algorithm(const std::string & algorithm,
240  const madara::knowledge::KnowledgeMap & args = madara::knowledge::KnowledgeMap());
241 
249 
255  void init_platform(const std::string & platform,
256  const madara::knowledge::KnowledgeMap & args =
257  madara::knowledge::KnowledgeMap());
258 
266 
270  void configure(const ControllerSettings & settings);
271 
272 #ifdef _GAMS_JAVA_
277  void init_algorithm(jobject algorithm);
278 
283  void init_platform(jobject platform);
284 #endif
285 
291  void init_vars(const madara::knowledge::KnowledgeRecord::Integer & id = 0,
292  const madara::knowledge::KnowledgeRecord::Integer & agents = -1);
293 
300  void init_vars(const std::string & self_prefix,
301  const std::string & group_name = "");
302 
308  void init_vars(const std::string & self_prefix,
309  const groups::GroupBase * group);
310 
318 
326 
332 
338 
344 
349  madara::knowledge::KnowledgeBase * get_kb(void);
350 
356 
360  void save_checkpoint(void);
361 
362  protected:
363 
366 
369 
372 
375 
377  madara::knowledge::KnowledgeBase & knowledge_;
378 
381 
384 
387 
390 
393 
396 
399  private:
400 
402  int run_once_(void);
403  };
404  }
405 }
406 
407 #endif // _GAMS_BASE_CONTROLLER_H_
Copyright(c) 2014-2018 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
Copyright(c) 2017 Carnegie Mellon University.
#define GAMS_EXPORT
Definition: GamsExport.h:20
Copyright(c) 2016 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
Base class for algorithm factories that classes derived from.
The base class that algorithms in GAMS use.
Definition: BaseAlgorithm.h:95
The basic controller that can be used to perform actions on platforms and algorithms.
int run_once(void)
Runs a single iteration of the MAPE loop Always sends updates after the iteration.
variables::Self * get_self(void)
Gets the Self.
void init_platform(platforms::BasePlatform *platform)
Initializes the controller with a user-provided platform.
virtual int monitor(void)
Defines the monitor function(the M of MAPE).
int run_hz(double loop_hz=0.0, double max_runtime=-1, double send_hz=-1.0)
Runs iterations of the MAPE loop with specified hertz.
BaseController(madara::knowledge::KnowledgeBase &knowledge, const ControllerSettings &settings=ControllerSettings())
Constructor.
void init_algorithm(const std::string &algorithm, const madara::knowledge::KnowledgeMap &args=madara::knowledge::KnowledgeMap())
Initializes an algorithm.
void save_checkpoint(void)
Saves a checkpoint according to the configured settings.
virtual int plan(void)
Defines the plan function(the P of MAPE).
int run_once_(void)
Code shared between run and run_once.
int run(double loop_period, double max_runtime=-1, double send_period=-1.0)
Runs iterations of the MAPE loop with specified periods.
int run(void)
Runs iterations of the MAPE loop with configured settings.
algorithms::BaseAlgorithm * get_algorithm(void)
Gets the current algorithm.
void init_platform(const std::string &platform, const madara::knowledge::KnowledgeMap &args=madara::knowledge::KnowledgeMap())
Initializes the platform.
void add_platform_factory(const std::vector< std::string > &aliases, platforms::PlatformFactory *factory)
Adds an aliased platform factory.
ControllerSettings settings_
Settings for controller management and qos.
platforms::BasePlatform * get_platform(void)
Gets the current platform.
platforms::BasePlatform * platform_
Platform on which the controller is running.
virtual ~BaseController()
Destructor.
algorithms::Algorithms accents_
Accents on the primary algorithm.
variables::Platforms platforms_
Containers for platform information.
void init_vars(platforms::BasePlatform &platform)
Initializes containers and knowledge base in a platform This is usually the first thing a developer s...
madara::knowledge::KnowledgeBase & knowledge_
Knowledge base.
variables::Agents agents_
Containers for agent-related variables.
void init_accent(const std::string &algorithm, const madara::knowledge::KnowledgeMap &args=madara::knowledge::KnowledgeMap())
Adds an accent algorithm.
variables::Self self_
Containers for self-referencing variables.
variables::Sensors sensors_
Containers for sensor information.
algorithms::BaseAlgorithm * algorithm_
Algorithm to perform.
variables::Sensors * get_sensors(void)
Gets the sensors map.
variables::Swarm swarm_
Containers for swarm-related variables.
void init_vars(const madara::knowledge::KnowledgeRecord::Integer &id=0, const madara::knowledge::KnowledgeRecord::Integer &agents=-1)
Initializes global variable containers.
void init_vars(const std::string &self_prefix, const std::string &group_name="")
Initializes global variable containers.
virtual int analyze(void)
Defines the analyze function(the A of MAPE).
void configure(const ControllerSettings &settings)
Configures the controller with initialization settings.
int checkpoint_count_
keeps track of the checkpoints saved in the control loop
void init_vars(const std::string &self_prefix, const groups::GroupBase *group)
Initializes global variable containers.
void add_algorithm_factory(const std::vector< std::string > &aliases, algorithms::AlgorithmFactory *factory)
Adds an aliased algorithm factory.
virtual int execute(void)
Defines the execute function(the E of MAPE).
void clear_accents(void)
Clears all accent algorithms.
void init_vars(algorithms::BaseAlgorithm &algorithm)
Initializes containers and knowledge base in an algorithm.
void init_algorithm(algorithms::BaseAlgorithm *algorithm)
Initializes the controller with a user-provided algorithm.
variables::Algorithms algorithms_
Containers for algorithm information.
virtual int system_analyze(void)
Analyzes the system to determine if platform or algorithm changes are necessary.
madara::knowledge::KnowledgeBase * get_kb(void)
Gets the KB.
Settings used for initializing GAMS controllers.
Base class for a group of agents.
Definition: GroupBase.h:91
The base platform for all platforms to use.
Definition: BasePlatform.h:113
Base class for platform factories to create BasePlatforms.
A container for self referencing information.
Definition: Self.h:70
A container for swarm information.
Definition: Swarm.h:76
std::vector< BaseAlgorithm * > Algorithms
std::map< std::string, PlatformStatus > Platforms
a map of sensor names to the sensor information
std::map< std::string, AlgorithmStatus > Algorithms
a map of sensor names to the sensor information
std::map< std::string, Sensor * > Sensors
a map of sensor names to the sensor information
Definition: Sensor.h:238
std::vector< Agent > Agents
An array of agent knowledge.
Definition: Agent.h:246
Contains all GAMS-related tools, classes and code.