-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherunner.hpp
More file actions
194 lines (166 loc) · 6.81 KB
/
erunner.hpp
File metadata and controls
194 lines (166 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Copyright (c) 2013-2015, Damian Vicino & Daniella Niyonkuru
* Modified by Daniella Niyonkuru (21/7/15 : Embedded CDBoost Version)
* Carleton University, Universite de Nice-Sophia Antipolis
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef BOOST_SIMULATION_PDEVS_ERUNNER_H
#define BOOST_SIMULATION_PDEVS_ERUNNER_H
#include <iostream>
#include <boost/simulation/pdevs/coordinator.hpp>
#include <boost/simulation/pdevs/driver.hpp>
#include <boost/any.hpp>
#include <boost/lexical_cast.hpp>
#include "eTime.h"
#include "SWO.h"
namespace boost {
namespace simulation {
namespace pdevs {
/**
* @brief The Runner class runs the simulation.
*
* The runner is in charge of setting up the coordinators and simulators, the initial
* conditions, the ending conditions and the loggers, then it runs the simulation and
* displays the results.
*/
template <class TIME, class MSG, class VALUE=int>
class erunner
{
TIME _next; //next scheduled event
std::shared_ptr<coordinator<TIME, MSG, nullqueue>> _coordinator; //ecoordinator of the top level coupled model.
std::shared_ptr<driver<TIME, MSG, VALUE>> _driver; // global driver to manage top ports connected to hardware
std::vector<std::pair<std::shared_ptr<port<TIME,MSG,VALUE>>, std::shared_ptr<model<TIME>>>> _input_ports;
std::vector<std::pair<std::shared_ptr<port<TIME,MSG,VALUE>>, std::shared_ptr<model<TIME>>>> _output_ports;
bool _silent;
void process_output(TIME t, std::vector<MSG>& m) noexcept {
for ( auto& msg : m){
MSG om = m.back(); // replace with msg
m.pop_back();
_driver->send_hardware_command(om); // Send signal to hardware
}
}
bool get_hardware_input(){
return false;
}
const TIME infinity;
public:
//contructor
/**
* @brief eRunner constructing from a M model connected to an output.
* @param cm is the coupled model to simulate.
* @param ip is the set of top input ports.
* @param op is the set of top output ports.
*/
explicit erunner(std::shared_ptr<coupled<TIME, MSG>> cm,
std::vector<std::pair<std::shared_ptr<port<TIME,MSG,VALUE>>, std::shared_ptr<model<TIME>>>> ip,
std::vector<std::pair<std::shared_ptr<port<TIME,MSG,VALUE>>, std::shared_ptr<model<TIME>>>> op) noexcept
: _input_ports(ip), _output_ports(op),infinity(cm->infinity)
{
for ( auto& inpport : ip){
std::shared_ptr<port<TIME, MSG, VALUE>> in_p = std::dynamic_pointer_cast<port<TIME, MSG, VALUE>>(inpport.first);
if ( in_p == nullptr){ //no input port provided
printf("NULL PTR TO INPUT PORT \n");
}
else
in_p->print();
}
for ( auto& outpport : op){
std::shared_ptr<port<TIME, MSG, VALUE>> out_p = std::dynamic_pointer_cast<port<TIME, MSG, VALUE>>(outpport.first);
if ( out_p == nullptr){ //no output port provided
printf("NULL PTR TO OUTPUT PORT \n");
}
else
out_p->print();
}
_coordinator.reset(new coordinator<TIME, MSG, nullqueue>{cm});
_driver.reset(new driver<TIME,MSG, VALUE>{ip,op});
_next = _coordinator->init(TIME(00,00,00,010)); //TIME::currentTime()
_silent = false;
}
/**
* @brief runUntil starts the simulation and stops when the next event is scheduled after t.
* @param t is the limit time for the simulation.
* @return the TIME of the next event to happen when simulation stopped.
*/
TIME runUntil(const TIME& t) noexcept
{
Time::SetStartTime();
if (_silent){
while (_next < t)
{
_coordinator->advanceSimulation( _next);
_next = _coordinator->next();
}
} else {
while ((Time::currentTime() < t) || ( _next < t)) // should be while currentTime is less then t
{
while(Time::currentTime() < _next){
MSG input_message;
if((_driver->get_hardware_event(input_message))){ // Hardware Event Detected
_coordinator->postHardwareEvent(input_message);
_next = Time::currentTime();
break;
}
}
auto out = _coordinator->collectOutputs(_next);
if (!out.empty()) process_output(_next, out);
_coordinator->advanceSimulation( _next);
_next = _coordinator->next();
}
}
return _next;
}
/**
* @brief runUntilPassivate starts the simulation and stops when there is no next internal event to happen.
*/
void runUntilPassivate() noexcept
{
if (_silent){
while ( _next != infinity )
{
_coordinator->advanceSimulation( _next);
_next = _coordinator->next();
}
} else {
while ( _next != infinity)
{
while(Time::currentTime() < _next){
MSG input_message;
if(_driver->get_hardware_event(input_message)){ // Hardware Event Detected
_coordinator->postHardwareEvent(input_message);
_next = Time::currentTime();
break;
}
}
auto out = _coordinator->collectOutputs(_next);
if (!out.empty()) process_output(_next, out);
_coordinator->advanceSimulation( _next);
_next = _coordinator->next();
}
}
}
};
}
}
}
#endif // BOOST_SIMULATION_PDEVS_ERUNNER_H