AERA
g_monitor.cpp
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2025 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2025 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2025 Icelandic Institute for Intelligent Machines
9 //_/_/ http://www.iiim.is
10 //_/_/
11 //_/_/ Copyright (c) 2010-2012 Eric Nivel
12 //_/_/ Center for Analysis and Design of Intelligent Agents
13 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
14 //_/_/ http://cadia.ru.is
15 //_/_/
16 //_/_/ Part of this software was developed by Eric Nivel
17 //_/_/ in the HUMANOBS EU research project, which included
18 //_/_/ the following parties:
19 //_/_/
20 //_/_/ Autonomous Systems Laboratory
21 //_/_/ Technical University of Madrid, Spain
22 //_/_/ http://www.aslab.org/
23 //_/_/
24 //_/_/ Communicative Machines
25 //_/_/ Edinburgh, United Kingdom
26 //_/_/ http://www.cmlabs.com/
27 //_/_/
28 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
29 //_/_/ University of Lugano and SUPSI, Switzerland
30 //_/_/ http://www.idsia.ch/
31 //_/_/
32 //_/_/ Institute of Cognitive Sciences and Technologies
33 //_/_/ Consiglio Nazionale delle Ricerche, Italy
34 //_/_/ http://www.istc.cnr.it/
35 //_/_/
36 //_/_/ Dipartimento di Ingegneria Informatica
37 //_/_/ University of Palermo, Italy
38 //_/_/ http://diid.unipa.it/roboticslab/
39 //_/_/
40 //_/_/
41 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
42 //_/_/
43 //_/_/ Redistribution and use in source and binary forms, with or without
44 //_/_/ modification, is permitted provided that the following conditions
45 //_/_/ are met:
46 //_/_/ - Redistributions of source code must retain the above copyright
47 //_/_/ and collaboration notice, this list of conditions and the
48 //_/_/ following disclaimer.
49 //_/_/ - Redistributions in binary form must reproduce the above copyright
50 //_/_/ notice, this list of conditions and the following disclaimer
51 //_/_/ in the documentation and/or other materials provided with
52 //_/_/ the distribution.
53 //_/_/
54 //_/_/ - Neither the name of its copyright holders nor the names of its
55 //_/_/ contributors may be used to endorse or promote products
56 //_/_/ derived from this software without specific prior
57 //_/_/ written permission.
58 //_/_/
59 //_/_/ - CADIA Clause: The license granted in and to the software
60 //_/_/ under this agreement is a limited-use license.
61 //_/_/ The software may not be used in furtherance of:
62 //_/_/ (i) intentionally causing bodily injury or severe emotional
63 //_/_/ distress to any person;
64 //_/_/ (ii) invading the personal privacy or violating the human
65 //_/_/ rights of any person; or
66 //_/_/ (iii) committing or preparing for any act of war.
67 //_/_/
68 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
69 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
70 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
71 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
73 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
74 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
75 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
76 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
81 //_/_/ OF SUCH DAMAGE.
82 //_/_/
83 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
84 
85 #include "g_monitor.h"
86 #include "mem.h"
87 #include "mdl_controller.h"
88 #include "factory.h"
89 
90 using namespace std::chrono;
91 using namespace r_code;
92 
93 namespace r_exec {
94 
95 _GMonitor::_GMonitor(PMDLController *controller,
96  BindingMap *bindings,
97  Timestamp deadline,
98  Timestamp sim_thz_timestamp,
99  Fact *goal,
100  Fact *f_imdl) : Monitor(controller,
101  bindings,
102  goal,
103  NULL),
104  deadline_(deadline),
105  sim_thz_timestamp_(sim_thz_timestamp),
106  f_imdl_(f_imdl) { // goal is f0->g->f1->object.
107 
108  simulating_ = (sim_thz_timestamp > Now());
109  sim_mode_ = goal->get_goal()->get_sim()->get_mode();
110  goal_target_ = target_->get_goal()->get_target(); // f1.
111 }
112 
113 void _GMonitor::store_simulated_outcome(_Fact *f_pred_f_success, Sim *sim) {
114 
115  if (f_pred_f_success->get_pred()->get_target()->is_fact()) {
116 
117  switch (sim->get_mode()) {
118  case SIM_MANDATORY:
119  sim_successes_.mandatory_solutions.push_back(std::pair<P<_Fact>, P<Sim> >(f_pred_f_success, sim));
120  OUTPUT_LINE(GOAL_MON, Utils::RelativeTime(Now()) << " goal monitor for " << target_->get_oid() <<
121  ": store mandatory solution " << f_pred_f_success->get_oid());
122  break;
123  case SIM_OPTIONAL:
124  sim_successes_.optional_solutions.push_back(std::pair<P<_Fact>, P<Sim> >(f_pred_f_success, sim));
125  OUTPUT_LINE(GOAL_MON, Utils::RelativeTime(Now()) << " goal monitor for " << target_->get_oid() <<
126  ": store optional solution " << f_pred_f_success->get_oid());
127  break;
128  default:
129  break;
130  }
131  } else {
132 
133  switch (sim->get_mode()) {
134  case SIM_MANDATORY:
135  sim_failures_.mandatory_solutions.push_back(std::pair<P<_Fact>, P<Sim> >(f_pred_f_success, sim));
136  OUTPUT_LINE(GOAL_MON, Utils::RelativeTime(Now()) << " goal monitor for " << target_->get_oid() <<
137  ": store mandatory solution " << f_pred_f_success->get_oid());
138  break;
139  case SIM_OPTIONAL:
140  sim_failures_.optional_solutions.push_back(std::pair<P<_Fact>, P<Sim> >(f_pred_f_success, sim));
141  OUTPUT_LINE(GOAL_MON, Utils::RelativeTime(Now()) << " goal monitor for " << target_->get_oid() <<
142  ": store optional solution " << f_pred_f_success->get_oid());
143  break;
144  default:
145  break;
146  }
147  }
148 }
149 
150 void _GMonitor::invalidate_sim_outcomes() {
151 
152  SolutionList::const_iterator solution;
153 
154  for (solution = sim_failures_.mandatory_solutions.begin(); solution != sim_failures_.mandatory_solutions.end(); ++solution)
155  (*solution).second->invalidate();
156 
157  for (solution = sim_failures_.optional_solutions.begin(); solution != sim_failures_.optional_solutions.end(); ++solution)
158  (*solution).second->invalidate();
159 
160  for (solution = sim_successes_.mandatory_solutions.begin(); solution != sim_successes_.mandatory_solutions.end(); ++solution)
161  (*solution).second->invalidate();
162 
163  for (solution = sim_successes_.optional_solutions.begin(); solution != sim_successes_.optional_solutions.end(); ++solution)
164  (*solution).second->invalidate();
165 }
166 
168 
169 GMonitor::GMonitor(PMDLController *controller,
170  BindingMap *bindings,
171  Timestamp deadline,
172  Timestamp sim_thz_timestamp,
173  Fact *goal,
174  Fact *f_imdl,
175  _Fact *predicted_evidence) : _GMonitor(controller,
176  bindings,
177  deadline,
178  sim_thz_timestamp,
179  goal,
180  f_imdl),
181  predicted_evidence_(predicted_evidence) { // goal is f0->g->f1->object.
182 
183  injected_goal_ = (predicted_evidence == NULL);
184  MonitoringJob<GMonitor> *j = new MonitoringJob<GMonitor>(this, simulating_ ? sim_thz_timestamp : deadline);
185  _Mem::Get()->push_time_job(j);
186 }
187 
188 void GMonitor::commit() { // the purpose is to invalidate damaging simulations; if anything remains, commit to all mandatory simulations and to the best optional one.
189 
190  Goal *monitored_goal = target_->get_goal();
191 
192  auto now = Now();
193 
194  SolutionList::const_iterator solution;
195 
196  for (solution = sim_failures_.mandatory_solutions.begin(); solution != sim_failures_.mandatory_solutions.end(); ++solution) { // check if any mandatory solution could result in the failure of more important a goal.
197 
198  if ((*solution).first->is_invalidated())
199  continue;
200  if ((*solution).second->is_invalidated())
201  continue;
202  Goal* goal = get_solution_goal((*solution).first);
203  if (!goal)
204  // We don't expect this.
205  continue;
206  if (goal->get_strength(now) > monitored_goal->get_strength(now)) { // cave in.
207 
208  invalidate_sim_outcomes(); // this stops any further propagation of the goal simulation.
209  return;
210  }
211  }
212 
213  for (solution = sim_failures_.optional_solutions.begin(); solution != sim_failures_.optional_solutions.end(); ++solution) { // check if any optional solutions could result in the failure of more important a goal; invalidate the culprits.
214 
215  if ((*solution).first->is_invalidated())
216  continue;
217  if ((*solution).second->is_invalidated())
218  continue;
219  Goal* goal = get_solution_goal((*solution).first);
220  if (!goal)
221  // We don't expect this.
222  continue;
223  if (goal->get_strength(now) > monitored_goal->get_strength(now))
224  (*solution).second->invalidate();
225  }
226 
227  _Fact* best_solution_f_p_f_success = NULL;
228  Sim *best_solution = NULL;
229  // Find the best optional solution.
230  for (solution = sim_successes_.optional_solutions.begin(); solution != sim_successes_.optional_solutions.end(); ++solution) {
231 
232  if ((*solution).first->is_invalidated())
233  continue;
234  if ((*solution).second->is_invalidated())
235  continue;
236  if (!best_solution) {
237  best_solution_f_p_f_success = (*solution).first;
238  best_solution = (*solution).second;
239  }
240  else {
241 
242  float32 s = (*solution).second->get_solution_cfd() / duration_cast<microseconds>((*solution).second->get_solution_before() - now).count();
243  float32 _s = best_solution->get_solution_cfd() / duration_cast<microseconds>(best_solution->get_solution_before() - now).count();
244  if (s > _s) {
245  best_solution_f_p_f_success = (*solution).first;
246  best_solution = (*solution).second;
247  }
248  }
249  }
250 
251  invalidate_sim_outcomes(); // this stops any further propagation of the goal simulation.
252 
253  if (best_solution) {
254 
255  ((PrimaryMDLController *)best_solution->solution_controller_)->abduce_no_simulation(
256  best_solution->get_f_super_goal(), best_solution->get_opposite(), goal_target_->get_cfd(), best_solution_f_p_f_success);
257 
258  // Commit to all mandatory solutions.
259  for (solution = sim_successes_.mandatory_solutions.begin(); solution != sim_successes_.mandatory_solutions.end(); ++solution)
260  ((PrimaryMDLController *)(*solution).second->solution_controller_)->abduce_no_simulation(
261  (*solution).second->get_f_super_goal(), (*solution).second->get_opposite(), goal_target_->get_cfd(), (*solution).first);
262  }
263 }
264 
265 bool GMonitor::reduce(_Fact *input) { // executed by a reduction core; invalidation check performed in Monitor::is_alive().
266 
267  if (target_->is_invalidated())
268  return true;
269 
270  if (!injected_goal_) {
271 
272  if (predicted_evidence_ && predicted_evidence_->is_invalidated()) { // the predicted evidence was wrong.
273 
274  ((PMDLController *)controller_)->register_predicted_goal_outcome(target_, bindings_, f_imdl_, false, injected_goal_); // report a predicted failure; this will inject the goal.
275  predicted_evidence_ = NULL;
276  injected_goal_ = true;
277  return false;
278  }
279  }
280 
281  Pred *prediction = input->get_pred();
282  if (prediction) { // input is f0->pred->f1->object.
283 
284  _Fact *_input = prediction->get_target(); // _input is f1->obj.
285  if (simulating_) { // injected_goal==true.
286  // Debug: It was passing target. Is it right to pass controller_?
287  Sim *sim = prediction->get_simulation(controller_);
288  if (sim) {
289 
290  Code *outcome = _input->get_reference(0);
291  if (outcome->code(0).asOpcode() == Opcodes::Success) { // _input is f1->success or |f1->success.
292 
293  _Fact *f_success = (_Fact *)outcome->get_reference(outcome->code(SUCCESS_OBJ).asIndex());
294  Goal *affected_goal = f_success->get_goal();
295  if (affected_goal) {
296 
297  store_simulated_outcome(input, sim);
298  return false;
299  }
300  } else { // report the simulated outcome: this will inject a simulated prediction of the outcome, to allow any g-monitor deciding on this ground.
301 
302  switch (_input->is_evidence(goal_target_)) {
303  case MATCH_SUCCESS_POSITIVE:
304  ((PMDLController *)controller_)->register_simulated_goal_outcome(target_, true, input); // report a simulated success.
305  return false;
306  case MATCH_SUCCESS_NEGATIVE:
307  ((PMDLController *)controller_)->register_simulated_goal_outcome(target_, false, input); // report a simulated failure.
308  return false;
309  case MATCH_FAILURE:
310  return false;
311  }
312  }
313  } else // during simulation (SIM_ROOT) if the prediction is actual, positive and comes true, we'll eventually catch an actual evidence; otherwise (positive that does not come true or negative), keep simulating: in any case ignore it.
314  return false;
315  } else {
316 
317  switch (_input->is_evidence(goal_target_)) {
318  case MATCH_SUCCESS_POSITIVE:
319  if (injected_goal_)
320  ((PMDLController *)controller_)->register_predicted_goal_outcome(target_, bindings_, f_imdl_, true, true); // report a predicted success.
321  if (predicted_evidence_ && _input->get_cfd() > predicted_evidence_->get_pred()->get_target()->get_cfd()) // bias toward cfd instead of age.
322  predicted_evidence_ = input;
323  return false;
324  case MATCH_SUCCESS_NEGATIVE:
325  ((PMDLController *)controller_)->register_predicted_goal_outcome(target_, bindings_, f_imdl_, false, injected_goal_); // report a predicted failure; this may invalidate the target.
326  predicted_evidence_ = NULL;
327  injected_goal_ = true;
328  return target_->is_invalidated();
329  case MATCH_FAILURE:
330  return false;
331  }
332  }
333  } else { // input is an actual fact.
334 
335  Goal *g = target_->get_goal();
336  if (g->ground_invalidated(input)) { // invalidate the goal and abduce from the super-goal.
337 
338  target_->invalidate();
339  // JTNote: abduce can update bindings_ . Should we pass a copy?
340  ((PrimaryMDLController *)controller_)->abduce(bindings_, g->get_sim()->get_f_super_goal(), g->get_sim()->get_opposite(), goal_target_->get_cfd());
341  return true;
342  }
343 
344  switch (input->is_evidence(goal_target_)) {
345  case MATCH_SUCCESS_POSITIVE:
346  if (input->is_fact() && goal_target_->is_anti_fact())
347  // This will be handled by GTPX::ack_pred_success, which will call register_goal_outcome.
348  return true;
349  ((PMDLController *)controller_)->register_goal_outcome(target_, true, input); // report a success.
350  return true;
351  case MATCH_SUCCESS_NEGATIVE:
352  ((PMDLController *)controller_)->register_goal_outcome(target_, false, input); // report a failure.
353  return true;
354  case MATCH_FAILURE:
355  return false;
356  }
357  }
358 
359  return false;
360 }
361 
362 void GMonitor::update(Timestamp &next_target) { // executed by a time core.
363 
364  if (target_->is_invalidated()) {
365 
366  ((PMDLController *)controller_)->remove_g_monitor(this);
367  next_target = Timestamp(seconds(0));
368  } else if (simulating_) {
369 
370  simulating_ = 0;
371  commit();
372  next_target = deadline_;
373  } else {
374 
375  ((PMDLController *)controller_)->register_goal_outcome(target_, false, NULL);
376  ((PMDLController *)controller_)->remove_g_monitor(this);
377  next_target = Timestamp(seconds(0));
378  }
379 }
380 
382 
383 RMonitor::RMonitor(PrimaryMDLController *controller,
384  BindingMap *bindings,
385  Timestamp deadline,
386  Timestamp sim_thz_timestamp,
387  Fact *goal,
388  Fact *f_imdl) : GMonitor(controller,
389  bindings,
390  deadline,
391  sim_thz_timestamp,
392  goal,
393  f_imdl,
394  NULL) { // goal is f0->g->f1->object.
395 
396  MonitoringJob<RMonitor> *j = new MonitoringJob<RMonitor>(this, deadline);
397  _Mem::Get()->push_time_job(j);
398 }
399 
400 bool RMonitor::signal(Pred* prediction) {
401 
402  if (target_->is_invalidated())
403  return true;
404 
405  // check_simulated_imdl can change the binding map, so pass a copy.
406  P<BindingMap> bindings_copy(new BindingMap(bindings_));
407  Sim* prediction_sim = prediction->get_simulation(target_->get_goal()->get_sim()->root_);
408  if (simulating_ && prediction_sim) { // report the simulated outcome: this will inject a simulated prediction of the outcome, to allow any g-monitor deciding on this ground.
409 
410  if (((PrimaryMDLController *)controller_)->check_simulated_imdl(target_, bindings_copy, prediction_sim))
411  ((PMDLController *)controller_)->register_simulated_goal_outcome(target_, true, target_); // report a simulated success.
412  else
413  ((PMDLController *)controller_)->register_simulated_goal_outcome(target_, false, NULL); // report a simulated failure.
414  return false;
415  } else if (((PrimaryMDLController *)controller_)->check_imdl(target_, bindings_copy))
416  return true;
417  return false;
418 }
419 
420 bool RMonitor::reduce(_Fact *input) { // catch simulated predictions only; requirements are observable in signal().
421 
422  if (target_->is_invalidated())
423  return true;
424 
425  Pred *prediction = input->get_pred();
426  if (prediction) { // input is f0->pred->f1->object.
427 
428  _Fact *_input = prediction->get_target(); // _input is f1->obj.
429  if (simulating_) { // injected_goal==true.
430  // Debug: It was passing target. Is it right to pass controller_?
431  Sim *sim = prediction->get_simulation(controller_);
432  if (sim) {
433 
434  Code *outcome = _input->get_reference(0);
435  if (outcome->code(0).asOpcode() == Opcodes::Success) { // _input is f1->success or |f1->success.
436 
437  _Fact *f_success = (_Fact *)outcome->get_reference(outcome->code(SUCCESS_OBJ).asIndex());
438  Goal *affected_goal = f_success->get_goal();
439  if (affected_goal) {
440 
441  store_simulated_outcome(input, sim);
442  return false;
443  }
444  }
445  }
446  }
447  }
448 
449  return false;
450 }
451 
452 void RMonitor::update(Timestamp &next_target) {
453 
454  if (target_->is_invalidated()) {
455 
456  ((PMDLController *)controller_)->remove_r_monitor(this);
457  next_target = Timestamp(seconds(0));
458  } else if (simulating_) {
459 
460  simulating_ = 0;
461  commit();
462  next_target = deadline_;
463  } else {
464 
465  ((PMDLController *)controller_)->register_goal_outcome(target_, false, NULL);
466  ((PMDLController *)controller_)->remove_r_monitor(this);
467  next_target = Timestamp(seconds(0));
468  }
469 }
470 
472 
473 SGMonitor::SGMonitor(PrimaryMDLController *controller,
474  BindingMap *bindings,
475  Timestamp sim_thz_timestamp,
476  Fact *goal,
477  Fact *f_imdl) : _GMonitor(controller,
478  bindings,
479  Timestamp(seconds(0)),
480  sim_thz_timestamp,
481  goal,
482  f_imdl) { // goal is f0->g->f1->object.
483 
484  MonitoringJob<SGMonitor> *j = new MonitoringJob<SGMonitor>(this, sim_thz_timestamp);
485  _Mem::Get()->push_time_job(j);
486 }
487 
488 void SGMonitor::commit() { // the purpose is to invalidate damaging simulations and let the rest flow upward.
489 
490  Goal *monitored_goal = target_->get_goal();
491 
492  auto now = Now();
493 
494  SolutionList::const_iterator solution;
495 
496  for (solution = sim_failures_.mandatory_solutions.begin(); solution != sim_failures_.mandatory_solutions.end(); ++solution) { // check if any mandatory solution could result in the failure of more important a goal.
497 
498  if ((*solution).second->is_invalidated())
499  continue;
500  Goal* goal = get_solution_goal((*solution).first);
501  if (!goal)
502  // We don't expect this.
503  continue;
504  if (goal->get_strength(now) > monitored_goal->get_strength(now)) { // cave in.
505 
506  (*solution).second->invalidate();
507  return;
508  }
509  }
510 
511  for (solution = sim_failures_.optional_solutions.begin(); solution != sim_failures_.optional_solutions.end(); ++solution) { // check if any optional solutions could result in the failure of more important a goal; invalidate the culprits.
512 
513  if ((*solution).second->is_invalidated())
514  continue;
515  Goal* goal = get_solution_goal((*solution).first);
516  if (!goal)
517  // We don't expect this.
518  continue;
519  if (goal->get_strength(now) > monitored_goal->get_strength(now))
520  (*solution).second->invalidate();
521  }
522 }
523 
524 bool SGMonitor::reduce(_Fact *input) {
525 
526  if (target_->is_invalidated())
527  return true;
528 
529  _Fact *_input;
530  Pred *prediction = input->get_pred();
531  if (prediction) { // input is f0->pred->f1->object.
532 
533  _input = prediction->get_target(); // _input is f1->obj.
534  // Debug: It was passing target. Is it right to pass controller_?
535  Sim *sim = prediction->get_simulation(controller_);
536  if (sim) {
537 
538  Code *outcome = _input->get_reference(0);
539  if (outcome->code(0).asOpcode() == Opcodes::Success) { // _input is f1->success or |f1->success.
540 
541  _Fact *f_success = (_Fact *)outcome->get_reference(outcome->code(SUCCESS_OBJ).asIndex());
542  Goal *affected_goal = f_success->get_goal();
543  if (affected_goal) {
544 
545  store_simulated_outcome(input, sim);
546  return false;
547  }
548  }
549  }
550  } else
551  _input = input;
552 
553  // Non-simulated input (can be actual or predicted): report the simulated outcome: this will inject a simulated prediction of the outcome, to allow any g-monitor deciding on this ground.
554  switch (_input->is_evidence(goal_target_)) {
555  case MATCH_SUCCESS_POSITIVE:
556  ((PMDLController *)controller_)->register_simulated_goal_outcome(target_, true, _input); // report a simulated success.
557  return false;
558  case MATCH_SUCCESS_NEGATIVE:
559  ((PMDLController *)controller_)->register_simulated_goal_outcome(target_, false, _input); // report a simulated failure.
560  return false;
561  case MATCH_FAILURE:
562  return false;
563  }
564 
565  return false;
566 }
567 
568 void SGMonitor::update(Timestamp &next_target) { // executed by a time core.
569 
570  if (!target_->is_invalidated())
571  commit();
572  ((PMDLController *)controller_)->remove_g_monitor(this);
573  next_target = Timestamp(seconds(0));
574 }
575 
577 
578 SRMonitor::SRMonitor(PrimaryMDLController *controller,
579  BindingMap *bindings,
580  Timestamp sim_thz_timestamp,
581  Fact *goal,
582  Fact *f_imdl) : SGMonitor(controller,
583  bindings,
584  sim_thz_timestamp,
585  goal,
586  f_imdl) { // goal is f0->g->f1->object.
587 
588  MonitoringJob<SRMonitor> *j = new MonitoringJob<SRMonitor>(this, sim_thz_timestamp);
589  _Mem::Get()->push_time_job(j);
590 }
591 
592 bool SRMonitor::signal(Pred* prediction) {
593 
594  if (target_->is_invalidated())
595  return true;
596 
597  // check_simulated_imdl can change the binding map, so pass a copy.
598  P<BindingMap> bindings_copy(new BindingMap(bindings_));
599  Sim* prediction_sim = prediction->get_simulation(target_->get_goal()->get_sim()->root_);
600  if (prediction_sim) {
601 
602  if (((PrimaryMDLController *)controller_)->check_simulated_imdl(target_, bindings_copy, prediction_sim))
603  ((PMDLController *)controller_)->register_simulated_goal_outcome(target_, true, target_); // report a simulated success.
604  } else {
605 
606  if (((PrimaryMDLController *)controller_)->check_simulated_imdl(target_, bindings_copy, NULL))
607  ((PMDLController *)controller_)->register_simulated_goal_outcome(target_, false, NULL); // report a simulated failure.
608  }
609  return false;
610 }
611 
612 bool SRMonitor::reduce(_Fact *input) {
613 
614  if (target_->is_invalidated())
615  return true;
616 
617  Pred *prediction = input->get_pred();
618  if (prediction) { // input is f0->pred->f1->object.
619 
620  _Fact *_input = prediction->get_target(); // _input is f1->obj.
621  // Debug: It was passing target. Is it right to pass controller_?
622  Sim *sim = prediction->get_simulation(controller_);
623  if (sim) {
624 
625  Code *outcome = _input->get_reference(0);
626  if (outcome->code(0).asOpcode() == Opcodes::Success) { // _input is f1->success or |f1->success.
627 
628  _Fact *f_success = (_Fact *)outcome->get_reference(outcome->code(SUCCESS_OBJ).asIndex());
629  Goal *affected_goal = f_success->get_goal();
630  if (affected_goal) {
631 
632  store_simulated_outcome(input, sim);
633  return false;
634  }
635  }
636  }
637  }
638 
639  return false;
640 }
641 
642 void SRMonitor::update(Timestamp &next_target) {
643 
644  if (!target_->is_invalidated())
645  commit();
646  ((PMDLController *)controller_)->remove_r_monitor(this);
647  next_target = Timestamp(seconds(0));
648 }
649 }
r_exec::Pred::get_simulation
Sim * get_simulation(uint16 i) const
Definition: factory.h:468
r_exec::RMonitor::signal
bool signal(Pred *prediction) override
Definition: g_monitor.cpp:400
r_exec::BindingMap
Definition: binding_map.h:248
r_exec::Goal
Definition: factory.h:517
r_exec::Sim
Definition: factory.h:248
r_exec::_GMonitor::store_simulated_outcome
void store_simulated_outcome(_Fact *f_pred_f_success, Sim *sim)
Definition: g_monitor.cpp:113
r_exec::_Fact
Definition: factory.h:155
core::P
Definition: base.h:103
r_code::Code
Definition: r_code/object.h:224
r_exec::Pred
Definition: factory.h:394
r_exec::PrimaryMDLController
Definition: mdl_controller.h:430
r_exec::SRMonitor::signal
bool signal(Pred *prediction) override
Definition: g_monitor.cpp:592
r_exec::PMDLController
Definition: mdl_controller.h:332