← Documentation
ADS-IDAC Engine 5.0.0
RoutineMonitor.cpp
Go to the documentation of this file.
1// RoutineMonitor.cpp: implementation of the RoutineMonitor class.
2//
4
5#include <iostream>
6#include <fstream>
7
8#include "RoutineMonitor.h"
9#include "ReasonMachine.h"
10#include "../Crew/OperatorDM.h"
11#include "../Global/SimulationContext.h"
12
13using namespace std;
14
15
17// Construction/Destruction
19
21 this->m_routine_monitor_Token = 11; //default, the next step is to get a monitor unit;
22 this->m_read_value_or_rate = 0; //0--read parameter value; 1--read parameter rate;
23 this->m_workon_unit_pt = NULL;
24}
25
27/*
28 vector<RoutineMonitorUnit *> m_monitor_list;
29*/
30 this->m_monitor_list.clear();
31}
32
34 //To be modified
35 //Based on the relevance and the last time monitored
36 RoutineMonitorUnit *unit_pt = NULL;
37 float high_score, score;
38 double update_time;
39 for (auto &e : this->m_monitor_list) {
40 update_time = e->GetLastUpdateTime();
41 if (update_time == 0) {
42 this->m_workon_unit_pt = e.get();
43 this->m_routine_monitor_Token = 12;
44 return 1;
45 }
46 score = (r5pars_.timesno - update_time) / e->m_desired_monitor_interval;
47 if (unit_pt == NULL) {
48 unit_pt = e.get();
49 high_score = score;
50 } else if (score > high_score) {
51 unit_pt = e.get();
52 high_score = score;
53 }
54 }
55 //the highest-scoring unit is selected only when its score (elapsed time over desired interval) exceeds 1.0
56 if (high_score > 1.0) {
57 //
58 this->m_workon_unit_pt = unit_pt;
59 this->m_routine_monitor_Token = 12;
60 }
61 return high_score;
62}
63
65 string prompt;
66 MRParameterUnit *MR_P_pt;
67 MRComponentStateUnit *MR_C_pt;
69 MRAlarmUnit *MR_A_pt;
70 ReasonMachine *RM_pt;
71 double time_consumption;
72 string rm_aux_info;
73
74 int operator_branch_num = 0;
75 if (this->m_operator == sim::Operator::ODM) {
76 RM_pt = g_sim.Decision_Maker.m_RM.get();
77 operator_branch_num = g_sim.Branch_center.getNumOfODMBranch();
78 } else if (this->m_operator == sim::Operator::OAT) {
79 RM_pt = g_sim.Action_Taker.m_RM.get();
80 operator_branch_num = g_sim.Branch_center.getNumOfOATBranch();
81 } else {}//add error message
82
83 if (operator_branch_num > 0) {
85 return;
86 }
87
88 //To be modified
89 //Send the request to get indication reading
90 if (this->m_workon_unit_pt->m_MR_unit_pt == NULL) {//error message;
91 return;
92 }
93
94 //To be modified with PSF
95 switch (this->m_workon_unit_pt->m_MR_unit_pt->m_ID_Type) {
96 case 201: MR_P_pt = (MRParameterUnit *) (this->m_workon_unit_pt->m_MR_unit_pt);
97 time_consumption = this->m_time_cost_for_P;
98 if (this->m_read_value_or_rate == 0) {
99 MR_P_pt->ReadParameterValue(time_consumption);
100 this->m_read_value_or_rate = 1;
101 prompt = "Send request to read: ";
102 prompt += MR_P_pt->m_parameter_indicator_ID;
103 this->PrintMessage(prompt);
104 //
105 rm_aux_info = "Reading requested by routine monitoring: " + MR_P_pt->m_parameter_indicator_ID;
106 RM_pt->WriteAuxInfo(rm_aux_info);
107 } else {
108 MR_P_pt->ReadParameterRate(time_consumption);
109 this->m_read_value_or_rate = 0;
110 prompt = "Send request to read: ";
111 prompt += MR_P_pt->m_parameter_rate_indicator_ID;
112 this->PrintMessage(prompt);
113 this->m_routine_monitor_Token = 11;
114 }
115 break;
116 case 202: MR_C_pt = (MRComponentStateUnit *) (this->m_workon_unit_pt->m_MR_unit_pt);
117 time_consumption = this->m_time_cost_for_C;
118 MR_C_pt->ReadComponentStateIndication(time_consumption);
119
120 prompt = "Send request to read: ";
121 if (MR_C_pt->m_CS_type == 0) {
122 prompt += MR_C_pt->Component_State_Indicator_ID;
123 rm_aux_info = "Reading requested by routine monitoring: " + MR_C_pt->Component_State_Indicator_ID;
124 } else {
125 prompt += MR_C_pt->Control_Value_Indicator_ID;
126 rm_aux_info = "Reading requested by routine monitoring: " + MR_C_pt->Control_Value_Indicator_ID;
127 }
128 this->PrintMessage(prompt);
129 RM_pt->WriteAuxInfo(rm_aux_info);
130
131 //Change the token value
132 this->m_routine_monitor_Token = 11;
133 break;
134 case 203: MR_A_pt = (MRAlarmUnit *) (this->m_workon_unit_pt->m_MR_unit_pt);
135 time_consumption = this->m_time_cost_for_A;
136 MR_A_pt->ReadAlarmIndication(time_consumption);
137
138 prompt = "Send request to read: ";
139 prompt += MR_A_pt->m_alarm_indicator_name;
140 this->PrintMessage(prompt);
141
142 rm_aux_info = "Reading requested by routine monitoring: " + MR_A_pt->m_alarm_indicator_name;
143 RM_pt->WriteAuxInfo(rm_aux_info);
144
145 //Change the token value
146 this->m_routine_monitor_Token = 11;
147 break;
148 case 204: MR_SS_pt = (MRSupportSystemStateUnit *) (this->m_workon_unit_pt->m_MR_unit_pt);
149 time_consumption = this->m_time_cost_for_SS;
150 MR_SS_pt->ReadSupportSystemStateIndication(time_consumption);
151
152 prompt = "Send request to read: ";
153 prompt += MR_SS_pt->Support_System_State_Indicator_ID;
154 this->PrintMessage(prompt);
155
156 rm_aux_info = "Reading requested by routine monitoring: " + MR_SS_pt->Support_System_State_Indicator_ID;
157 RM_pt->WriteAuxInfo(rm_aux_info);
158
159 //Change the token value
160 this->m_routine_monitor_Token = 11;
161 break;
162 default:
163 //error message, unrecognized indication
164 break;
165 }
166 RM_pt->AddConsumptedDeltaT(time_consumption);
167
168}
169
171 string directory, str, str1, str_ID, file_name;
172 ifstream fin;
173 int n_Unit, i;
174 double v1;
175 RoutineMonitorUnit *M_pt;
176 string err_mes;
177
178 //get the input file directory
179 if (this->m_operator == sim::Operator::ODM) {
180 file_name = g_sim.SScheduler.getODMDirectory();
181 file_name += "RM/";
182 file_name += "KB_ODM_Routine_Monitor_List.txt";
183 } else if (this->m_operator == sim::Operator::OAT) {
184 file_name = g_sim.SScheduler.getOATDirectory();
185 file_name += "RM/";
186 file_name += "KB_OAT_Routine_Monitor_List.txt";
187 } else {}//error message
188
189 fin.open(file_name.c_str(), ios::in);
190 if (!fin.is_open()) {
191 err_mes = "could not open file: " + file_name;
192 this->ErrorMessage(err_mes);
193 return;
194 } else {
195 //start to read information from the input file
196 fin >> str >> v1;
197 if (str.compare("Time_Cost_For_Reading_A_Parameter:") != 0) {
198 err_mes = "Time_Cost_For_Reading_A_Parameter is missing in Routine_Monitor_List txt file";
199 this->ErrorMessage(err_mes);
200 return;
201 }
202 this->m_time_cost_for_A = v1;
203
204 fin >> str >> v1;
205 if (str.compare("Time_Cost_For_Reading_A_Component_State:") != 0) {
206 err_mes = "Time_Cost_For_Reading_A_Component_State is missing in Routine_Monitor_List txt file";
207 this->ErrorMessage(err_mes);
208 return;
209 }
210 this->m_time_cost_for_P = v1;
211
212 fin >> str >> v1;
213 if (str.compare("Time_Cost_For_Reading_A_Support_System_State:") != 0) {
214 err_mes = "Time_Cost_For_Reading_A_Support_System_State is missing in Routine_Monitor_List txt file";
215 this->ErrorMessage(err_mes);
216 return;
217 }
218 this->m_time_cost_for_SS = v1;
219
220 fin >> str >> v1;
221 if (str.compare("Time_Cost_For_Reading_A_Alarm:") != 0) {
222 err_mes = "Time_Cost_For_Reading_A_Alarm is missing in Routine_Monitor_List txt file";
223 this->ErrorMessage(err_mes);
224 return;
225 }
226 this->m_time_cost_for_C = v1;
227
228 fin >> str >> n_Unit;
229
230 if (str.compare("Number_of_Monitor_Item") != 0) {
231 err_mes = "Number_of_Monitor_Item is missing in Routine_Monitor_List txt file";
232 this->ErrorMessage(err_mes);
233 return;
234 }
235 string ID_name;
236 stringstream conv;
237 for (i = 0; i < n_Unit; i++) {
238 conv.str("");
239 conv << this->m_monitor_list.size();
240 ID_name = "Routine_Monitor_" + conv.str();
241 auto M_owned = std::make_unique<RoutineMonitorUnit>();
242 M_pt = M_owned.get();
243 M_pt->m_ID = ID_name;
244 M_pt->m_operator = this->m_operator;
246 this->m_monitor_list.push_back(std::move(M_owned));
247 }
248 fin.close();
249 }
250}
251
252//DEL void RoutineMonitor::SendMonitorRequestToOneIndication()
253//DEL {
254//DEL
255//DEL }
256
258 cout << r5pars_.timesno << " " << str << " in RoutineMonitor.cpp" << '\n';
259 ofstream fout("err messages.txt", ios::app);
260 fout << r5pars_.timesno << " " << str << '\n';
261 fout.close();
262 //r5pars_.terminateall = true;
263}
264
266 cout << "\n";
267 cout << "Time: " << r5pars_.timesno << ":\n" << str << '\n';
268
269 //test
270 ofstream fout("RMI.txt", ios::app);
271 fout << "\n" << "Time: " << r5pars_.timesno << ":\n" << str << '\n';
272 fout.close();
273}
274
276 this->m_operator = _M_pt->m_operator;
283
284 RoutineMonitorUnit *monitor_pt;
285
286 for (auto &e : _M_pt->m_monitor_list) {
287 auto monitor_owned = std::make_unique<RoutineMonitorUnit>();
288 monitor_pt = monitor_owned.get();
289 monitor_pt->CopyRMUinfoFromAnother(e.get());
290 this->m_monitor_list.push_back(std::move(monitor_owned));
291
292 if (_M_pt->m_workon_unit_pt == e.get()) { this->m_workon_unit_pt = monitor_pt; }
293 }
294
295}
296
298 ReasonMachine *RM_pt;
299 RM_pt = (ReasonMachine *) _pt;
300 for (auto &e : this->m_monitor_list) {
301 e->BuildConnectionsForThisRoutineMonitorUnit(_pt);
302 }
303}
304
306 float highest_score, score;
307 double update_time;
308
309 ReasonMachine *RM_pt;
310 if (this->m_operator == sim::Operator::ODM) { RM_pt = g_sim.Decision_Maker.m_RM.get(); }
311 else if (this->m_operator == sim::Operator::OAT) { RM_pt = g_sim.Action_Taker.m_RM.get(); }
312 else {}//add error message
313
314 highest_score = 0;
315 for (auto &e : this->m_monitor_list) {
316 update_time = e->GetLastUpdateTime();
317 score = (r5pars_.timesno - update_time) / e->m_desired_monitor_interval;
318 if (score > highest_score) {
319 highest_score = score;
320 }
321 }
322
323 highest_score *= 1 / RM_pt->m_routine_monitor_interval_multiplier;
324 return highest_score;
325}
Diagnostic reasoning engine — knowledge representation, mental model, and inference.
SimulationContext g_sim
Global simulation context: the single instance, defined in SimulationContext.cpp.
document e
int getNumOfOATBranch()
int getNumOfODMBranch()
int m_ID_Type
Definition: IDElement.h:15
std::string m_ID
Definition: IDElement.h:16
void ReadAlarmIndication(double)
std::string m_alarm_indicator_name
Definition: MRAlarmUnit.h:31
std::string Control_Value_Indicator_ID
void ReadComponentStateIndication(double)
std::string Component_State_Indicator_ID
void ReadParameterRate(double)
std::string m_parameter_indicator_ID
std::string m_parameter_rate_indicator_ID
void ReadParameterValue(double)
std::unique_ptr< ReasonMachine > m_RM
Definition: Operator.h:55
void WaitOneStepThereIsOneOrMoreOperatorBranches()
void AddConsumptedDeltaT(double)
void WriteAuxInfo(std::string str)
float m_routine_monitor_interval_multiplier
void ReadRoutineMonitorUnitInfoFromInputFile(std::ifstream &)
void CopyRMUinfoFromAnother(RoutineMonitorUnit *)
float m_time_cost_for_SS
void PrintMessage(std::string)
void ErrorMessage(std::string)
int m_routine_monitor_Token
float GetTheLongestIntervalRatio()
float m_time_cost_for_C
void BuildConnectionsForRoutineMonitor(void *)
void ReadRoutineMonitorInforFromInputFile()
float m_time_cost_for_P
RoutineMonitorUnit * m_workon_unit_pt
virtual ~RoutineMonitor()
float m_time_cost_for_A
std::vector< std::unique_ptr< RoutineMonitorUnit > > m_monitor_list
bool m_read_value_or_rate
void CopyRoutineMonitorInfoFromAnother(RoutineMonitor *)
float GetaMonitorUnit11()
void WorkOnASelectedUnit12()
std::string getOATDirectory()
Definition: Scheduler.cpp:622
std::string getODMDirectory()
Definition: Scheduler.cpp:619
struct @0 r5pars_
constexpr int ODM
Definition: SimConstants.h:145
constexpr int OAT
Definition: SimConstants.h:144
Scheduler SScheduler
Simulation scheduling, parses CLI options.
OperatorAT Action_Taker
Crew action execution model.
OperatorDM Decision_Maker
Crew decision-making model.
BranchRecord Branch_center
Dynamic event tree state.