GAMS  1.4.0
HiveController.h
Go to the documentation of this file.
1 
54 #pragma once
55 
56 #include "gams/GamsExport.h"
57 #include "gams/variables/Agent.h"
58 #include "gams/variables/Swarm.h"
59 #include "gams/variables/Self.h"
60 #include "gams/variables/Sensor.h"
68 #include "madara/transport/SharedMemoryPush.h"
69 
70 #include "madara/knowledge/containers/String.h"
71 #include "madara/knowledge/containers/Vector.h"
72 #include "madara/knowledge/Hive.h"
73 
74 #ifdef _GAMS_JAVA_
75 #include <jni.h>
76 #endif
77 
78 namespace gams
79 {
80  namespace controllers
81  {
86  {
87  public:
92 
101  madara::knowledge::Hive & hive,
102  size_t init_id = 0, size_t num_controllers = 1,
103  const ControllerSettings & settings = ControllerSettings());
104 
108  virtual ~HiveController();
109 
119  const std::vector <std::string> & aliases,
120  algorithms::AlgorithmFactory * factory);
121 
131  const std::vector <std::string> & aliases,
132  platforms::PlatformFactory * factory);
133 
149  const madara::transport::QoSTransportSettings & source_settings);
150 
154  void clear_knowledge(void);
155 
161  void evaluate(const std::string & logic,
162  const madara::knowledge::EvalSettings & settings =
163  madara::knowledge::EvalSettings::DELAY);
164 
171  void evaluate(size_t controller_index, const std::string & logic,
172  const madara::knowledge::EvalSettings & settings =
173  madara::knowledge::EvalSettings::DELAY);
174 
181  const std::string & algorithm,
182  const madara::knowledge::KnowledgeMap & args = madara::knowledge::KnowledgeMap());
183 
190  void init_accent(size_t controller_index,
191  const std::string & algorithm,
192  const madara::knowledge::KnowledgeMap & args = madara::knowledge::KnowledgeMap());
193 
200  const std::string & algorithm,
201  const madara::knowledge::KnowledgeMap & args = madara::knowledge::KnowledgeMap());
202 
209  void init_algorithm(size_t controller_index,
210  const std::string & algorithm,
211  const madara::knowledge::KnowledgeMap & args = madara::knowledge::KnowledgeMap());
212 
220  void init_algorithm(size_t controller_index,
221  algorithms::BaseAlgorithm * algorithm);
222 
229  const std::string & platform,
230  const madara::knowledge::KnowledgeMap & args =
231  madara::knowledge::KnowledgeMap());
232 
239  void init_platform(size_t controller_index,
240  const std::string & platform,
241  const madara::knowledge::KnowledgeMap & args =
242  madara::knowledge::KnowledgeMap());
243 
251  void init_platform(size_t controller_index,
252  platforms::BasePlatform * platform);
253 
254 #ifdef _GAMS_JAVA_
260  void init_algorithm(size_t controller_index, jobject algorithm);
261 
267  void init_platform(size_t controller_index, jobject platform);
268 #endif
269 
275  void init_vars(const madara::knowledge::KnowledgeRecord::Integer & id = 0,
276  const madara::knowledge::KnowledgeRecord::Integer & processes = -1);
277 
284  void refresh_vars(bool init_non_self_vars = false);
285 
293  void init_vars(size_t controller_index,
294  platforms::BasePlatform & platform);
295 
303  void init_vars(size_t controller_index,
304  algorithms::BaseAlgorithm & algorithm);
305 
311  algorithms::BaseAlgorithm * get_algorithm(size_t controller_index = 0);
312 
318  platforms::BasePlatform * get_platform(size_t controller_index = 0);
319 
324  size_t get_num_controllers(void);
325 
331  madara::knowledge::KnowledgeBase get_kb(size_t controller_index);
332 
337  void clear_accents(size_t controller_index = 0);
338 
346  void resize(size_t init_id, size_t num_controllers,
347  bool init_non_self_vars = false);
348 
355  int run_once(void);
356 
361  int run(void);
362 
367  inline void set_hive(madara::knowledge::Hive& hive)
368  {
369  hive_ = &hive;
370  }
371 
383  int run(double loop_period,
384  double max_runtime = -1,
385  double send_period = -1.0);
386 
397  inline int run_hz(double loop_hz = 0.0,
398  double max_runtime = -1,
399  double send_hz = -1.0)
400  {
401  double loop_rate, send_rate;
402 
403  // check for bad data
404  if(loop_hz <= 0.0)
405  loop_rate = 0.0;
406  // otherwise, set rate to 1s divided by the intended hz
407  else
408  loop_rate = 1.0 / loop_hz;
409 
410  // copy from loop hz if send hz is non-positive
411  if(send_hz <= 0)
412  send_rate = loop_rate;
413  // otherwise, set rate to 1s divided by the intended hz
414  else
415  send_rate = 1.0 / send_hz;
416 
417  return run(loop_rate, max_runtime, send_rate);
418  }
419 
420  protected:
421 
423  size_t offset_;
424 
426  std::vector <BaseController *> controllers_;
427 
429  madara::knowledge::Hive * hive_;
430 
433  };
434  }
435 }
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) 2014 Carnegie Mellon University.
#define GAMS_EXPORT
Definition: GamsExport.h:20
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
Settings used for initializing GAMS controllers.
A controller that has the capability of managing multiple controllers.
algorithms::BaseAlgorithm * get_algorithm(size_t controller_index=0)
Gets the current algorithm at the controller index.
void init_vars(size_t controller_index, algorithms::BaseAlgorithm &algorithm)
Initializes containers and knowledge base in an algorithm.
madara::knowledge::Hive * hive_
Pointer to a hive mind managed by parent.
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.
void init_algorithm(const std::string &algorithm, const madara::knowledge::KnowledgeMap &args=madara::knowledge::KnowledgeMap())
Initializes all managed controllers with an algorithm.
void clear_accents(size_t controller_index=0)
Clears all accent algorithms.
int run(double loop_period, double max_runtime=-1, double send_period=-1.0)
Runs iterations of the MAPE loop with specified periods.
void add_transports(const madara::transport::QoSTransportSettings &source_settings)
Add transports to the knowledge bases that are being used by each controller.
void add_platform_factory(const std::vector< std::string > &aliases, platforms::PlatformFactory *factory)
Adds an aliased platform factory.
void refresh_vars(bool init_non_self_vars=false)
Calls init_vars for each controller.
int run_once(void)
Runs a single iteration of the MAPE loop Always sends updates after the iteration.
void init_algorithm(size_t controller_index, const std::string &algorithm, const madara::knowledge::KnowledgeMap &args=madara::knowledge::KnowledgeMap())
Initializes a specific controller with an algorithm.
int run(void)
Runs iterations of the MAPE loop with configured settings.
void init_algorithm(size_t controller_index, algorithms::BaseAlgorithm *algorithm)
Initializes the controller with a user-provided algorithm.
virtual ~HiveController()
Destructor.
void set_hive(madara::knowledge::Hive &hive)
Sets the hive to control a portion of.
void init_accent(size_t controller_index, const std::string &algorithm, const madara::knowledge::KnowledgeMap &args=madara::knowledge::KnowledgeMap())
Adds an accent algorithm.
platforms::BasePlatform * get_platform(size_t controller_index=0)
Gets the current platform at the controller index.
void init_vars(const madara::knowledge::KnowledgeRecord::Integer &id=0, const madara::knowledge::KnowledgeRecord::Integer &processes=-1)
Initializes global variable containers.
ControllerSettings settings_
Settings for controller management and qos.
std::vector< BaseController * > controllers_
Controllers that need to be instrumented.
size_t offset_
the starting id within offsets and transports
void init_platform(size_t controller_index, platforms::BasePlatform *platform)
Initializes the controller with a user-provided platform.
void evaluate(const std::string &logic, const madara::knowledge::EvalSettings &settings=madara::knowledge::EvalSettings::DELAY)
Evaluates a karl logic across all controllers and knowledge bases.
void add_algorithm_factory(const std::vector< std::string > &aliases, algorithms::AlgorithmFactory *factory)
Adds an aliased algorithm factory.
void init_vars(size_t controller_index, platforms::BasePlatform &platform)
Initializes containers and knowledge base in a platform This is usually the first thing a developer s...
void clear_knowledge(void)
Clears all controller knowledge bases.
size_t get_num_controllers(void)
Returns the number of controllers being managed.
void init_platform(size_t controller_index, const std::string &platform, const madara::knowledge::KnowledgeMap &args=madara::knowledge::KnowledgeMap())
Initializes the platform.
void evaluate(size_t controller_index, const std::string &logic, const madara::knowledge::EvalSettings &settings=madara::knowledge::EvalSettings::DELAY)
Evaluates a karl logic across all controllers and knowledge bases.
HiveController()
Default constructor.
void init_platform(const std::string &platform, const madara::knowledge::KnowledgeMap &args=madara::knowledge::KnowledgeMap())
Initializes all managed controllers with the platform.
madara::knowledge::KnowledgeBase get_kb(size_t controller_index)
Returns the kb that is being used by the internal controller.
HiveController(madara::knowledge::Hive &hive, size_t init_id=0, size_t num_controllers=1, const ControllerSettings &settings=ControllerSettings())
Constructor.
void init_accent(const std::string &algorithm, const madara::knowledge::KnowledgeMap &args=madara::knowledge::KnowledgeMap())
Initializes all managed controllers with an accent algorithm.
void resize(size_t init_id, size_t num_controllers, bool init_non_self_vars=false)
Resizes the number of controllers and knowledge bases.
The base platform for all platforms to use.
Definition: BasePlatform.h:113
Base class for platform factories to create BasePlatforms.
Contains all GAMS-related tools, classes and code.