SMACC2
Loading...
Searching...
No Matches
smacc2_event_generator_library
eg_random_generator
src
eg_random_generator
eg_random_generator.cpp
Go to the documentation of this file.
1
// Copyright 2021 RobosoftAI Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <
eg_random_generator/eg_random_generator.hpp
>
16
17
namespace
smacc2
18
{
19
namespace
state_reactors
20
{
21
EgRandomGenerator::EgRandomGenerator
(
22
RandomGenerateReactorMode
mode
,
double
evAMin
,
double
evAMax
,
double
evBMin
,
double
evBMax
,
23
double
evCMin
,
double
evCMax
)
24
: mode_(
mode
),
25
evAMin_(
evAMin
),
26
evAMax_(
evAMax
),
27
evBMin_(
evBMin
),
28
evBMax_(
evBMax
),
29
evCMin_(
evCMin
),
30
evCMax_(
evCMax
)
31
{
32
auto
values
= {
evAMin
,
evAMax
,
evBMin
,
evBMax
,
evCMin
,
evCMax
};
33
34
this->
minValue
= std::numeric_limits<double>::max();
35
this->
maxValue
= std::numeric_limits<double>::min();
36
for
(
auto
&
v
:
values
)
37
{
38
if
(
v
<
minValue
)
39
{
40
minValue
=
v
;
41
}
42
43
if
(
v
>
maxValue
)
44
{
45
maxValue
=
v
;
46
}
47
}
48
}
49
50
void
EgRandomGenerator::postRandomEvents
()
51
{
52
int
range
= this->
maxValue
- this->
minValue
;
53
int
result = (
rand
() %
range
) +
minValue
;
54
55
if
(result >=
evAMin_
&& result <=
evAMax_
)
56
{
57
this->
postEventA
();
58
}
59
60
if
(result >=
evBMin_
&& result <=
evBMax_
)
61
{
62
this->
postEventB
();
63
}
64
65
if
(result >=
evCMin_
&& result <=
evCMax_
)
66
{
67
this->
postEventC
();
68
}
69
}
70
71
void
EgRandomGenerator::onEntry
()
72
{
73
if
(
mode_
==
RandomGenerateReactorMode::ONE_SHOT
)
74
{
75
this->
postRandomEvents
();
76
}
77
}
78
79
void
EgRandomGenerator::update
()
80
{
81
if
(
mode_
==
RandomGenerateReactorMode::ON_UPDATE
)
82
{
83
this->
postRandomEvents
();
84
}
85
}
86
}
// namespace state_reactors
87
}
// namespace smacc2
smacc2::state_reactors::EgRandomGenerator::evBMax_
double evBMax_
Definition
eg_random_generator.hpp:81
smacc2::state_reactors::EgRandomGenerator::postRandomEvents
void postRandomEvents()
Definition
eg_random_generator.cpp:50
smacc2::state_reactors::EgRandomGenerator::evBMin_
double evBMin_
Definition
eg_random_generator.hpp:80
smacc2::state_reactors::EgRandomGenerator::update
void update() override
Definition
eg_random_generator.cpp:79
smacc2::state_reactors::EgRandomGenerator::evAMin_
double evAMin_
Definition
eg_random_generator.hpp:78
smacc2::state_reactors::EgRandomGenerator::evCMax_
double evCMax_
Definition
eg_random_generator.hpp:83
smacc2::state_reactors::EgRandomGenerator::EgRandomGenerator
EgRandomGenerator(RandomGenerateReactorMode mode, double evAMin=1, double evAMax=4, double evBMin=5, double evBMax=8, double evCMin=9, double evCMax=12)
Definition
eg_random_generator.cpp:21
smacc2::state_reactors::EgRandomGenerator::maxValue
double maxValue
Definition
eg_random_generator.hpp:86
smacc2::state_reactors::EgRandomGenerator::postEventC
std::function< void()> postEventC
Definition
eg_random_generator.hpp:76
smacc2::state_reactors::EgRandomGenerator::onEntry
void onEntry() override
Definition
eg_random_generator.cpp:71
smacc2::state_reactors::EgRandomGenerator::postEventA
std::function< void()> postEventA
Definition
eg_random_generator.hpp:74
smacc2::state_reactors::EgRandomGenerator::minValue
double minValue
Definition
eg_random_generator.hpp:85
smacc2::state_reactors::EgRandomGenerator::postEventB
std::function< void()> postEventB
Definition
eg_random_generator.hpp:75
smacc2::state_reactors::EgRandomGenerator::evCMin_
double evCMin_
Definition
eg_random_generator.hpp:82
smacc2::state_reactors::EgRandomGenerator::mode_
RandomGenerateReactorMode mode_
Definition
eg_random_generator.hpp:71
smacc2::state_reactors::EgRandomGenerator::evAMax_
double evAMax_
Definition
eg_random_generator.hpp:79
eg_random_generator.hpp
smacc2::state_reactors::RandomGenerateReactorMode
RandomGenerateReactorMode
Definition
eg_random_generator.hpp:43
smacc2::state_reactors::RandomGenerateReactorMode::ON_UPDATE
@ ON_UPDATE
smacc2::state_reactors::RandomGenerateReactorMode::ONE_SHOT
@ ONE_SHOT
smacc2
Definition
callback_counter_semaphore.hpp:30
smacc2::default_events::EvTopicMessage
Definition
smacc_default_events.hpp:155
Generated by
1.9.8