85 #include "g_monitor.h"
87 #include "mdl_controller.h"
90 using namespace std::chrono;
91 using namespace r_code;
95 _GMonitor::_GMonitor(PMDLController *controller,
98 Timestamp sim_thz_timestamp,
100 Fact *f_imdl) : Monitor(controller,
105 sim_thz_timestamp_(sim_thz_timestamp),
108 simulating_ = (sim_thz_timestamp > Now());
109 sim_mode_ = goal->get_goal()->get_sim()->get_mode();
110 goal_target_ = target_->get_goal()->get_target();
115 if (f_pred_f_success->get_pred()->get_target()->is_fact()) {
117 switch (sim->get_mode()) {
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());
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());
133 switch (sim->get_mode()) {
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());
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());
150 void _GMonitor::invalidate_sim_outcomes() {
152 SolutionList::const_iterator solution;
154 for (solution = sim_failures_.mandatory_solutions.begin(); solution != sim_failures_.mandatory_solutions.end(); ++solution)
155 (*solution).second->invalidate();
157 for (solution = sim_failures_.optional_solutions.begin(); solution != sim_failures_.optional_solutions.end(); ++solution)
158 (*solution).second->invalidate();
160 for (solution = sim_successes_.mandatory_solutions.begin(); solution != sim_successes_.mandatory_solutions.end(); ++solution)
161 (*solution).second->invalidate();
163 for (solution = sim_successes_.optional_solutions.begin(); solution != sim_successes_.optional_solutions.end(); ++solution)
164 (*solution).second->invalidate();
169 GMonitor::GMonitor(PMDLController *controller,
170 BindingMap *bindings,
172 Timestamp sim_thz_timestamp,
175 _Fact *predicted_evidence) : _GMonitor(controller,
181 predicted_evidence_(predicted_evidence) {
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);
188 void GMonitor::commit() {
190 Goal *monitored_goal = target_->get_goal();
194 SolutionList::const_iterator solution;
196 for (solution = sim_failures_.mandatory_solutions.begin(); solution != sim_failures_.mandatory_solutions.end(); ++solution) {
198 if ((*solution).first->is_invalidated())
200 if ((*solution).second->is_invalidated())
202 Goal* goal = get_solution_goal((*solution).first);
206 if (goal->get_strength(now) > monitored_goal->get_strength(now)) {
208 invalidate_sim_outcomes();
213 for (solution = sim_failures_.optional_solutions.begin(); solution != sim_failures_.optional_solutions.end(); ++solution) {
215 if ((*solution).first->is_invalidated())
217 if ((*solution).second->is_invalidated())
219 Goal* goal = get_solution_goal((*solution).first);
223 if (goal->get_strength(now) > monitored_goal->get_strength(now))
224 (*solution).second->invalidate();
227 _Fact* best_solution_f_p_f_success = NULL;
228 Sim *best_solution = NULL;
230 for (solution = sim_successes_.optional_solutions.begin(); solution != sim_successes_.optional_solutions.end(); ++solution) {
232 if ((*solution).first->is_invalidated())
234 if ((*solution).second->is_invalidated())
236 if (!best_solution) {
237 best_solution_f_p_f_success = (*solution).first;
238 best_solution = (*solution).second;
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();
245 best_solution_f_p_f_success = (*solution).first;
246 best_solution = (*solution).second;
251 invalidate_sim_outcomes();
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);
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);
265 bool GMonitor::reduce(_Fact *input) {
267 if (target_->is_invalidated())
270 if (!injected_goal_) {
272 if (predicted_evidence_ && predicted_evidence_->is_invalidated()) {
274 ((PMDLController *)controller_)->register_predicted_goal_outcome(target_, bindings_, f_imdl_,
false, injected_goal_);
275 predicted_evidence_ = NULL;
276 injected_goal_ =
true;
281 Pred *prediction = input->get_pred();
284 _Fact *_input = prediction->get_target();
287 Sim *sim = prediction->get_simulation(controller_);
290 Code *outcome = _input->get_reference(0);
291 if (outcome->code(0).asOpcode() == Opcodes::Success) {
293 _Fact *f_success = (_Fact *)outcome->get_reference(outcome->code(SUCCESS_OBJ).asIndex());
294 Goal *affected_goal = f_success->get_goal();
302 switch (_input->is_evidence(goal_target_)) {
303 case MATCH_SUCCESS_POSITIVE:
304 ((PMDLController *)controller_)->register_simulated_goal_outcome(target_,
true, input);
306 case MATCH_SUCCESS_NEGATIVE:
307 ((PMDLController *)controller_)->register_simulated_goal_outcome(target_,
false, input);
317 switch (_input->is_evidence(goal_target_)) {
318 case MATCH_SUCCESS_POSITIVE:
320 ((PMDLController *)controller_)->register_predicted_goal_outcome(target_, bindings_, f_imdl_,
true,
true);
321 if (predicted_evidence_ && _input->get_cfd() > predicted_evidence_->get_pred()->get_target()->get_cfd())
322 predicted_evidence_ = input;
324 case MATCH_SUCCESS_NEGATIVE:
325 ((PMDLController *)controller_)->register_predicted_goal_outcome(target_, bindings_, f_imdl_,
false, injected_goal_);
326 predicted_evidence_ = NULL;
327 injected_goal_ =
true;
328 return target_->is_invalidated();
335 Goal *g = target_->get_goal();
336 if (g->ground_invalidated(input)) {
338 target_->invalidate();
340 ((PrimaryMDLController *)controller_)->abduce(bindings_, g->get_sim()->get_f_super_goal(), g->get_sim()->get_opposite(), goal_target_->get_cfd());
344 switch (input->is_evidence(goal_target_)) {
345 case MATCH_SUCCESS_POSITIVE:
346 if (input->is_fact() && goal_target_->is_anti_fact())
349 ((PMDLController *)controller_)->register_goal_outcome(target_,
true, input);
351 case MATCH_SUCCESS_NEGATIVE:
352 ((PMDLController *)controller_)->register_goal_outcome(target_,
false, input);
362 void GMonitor::update(Timestamp &next_target) {
364 if (target_->is_invalidated()) {
366 ((PMDLController *)controller_)->remove_g_monitor(
this);
367 next_target = Timestamp(seconds(0));
368 }
else if (simulating_) {
372 next_target = deadline_;
375 ((PMDLController *)controller_)->register_goal_outcome(target_,
false, NULL);
376 ((PMDLController *)controller_)->remove_g_monitor(
this);
377 next_target = Timestamp(seconds(0));
383 RMonitor::RMonitor(PrimaryMDLController *controller,
384 BindingMap *bindings,
386 Timestamp sim_thz_timestamp,
388 Fact *f_imdl) : GMonitor(controller,
396 MonitoringJob<RMonitor> *j =
new MonitoringJob<RMonitor>(
this, deadline);
397 _Mem::Get()->push_time_job(j);
402 if (target_->is_invalidated())
407 Sim* prediction_sim = prediction->
get_simulation(target_->get_goal()->get_sim()->root_);
408 if (simulating_ && prediction_sim) {
410 if (((
PrimaryMDLController *)controller_)->check_simulated_imdl(target_, bindings_copy, prediction_sim))
411 ((
PMDLController *)controller_)->register_simulated_goal_outcome(target_,
true, target_);
413 ((
PMDLController *)controller_)->register_simulated_goal_outcome(target_,
false, NULL);
420 bool RMonitor::reduce(
_Fact *input) {
422 if (target_->is_invalidated())
425 Pred *prediction = input->get_pred();
428 _Fact *_input = prediction->get_target();
434 Code *outcome = _input->get_reference(0);
435 if (outcome->code(0).asOpcode() == Opcodes::Success) {
437 _Fact *f_success = (
_Fact *)outcome->get_reference(outcome->code(SUCCESS_OBJ).asIndex());
438 Goal *affected_goal = f_success->get_goal();
452 void RMonitor::update(Timestamp &next_target) {
454 if (target_->is_invalidated()) {
456 ((PMDLController *)controller_)->remove_r_monitor(
this);
457 next_target = Timestamp(seconds(0));
458 }
else if (simulating_) {
462 next_target = deadline_;
465 ((PMDLController *)controller_)->register_goal_outcome(target_,
false, NULL);
466 ((PMDLController *)controller_)->remove_r_monitor(
this);
467 next_target = Timestamp(seconds(0));
473 SGMonitor::SGMonitor(PrimaryMDLController *controller,
474 BindingMap *bindings,
475 Timestamp sim_thz_timestamp,
477 Fact *f_imdl) : _GMonitor(controller,
479 Timestamp(seconds(0)),
484 MonitoringJob<SGMonitor> *j =
new MonitoringJob<SGMonitor>(
this, sim_thz_timestamp);
485 _Mem::Get()->push_time_job(j);
488 void SGMonitor::commit() {
490 Goal *monitored_goal = target_->get_goal();
494 SolutionList::const_iterator solution;
496 for (solution = sim_failures_.mandatory_solutions.begin(); solution != sim_failures_.mandatory_solutions.end(); ++solution) {
498 if ((*solution).second->is_invalidated())
500 Goal* goal = get_solution_goal((*solution).first);
504 if (goal->get_strength(now) > monitored_goal->get_strength(now)) {
506 (*solution).second->invalidate();
511 for (solution = sim_failures_.optional_solutions.begin(); solution != sim_failures_.optional_solutions.end(); ++solution) {
513 if ((*solution).second->is_invalidated())
515 Goal* goal = get_solution_goal((*solution).first);
519 if (goal->get_strength(now) > monitored_goal->get_strength(now))
520 (*solution).second->invalidate();
524 bool SGMonitor::reduce(_Fact *input) {
526 if (target_->is_invalidated())
530 Pred *prediction = input->get_pred();
533 _input = prediction->get_target();
535 Sim *sim = prediction->get_simulation(controller_);
538 Code *outcome = _input->get_reference(0);
539 if (outcome->code(0).asOpcode() == Opcodes::Success) {
541 _Fact *f_success = (_Fact *)outcome->get_reference(outcome->code(SUCCESS_OBJ).asIndex());
542 Goal *affected_goal = f_success->get_goal();
554 switch (_input->is_evidence(goal_target_)) {
555 case MATCH_SUCCESS_POSITIVE:
556 ((PMDLController *)controller_)->register_simulated_goal_outcome(target_,
true, _input);
558 case MATCH_SUCCESS_NEGATIVE:
559 ((PMDLController *)controller_)->register_simulated_goal_outcome(target_,
false, _input);
568 void SGMonitor::update(Timestamp &next_target) {
570 if (!target_->is_invalidated())
572 ((PMDLController *)controller_)->remove_g_monitor(
this);
573 next_target = Timestamp(seconds(0));
578 SRMonitor::SRMonitor(PrimaryMDLController *controller,
579 BindingMap *bindings,
580 Timestamp sim_thz_timestamp,
582 Fact *f_imdl) : SGMonitor(controller,
588 MonitoringJob<SRMonitor> *j =
new MonitoringJob<SRMonitor>(
this, sim_thz_timestamp);
589 _Mem::Get()->push_time_job(j);
594 if (target_->is_invalidated())
599 Sim* prediction_sim = prediction->
get_simulation(target_->get_goal()->get_sim()->root_);
600 if (prediction_sim) {
602 if (((
PrimaryMDLController *)controller_)->check_simulated_imdl(target_, bindings_copy, prediction_sim))
603 ((
PMDLController *)controller_)->register_simulated_goal_outcome(target_,
true, target_);
607 ((
PMDLController *)controller_)->register_simulated_goal_outcome(target_,
false, NULL);
612 bool SRMonitor::reduce(
_Fact *input) {
614 if (target_->is_invalidated())
617 Pred *prediction = input->get_pred();
620 _Fact *_input = prediction->get_target();
625 Code *outcome = _input->get_reference(0);
626 if (outcome->code(0).asOpcode() == Opcodes::Success) {
628 _Fact *f_success = (
_Fact *)outcome->get_reference(outcome->code(SUCCESS_OBJ).asIndex());
629 Goal *affected_goal = f_success->get_goal();
642 void SRMonitor::update(Timestamp &next_target) {
644 if (!target_->is_invalidated())
646 ((PMDLController *)controller_)->remove_r_monitor(
this);
647 next_target = Timestamp(seconds(0));