RandomChaining.cpp
Go to the documentation of this file.
1
2#include "RandomChaining.h"
3
4#include <chrono>
5#include <cstdlib>
6#include <thread>
7
9
11{
12 namespace util
13 {
14 int
15 randomgen(int max, int min) //Pass in range
16 {
17 srand(time(NULL)); //Changed from rand(). srand() seeds rand for you.
18 int random = rand() % max + min;
19 return random;
20 }
21 } // namespace util
22
26
29 {
30 return SkillDescription{.skillId = armarx::skills::SkillID{.skillName = "RandomChaining"},
31 .description = "This skill calls 100 random subskills from the "
32 "skillProviderExample excluding the segfault skill.",
33 .rootProfileDefaults = armarx::aron::make_dict(),
35 }
36
38 RandomChainingSkill::main(const MainInput& in)
39 {
40 std::vector<std::string> subskillNames = {"Timeout",
41 "Chaining",
42 "Foo",
43 "HelloWorld",
44 "Incomplete",
45 "ShowMeCallbacks",
46 "BusyWaiting",
47 "Recursive"};
48
49 ARMARX_CHECK(subskillNames.size() > 0);
50
51 {
52 std::vector<skills::SkillExecutionHandlePtr> handles;
53
54 for (unsigned int i = 0; i < 100; ++i)
55 {
57
58 auto index = util::randomgen(subskillNames.size() - 1, 0);
59
60 auto subskillName = subskillNames[index];
61
62
63 skills::SkillID skillId{.providerId = *getSkillId().providerId,
64 .skillName = subskillName};
65
66 if (util::randomgen(10, 0) < 2)
67 {
68 callSubskill(skillId);
69 }
70 else
71 {
72 auto handle = callSubskillAsync(skillId);
73
74 auto sleep_milliseconds = util::randomgen(1000, 0);
75
76 ARMARX_INFO << "SLEEP FOR " << sleep_milliseconds << "ms";
77 std::this_thread::sleep_for(std::chrono::milliseconds(sleep_milliseconds));
78
79 // avoid call of d'tors before skill is done
80 handles.emplace_back(std::move(handle));
81 }
82 }
84 }
85
86 return {TerminatedSkillStatus::Succeeded, nullptr};
87 }
88} // namespace armarx::skills::provider
uint8_t index
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition Duration.cpp:48
std::optional< ProviderID > providerId
Definition SkillID.h:40
std::optional< TerminatedSkillStatusUpdate > callSubskill(const SkillID &skillId)
Call a subskill with the given ID and its default parameters.
Definition Skill.cpp:119
virtual MainResult main()
Override this method with the actual implementation.
Definition Skill.cpp:542
void throwIfSkillShouldTerminate(const std::string &abortedMessage="") const
Definition Skill.cpp:389
SkillID getSkillId() const
Get the id of the skill.
Definition Skill.cpp:587
SkillExecutionHandlePtr callSubskillAsync(const SkillID &skillId, std::function< void(aron::data::DictPtr &)> parametersFunction)
Definition Skill.cpp:148
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
aron::data::DictPtr make_dict(_Args &&... args)
Definition Dict.h:107
int randomgen(int max, int min)
JoinAllResult joinAll(const std::vector< armarx::skills::SkillExecutionHandle * > &handles)
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
A result struct for th main method of a skill.
Definition Skill.h:62