← Documentation
ADS-IDAC Engine 5.0.0
MRSupportSystemStateUnit.cpp
Go to the documentation of this file.
2#include "../Crew/OperatorDM.h"
3#include <sstream>
4#include "../Global/SimulationContext.h"
5using namespace std;
6
7
9 this->m_ID_Type = 204;
10 this->m_Concept_ID_pt = NULL;
11 this->m_recent_state_statement = NULL;
12 this->m_recent_action_statement = NULL;
13 this->m_concept_ID = "";
17 //201--MRParameterUnit
18 //202--MRComponentStateUnit
19 //203--MRAlarmUnit
20 //204--MRSupportSystemStateUnit
21
22}
24 this->m_history_state.clear();
25}
27 ReasonMachine *RM_pt;
28 Investigator *invest_pt;
29 if (_operator == sim::Operator::ODM) { RM_pt = g_sim.Decision_Maker.m_RM.get(); }
30 else if (_operator == sim::Operator::OAT) { RM_pt = g_sim.Action_Taker.m_RM.get(); }
31 else {}//add error message
32 //update the relevant investigation
33 if (this->m_recent_action_statement != NULL) {
34 if (this->m_recent_action_statement->m_ID_investigator.compare("") != 0) {
36 if (invest_pt != NULL) {
37 if (invest_pt->m_investigator_token < 3029) {
38 invest_pt->m_last_active_time = r5pars_.timesno;
39 if (invest_pt->m_investigation_closed == true) {
40 invest_pt->ReopenThisInvestigator();
41 }
42 }
43 }
44 }
45 }
46
47 if (this->m_recent_state_statement != NULL) {
48 if (this->m_recent_state_statement->m_ID_investigator.compare("") != 0) {
50 if (invest_pt != NULL) {
51 if (invest_pt->m_investigator_token < 3029) {
52 invest_pt->m_last_active_time = r5pars_.timesno;
53 if (invest_pt->m_investigation_closed == true) {
54 invest_pt->ReopenThisInvestigator();
55 }
56 }
57 }
58 }
59 }
60}
62 ReasonMachine *RM_pt;
63 RM_pt = (ReasonMachine *) _pt;
64
65 if (this->m_concept_ID.compare("") != 0) {
66 this->m_Concept_ID_pt = RM_pt->m_Concept_Base->FindConceptByID(this->m_concept_ID);
67 if (this->m_Concept_ID_pt == NULL) {
68 this->ErrorMessage("could not find the concept " + this->m_concept_ID);
69 return;
70 }
71 }
72 if (this->m_ID_recent_action_statement.compare("") != 0) {
74 if (this->m_recent_action_statement == NULL) {
75 this->ErrorMessage("could not find the concept " + this->m_ID_recent_action_statement);
76 return;
77 }
78 }
80
81 if (this->m_ID_recent_state_statement.compare("") != 0) {
83 if (this->m_recent_state_statement == NULL) {
84 this->ErrorMessage("could not find the concept " + this->m_ID_recent_state_statement);
85 return;
86 }
87 }
89}
92 this->m_operator = _s_pt->m_operator;
94
95 if(_s_pt->m_Concept_ID_pt != NULL)
96 {this->m_concept_ID = _s_pt->m_Concept_ID_pt->m_ID;}
97 else
98 {this->m_concept_ID = _s_pt->m_concept_ID;}
99
100 //statement
101 if(_s_pt->m_recent_action_statement != NULL)
103 else
105
106 if(_s_pt->m_recent_state_statement != NULL)
108 else
110
111 //recent reading
112 if(_s_pt->m_recent_state)
113 {
114 this->m_recent_state = make_unique<MRStateSchema>();
115 this->m_recent_state->CopyStateSchemaFromAnother(_s_pt->m_recent_state.get());
116 }
117 for(auto &e : _s_pt->m_history_state)
118 {
119 auto state_pt = make_unique<MRStateSchema>();
120 state_pt->CopyStateSchemaFromAnother(e.get());
121 this->m_history_state.push_back(std::move(state_pt));
122 }
123}
125 string prompt, str;
126
127 bool state_bl;
128 SentenceUnit * found_sent_pt;
129 ReasonMachine * RM_pt;
130 Statement * statement_pt;
131 bool NewStatement_bl = false;
132
133 if (_operator == sim::Operator::ODM)
134 {RM_pt = g_sim.Decision_Maker.m_RM.get();}
135 else if (_operator == sim::Operator::OAT)
136 {RM_pt = g_sim.Action_Taker.m_RM.get();}
137 else
138 {}//add error message
139 //convert the key words into 0/1
140 if (_state == sim::State::On || _state == sim::State::Open)
141 {
142 state_bl = true;
143 str = "ON";
144 }
145 else if(_state == sim::State::Off || _state == sim::State::Close)
146 {
147 state_bl = false;
148 str = "OFF";
149 }
150
151 prompt = "Received new reading of ";
152 prompt += this->Support_System_State_Indicator_ID;
153 prompt += ": ";
154 prompt += str;
155 this->PrintMessage(prompt);
156
157 //check whether this is consistent with what is observed before
158 if((this->m_recent_state != NULL) && (this->m_recent_state->m_perceived_state != state_bl))
159 {
160 //state changed
161 NewStatement_bl = true;
162 if(state_bl == true)
163 {
164 //add one statement: "xxx turn_on"
165 SentenceUnit turn_on_sent;
166 turn_on_sent.m_sentence_type = 1;
167 turn_on_sent.m_subject_pt = this->m_Concept_ID_pt;
168 turn_on_sent.m_process_pt = RM_pt->m_Concept_Base->getTurnOnPt();
169 found_sent_pt = RM_pt->CheckExistInSemanticPoolBySentence(&turn_on_sent);
170 }
171 else
172 {
173 //add one statement to say it is turned off
174 SentenceUnit turn_off_sent;
175 turn_off_sent.m_sentence_type = 1;
176 turn_off_sent.m_subject_pt = this->m_Concept_ID_pt;
177 turn_off_sent.m_process_pt = RM_pt->m_Concept_Base->getTurnOffPt();
178 found_sent_pt = RM_pt->CheckExistInSemanticPoolBySentence(&turn_off_sent);
179 }
180 if(found_sent_pt != 0)
181 {
182 auto stmt = make_unique<Statement>();
183 stmt->m_factual_type = 1;
184 stmt->m_confidence =1;
185 stmt->m_sent_pt = found_sent_pt;
186 stmt->m_state = true;
187 stmt->m_time_range.m_time_after = this->m_recent_state->m_time_perceived;
188 stmt->m_time_range.m_time_before = r5pars_.timesno;
189 statement_pt = stmt.get();
190 RM_pt->AddANewStatement(stmt.release());
191 this->m_recent_action_statement = statement_pt;
192 }
193 }
194 else if(this->m_recent_state == NULL)
195 {
196 NewStatement_bl = true;
197 }
198 else
199 {
200 NewStatement_bl = false;
201 //the state has not changed
202 if(this->m_recent_state_statement != NULL)
203 {
206 }
207 else
208 {}//error message
209 }
210 if(NewStatement_bl == true)
211 {
212 if(state_bl == true)
213 {
214 //add one statement to say "xxx is on"
215 SentenceUnit is_on_sent;
216 is_on_sent.m_sentence_type = 2;
217 is_on_sent.m_subject_pt = this->m_Concept_ID_pt;
218 is_on_sent.m_state_pt = RM_pt->m_Concept_Base->getONpt();
219 found_sent_pt = RM_pt->CheckExistInSemanticPoolBySentence(&is_on_sent);
220 }
221 else
222 {
223 //add one statement to say "xxx is OFF"
224 SentenceUnit is_off_sent;
225 is_off_sent.m_sentence_type = 2;
226 is_off_sent.m_subject_pt = this->m_Concept_ID_pt;
227 is_off_sent.m_state_pt = RM_pt->m_Concept_Base->getOFFpt();
228 found_sent_pt = RM_pt->CheckExistInSemanticPoolBySentence(&is_off_sent);
229 }
230 if(found_sent_pt != 0)
231 {
232 auto stmt = make_unique<Statement>();
233 stmt->m_factual_type = 1;
234 stmt->m_confidence =1;
235 stmt->m_sent_pt = found_sent_pt;
236 stmt->m_state = true;
237 stmt->m_time_generated = r5pars_.timesno;
238 stmt->m_time_range.m_start_time = r5pars_.timesno;
239 if(this->m_recent_state)
240 {stmt->m_time_range.m_time_after = this->m_recent_state->m_time_perceived;}
241 stmt->m_time_range.m_end_time = r5pars_.timesno;
242 statement_pt = stmt.get();
243 RM_pt->AddANewStatement(stmt.release());
244 this->m_recent_state_statement = statement_pt;
245 statement_pt->m_tbi_bl = false;
246 }
247 else
248 {}//warning message/error message
249 }
250
251 if(this->m_recent_state)
252 {
253 this->m_history_state.push_back(std::move(this->m_recent_state));
254 }
255 this->m_recent_state = make_unique<MRStateSchema>();
256 this->m_recent_state->m_perceived_state = state_bl;
257 this->m_recent_state->m_time_perceived = r5pars_.timesno;
258 //To be added, compare the current state with the expectation
259}
261 Operator * operator_pt;
262 switch (this->m_operator)
263 {
265 operator_pt = &g_sim.Action_Taker;
266 break;
268 operator_pt = &g_sim.Decision_Maker;
269 break;
270 default:
271 break;
272 //error message
273 }
274 //be careful, verify an uninitiated string variable is ""
276}
277void MRSupportSystemStateUnit::AddNewState(int _state, int _operator, bool _expecting) {
278 string ID_name;
279 string prompt, str;
280 stringstream conv;
281
282
283 if(r5pars_.timesno > 969)
284 {this->PrintMessage("check");}
285
286 bool state_bl;
287 SentenceUnit * found_sent_pt;
288 ReasonMachine * RM_pt;
289 Investigator * invest_pt;
290 Statement * statement_pt;
291 bool NewStatement_bl = false;
292
293 if (_operator == sim::Operator::ODM)
294 {RM_pt = g_sim.Decision_Maker.m_RM.get();}
295 else if (_operator == sim::Operator::OAT)
296 {RM_pt = g_sim.Action_Taker.m_RM.get();}
297 else
298 {}//add error message
299 //convert the key words into 0/1
300 if (_state == sim::State::On || _state == sim::State::Open)
301 {
302 state_bl = true;
303 str = "ON";
304 } else if (_state == 3023 || _state == 3099)
305 {
306 state_bl = false;
307 str = "OFF";
308 }
309
310 prompt = "Received new reading of ";
311 prompt += this->Support_System_State_Indicator_ID;
312 prompt += ": ";
313 prompt += str;
314 this->PrintMessage(prompt);
315
316 RM_pt->AddDeltaToCognitiveLoad(0.024);
317 //check whether this is consistant with what is observed before
318 if((this->m_recent_state != NULL) && (this->m_recent_state->m_perceived_state != state_bl))
319 {
320 //state changed
321 NewStatement_bl = true;
322 if(state_bl == true)
323 {
324 //add one statement: "xxx turn_on"
325 SentenceUnit turn_on_sent;
326 turn_on_sent.m_sentence_type = 1;
327 turn_on_sent.m_subject_pt = this->m_Concept_ID_pt;
328 turn_on_sent.m_process_pt = RM_pt->m_Concept_Base->getTurnOnPt();
329 found_sent_pt = RM_pt->CheckExistInSemanticPoolBySentence(&turn_on_sent);
330 }
331 else
332 {
333 //add one statement to say it is turned off
334 SentenceUnit turn_off_sent;
335 turn_off_sent.m_sentence_type = 1;
336 turn_off_sent.m_subject_pt = this->m_Concept_ID_pt;
337 turn_off_sent.m_process_pt = RM_pt->m_Concept_Base->getTurnOffPt();
338 found_sent_pt = RM_pt->CheckExistInSemanticPoolBySentence(&turn_off_sent);
339 }
340 if(found_sent_pt != 0)
341 {
342 auto stmt = make_unique<Statement>();
343 stmt->m_factual_type = 1;
344 stmt->m_confidence =1;
345 stmt->m_sent_pt = found_sent_pt;
346 stmt->m_state = true;
347 stmt->m_time_range.m_time_after = this->m_recent_state->m_time_perceived;
348 stmt->m_time_range.m_time_before = r5pars_.timesno;
349 statement_pt = stmt.get();
350 RM_pt->AddANewStatement(stmt.release());
351 this->m_recent_action_statement = statement_pt;
352 RM_pt->AddDeltaToSystemDynamics(0.12);
353
354 if(_expecting == true)
355 {
357 if(sent_pt->m_sentence_type == statement_pt->m_sent_pt->m_sentence_type)
358 {
359 RM_pt->m_working_on_invest_pt->m_Statement_pt = statement_pt;
360 RM_pt->AddDeltaToCognitiveLoad(0.024);
361 statement_pt->m_investigator_ID_pt = (IDElement*)(RM_pt->m_working_on_invest_pt);
362 }
363 }
364
365 //Add an investigator for this statement
366 if(statement_pt->m_investigator_ID_pt == NULL)
367 {
368 auto inv = make_unique<Investigator>();
369 inv->m_note = "by a statement";
370 inv->m_motivation = 1;
371 inv->m_Statement_pt = statement_pt;
372 RM_pt->AddDeltaToCognitiveLoad(0.024);
373 inv->m_Happen = 3;
374 inv->m_operator = this->m_operator;
375 inv->m_root_of_a_thought_line = true;
376 invest_pt = inv.get();
377 RM_pt->AddNewInvestigator(inv.release());
378 //link the statement to the statement pointer
379 statement_pt->m_investigator_ID_pt = (IDElement*)invest_pt;
380 }
381 }
382 }
383 else if(this->m_recent_state == NULL)
384 {
385 NewStatement_bl = true;
386 }
387 else
388 {
389 NewStatement_bl = false;
390 //the state has not changed
391 if(this->m_recent_state_statement != NULL)
392 {
395 }
396 else
397 {}//error message
398 }
399 if(NewStatement_bl == true)
400 {
401 if(state_bl == true)
402 {
403 //add one statement to say "xxx is on"
404 SentenceUnit is_on_sent;
405 is_on_sent.m_sentence_type = 2;
406 is_on_sent.m_subject_pt = this->m_Concept_ID_pt;
407 is_on_sent.m_state_pt = RM_pt->m_Concept_Base->getONpt();
408 found_sent_pt = RM_pt->CheckExistInSemanticPoolBySentence(&is_on_sent);
409 }
410 else
411 {
412 //add one statement to say "xxx is OFF"
413 SentenceUnit is_off_sent;
414 is_off_sent.m_sentence_type = 2;
415 is_off_sent.m_subject_pt = this->m_Concept_ID_pt;
416 is_off_sent.m_state_pt = RM_pt->m_Concept_Base->getOFFpt();
417 found_sent_pt = RM_pt->CheckExistInSemanticPoolBySentence(&is_off_sent);
418 }
419 if(found_sent_pt != 0)
420 {
421 auto stmt = make_unique<Statement>();
422 stmt->m_factual_type = 1;
423 stmt->m_confidence =1;
424 stmt->m_sent_pt = found_sent_pt;
425 stmt->m_state = true;
426 stmt->m_time_generated = r5pars_.timesno;
427 stmt->m_time_range.m_start_time = r5pars_.timesno;
428 if(this->m_recent_state)
429 {stmt->m_time_range.m_time_after = this->m_recent_state->m_time_perceived;}
430 stmt->m_time_range.m_end_time = r5pars_.timesno;
431 statement_pt = stmt.get();
432 RM_pt->AddANewStatement(stmt.release());
433 this->m_recent_state_statement = statement_pt;
434 statement_pt->m_tbi_bl = false;
435
436 if(_expecting == true)
437 {
439 if(sent_pt->m_sentence_type == statement_pt->m_sent_pt->m_sentence_type)
440 {
441 RM_pt->m_working_on_invest_pt->m_Statement_pt = statement_pt;
442 RM_pt->AddDeltaToCognitiveLoad(0.024);
443 statement_pt->m_investigator_ID_pt = (IDElement*)(RM_pt->m_working_on_invest_pt);
444 }
445 }
446 }
447 else
448 {}//warning message/error message
449 }
450
451 if(this->m_recent_state)
452 {
453 this->m_history_state.push_back(std::move(this->m_recent_state));
454 }
455 this->m_recent_state = make_unique<MRStateSchema>();
456 this->m_recent_state->m_perceived_state = state_bl;
457 this->m_recent_state->m_time_perceived = r5pars_.timesno;
458 //To be added, compare the current state with the expectation
459
460 this->UpdateTheRelevantInvestigator(_operator);
461}
463 MRStateSchema * found_state_schema_pt = NULL;
464
465 for(auto &e : this->m_history_state)
466 {
467 if(e->m_time_perceived > _end)
468 {
469 found_state_schema_pt = e.get();
470 return found_state_schema_pt;
471 }
472 }
473
474 if(this->m_recent_state->m_time_perceived > _end)
475 {
476 found_state_schema_pt = this->m_recent_state.get();
477 }
478 return found_state_schema_pt;
479}
481 MRStateSchema * found_state_schema_pt = NULL;
482
483 if(this->m_recent_state == NULL)
484 {return NULL;}
485
486 if(this->m_recent_state->m_time_perceived < _start)
487 {
488 found_state_schema_pt = this->m_recent_state.get();
489 return found_state_schema_pt;
490 }
491 else
492 {
493 for(auto ritr = this->m_history_state.rbegin(); ritr != this->m_history_state.rend(); ++ritr)
494 {
495 if((*ritr)->m_time_perceived < _start)
496 {
497 found_state_schema_pt = ritr->get();
498 return found_state_schema_pt;
499 }
500 }
501 }
502 return found_state_schema_pt;
503}
505 MRStateSchema * found_state_schema_pt = NULL;
506 if(this->m_recent_state->m_time_perceived >= _start && this->m_recent_state->m_time_perceived <= _end)
507 {
508 found_state_schema_pt = this->m_recent_state.get();
509 return found_state_schema_pt;
510 }
511 else if(this->m_recent_state->m_time_perceived > _end)
512 {
513 for(auto &e : this->m_history_state)
514 {
515 if(e->m_time_perceived >= _start && e->m_time_perceived <= _end)
516 {
517 found_state_schema_pt = e.get();
518 }
519 }
520 }
521 return found_state_schema_pt;
522}
523
525{
526 cout << "Time: " << r5pars_.timesno << ": " << str << '\n';
527
528 //test
529 ofstream fout("RMI.txt", ios::app);
530 fout << "Time: " << r5pars_.timesno << ": " << str << '\n';
531 fout.close();
532}
533
535{
536 cout << r5pars_.timesno << " " << str << " in MRSupportSystemStateUnit.cpp" << '\n';
537 ofstream fout("err messages.txt", ios::app);
538 fout << r5pars_.timesno << " " << str << '\n';
539 fout.close();
540}
SimulationContext g_sim
Global simulation context: the single instance, defined in SimulationContext.cpp.
document e
void CopyIDElementToRestartInfo(IDElement *)
Definition: IDElement.cpp:23
int m_ID_Type
Definition: IDElement.h:15
std::string m_ID
Definition: IDElement.h:16
float m_last_active_time
Definition: Investigator.h:87
bool m_investigation_closed
Definition: Investigator.h:88
SentenceUnit * GetSemanticSentenceOfThisInvestigator()
void ReopenThisInvestigator()
int m_investigator_token
Definition: Investigator.h:99
Statement * m_Statement_pt
Definition: Investigator.h:130
MRStateSchema * CheckLastSupportSystemStateReadingBeforeATime(float _start)
MRStateSchema * CheckTheFirstSupportSystemStateReadingAfterATime(float _end)
std::unique_ptr< MRStateSchema > m_recent_state
std::vector< std::unique_ptr< MRStateSchema > > m_history_state
void CopyMRSupportSystemStateUnitInfoFromAnother(MRSupportSystemStateUnit *)
void BuildConnectionsForThisMRSupportSystemStateUnit(void *)
MRStateSchema * CheckSupportSystemStateWithinTimeRange(float _start, float _end)
std::unique_ptr< ReasonMachine > m_RM
Definition: Operator.h:55
void ActiveFetchSupportSystemStateInfo(std::string, int)
Definition: Operator.cpp:1480
std::unique_ptr< KBConcept > m_Concept_Base
void ActivationUpdateByRefreshAStatement(Statement *)
Investigator * m_working_on_invest_pt
void AddDeltaToCognitiveLoad(float)
void AddANewStatement(Statement *)
Statement * FindStatementByID(std::string)
Investigator * FindInvestigatorByID(std::string)
SentenceUnit * CheckExistInSemanticPoolBySentence(SentenceUnit *)
void AddDeltaToSystemDynamics(float)
void AddNewInvestigator(Investigator *)
IDElement * m_process_pt
Definition: SentenceUnit.h:38
int m_sentence_type
Definition: SentenceUnit.h:34
IDElement * m_state_pt
Definition: SentenceUnit.h:36
IDElement * m_subject_pt
Definition: SentenceUnit.h:40
bool m_tbi_bl
Definition: Statement.h:39
SentenceUnit * m_sent_pt
Definition: Statement.h:43
void RefreshStatementEndTimeToCurrentTime(int)
Definition: Statement.cpp:73
IDElement * m_investigator_ID_pt
Definition: Statement.h:42
std::string m_ID_investigator
Definition: Statement.h:48
struct @0 r5pars_
Definition: RMTokens.h:21
constexpr int ODM
Definition: SimConstants.h:145
constexpr int OAT
Definition: SimConstants.h:144
constexpr int On
Definition: SimConstants.h:105
constexpr int Off
Definition: SimConstants.h:106
constexpr int Open
Definition: SimConstants.h:116
constexpr int Close
Definition: SimConstants.h:117
OperatorAT Action_Taker
Crew action execution model.
OperatorDM Decision_Maker
Crew decision-making model.