87 #include "mdl_controller.h" 
   89 #include "model_base.h" 
   92 using namespace std::chrono;
 
   93 using namespace r_code;
 
   97 MDLOverlay::MDLOverlay(Controller *c, 
const HLPBindingMap *bindings) : HLPOverlay(c, bindings, true) {
 
  100 MDLOverlay::~MDLOverlay() {
 
  105 PrimaryMDLOverlay::PrimaryMDLOverlay(Controller *c, 
const HLPBindingMap *bindings) : MDLOverlay(c, bindings) {
 
  108 PrimaryMDLOverlay::~PrimaryMDLOverlay() {
 
  111 bool PrimaryMDLOverlay::reduce(_Fact *input, Fact *f_p_f_imdl, MDLController *req_controller) {
 
  113   OUTPUT_LINE(MDL_IN, Utils::RelativeTime(Now()) << 
" mdl: " << controller_->get_object()->get_oid() << 
" <- input: " << input->get_oid());
 
  115   Pred *prediction = input->get_pred();
 
  119     input_object = prediction->get_target();
 
  120     is_simulation = prediction->is_simulation();
 
  123     input_object = input;
 
  124     is_simulation = 
false;
 
  128   bm->reset_fwd_timings(input_object);
 
  129   switch (bm->match_fwd_lenient(input_object, ((MDLController *)controller_)->get_lhs())) {
 
  130   case MATCH_SUCCESS_POSITIVE: {
 
  134     bool is_req = ((MDLController *)controller_)->is_requirement();
 
  136     Fact *f_imdl = ((MDLController *)controller_)->get_f_ihlp(bm, 
false);
 
  137     RequirementsPair r_p;
 
  138     vector<BindingResult> bind_results;
 
  139     bool wr_enabled = 
false;
 
  140     ChainingStatus c_s = ((MDLController *)controller_)->retrieve_imdl_fwd(bm, f_imdl, r_p, bind_results, req_controller, wr_enabled);
 
  141     f_imdl->get_reference(0)->code(I_HLP_WEAK_REQUIREMENT_ENABLED) = Atom::Boolean(wr_enabled);
 
  143     bool chaining_allowed = (c_s >= WEAK_REQUIREMENT_ENABLED);
 
  144     bool did_check_simulated_chaining = 
false;
 
  145     bool check_simulated_chaining_result;
 
  147     case WEAK_REQUIREMENT_DISABLED:
 
  148     case STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT: 
 
  151         bind_results.clear();
 
  152         check_simulated_chaining_result = check_simulated_chaining(bm, f_imdl, prediction, bind_results);
 
  153         did_check_simulated_chaining = 
true;
 
  154         if (check_simulated_chaining_result)
 
  155           chaining_allowed = 
true;
 
  160       if (!chaining_allowed && ((MDLController *)controller_)->has_tpl_args()) 
 
  163         f_imdl->get_reference(0)->code(I_HLP_WEAK_REQUIREMENT_ENABLED) = Atom::Boolean(
false);
 
  164     case STRONG_REQUIREMENT_DISABLED_WEAK_REQUIREMENT: 
 
  167         if (!did_check_simulated_chaining) {
 
  168           bind_results.clear();
 
  169           check_simulated_chaining_result = check_simulated_chaining(bm, f_imdl, prediction, bind_results);
 
  170           did_check_simulated_chaining = 
true;
 
  172         if (check_simulated_chaining_result)
 
  173           chaining_allowed = 
true;
 
  177     case WEAK_REQUIREMENT_ENABLED:
 
  178       if (bind_results.size() == 0)
 
  180         bind_results.push_back(BindingResult(bm, NULL, NULL));
 
  181       vector<P<_Fact> > already_predicted;
 
  182       for (
size_t i = 0; i < bind_results.size(); ++i) {
 
  184         bindings_ = bind_results[i].map_;
 
  188         if (evaluate_fwd_guards()) { 
 
  190           P<Fact> f_imdl_copy = 
new Fact(
 
  191             bindings_->bind_pattern(f_imdl->get_reference(0)), f_imdl->get_after(), f_imdl->get_before(),
 
  192             f_imdl->get_cfd(), f_imdl->get_psln_thr());
 
  193           ((PrimaryMDLController*)controller_)->predict(bindings_, input, f_imdl_copy, chaining_allowed, r_p, bind_results[i].ground_,
 
  194             bind_results[i].ground_mk_rdx_, already_predicted);
 
  203     bindings_ = original_bindings;
 
  204     if (f_p_f_imdl == NULL) 
 
  205       store_evidence(input, prediction, is_simulation);
 
  207   }
case MATCH_SUCCESS_NEGATIVE: 
 
  208     if (f_p_f_imdl == NULL) 
 
  209       store_evidence(input, prediction, is_simulation);
 
  217 bool PrimaryMDLOverlay::check_simulated_chaining(
const HLPBindingMap *bm, Fact *f_imdl, Pred *prediction, vector<BindingResult >& results) {
 
  219   for (uint32 i = 0; i < prediction->get_simulations_size(); ++i) {
 
  221     switch (((MDLController *)controller_)->retrieve_simulated_imdl_fwd(bm, f_imdl, prediction->get_simulation(i), results)) {
 
  223     case WEAK_REQUIREMENT_ENABLED:
 
  235 SecondaryMDLOverlay::SecondaryMDLOverlay(Controller *c, 
const HLPBindingMap *bindings) : MDLOverlay(c, bindings) {
 
  238 SecondaryMDLOverlay::~SecondaryMDLOverlay() {
 
  241 bool SecondaryMDLOverlay::reduce(_Fact *input, Fact *f_p_f_imdl, MDLController *req_controller) { 
 
  243   bm->reset_fwd_timings(input);
 
  244   switch (bm->match_fwd_lenient(input, ((MDLController *)controller_)->get_lhs())) {
 
  245   case MATCH_SUCCESS_POSITIVE: {
 
  251     Fact *f_imdl = ((MDLController *)controller_)->get_f_ihlp(bm, 
false);
 
  252     RequirementsPair r_p;
 
  253     vector<BindingResult> bind_results;
 
  254     bool wr_enabled = 
false;
 
  255     ChainingStatus c_s = ((MDLController *)controller_)->retrieve_imdl_fwd(bm, f_imdl, r_p, bind_results, req_controller, wr_enabled);
 
  256     f_imdl->get_reference(0)->code(I_HLP_WEAK_REQUIREMENT_ENABLED) = Atom::Boolean(wr_enabled);
 
  257     bool chaining_allowed = (c_s >= NO_REQUIREMENT);
 
  259     case WEAK_REQUIREMENT_DISABLED:
 
  260     case STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT: 
 
  262       if (((MDLController *)controller_)->has_tpl_args()) 
 
  265         f_imdl->get_reference(0)->code(I_HLP_WEAK_REQUIREMENT_ENABLED) = Atom::Boolean(
false);
 
  266     case STRONG_REQUIREMENT_DISABLED_WEAK_REQUIREMENT:
 
  267     case WEAK_REQUIREMENT_ENABLED:
 
  268       if (bind_results.size() == 0)
 
  270         bind_results.push_back(BindingResult(bm, NULL, NULL));
 
  271       vector<P<_Fact> > already_predicted;
 
  272       for (
size_t i = 0; i < bind_results.size(); ++i) {
 
  274         bindings_ = bind_results[i].map_;
 
  278         if (evaluate_fwd_guards()) { 
 
  279           f_imdl->set_reference(0, bindings_->bind_pattern(f_imdl->get_reference(0))); 
 
  280           ((SecondaryMDLController*)controller_)->predict(bindings_, input, NULL, 
true, r_p, bind_results[i].ground_,
 
  281             bind_results[i].ground_mk_rdx_, already_predicted);
 
  290     bindings_ = original_bindings;
 
  292   }
case MATCH_SUCCESS_NEGATIVE:
 
  302 MDLController *MDLController::New(View *view, 
bool &inject_in_secondary_group) {
 
  304   Code *unpacked_mdl = view->object_->get_reference(view->object_->references_size() - MDL_HIDDEN_REFS);
 
  305   uint16 obj_set_index = unpacked_mdl->code(MDL_OBJS).asIndex();
 
  306   Code *rhs = unpacked_mdl->get_reference(unpacked_mdl->code(obj_set_index + 2).asIndex());
 
  308   if (rhs->get_reference(0)->code(0).asOpcode() == Opcodes::Ent) { 
 
  310     inject_in_secondary_group = 
false;
 
  311     return new TopLevelMDLController(view);
 
  314   inject_in_secondary_group = 
true;
 
  315   return new PrimaryMDLController(view);
 
  318 MDLController::MDLController(
_View *view) : HLPController(view) {
 
  320   Code *
object = get_unpacked_object();
 
  321   uint16 obj_set_index = 
object->code(MDL_OBJS).asIndex();
 
  322   lhs_ = 
object->get_reference(object->code(obj_set_index + 1).asIndex());
 
  323   rhs_ = 
object->get_reference(object->code(obj_set_index + 2).asIndex());
 
  325   Group *host = get_host();
 
  326   controllers_.resize(2);
 
  328   Code *rhs_ihlp = rhs_->get_reference(0);
 
  329   requirement_type_ = NOT_A_REQUIREMENT;
 
  330   controllers_[RHSController] = NULL;
 
  331   uint16 rhs_opcode = rhs_ihlp->code(0).asOpcode();
 
  332   if (rhs_opcode == Opcodes::ICst ||
 
  333       rhs_opcode == Opcodes::IMdl) {
 
  335     Code *rhs_hlp = rhs_ihlp->get_reference(0);
 
  339       if (rhs_opcode == Opcodes::IMdl)
 
  340         requirement_type_ = (rhs_->code(0).asOpcode() == Opcodes::AntiFact ? STRONG_REQUIREMENT : WEAK_REQUIREMENT);
 
  341       controllers_[RHSController] = (HLPController *)rhs_hlp_v->controller_;
 
  345   Code *lhs_ihlp = lhs_->get_reference(0);
 
  347   controllers_[LHSController] = NULL;
 
  348   uint16 lhs_opcode = lhs_ihlp->code(0).asOpcode();
 
  349   if (lhs_opcode == Opcodes::ICst ||
 
  350       lhs_opcode == Opcodes::IMdl) {
 
  352     Code *lhs_hlp = lhs_ihlp->get_reference(0);
 
  356       if (lhs_opcode == Opcodes::IMdl)
 
  358       controllers_[LHSController] = (HLPController *)lhs_hlp_v->controller_;
 
  362   is_cmd_ = (lhs_opcode == Opcodes::Cmd);
 
  365 float32 MDLController::get_success_rate()
 const {
 
  367   return get_core_object()->code(MDL_SR).asFloat();
 
  370 bool MDLController::monitor_predictions(
_Fact *input) { 
 
  372   Pred *pred = input->get_pred();
 
  373   if (pred && pred->is_simulation()) 
 
  378   p_monitorsCS_.enter();
 
  379   for (m = p_monitors_.begin(); m != p_monitors_.end();) {
 
  381     if ((*m)->reduce(input)) {
 
  383       m = p_monitors_.erase(m);
 
  388   p_monitorsCS_.leave();
 
  393 void MDLController::add_monitor(PMonitor *m) {
 
  395   p_monitorsCS_.enter();
 
  396   p_monitors_.push_front(m);
 
  397   p_monitorsCS_.leave();
 
  400 void MDLController::remove_monitor(PMonitor *m) {
 
  402   p_monitorsCS_.enter();
 
  403   p_monitors_.remove(m);
 
  404   p_monitorsCS_.leave();
 
  407 void MDLController::add_requirement_to_rhs() {
 
  409   if (requirement_type_ != NOT_A_REQUIREMENT) {
 
  411     HLPController *c = controllers_[RHSController];
 
  413       c->add_requirement(requirement_type_ == STRONG_REQUIREMENT);
 
  417 void MDLController::remove_requirement_from_rhs() {
 
  419   if (requirement_type_ != NOT_A_REQUIREMENT) {
 
  421     HLPController *c = controllers_[RHSController];
 
  423       c->remove_requirement(requirement_type_ == STRONG_REQUIREMENT);
 
  429   requirements_.CS_.enter();
 
  432   for (_e = cache->begin(); _e != cache->end();) {
 
  434     if ((*_e).is_too_old(now)) 
 
  435       _e = cache->erase(_e);
 
  440   cache->push_front(e);
 
  441   requirements_.CS_.leave();
 
  444 ChainingStatus MDLController::retrieve_simulated_imdl_fwd(
const HLPBindingMap *bm, Fact *f_imdl, Sim* sim, vector<BindingResult>& results) {
 
  448   uint32 r_count = get_requirement_count(wr_count, sr_count);
 
  450     return NO_REQUIREMENT;
 
  452   bool save_f_imdl_wr_enabled = f_imdl->get_reference(0)->code(I_HLP_WEAK_REQUIREMENT_ENABLED).asBoolean();
 
  455     r = WEAK_REQUIREMENT_DISABLED;
 
  456     requirements_.CS_.enter();
 
  459     for (e = simulated_requirements_.positive_evidences_.begin(); e != simulated_requirements_.positive_evidences_.end();) {
 
  461       if ((*e).is_too_old(now)) 
 
  462         e = simulated_requirements_.positive_evidences_.erase(e);
 
  465         if ((*e).evidence_->get_pred()->has_simulation(sim)) {
 
  467           _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
  468           HLPBindingMap _original(bm); 
 
  470           f_imdl->get_reference(0)->code(I_HLP_WEAK_REQUIREMENT_ENABLED) = Atom::Boolean(
 
  471             _f_imdl->get_reference(0)->code(I_HLP_WEAK_REQUIREMENT_ENABLED).asBoolean());
 
  472           if (_original.match_fwd_strict(_f_imdl, f_imdl)) { 
 
  474             r = WEAK_REQUIREMENT_ENABLED;
 
  475             results.push_back(BindingResult(
new HLPBindingMap(_original), (*e).evidence_, (*e).mk_rdx_));
 
  483     requirements_.CS_.leave();
 
  485     f_imdl->get_reference(0)->code(I_HLP_WEAK_REQUIREMENT_ENABLED) = Atom::Boolean(save_f_imdl_wr_enabled);
 
  491       requirements_.CS_.enter();
 
  494       for (e = simulated_requirements_.negative_evidences_.begin(); e != simulated_requirements_.negative_evidences_.end();) {
 
  496         if ((*e).is_too_old(now)) 
 
  497           e = simulated_requirements_.negative_evidences_.erase(e);
 
  500           if ((*e).evidence_->get_pred()->has_simulation(sim)) {
 
  502             _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
  503             HLPBindingMap _original(bm); 
 
  504             if (_original.match_fwd_lenient(_f_imdl, f_imdl) == MATCH_SUCCESS_NEGATIVE) { 
 
  506               results.push_back(BindingResult(
new HLPBindingMap(_original), NULL, NULL));
 
  507               requirements_.CS_.leave();
 
  508               return STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT;
 
  515       requirements_.CS_.leave();
 
  516       return NO_REQUIREMENT;
 
  519       r = WEAK_REQUIREMENT_DISABLED;
 
  520       float32 negative_cfd = 0;
 
  521       requirements_.CS_.enter();
 
  523       _Fact* strong_requirement_ground = NULL;
 
  524       HLPBindingMap strong_bm;
 
  526       for (e = simulated_requirements_.negative_evidences_.begin(); e != simulated_requirements_.negative_evidences_.end();) {
 
  528         if ((*e).is_too_old(now)) 
 
  529           e = simulated_requirements_.negative_evidences_.erase(e);
 
  532           if ((*e).evidence_->get_pred()->has_simulation(sim)) {
 
  534             _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
  535             HLPBindingMap _original(bm); 
 
  536             if (_original.match_fwd_lenient(_f_imdl, f_imdl) == MATCH_SUCCESS_NEGATIVE) {
 
  538               negative_cfd = (*e).confidence_;
 
  539               r = STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT;
 
  541               strong_bm = _original;
 
  542               strong_requirement_ground = (*e).evidence_;
 
  551       for (e = simulated_requirements_.positive_evidences_.begin(); e != simulated_requirements_.positive_evidences_.end();) {
 
  553         if ((*e).is_too_old(now)) 
 
  554           e = simulated_requirements_.positive_evidences_.erase(e);
 
  556           if ((*e).evidence_->get_pred()->has_simulation(sim)) {
 
  558             _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
  559             HLPBindingMap _original(bm); 
 
  560             if (_original.match_fwd_strict(_f_imdl, f_imdl)) {
 
  562               bool strong_matches_weak =
 
  563                 (strong_requirement_ground && HLPBindingMap(_original).match_fwd_lenient
 
  564                   (_f_imdl, strong_requirement_ground->get_pred()->get_target()) == MATCH_SUCCESS_NEGATIVE);
 
  565               if (!strong_matches_weak || (*e).confidence_ > negative_cfd) {
 
  567                 r = WEAK_REQUIREMENT_ENABLED;
 
  568                 results.push_back(BindingResult(
new HLPBindingMap(_original), (*e).evidence_, (*e).mk_rdx_));
 
  572                 if (r != WEAK_REQUIREMENT_ENABLED) {
 
  574                   ground = (*e).evidence_;
 
  575                   r = STRONG_REQUIREMENT_DISABLED_WEAK_REQUIREMENT;
 
  584       if (r == STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT || r == STRONG_REQUIREMENT_DISABLED_WEAK_REQUIREMENT)
 
  585         results.push_back(BindingResult(
new HLPBindingMap(strong_bm), ground, NULL));
 
  587       requirements_.CS_.leave();
 
  593 ChainingStatus MDLController::retrieve_simulated_imdl_bwd(HLPBindingMap *bm, Fact *f_imdl, Sim* prediction_sim, Fact *&ground, Fact *&strong_requirement_ground) {
 
  597   uint32 r_count = get_requirement_count(wr_count, sr_count);
 
  599   strong_requirement_ground = NULL;
 
  601     return NO_REQUIREMENT;
 
  605     r = WEAK_REQUIREMENT_DISABLED;
 
  606     requirements_.CS_.enter();
 
  609     for (e = simulated_requirements_.positive_evidences_.begin(); e != simulated_requirements_.positive_evidences_.end();) {
 
  611       if ((*e).is_too_old(now)) 
 
  612         e = simulated_requirements_.positive_evidences_.erase(e);
 
  615         if ((*e).evidence_->get_pred()->has_simulation(prediction_sim)) {
 
  617           _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
  618           HLPBindingMap _original(bm); 
 
  620           if (_original.match_fwd_strict(_f_imdl, f_imdl)) { 
 
  622             bm->load(&_original);
 
  623             r = WEAK_REQUIREMENT_ENABLED;
 
  624             ground = (*e).evidence_;
 
  632     requirements_.CS_.leave();
 
  638       requirements_.CS_.enter();
 
  641       for (e = simulated_requirements_.negative_evidences_.begin(); e != simulated_requirements_.negative_evidences_.end();) {
 
  643         if ((*e).is_too_old(now)) 
 
  644           e = simulated_requirements_.negative_evidences_.erase(e);
 
  647           if ((*e).evidence_->get_pred()->has_simulation(prediction_sim)) {
 
  649             _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
  650             HLPBindingMap _original(bm); 
 
  652             if (_original.match_fwd_lenient(_f_imdl, f_imdl) == MATCH_SUCCESS_NEGATIVE) { 
 
  654               bm->load(&_original);
 
  655               strong_requirement_ground = (*e).evidence_;
 
  656               requirements_.CS_.leave();
 
  657               return STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT;
 
  664       requirements_.CS_.leave();
 
  665       return NO_REQUIREMENT;
 
  668       r = WEAK_REQUIREMENT_DISABLED;
 
  669       float32 negative_cfd = 0;
 
  670       requirements_.CS_.enter();
 
  672       HLPBindingMap strong_bm;
 
  674       for (e = simulated_requirements_.negative_evidences_.begin(); e != simulated_requirements_.negative_evidences_.end();) {
 
  676         if ((*e).is_too_old(now)) 
 
  677           e = simulated_requirements_.negative_evidences_.erase(e);
 
  680           if ((*e).evidence_->get_pred()->has_simulation(prediction_sim)) {
 
  682             _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
  683             HLPBindingMap _original(bm); 
 
  685             if (_original.match_fwd_lenient(_f_imdl, f_imdl) == MATCH_SUCCESS_NEGATIVE) {
 
  687               negative_cfd = (*e).confidence_;
 
  688               r = STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT;
 
  690               strong_bm = _original;
 
  691               strong_requirement_ground = (*e).evidence_;
 
  699       HLPBindingMap result_bm(bm);
 
  700       for (e = simulated_requirements_.positive_evidences_.begin(); e != simulated_requirements_.positive_evidences_.end();) {
 
  702         if ((*e).is_too_old(now)) 
 
  703           e = simulated_requirements_.positive_evidences_.erase(e);
 
  705           if ((*e).evidence_->get_pred()->has_simulation(prediction_sim)) {
 
  707             _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
  708             HLPBindingMap _original(bm); 
 
  710             if (_original.match_fwd_strict(_f_imdl, f_imdl)) {
 
  712               bool strong_matches_weak =
 
  713                 (strong_requirement_ground && HLPBindingMap(_original).match_fwd_lenient
 
  714                   (_f_imdl, strong_requirement_ground->get_pred()->get_target()) == MATCH_SUCCESS_NEGATIVE);
 
  715               if (!strong_matches_weak || (*e).confidence_ > negative_cfd ||
 
  716                   _f_imdl->get_after() >= strong_requirement_ground->get_pred()->get_target()->get_before()) {
 
  718                 if (r != WEAK_REQUIREMENT_ENABLED) {
 
  719                   r = WEAK_REQUIREMENT_ENABLED;
 
  721                   result_bm.load(&_original);
 
  722                   ground = (*e).evidence_;
 
  727                 if (strong_requirement_ground &&
 
  728                     strong_requirement_ground->get_pred()->get_target()->get_after() < _f_imdl->get_before()) {
 
  729                   if ((*e).evidence_->get_pred()->has_defeasible_consequence())
 
  730                     (*e).evidence_->get_pred()->get_defeasible_consequence()->invalidate();
 
  731                   if (r != WEAK_REQUIREMENT_ENABLED) {
 
  732                     ground = (*e).evidence_;
 
  733                     r = STRONG_REQUIREMENT_DISABLED_WEAK_REQUIREMENT;
 
  743       if (r == STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT || r == STRONG_REQUIREMENT_DISABLED_WEAK_REQUIREMENT)
 
  744         bm->load(&strong_bm);
 
  746         bm->load(&result_bm);
 
  748       requirements_.CS_.leave();
 
  755 ChainingStatus MDLController::retrieve_imdl_fwd(
const HLPBindingMap *bm, Fact *f_imdl, RequirementsPair &r_p, std::vector<BindingResult>& results, MDLController *req_controller, 
bool &wr_enabled) {
 
  759   uint32 r_count = get_requirement_count(wr_count, sr_count);
 
  762     return NO_REQUIREMENT;
 
  766 #if 0 // JTNote: We set ground = NULL above, so (ground != NULL) is never true. 
  767     if (ground != NULL) { 
 
  769       r_p.weak_requirements_.controllers.insert(req_controller);
 
  770       r_p.weak_requirements_.f_imdl = ground;
 
  771       r_p.weak_requirements_.chaining_was_allowed = 
true;
 
  772       return WEAK_REQUIREMENT_ENABLED;
 
  776     r = WEAK_REQUIREMENT_DISABLED;
 
  777     requirements_.CS_.enter();
 
  780     for (e = requirements_.positive_evidences_.begin(); e != requirements_.positive_evidences_.end();) {
 
  782       Code *imdl = (*e).evidence_->get_pred()->get_target()->get_reference(0);
 
  783       uint16 tpl_index = imdl->code(I_HLP_TPL_ARGS).asIndex();
 
  785       if ((*e).is_too_old(now)) 
 
  786         e = requirements_.positive_evidences_.erase(e);
 
  787       else if ((*e).is_out_of_range(now))
 
  791         _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
  792         HLPBindingMap _original(bm); 
 
  793         if (_original.match_fwd_strict(_f_imdl, f_imdl)) { 
 
  794 #ifdef WITH_DETAIL_OID 
  795           OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" fact (" << f_imdl->get_detail_oid() << 
") imdl mdl " <<
 
  796             f_imdl->get_reference(0)->get_reference(0)->get_oid() << 
" matches evidence fact (" <<
 
  797             _f_imdl->get_detail_oid() << 
") imdl mdl " << _f_imdl->get_reference(0)->get_reference(0)->get_oid());
 
  799           if ((*e).chaining_was_allowed_) {
 
  801             r = WEAK_REQUIREMENT_ENABLED;
 
  802             results.push_back(BindingResult(
new HLPBindingMap(_original), (*e).evidence_, (*e).mk_rdx_));
 
  806           r_p.weak_requirements_.controllers.insert((*e).controller_);
 
  807           r_p.weak_requirements_.f_imdl = _f_imdl;
 
  808           r_p.weak_requirements_.chaining_was_allowed = (*e).chaining_was_allowed_;
 
  814     requirements_.CS_.leave();
 
  821       requirements_.CS_.enter();
 
  824       for (e = requirements_.negative_evidences_.begin(); e != requirements_.negative_evidences_.end();) {
 
  826         if ((*e).is_too_old(now)) 
 
  827           e = requirements_.positive_evidences_.erase(e);
 
  828         else if ((*e).is_out_of_range(now))
 
  832           _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
  833           HLPBindingMap _original(bm); 
 
  834           if (_original.match_fwd_lenient(_f_imdl, f_imdl) == MATCH_SUCCESS_NEGATIVE) { 
 
  836             if (r == NO_REQUIREMENT && (*e).chaining_was_allowed_) 
 
  837               r = STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT;
 
  839             r_p.strong_requirements_.controllers.insert((*e).controller_);
 
  840             r_p.strong_requirements_.f_imdl = _f_imdl;
 
  841             r_p.strong_requirements_.chaining_was_allowed = (*e).chaining_was_allowed_;
 
  847       requirements_.CS_.leave();
 
  851       r = WEAK_REQUIREMENT_DISABLED;
 
  852       requirements_.CS_.enter();
 
  853       float32 negative_cfd = 0;
 
  856       _Fact* strong_requirement_ground = NULL;
 
  857       HLPBindingMap strong_bm;
 
  859       for (e = requirements_.negative_evidences_.begin(); e != requirements_.negative_evidences_.end();) {
 
  861         if ((*e).is_too_old(now)) 
 
  862           e = requirements_.negative_evidences_.erase(e);
 
  863         else if ((*e).is_out_of_range(now))
 
  867           _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
  868           HLPBindingMap _original(bm); 
 
  869           if (_original.match_fwd_lenient(_f_imdl, f_imdl) == MATCH_SUCCESS_NEGATIVE) {
 
  871             if (r == WEAK_REQUIREMENT_DISABLED && (*e).chaining_was_allowed_) { 
 
  873               negative_cfd = (*e).confidence_;
 
  874               r = STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT;
 
  876               strong_bm = _original;
 
  877               strong_requirement_ground = (*e).evidence_;
 
  880             r_p.strong_requirements_.controllers.insert((*e).controller_);
 
  881             r_p.strong_requirements_.f_imdl = _f_imdl;
 
  882             r_p.strong_requirements_.chaining_was_allowed = (*e).chaining_was_allowed_;
 
  888 #if 0 // JTNote: We set ground = NULL above, so (ground != NULL) is never true. 
  889       if (ground != NULL) { 
 
  891         requirements_.CS_.leave();
 
  892         float32 confidence = ground->get_pred()->get_target()->get_cfd();
 
  893         if (confidence > negative_cfd) {
 
  895           r = WEAK_REQUIREMENT_ENABLED;
 
  896           r_p.weak_requirements_.controllers.insert(req_controller);
 
  897           r_p.weak_requirements_.f_imdl = ground;
 
  898           r_p.weak_requirements_.chaining_was_allowed = 
true;
 
  905       for (e = requirements_.positive_evidences_.begin(); e != requirements_.positive_evidences_.end();) {
 
  907         if ((*e).is_too_old(now)) 
 
  908           e = requirements_.positive_evidences_.erase(e);
 
  909         else if ((*e).is_out_of_range(now))
 
  913           _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
  914           HLPBindingMap _original(bm); 
 
  915           if (_original.match_fwd_strict(_f_imdl, f_imdl)) {
 
  917             if ((*e).chaining_was_allowed_) {
 
  919               bool strong_matches_weak =
 
  920                 (strong_requirement_ground && HLPBindingMap(_original).match_fwd_lenient
 
  921                   (_f_imdl, strong_requirement_ground->get_pred()->get_target()) == MATCH_SUCCESS_NEGATIVE);
 
  922               if (!strong_matches_weak || (*e).confidence_ > negative_cfd) {
 
  924                 r = WEAK_REQUIREMENT_ENABLED;
 
  925                 results.push_back(BindingResult(
new HLPBindingMap(_original), (*e).evidence_, (*e).mk_rdx_));
 
  927                 wr_enabled = strong_matches_weak;
 
  931                 if (r != WEAK_REQUIREMENT_ENABLED) {
 
  933                   ground = (*e).evidence_;
 
  934                   r = STRONG_REQUIREMENT_DISABLED_WEAK_REQUIREMENT;
 
  940             r_p.weak_requirements_.controllers.insert((*e).controller_);
 
  941             r_p.weak_requirements_.f_imdl = _f_imdl;
 
  942             r_p.weak_requirements_.chaining_was_allowed = (*e).chaining_was_allowed_;
 
  948       if (r == STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT || r == STRONG_REQUIREMENT_DISABLED_WEAK_REQUIREMENT)
 
  949         results.push_back(BindingResult(
new HLPBindingMap(strong_bm), ground, NULL));
 
  951       requirements_.CS_.leave();
 
  957 ChainingStatus MDLController::retrieve_imdl_bwd(HLPBindingMap *bm, Fact *f_imdl, Fact *&ground, Fact *&strong_requirement_ground) {
 
  961   uint32 r_count = get_requirement_count(wr_count, sr_count);
 
  963   strong_requirement_ground = NULL;
 
  965     return NO_REQUIREMENT;
 
  969     r = WEAK_REQUIREMENT_DISABLED;
 
  970     requirements_.CS_.enter();
 
  973     for (e = requirements_.positive_evidences_.begin(); e != requirements_.positive_evidences_.end();) {
 
  975       if ((*e).is_too_old(now)) 
 
  976         e = requirements_.positive_evidences_.erase(e);
 
  979         _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
  980         HLPBindingMap _original(bm); 
 
  982         if (_original.match_fwd_strict(_f_imdl, f_imdl)) { 
 
  984           r = WEAK_REQUIREMENT_ENABLED;
 
  985           bm->load(&_original);
 
  986           ground = (*e).evidence_;
 
  993     requirements_.CS_.leave();
 
 1001       requirements_.CS_.enter();
 
 1004       for (e = requirements_.negative_evidences_.begin(); e != requirements_.negative_evidences_.end();) {
 
 1006         if ((*e).is_too_old(now)) 
 
 1007           e = requirements_.negative_evidences_.erase(e);
 
 1010           _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
 1011           HLPBindingMap _original(bm); 
 
 1013           if (_original.match_fwd_lenient(_f_imdl, f_imdl) == MATCH_SUCCESS_NEGATIVE) { 
 
 1015             strong_requirement_ground = (*e).evidence_;
 
 1016             requirements_.CS_.leave();
 
 1017             return STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT;
 
 1023       requirements_.CS_.leave();
 
 1024       return NO_REQUIREMENT;
 
 1027       r = WEAK_REQUIREMENT_DISABLED;
 
 1028       float32 negative_cfd = 0;
 
 1029       requirements_.CS_.enter();
 
 1032       for (e = requirements_.negative_evidences_.begin(); e != requirements_.negative_evidences_.end();) {
 
 1034         if ((*e).is_too_old(now)) 
 
 1035           e = requirements_.negative_evidences_.erase(e);
 
 1038           _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
 1039           HLPBindingMap _original(bm); 
 
 1041           if (_original.match_fwd_lenient(_f_imdl, f_imdl) == MATCH_SUCCESS_NEGATIVE) {
 
 1043             negative_cfd = (*e).confidence_;
 
 1044             r = STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT;
 
 1045             strong_requirement_ground = (*e).evidence_;
 
 1052       HLPBindingMap result_bm(bm);
 
 1053       for (e = requirements_.positive_evidences_.begin(); e != requirements_.positive_evidences_.end();) {
 
 1055         if ((*e).is_too_old(now)) 
 
 1056           e = requirements_.positive_evidences_.erase(e);
 
 1058           _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
 1059           HLPBindingMap _original(bm); 
 
 1061           if (_original.match_fwd_strict(_f_imdl, f_imdl)) {
 
 1063             bool strong_matches_weak =
 
 1064               (strong_requirement_ground && HLPBindingMap(_original).match_fwd_lenient
 
 1065                 (_f_imdl, strong_requirement_ground->get_pred()->get_target()) == MATCH_SUCCESS_NEGATIVE);
 
 1066             if (!strong_matches_weak || (*e).confidence_ > negative_cfd) {
 
 1068               if (r != WEAK_REQUIREMENT_ENABLED) {
 
 1069                 r = WEAK_REQUIREMENT_ENABLED;
 
 1071                 result_bm.load(&_original);
 
 1072                 ground = (*e).evidence_;
 
 1075               if (r != WEAK_REQUIREMENT_ENABLED) {
 
 1076                 ground = (*e).evidence_;
 
 1077                 r = STRONG_REQUIREMENT_DISABLED_WEAK_REQUIREMENT;
 
 1085       if (r == WEAK_REQUIREMENT_ENABLED)
 
 1086         bm->load(&result_bm);
 
 1088       requirements_.CS_.leave();
 
 1096 void MDLController::register_requirement(_Fact *f_pred, RequirementsPair &r_p) {
 
 1098   if (r_p.weak_requirements_.controllers.size() > 0 || r_p.strong_requirements_.controllers.size() > 0)
 
 1099     active_requirements_.insert(std::make_pair(f_pred, r_p));
 
 1102 bool MDLController::get_imdl_template_timings(
 
 1103     r_code::Code* imdl, Timestamp& after, Timestamp& before, uint16* after_ts_index, uint16* before_ts_index) {
 
 1104   auto template_set_index = imdl->code(I_HLP_TPL_ARGS).asIndex();
 
 1105   auto template_set_count = imdl->code(template_set_index).getAtomCount();
 
 1106   auto template_ti_index = template_set_index + template_set_count;
 
 1107   if (!(template_set_count >= 1 && imdl->code(template_ti_index).getDescriptor() == Atom::I_PTR &&
 
 1108         imdl->code(imdl->code(template_ti_index).asIndex()).asOpcode() == Opcodes::TI))
 
 1110   auto ti_index = imdl->code(template_ti_index).asIndex();
 
 1111   auto after_index = ti_index + 1;
 
 1112   auto before_index = ti_index + 2;
 
 1117     *after_ts_index = imdl->code(after_index).asIndex();
 
 1118   if (before_ts_index)
 
 1119     *before_ts_index = imdl->code(before_index).asIndex();
 
 1126 MDLController::RequirementEntry::RequirementEntry() : PredictedEvidenceEntry(), controller_(NULL), chaining_was_allowed_(false) {
 
 1129 MDLController::RequirementEntry::RequirementEntry(_Fact *f_p_f_imdl, MkRdx* mk_rdx, MDLController *c, 
bool chaining_was_allowed) : PredictedEvidenceEntry(f_p_f_imdl), mk_rdx_(mk_rdx), controller_(c), chaining_was_allowed_(chaining_was_allowed) {
 
 1134 PMDLController::PMDLController(
_View *view) : MDLController(view) {
 
 1137 void PMDLController::add_g_monitor(_GMonitor *m) {
 
 1139   g_monitorsCS_.enter();
 
 1140   g_monitors_.push_front(m);
 
 1141   g_monitorsCS_.leave();
 
 1144 void PMDLController::remove_g_monitor(_GMonitor *m) {
 
 1146   g_monitorsCS_.enter();
 
 1147   g_monitors_.remove(m);
 
 1148   g_monitorsCS_.leave();
 
 1151 void PMDLController::add_r_monitor(_GMonitor *m) {
 
 1153   g_monitorsCS_.enter();
 
 1154   r_monitors_.push_front(m);
 
 1155   g_monitorsCS_.leave();
 
 1158 void PMDLController::remove_r_monitor(_GMonitor *m) {
 
 1160   g_monitorsCS_.enter();
 
 1161   r_monitors_.remove(m);
 
 1162   g_monitorsCS_.leave();
 
 1165 void PMDLController::inject_goal(HLPBindingMap *bm, Fact *goal, Fact *f_imdl)
 const {
 
 1167   Group *primary_grp = get_host();
 
 1168   auto before = goal->get_before();
 
 1170   int32 resilience = _Mem::Get()->get_goal_pred_success_res(primary_grp, now, before - now);
 
 1172   View *view = 
new View(View::SYNC_ONCE, now, 1, resilience, primary_grp, primary_grp, goal); 
 
 1173   _Mem::Get()->inject(view);
 
 1175   MkRdx *mk_rdx = 
new MkRdx(f_imdl, goal->get_goal()->get_super_goal(), goal, 1, bm);
 
 1178   OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" mdl " 
 1179     << f_imdl->get_reference(0)->get_reference(0)->get_oid() << 
" abduce -> mk.rdx " << mk_rdx->get_oid());
 
 1182 void PMDLController::inject_simulation(Fact *goal_pred, Timestamp injectionTime)
 const { 
 
 1184   Group *primary_grp = get_host();
 
 1185   auto before = ((_Fact *)goal_pred->get_reference(0)->get_reference(0))->get_before();
 
 1187   int32 resilience = _Mem::Get()->get_goal_pred_success_res(primary_grp, now, before - now);
 
 1189   View *view = 
new View(View::SYNC_ONCE, injectionTime, 1, resilience, primary_grp, primary_grp, goal_pred); 
 
 1190   _Mem::Get()->inject(view);
 
 1193 bool PMDLController::monitor_goals(_Fact *input) {
 
 1197   g_monitorsCS_.enter();
 
 1198   for (m = g_monitors_.begin(); m != g_monitors_.end();) {
 
 1200     if ((*m)->reduce(input)) {
 
 1202       m = g_monitors_.erase(m);
 
 1207   g_monitorsCS_.leave();
 
 1211 void PMDLController::register_predicted_goal_outcome(Fact *goal, HLPBindingMap *bm, Fact *f_imdl, 
bool success, 
bool injected_goal) { 
 
 1218       inject_goal(bm, goal, f_imdl);
 
 1221       if (goal->is_invalidated()) { 
 
 1223         Fact *new_goal = 
new Fact(goal);
 
 1224         Goal *g = new_goal->get_goal();
 
 1226         auto deadline = g->get_target()->get_before();
 
 1227         auto sim_thz = get_sim_thz(now, deadline);
 
 1229         Sim *new_sim = 
new Sim(SIM_ROOT, sim_thz, g->get_sim()->get_f_super_goal(), 
false, 
this, 1);
 
 1231         g->set_sim(new_sim);
 
 1233         add_g_monitor(
new GMonitor(
this, bm, deadline, now + sim_thz, new_goal, f_imdl, NULL));
 
 1235         inject_goal(bm, new_goal, f_imdl);
 
 1241 inline microseconds PMDLController::get_sim_thz(Timestamp now, Timestamp deadline)
 const {
 
 1243   auto min_sim_thz = _Mem::Get()->get_min_sim_time_horizon(); 
 
 1244   auto sim_thz = _Mem::Get()->get_sim_time_horizon(deadline - now);
 
 1245   if (sim_thz > min_sim_thz) {
 
 1247     sim_thz -= min_sim_thz;
 
 1248     auto max_sim_thz = _Mem::Get()->get_max_sim_time_horizon();
 
 1249     if (sim_thz > max_sim_thz)
 
 1250       sim_thz = max_sim_thz;
 
 1253     return microseconds(0);
 
 1258 TopLevelMDLController::TopLevelMDLController(
_View *view) : PMDLController(view) {
 
 1264 void TopLevelMDLController::take_input(
r_exec::View *input) {
 
 1266   if (input->object_->code(0).asOpcode() == Opcodes::Fact ||
 
 1267     input->object_->code(0).asOpcode() == Opcodes::AntiFact) 
 
 1268     Controller::__take_input<TopLevelMDLController>(input);
 
 1271 void TopLevelMDLController::reduce(
r_exec::View *input) { 
 
 1273   if (input->object_->is_invalidated())
 
 1276   Goal *goal = ((_Fact *)input->object_)->get_goal();
 
 1277   if (goal && (goal->is_drive() || goal->is_imdl_drive())) {
 
 1279     _Fact *goal_target = goal->get_target(); 
 
 1281     if (confidence <= get_host()->code(GRP_SLN_THR).asFloat()) 
 
 1285     bm->reset_bwd_timings(goal_target);
 
 1286     if (bm->match_bwd_strict(goal_target, rhs_)) { 
 
 1289       OUTPUT_LINE(MDL_IN, Utils::RelativeTime(Now()) << 
" -> drive " <<
 
 1290         input->object_->get_oid() << 
", ijt " << Utils::RelativeTime(input->get_ijt()));
 
 1291       abduce(bm, (Fact*)input->object_, confidence);
 
 1293     else if (!goal->is_requirement()) { 
 
 1295       Code *imdl = goal_target->get_reference(0);
 
 1296       if (imdl->code(0).asOpcode() == Opcodes::IMdl && imdl->get_reference(0) == get_object()) { 
 
 1298         bm = 
new HLPBindingMap(bindings_);
 
 1299         bm->reset_bwd_timings(goal_target);
 
 1300         bm->init_from_f_ihlp(goal_target);
 
 1303         OUTPUT_LINE(MDL_IN, Utils::RelativeTime(Now()) << 
" -> drive " <<
 
 1304           input->object_->get_oid() << 
", ijt " << Utils::RelativeTime(input->get_ijt()));
 
 1305         abduce(bm, (Fact *)input->object_, confidence);
 
 1310     PrimaryMDLOverlay o(
this, bindings_);
 
 1311     o.reduce(input->object_, NULL, NULL); 
 
 1313     monitor_goals(input->object_);
 
 1317 void TopLevelMDLController::abduce(HLPBindingMap *bm, Fact *super_goal, float32 confidence) { 
 
 1321     P<_Fact> bound_lhs = (_Fact *)bm->bind_pattern(get_lhs());
 
 1325     switch (check_evidences(bound_lhs, evidence)) {
 
 1326     case MATCH_SUCCESS_POSITIVE: 
 
 1327       register_drive_outcome(super_goal, 
true);
 
 1329     case MATCH_SUCCESS_NEGATIVE: 
 
 1330       register_drive_outcome(super_goal, 
false);
 
 1333       f_imdl = get_f_ihlp(bm, 
false);
 
 1334       f_imdl->set_reference(0, bm->bind_pattern(f_imdl->get_reference(0))); 
 
 1335       bound_lhs->set_cfd(confidence);
 
 1336       switch (check_predicted_evidences(bound_lhs, evidence)) {
 
 1337       case MATCH_SUCCESS_POSITIVE:
 
 1339       case MATCH_SUCCESS_NEGATIVE:
 
 1344       abduce_lhs(bm, super_goal, bound_lhs, f_imdl, evidence);
 
 1350 void TopLevelMDLController::abduce_lhs(HLPBindingMap *bm,
 
 1352   _Fact *sub_goal_target, 
 
 1357   auto deadline = sub_goal_target->get_before();
 
 1358   auto sim_thz = get_sim_thz(now, deadline);
 
 1359   Sim *sub_sim = 
new Sim(SIM_ROOT, sim_thz, super_goal, 
false, 
this, 1);
 
 1361   sub_sim->register_goal_target(sub_goal_target);
 
 1363   Goal *sub_goal = 
new Goal(sub_goal_target, super_goal->get_goal()->get_actor(), sub_sim, 1);
 
 1364   Fact *f_sub_goal = 
new Fact(sub_goal, now, now, 1, 1);
 
 1367     inject_goal(bm, f_sub_goal, f_imdl);
 
 1368   add_g_monitor(
new GMonitor(
this, bm, deadline, now + sim_thz, f_sub_goal, f_imdl, evidence));
 
 1369   OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" mdl " << get_object()->get_oid() << 
" -> fact " << f_sub_goal->get_oid() << 
 
 1370     " goal [" << Utils::RelativeTime(sub_goal_target->get_after()) << 
"," << Utils::RelativeTime(sub_goal_target->get_before()) << 
"]");
 
 1373 void TopLevelMDLController::predict(HLPBindingMap *bm, _Fact *input, Fact *f_imdl, 
bool chaining_was_allowed, RequirementsPair &r_p, Fact *ground,
 
 1374   MkRdx* ground_mk_rdx, vector<
P<_Fact> >& already_predicted) { 
 
 1377 void TopLevelMDLController::register_pred_outcome(Fact *f_pred, 
Code* mk_rdx, 
bool success, _Fact *evidence, float32 confidence, 
bool rate_failures) {
 
 1380 void TopLevelMDLController::register_goal_outcome(Fact *goal, 
bool success, _Fact *evidence)
 const {
 
 1386   Code *f_goal_success;
 
 1390     goal_success = 
new Success(goal, evidence, 1);
 
 1391     f_goal_success = 
new Fact(goal_success, now, now, 1, 1);
 
 1397       absentee = goal->get_goal()->get_target()->get_absentee();
 
 1398       goal_success = 
new Success(goal, absentee, 1);
 
 1402       goal_success = 
new Success(goal, evidence, 1);
 
 1404     f_goal_success = 
new AntiFact(goal_success, now, now, 1, 1);
 
 1407   Group *primary_host = get_host();
 
 1408   uint16 out_group_count = get_out_group_count() - 1;
 
 1409   Group *drives_host = (Group *)get_out_group(out_group_count); 
 
 1410   for (uint16 i = 0; i < out_group_count; ++i) { 
 
 1412     Group *out_group = (Group *)get_out_group(i);
 
 1413     int32 resilience = _Mem::Get()->get_goal_pred_success_res(out_group, now, seconds(0));
 
 1414     View *view = 
new View(View::SYNC_ONCE, now, 1, resilience, out_group, primary_host, f_goal_success);
 
 1415     _Mem::Get()->inject(view);
 
 1419       view = 
new View(View::SYNC_ONCE, now, 1, 1, out_group, primary_host, absentee);
 
 1420       _Mem::Get()->inject(view);
 
 1424   register_drive_outcome(goal->get_goal()->get_sim()->get_f_super_goal(), success);
 
 1425   OUTPUT_LINE(GOAL_MON, Utils::RelativeTime(Now()) << 
" fact " << f_goal_success->get_oid() << 
": " << goal->get_oid() <<
 
 1426     " goal " << (success ? 
"success" : 
"failure") << 
" (TopLevel)");
 
 1429 void TopLevelMDLController::register_drive_outcome(Fact *drive, 
bool success)
 const {
 
 1431   drive->invalidate();
 
 1434   Code *drive_success = 
new Success(drive, NULL, 1);
 
 1435   Code *f_drive_success;
 
 1437     f_drive_success = 
new Fact(drive_success, now, now, 1, 1);
 
 1439     f_drive_success = 
new AntiFact(drive_success, now, now, 1, 1);
 
 1441   Group *primary_host = get_host();
 
 1442   uint16 out_group_count = get_out_group_count() - 1;
 
 1443   Group *drives_host = (Group *)get_out_group(out_group_count); 
 
 1444   View *view = 
new View(View::SYNC_ONCE, now, 1, 1, drives_host, primary_host, f_drive_success); 
 
 1445   _Mem::Get()->inject(view); 
 
 1448 void PMDLController::inject_simulated_goal_success(Fact *goal, 
bool success, _Fact *evidence)
 const { 
 
 1450   Code *success_object = 
new Success(goal, evidence, 1);
 
 1451   Pred *evidence_pred = evidence->get_pred();
 
 1452   float32 confidence = evidence_pred->get_target()->get_cfd();
 
 1453   _Fact* evidence_f_target = evidence_pred->get_target();
 
 1454   _Fact *f_success_object;
 
 1456     f_success_object = 
new Fact(success_object, evidence_f_target->get_after(), evidence_f_target->get_before(), confidence, 1);
 
 1458     f_success_object = 
new AntiFact(success_object, evidence_f_target->get_after(), evidence_f_target->get_before(), confidence, 1);
 
 1460   Pred *pred = 
new Pred(f_success_object, evidence_pred, 1);
 
 1462   Fact *f_pred = 
new Fact(pred, now, now, 1, 1);
 
 1464   Group *primary_host = get_host();
 
 1465   int32 resilience = _Mem::Get()->get_goal_pred_success_res(primary_host, now, seconds(0));
 
 1466   View *view = 
new View(View::SYNC_ONCE, now, 1, resilience, primary_host, primary_host, f_pred);
 
 1467   _Mem::Get()->inject(view); 
 
 1468   OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" mdl " << get_object()->get_oid() << 
": fact " <<
 
 1469     evidence->get_oid() << 
" pred -> fact " << f_pred->get_oid() << 
" simulated pred");
 
 1472 void TopLevelMDLController::register_simulated_goal_outcome(Fact* goal, 
bool success, _Fact* evidence)
 const { 
 
 1473   inject_simulated_goal_success(goal, success, evidence);
 
 1476 void TopLevelMDLController::register_req_outcome(Fact *f_pred, 
bool success, 
bool rate_failures) {
 
 1481 PrimaryMDLController::PrimaryMDLController(
_View *view) : PMDLController(view) {
 
 1485 void PrimaryMDLController::set_secondary(SecondaryMDLController *secondary) {
 
 1487   secondary_ = secondary;
 
 1488   add_requirement_to_rhs();
 
 1489   secondary->add_requirement_to_rhs();
 
 1494   bool is_simulation = f_p_f_imdl->get_pred()->is_simulation();
 
 1495   _Fact *f_imdl = f_p_f_imdl->get_pred()->get_target();
 
 1499   if (f_imdl->is_fact()) {
 
 1501       _store_requirement(&simulated_requirements_.positive_evidences_, e);
 
 1503       _store_requirement(&requirements_.positive_evidences_, e);
 
 1508       _store_requirement(&simulated_requirements_.negative_evidences_, e);
 
 1510       _store_requirement(&requirements_.negative_evidences_, e);
 
 1514   if (!f_imdl->is_fact() && is_simulation && f_p_f_imdl->get_pred()->
get_simulations_size() == 1) {
 
 1517     uint32 r_count = get_requirement_count(wr_count, sr_count);
 
 1518     if (wr_count > 0 && sr_count > 0) {
 
 1521       requirements_.CS_.enter();
 
 1522       for (
auto e = simulated_requirements_.positive_evidences_.begin();
 
 1523            e != simulated_requirements_.positive_evidences_.end(); ++e) {
 
 1526         if ((*e).evidence_->get_pred()->has_defeasible_consequence() &&
 
 1527             (*e).evidence_->get_pred()->has_simulation(sim)) {
 
 1528           _Fact *_f_imdl = (*e).evidence_->get_pred()->get_target();
 
 1530           _original.reset_fwd_timings(f_imdl);
 
 1532           if (_original.match_fwd_lenient(_f_imdl, f_imdl) == MATCH_SUCCESS_NEGATIVE &&
 
 1533               f_imdl->get_cfd() >= (*e).confidence_) {
 
 1536             (*e).evidence_->get_pred()->get_defeasible_consequence()->invalidate();
 
 1537 #ifdef WITH_DETAIL_OID 
 1538             OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" mdl " << get_object()->get_oid() << 
": fact (" <<
 
 1539               to_string((*e).evidence_->get_detail_oid()) << 
") pred fact imdl, simulated pred disabled by fact (" <<
 
 1540               to_string(f_p_f_imdl->get_detail_oid()) << 
") pred |fact imdl");
 
 1545       requirements_.CS_.leave();
 
 1550   if (f_imdl->is_fact()) {
 
 1552     g_monitorsCS_.enter();
 
 1553     for (m = r_monitors_.begin(); m != r_monitors_.end();) { 
 
 1555       if (!(*m)->is_alive())
 
 1556         m = r_monitors_.erase(m);
 
 1559         if ((*m)->signal(f_p_f_imdl->get_pred()))
 
 1560           m = r_monitors_.erase(m);
 
 1565     g_monitorsCS_.leave();
 
 1567     reduce_cache<PrimaryMDLController>((
Fact *)f_p_f_imdl, controller);
 
 1571     secondary_->
store_requirement(f_p_f_imdl, NULL, controller, chaining_was_allowed);
 
 1574 void PrimaryMDLController::take_input(
r_exec::View *input) {
 
 1576   if (become_invalidated())
 
 1579   if (input->object_->code(0).asOpcode() == Opcodes::Fact ||
 
 1580     input->object_->code(0).asOpcode() == Opcodes::AntiFact) 
 
 1581     Controller::__take_input<PrimaryMDLController>(input);
 
 1584 void PrimaryMDLController::predict(HLPBindingMap *bm, _Fact *input, Fact *f_imdl, 
bool chaining_was_allowed, RequirementsPair &r_p, Fact *ground,
 
 1585   MkRdx* ground_mk_rdx, vector<
P<_Fact> >& already_predicted) {
 
 1587   _Fact *bound_rhs = (_Fact *)bm->bind_pattern(rhs_); 
 
 1589   for (
auto i = already_predicted.begin(); i != already_predicted.end(); ++i) {
 
 1598   Pred *prediction = input->get_pred();
 
 1601     is_simulation = prediction->is_simulation();
 
 1602     if (chaining_was_allowed)
 
 1608     is_simulation = 
false;
 
 1609     if (chaining_was_allowed)
 
 1615   bound_rhs->set_cfd(confidence);
 
 1619     pred = 
new Pred(bound_rhs, prediction, 1);
 
 1621     pred = 
new Pred(bound_rhs, 1);
 
 1623   Fact *production = 
new Fact(pred, now, now, 1, 1);
 
 1625   if (prediction && !is_simulation) { 
 
 1627     pred->grounds_.push_back(input);
 
 1629       pred->grounds_.push_back(ground);
 
 1632   if (is_requirement()) {
 
 1634     if (is_invalidated())
 
 1638     MkRdx* mk_rdx = 
new MkRdx(f_imdl, (
Code *)input, production, 1, bm);
 
 1640     OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" mdl " << get_object()->get_oid() << 
" predict imdl -> mk.rdx " << mk_rdx->get_oid());
 
 1642     PrimaryMDLController *c = (PrimaryMDLController *)controllers_[RHSController]; 
 
 1643     c->store_requirement(production, mk_rdx, 
this, chaining_was_allowed); 
 
 1644 #ifdef WITH_DETAIL_OID 
 1645     OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" fact (" << f_imdl->get_detail_oid() << 
") imdl mdl " << get_object()->get_oid() <<
 
 1646       ": " << input->get_oid() << 
" -> fact (" << production->get_detail_oid() << 
") pred fact (" <<
 
 1647       bound_rhs->get_detail_oid() << 
") imdl mdl " << bound_rhs->get_reference(0)->get_reference(0)->get_oid());
 
 1649     OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" mdl " << get_object()->get_oid() << 
": " << input->get_oid() << 
 
 1650       " -> fact pred fact imdl mdl " << bound_rhs->get_reference(0)->get_reference(0)->get_oid());
 
 1655   if (!is_simulation) { 
 
 1657     register_requirement(production, r_p);
 
 1659     if (!chaining_was_allowed) { 
 
 1661       PMonitor *m = 
new PMonitor(
this, bm, production, NULL, 
false); 
 
 1662       MDLController::add_monitor(m);
 
 1665       auto before = bound_rhs->get_before();
 
 1670         Fact *f_pred_f_imdl = 
new Fact(
new Pred(f_imdl, 1), now, now, 1, 1);
 
 1671         if (!inject_prediction(production, f_pred_f_imdl, confidence, before - now, NULL))
 
 1673         already_predicted.push_back(bound_rhs);
 
 1675 #ifdef WITH_DETAIL_OID 
 1676         f_imdl_info = 
" fact (" + to_string(f_imdl->get_detail_oid()) + 
") imdl";
 
 1678         OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << f_imdl_info << 
" mdl " << 
 
 1679           get_object()->get_oid() << 
": " << input->get_oid() << 
" -> fact " << production->get_oid() << 
 
 1680           " pred fact mk.val VALUE " << bound_rhs->get_reference(0)->trace_string(MK_VAL_VALUE));
 
 1685           mk_rdx = 
new MkRdx(f_imdl, (
Code *)input, ground_mk_rdx, production, 1, bm);
 
 1687           mk_rdx = 
new MkRdx(f_imdl, (
Code *)input, production, 1, bm);
 
 1688         bool rate_failures = inject_prediction(production, f_imdl, confidence, before - now, mk_rdx);
 
 1689         PMonitor *m = 
new PMonitor(
this, bm, production, mk_rdx, rate_failures); 
 
 1690         MDLController::add_monitor(m);
 
 1691         Group *secondary_host = secondary_->get_view()->get_host(); 
 
 1692         View *view = 
new View(View::SYNC_ONCE, now, confidence, 1, get_view()->get_host(), secondary_host, f_imdl); 
 
 1693         _Mem::Get()->inject(view);
 
 1694         OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" mdl " << get_object()->get_oid() << 
" predict -> mk.rdx " << mk_rdx->get_oid());
 
 1696 #ifdef WITH_DETAIL_OID 
 1697         f_imdl_info = 
"(" + to_string(f_imdl->get_detail_oid()) + 
")";
 
 1699         OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" fact " << f_imdl->get_oid() << f_imdl_info << 
" imdl mdl " << 
 
 1700           get_object()->get_oid() << 
": " << input->get_oid() << 
" -> fact " << production->get_oid() << 
 
 1701           " pred fact mk.val VALUE " << bound_rhs->get_reference(0)->trace_string(MK_VAL_VALUE));
 
 1710       get_requirement_count(wr_count, sr_count);
 
 1711       if (wr_count > 0 && sr_count > 0)
 
 1713         pred->defeasible_validities_.insert(ground->get_pred()->get_defeasible_consequence());
 
 1717     if (!HLPController::inject_prediction(production, confidence)) 
 
 1719     already_predicted.push_back(bound_rhs);
 
 1721 #ifdef WITH_DETAIL_OID 
 1723       ground_info = 
", using req (" + to_string(ground->get_detail_oid()) + 
")";
 
 1725     OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" mdl " << get_object()->get_oid() << 
": fact " <<
 
 1726       input->get_oid() << 
" pred -> fact " << production->get_oid() << 
" simulated pred" << ground_info);
 
 1728     if (is_cmd() || is_reuse()) {
 
 1730       Fact *f_pred_f_imdl = 
new Fact(
new Pred(f_imdl, prediction, 1), now, now, 1, 1);
 
 1731       if (!HLPController::inject_prediction(f_pred_f_imdl, confidence))
 
 1733       OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" mdl " << get_object()->get_oid() << 
": fact " <<
 
 1734         input->get_oid() << 
" pred -> fact " << f_pred_f_imdl->get_oid() << 
" simulated pred fact imdl" << ground_info);
 
 1739 bool PrimaryMDLController::inject_prediction(Fact *prediction, Fact *f_imdl, float32 confidence, Timestamp::duration time_to_live, 
Code *mk_rdx)
 const { 
 
 1742   Group *primary_host = get_host();
 
 1743   float32 sln_thr = primary_host->code(GRP_SLN_THR).asFloat();
 
 1744   if (confidence > sln_thr) { 
 
 1746     int32 resilience = _Mem::Get()->get_goal_pred_success_res(primary_host, now, time_to_live);
 
 1747     View *view = 
new View(View::SYNC_ONCE, now, confidence, resilience, primary_host, primary_host, prediction); 
 
 1748     _Mem::Get()->inject(view);
 
 1750     view = 
new View(View::SYNC_ONCE, now, 1, 1, primary_host, primary_host, f_imdl); 
 
 1751     _Mem::Get()->inject(view);
 
 1760 void PrimaryMDLController::reduce(
r_exec::View *input) { 
 
 1765   if (input->object_->is_invalidated())
 
 1769   assumptionsCS_.enter();
 
 1770   for (a = assumptions_.begin(); a != assumptions_.end();) { 
 
 1772     if ((*a)->is_invalidated()) 
 
 1773       a = assumptions_.erase(a);
 
 1774     else if (((
Code *)*a) == input->object_) {
 
 1776       a = assumptions_.erase(a);
 
 1777       assumptionsCS_.leave();
 
 1782   assumptionsCS_.leave();
 
 1784   Goal *goal = ((_Fact *)input->object_)->get_goal();
 
 1785   if (goal && goal->is_self_goal() && !goal->is_drive()) {
 
 1787     _Fact *goal_target = goal->get_target(); 
 
 1789     Code *host = get_host();
 
 1790     if (confidence <= host->code(GRP_SLN_THR).asFloat()) 
 
 1794     bm->reset_bwd_timings(goal_target);
 
 1795     bool opposite = 
false;
 
 1796     MatchResult match_result = bm->match_bwd_lenient(goal_target, rhs_);
 
 1797     switch (match_result) {
 
 1798     case MATCH_SUCCESS_NEGATIVE:
 
 1800     case MATCH_SUCCESS_POSITIVE:
 
 1801       abduce(bm, (Fact *)input->object_, opposite, confidence);
 
 1804       if (!goal->is_requirement() && goal_target->is_fact()) { 
 
 1806         Code *imdl = goal_target->get_reference(0);
 
 1807         if (imdl->code(0).asOpcode() == Opcodes::IMdl && imdl->get_reference(0) == get_object()) { 
 
 1809           bm = 
new HLPBindingMap(bindings_);
 
 1810           bm->reset_bwd_timings(goal_target);
 
 1811           bm->init_from_f_ihlp(goal_target);
 
 1813           abduce(bm, (Fact *)input->object_, opposite, confidence);
 
 1820     PrimaryMDLOverlay o(
this, bindings_);
 
 1821     bool match = o.reduce((_Fact *)input->object_, NULL, NULL);
 
 1822     bool matched_p_monitor = 
false;
 
 1823     bool matched_g_monitor = 
false;
 
 1825       matched_p_monitor = monitor_predictions((_Fact*)input->object_);
 
 1826     if (!matched_p_monitor)
 
 1828       matched_g_monitor = monitor_goals((_Fact*)input->object_);
 
 1829     if (!match && !matched_p_monitor && !matched_g_monitor)
 
 1830       assume((_Fact *)input->object_);
 
 1832     check_last_match_time(match);
 
 1836 void PrimaryMDLController::debug(View *input) {
 
 1839 void PrimaryMDLController::reduce_batch(Fact *f_p_f_imdl, MDLController *controller) {
 
 1844   reduce_cache<EvidenceEntry>(&evidences_, f_p_f_imdl, controller);
 
 1845   reduce_cache<PredictedEvidenceEntry>(&predicted_evidences_, f_p_f_imdl, controller);
 
 1848 void PrimaryMDLController::abduce(HLPBindingMap *bm, Fact *super_goal, 
bool opposite, float32 confidence) { 
 
 1850   if (!abduction_allowed(bm))
 
 1853   P<Fact> f_imdl = get_f_ihlp(bm, 
false);
 
 1854   Sim *sim = super_goal->get_goal()->get_sim();
 
 1855   microseconds sim_thz(0);
 
 1857     sim_thz = sim->get_thz(); 
 
 1858   auto min_sim_thz = _Mem::Get()->get_min_sim_time_horizon() / 2; 
 
 1861   if (sim_thz > min_sim_thz) {
 
 1863     sim_thz -= min_sim_thz;
 
 1866     switch (sim->get_mode()) {
 
 1868       sub_sim = 
new Sim(opposite ? SIM_MANDATORY : SIM_OPTIONAL, sim_thz, super_goal, opposite, sim->root_, 1, 
this, confidence, now + sim_thz);
 
 1871       sub_sim = 
new Sim(opposite ? SIM_MANDATORY : SIM_OPTIONAL, sim_thz, super_goal, opposite, sim->root_, 1, 
this, sim->get_solution_cfd(), sim->get_solution_before());
 
 1877       sub_sim = 
new Sim(sim->get_mode(), sim_thz, super_goal, opposite, sim->root_, 1, 
this, sim->get_solution_cfd(), sim->get_solution_before());
 
 1882     Fact *strong_requirement_ground;
 
 1884     P<Code> save_imdl = f_imdl->get_reference(0);
 
 1885     ChainingStatus c_s = retrieve_imdl_bwd(bm, f_imdl, ground, strong_requirement_ground);
 
 1887     case WEAK_REQUIREMENT_ENABLED:
 
 1888       f_imdl->get_reference(0)->code(I_HLP_WEAK_REQUIREMENT_ENABLED) = Atom::Boolean(
true);
 
 1889     case NO_REQUIREMENT:
 
 1890       if (sub_sim->get_mode() == SIM_ROOT)
 
 1891         abduce_lhs(bm, super_goal, f_imdl, opposite, confidence, sub_sim, ground, 
true);
 
 1893         abduce_simulated_lhs(bm, super_goal, f_imdl, opposite, confidence, sub_sim, ground);
 
 1900           save_imdl->code(I_HLP_WEAK_REQUIREMENT_ENABLED) = Atom::Boolean(
false);
 
 1901           abduce_simulated_imdl(save_bm, super_goal,
 
 1902             new Fact(save_imdl, f_imdl->get_after(), f_imdl->get_before(), confidence, 1), opposite, confidence, sub_sim);
 
 1914       Fact *sim_strong_requirement_ground;
 
 1915       ChainingStatus sim_c_s = retrieve_simulated_imdl_bwd(bm, f_imdl, sim, sim_ground, sim_strong_requirement_ground);
 
 1916       if (c_s == STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT && sim_c_s == NO_REQUIREMENT)
 
 1918         sim_c_s = STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT;
 
 1920       case WEAK_REQUIREMENT_ENABLED:
 
 1921         f_imdl->get_reference(0)->code(I_HLP_WEAK_REQUIREMENT_ENABLED) = Atom::Boolean(
true);
 
 1922       case NO_REQUIREMENT:
 
 1923         if (sub_sim->get_mode() == SIM_ROOT)
 
 1924           abduce_lhs(bm, super_goal, f_imdl, opposite, confidence, sub_sim, NULL, 
true);
 
 1926           abduce_simulated_lhs(bm, super_goal, f_imdl, opposite, confidence, sub_sim, sim_ground);
 
 1929 #ifdef WITH_DETAIL_OID 
 1930         if (c_s == STRONG_REQUIREMENT_DISABLED_WEAK_REQUIREMENT && sim_c_s == WEAK_REQUIREMENT_DISABLED)
 
 1933           OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" mdl " << get_object()->get_oid() << 
": fact (" <<
 
 1934             to_string(ground->get_detail_oid()) << 
") pred fact imdl, from goal req " << super_goal->get_oid() <<
 
 1935             ", pred disabled by fact (" << to_string(strong_requirement_ground->get_detail_oid()) <<
 
 1936             ") pred |fact imdl");
 
 1938         sub_sim->is_requirement_ = 
true;
 
 1939         if (sub_sim->get_mode() == SIM_ROOT)
 
 1940           abduce_imdl(bm, super_goal, f_imdl, opposite, confidence, sub_sim);
 
 1942           abduce_simulated_imdl(bm, super_goal, f_imdl, opposite, confidence, sub_sim);
 
 1951     Fact *strong_requirement_ground;
 
 1952     SimMode mode = SIM_ROOT;
 
 1954       mode = sim->get_mode();
 
 1957       switch (retrieve_imdl_bwd(bm, f_imdl, ground, strong_requirement_ground)) {
 
 1958       case WEAK_REQUIREMENT_ENABLED:
 
 1959         f_imdl->get_reference(0)->code(I_HLP_WEAK_REQUIREMENT_ENABLED) = Atom::Boolean(
true);
 
 1960       case NO_REQUIREMENT:
 
 1961         sub_sim = 
new Sim(SIM_ROOT, seconds(0), super_goal, opposite, 
this, 1);
 
 1962         abduce_lhs(bm, super_goal, f_imdl, opposite, confidence, sub_sim, ground, 
false);
 
 1965         sub_sim = 
new Sim(SIM_ROOT, seconds(0), super_goal, opposite, 
this, 1);
 
 1966         sub_sim->is_requirement_ = 
true;
 
 1967         abduce_imdl(bm, super_goal, f_imdl, opposite, confidence, sub_sim);
 
 1973       predict_simulated_lhs(bm, opposite, confidence, sim);
 
 1982   Goal *super_goal = f_super_goal->get_goal();
 
 1983   _Fact *f_goal_target = super_goal->get_target();
 
 1985   bm->reset_bwd_timings(f_goal_target);
 
 1986   MatchResult match_result = bm->match_bwd_lenient(f_goal_target, get_rhs());
 
 1987   if (!(match_result == MATCH_SUCCESS_NEGATIVE || match_result == MATCH_SUCCESS_POSITIVE)) {
 
 1989     Code *imdl = f_goal_target->get_reference(0);
 
 1990     if (imdl->code(0).asOpcode() == Opcodes::IMdl && imdl->get_reference(0) == get_object()) {
 
 1993       bm->reset_bwd_timings(f_goal_target);
 
 1994       bm->init_from_f_ihlp(f_goal_target);
 
 2002     (f_goal_target, super_goal->get_actor(), 
new Sim(SIM_ROOT, seconds(0), super_goal->
get_sim()->
get_f_super_goal(), 
false, 
this, 1),
 
 2003      super_goal->get_psln_thr());
 
 2005     super_goal_copy, f_super_goal->get_after(), f_super_goal->get_before(), f_super_goal->get_cfd(),
 
 2006     f_super_goal->get_psln_thr());
 
 2007 #ifdef WITH_DETAIL_OID 
 2009     OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" sim commit: fact " << f_p_f_success->get_oid() <<
 
 2010       " pred fact success -> fact (" << f_super_goal_copy->get_detail_oid() << 
") goal");
 
 2013   abduce(bm, f_super_goal_copy, opposite, confidence);
 
 2016 void PrimaryMDLController::abduce_lhs(
HLPBindingMap *bm, 
Fact *super_goal, 
Fact *f_imdl, 
bool opposite, float32 confidence, 
Sim *sim, 
Fact *ground, 
bool set_before) { 
 
 2022       bound_lhs->set_opposite();
 
 2023     bound_lhs->set_cfd(confidence);
 
 2026     switch (check_evidences(bound_lhs, evidence)) {
 
 2027     case MATCH_SUCCESS_POSITIVE: 
 
 2029     case MATCH_SUCCESS_NEGATIVE: 
 
 2031     case MATCH_FAILURE: {
 
 2033       f_imdl->set_reference(0, bm->bind_pattern(f_imdl->get_reference(0))); 
 
 2035       switch (check_predicted_evidences(bound_lhs, evidence)) {
 
 2036       case MATCH_SUCCESS_POSITIVE:
 
 2038       case MATCH_SUCCESS_NEGATIVE:
 
 2044       Goal *sub_goal = 
new Goal(bound_lhs, super_goal->get_goal()->get_actor(), sim, 1);
 
 2045       sub_goal->ground_ = ground;
 
 2050       Fact *f_sub_goal = 
new Fact(sub_goal, now, now, 1, 1);
 
 2052       add_g_monitor(
new GMonitor(
this, bm, bound_lhs->get_before(), Timestamp(seconds(0)), f_sub_goal, f_imdl, evidence));
 
 2055         inject_goal(bm, f_sub_goal, f_imdl);
 
 2056         OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" mdl " << get_object()->get_oid() << 
" -> fact " << f_sub_goal->get_oid() << 
 
 2057           " goal [" << Utils::RelativeTime(sub_goal->get_target()->get_after()) << 
"," << Utils::RelativeTime(sub_goal->get_target()->get_before()) << 
"]");
 
 2065 void PrimaryMDLController::abduce_imdl(HLPBindingMap *bm, Fact *super_goal, Fact *f_imdl, 
bool opposite, float32 confidence, Sim *sim) { 
 
 2069   Timestamp f_imdl_after, f_imdl_before;
 
 2071     Utils::SetTimestamp<Code>(f_imdl, FACT_AFTER, f_imdl_after);
 
 2072     Utils::SetTimestamp<Code>(f_imdl, FACT_BEFORE, f_imdl_before);
 
 2074     bm->reset_fwd_timings(f_imdl);
 
 2076   f_imdl->set_cfd(confidence);
 
 2078   Goal *sub_goal = 
new Goal(f_imdl, super_goal->get_goal()->get_actor(), sim, 1);
 
 2081   Fact *f_sub_goal = 
new Fact(sub_goal, now, now, 1, 1);
 
 2082   add_r_monitor(
new RMonitor(
this, bm, super_goal->get_goal()->get_target()->get_before(), now + sim->get_thz(), f_sub_goal, f_imdl)); 
 
 2083   inject_goal(bm, f_sub_goal, f_imdl);
 
 2084   string f_imdl_detail_info;
 
 2085 #ifdef WITH_DETAIL_OID 
 2086   f_imdl_detail_info = 
"(" + to_string(f_imdl->get_detail_oid()) + 
")";
 
 2088   OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" " << get_object()->get_oid() << 
" -> fact " << f_sub_goal->get_oid() << 
" goal fact " << 
 
 2089     f_imdl->get_oid() << f_imdl_detail_info << 
" imdl[" << f_imdl->get_reference(0)->get_reference(0)->get_oid() << 
"][" <<
 
 2090     Utils::RelativeTime(sub_goal->get_target()->get_after()) << 
"," << Utils::RelativeTime(sub_goal->get_target()->get_before()) << 
"]");
 
 2094 _Fact* PrimaryMDLController::abduce_simulated_lhs(HLPBindingMap *bm, Fact *super_goal, Fact *f_imdl, 
bool opposite, float32 confidence,
 
 2095   Sim *sim, Fact *ground, Fact* goal_requirement) {
 
 2097   _Fact* injected_lhs = NULL;
 
 2100     P<_Fact> bound_lhs = (_Fact *)bm->bind_pattern(get_lhs());
 
 2102       bound_lhs->set_opposite();
 
 2103     bound_lhs->set_cfd(confidence);
 
 2106     switch (check_evidences(bound_lhs, evidence)) {
 
 2107     case MATCH_SUCCESS_POSITIVE: 
 
 2108       register_simulated_goal_outcome(super_goal, 
true, evidence);
 
 2110     case MATCH_SUCCESS_NEGATIVE: 
 
 2111       register_simulated_goal_outcome(super_goal, 
false, evidence);
 
 2114       switch (check_predicted_evidences(bound_lhs, evidence)) {
 
 2115       case MATCH_SUCCESS_POSITIVE: 
 
 2116         register_simulated_goal_outcome(super_goal, 
true, evidence);
 
 2118       case MATCH_SUCCESS_NEGATIVE:
 
 2119         register_simulated_goal_outcome(super_goal, 
false, evidence);
 
 2121       case MATCH_FAILURE: {
 
 2124         f_imdl->set_reference(0, bm->bind_pattern(f_imdl->get_reference(0))); 
 
 2129           Pred* ground_pred = ground->get_pred();
 
 2131           Pred *pred = 
new Pred(bound_lhs, ground_pred, 1);
 
 2132           Fact* fact_pred_bound_lhs = 
new Fact(pred, now, now, 1, 1);
 
 2133           if (ground_pred && ground_pred->get_simulations_size() == 1) {
 
 2135             vector<P<_Fact> >& sim_already_signalled = ground_pred->get_simulation((uint16)0)->already_signalled_;
 
 2138             for (
auto signalled = sim_already_signalled.begin(); signalled != sim_already_signalled.end(); ++signalled) {
 
 2140               Pred* signalled_pred = (*signalled)->get_pred();
 
 2146                 for (
auto d = signalled_pred->defeasible_validities_.begin();
 
 2147                      d != signalled_pred->defeasible_validities_.end();) {
 
 2148                   if (ground_pred->defeasible_validities_.find(*d) == ground_pred->defeasible_validities_.end())
 
 2149                     d = signalled_pred->defeasible_validities_.erase(d);
 
 2162             sim_already_signalled.push_back(fact_pred_bound_lhs);
 
 2165           inject_simulation(fact_pred_bound_lhs, now);
 
 2166           injected_lhs = fact_pred_bound_lhs;
 
 2169 #ifdef WITH_DETAIL_OID 
 2170           ground_info = 
" (" + to_string(ground->get_detail_oid()) + 
")";
 
 2172           string goal_requirement_info = 
"";
 
 2173           if (goal_requirement)
 
 2174             goal_requirement_info = 
", from goal req " + to_string(goal_requirement->get_oid());
 
 2176           OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(now) << 
" mdl " << get_object()->get_oid() << 
": fact" <<
 
 2177             ground_info << 
" pred fact imdl -> fact " << fact_pred_bound_lhs->get_oid() << 
" simulated pred" <<
 
 2178             goal_requirement_info);
 
 2190           auto sub_sim = 
new Sim(
 
 2191             sim->get_mode(), sim->get_thz(), super_goal, opposite, sim->root_, 1, 
this, sim->get_solution_cfd(),
 
 2192             sim->get_solution_before());
 
 2193           Pred *pred_bound_lhs = 
new Pred(bound_lhs, sub_sim, 1);
 
 2194           auto forward_simulation_time = max(now, sim->get_solution_before() - sim->get_thz() / 2);
 
 2195           Fact* f_pred_bound_lhs = 
new Fact(pred_bound_lhs, forward_simulation_time, forward_simulation_time, 1, 1);
 
 2196           inject_simulation(f_pred_bound_lhs, forward_simulation_time);
 
 2197           injected_lhs = f_pred_bound_lhs;
 
 2198           string f_pred_bound_lhs_info;
 
 2200 #ifdef WITH_DETAIL_OID 
 2201           f_pred_bound_lhs_info = 
" (" + to_string(f_pred_bound_lhs->get_detail_oid()) + 
")";
 
 2203             ground_info = 
", using req (" + to_string(ground->get_detail_oid()) + 
")";
 
 2205           OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(now) << 
" mdl " << get_object()->get_oid() << 
": fact " <<
 
 2206             super_goal->get_oid() << 
" super_goal -> fact" << f_pred_bound_lhs_info << 
" simulated pred start" <<
 
 2207             ground_info << 
", ijt " << Utils::RelativeTime(forward_simulation_time));
 
 2211         if (!sim->register_goal_target(bound_lhs))
 
 2215         Goal *sub_goal = 
new Goal(bound_lhs, super_goal->get_goal()->get_actor(), sim, 1);
 
 2217         Fact *f_sub_goal = 
new Fact(sub_goal, now, now, 1, 1);
 
 2219         add_g_monitor(
new SGMonitor(
this, bm, now + sim->get_thz(), f_sub_goal, f_imdl));
 
 2220         inject_simulation(f_sub_goal, now);
 
 2221         injected_lhs = f_sub_goal;
 
 2222         OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(now) << 
" mdl " << get_object()->get_oid() << 
": fact " <<
 
 2223           super_goal->get_oid() << 
" super_goal -> fact " << f_sub_goal->get_oid() << 
" simulated goal");
 
 2231   return injected_lhs;
 
 2234 void PrimaryMDLController::abduce_simulated_imdl(HLPBindingMap *bm, Fact *super_goal, Fact *f_imdl, 
bool opposite, float32 confidence, Sim *sim) { 
 
 2236   if (!sim->is_requirement_ && !sim->register_goal_target(f_imdl))
 
 2242   Timestamp f_imdl_after, f_imdl_before;
 
 2244     Utils::SetTimestamp<Code>(f_imdl, FACT_AFTER, f_imdl_after);
 
 2245     Utils::SetTimestamp<Code>(f_imdl, FACT_BEFORE, f_imdl_before);
 
 2247     bm->reset_fwd_timings(f_imdl);
 
 2249   f_imdl->set_cfd(confidence);
 
 2251   Goal *sub_goal = 
new Goal(f_imdl, super_goal->get_goal()->get_actor(), sim, 1);
 
 2254   Fact *f_sub_goal = 
new Fact(sub_goal, now, now, 1, 1);
 
 2255   add_r_monitor(
new SRMonitor(
this, bm, now + sim->get_thz(), f_sub_goal, f_imdl));
 
 2256   inject_simulation(f_sub_goal, now);
 
 2257   OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" mdl " << get_object()->get_oid() << 
": fact " <<
 
 2258     super_goal->get_oid() << 
" super_goal -> fact " << f_sub_goal->get_oid() << 
" simulated goal");
 
 2261 bool PrimaryMDLController::check_imdl(Fact *goal, HLPBindingMap *bm) { 
 
 2263   Goal *g = goal->get_goal();
 
 2264   Fact *f_imdl = (Fact *)g->get_target();
 
 2266   Sim *sim = g->get_sim();
 
 2268   Fact *strong_requirement_ground;
 
 2269   switch (retrieve_imdl_bwd(bm, f_imdl, ground, strong_requirement_ground)) {
 
 2270   case WEAK_REQUIREMENT_ENABLED:
 
 2271     f_imdl->get_reference(0)->code(I_HLP_WEAK_REQUIREMENT_ENABLED) = Atom::Boolean(
true);
 
 2272   case NO_REQUIREMENT:
 
 2275       f_imdl->set_reference(0, bm->bind_pattern(f_imdl->get_reference(0))); 
 
 2276       abduce_lhs(bm, sim->get_f_super_goal(), f_imdl, sim->get_opposite(), f_imdl->get_cfd(), 
new Sim(SIM_ROOT, seconds(0), sim->get_f_super_goal(), sim->get_opposite(), 
this, 1), ground, 
false);
 
 2288   Goal *g = goal->get_goal();
 
 2289   Fact *f_imdl = (
Fact *)g->get_target();
 
 2292   Fact *strong_requirement_ground;
 
 2294     c_s = retrieve_simulated_imdl_bwd(bm, f_imdl, prediction_sim, ground, strong_requirement_ground);
 
 2296     c_s = retrieve_imdl_bwd(bm, f_imdl, ground, strong_requirement_ground);
 
 2300   case WEAK_REQUIREMENT_ENABLED:
 
 2301     f_imdl->get_reference(0)->code(I_HLP_WEAK_REQUIREMENT_ENABLED) = Atom::Boolean(
true);
 
 2302   case NO_REQUIREMENT:
 
 2306         bm->bind_pattern(f_imdl->get_reference(0)), f_imdl->get_after(), f_imdl->get_before(),
 
 2307         f_imdl->get_cfd(), f_imdl->get_psln_thr());
 
 2309       _Fact* injected_lhs = abduce_simulated_lhs(bm, sim->
get_f_super_goal(), f_imdl_copy, sim->
get_opposite(), f_imdl->get_cfd(), prediction_sim ? NULL : 
new Sim(sim), ground, goal);
 
 2311       if (c_s == WEAK_REQUIREMENT_ENABLED && prediction_sim && injected_lhs) {
 
 2315         get_requirement_count(wr_count, sr_count);
 
 2316         if (wr_count > 0 && sr_count > 0) {
 
 2326     if (c_s == STRONG_REQUIREMENT_DISABLED_WEAK_REQUIREMENT && prediction_sim && ground && strong_requirement_ground) {
 
 2328 #ifdef WITH_DETAIL_OID 
 2329       OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" mdl " << get_object()->get_oid() << 
": fact (" <<
 
 2330         to_string(ground->get_detail_oid()) << 
") pred fact imdl, from goal req " << goal->get_oid() <<
 
 2331         ", simulated pred disabled by fact (" << to_string(strong_requirement_ground->get_detail_oid()) <<
 
 2332         ") pred |fact imdl");
 
 2339 inline void PrimaryMDLController::predict_simulated_lhs(
HLPBindingMap *bm, 
bool opposite, float32 confidence, 
Sim *sim) {
 
 2341   _Fact *bound_lhs = (
_Fact *)bm->bind_pattern(get_lhs());
 
 2343     bound_lhs->set_opposite();
 
 2344   bound_lhs->set_cfd(confidence);
 
 2346   predict_simulated_evidence(bound_lhs, sim);
 
 2349 inline Fact* PrimaryMDLController::predict_simulated_evidence(_Fact *evidence, Sim *sim) {
 
 2351   Pred *pred = 
new Pred(evidence, sim, 1);
 
 2354   Fact* fact_pred = 
new Fact(pred, now, now, 1, 1);
 
 2355   inject_simulation(fact_pred, now);
 
 2359 void PrimaryMDLController::register_pred_outcome(Fact *f_pred, 
Code* mk_rdx, 
bool success, _Fact *evidence, float32 confidence, 
bool rate_failures) {
 
 2361   f_pred->invalidate();
 
 2363   if (confidence == 1) 
 
 2364     register_req_outcome(f_pred, success, rate_failures);
 
 2366   if (requirement_type_)
 
 2369   _Fact *f_evidence = evidence;
 
 2371     f_evidence = f_pred->get_pred()->get_target()->get_absentee();
 
 2373   Success *success_object = 
new Success(f_pred, f_evidence, mk_rdx, 1);
 
 2374   Code *f_success_object;
 
 2378     f_success_object = 
new Fact(success_object, now, now, confidence, 1);
 
 2380     f_success_object = 
new AntiFact(success_object, now, now, confidence, 1);
 
 2382   Group *primary_host = get_host();
 
 2383   uint16 out_group_count = get_out_group_count();
 
 2384   for (uint16 i = 0; i < out_group_count; ++i) { 
 
 2386     Group *out_group = (Group *)get_out_group(i);
 
 2387     int32 resilience = _Mem::Get()->get_goal_pred_success_res(out_group, now, seconds(0));
 
 2388     View *view = 
new View(View::SYNC_ONCE, now, 1, resilience, out_group, primary_host, f_success_object);
 
 2389     _Mem::Get()->inject(view);
 
 2393       view = 
new View(View::SYNC_ONCE, now, 1, 1, out_group, primary_host, f_evidence);
 
 2394       _Mem::Get()->inject(view);
 
 2399     OUTPUT_LINE(PRED_MON, Utils::RelativeTime(now) << 
" fact " << evidence->get_oid() << 
" -> fact " <<
 
 2400       f_success_object->get_oid() << 
" success fact " << f_pred->get_oid() << 
" pred");
 
 2402     OUTPUT_LINE(PRED_MON, Utils::RelativeTime(now) << 
" |fact " << f_success_object->get_oid() <<
 
 2403     " fact " << f_pred->get_oid() << 
" pred failure");
 
 2406 void PrimaryMDLController::register_req_outcome(Fact *f_pred, 
bool success, 
bool rate_failures) {
 
 2410   else if (rate_failures)
 
 2413   active_requirementsCS_.enter();
 
 2414   unordered_map<P<_Fact>, RequirementsPair, 
PHash<_Fact> >::const_iterator r = active_requirements_.find(f_pred);
 
 2415   if (r != active_requirements_.end()) { 
 
 2417     for (
auto c = r->second.weak_requirements_.controllers.begin(); c != r->second.weak_requirements_.controllers.end(); ++c) {
 
 2419       if (!(*c)->is_invalidated())
 
 2420         (*c)->register_req_outcome(r->second.weak_requirements_.f_imdl, success, r->second.weak_requirements_.chaining_was_allowed);
 
 2422     for (
auto c = r->second.strong_requirements_.controllers.begin(); c != r->second.strong_requirements_.controllers.end(); ++c) {
 
 2424       if (!(*c)->is_invalidated())
 
 2425         (*c)->register_req_outcome(r->second.strong_requirements_.f_imdl, !success, r->second.strong_requirements_.chaining_was_allowed);
 
 2427     active_requirements_.erase(r);
 
 2429   active_requirementsCS_.leave();
 
 2432 void PrimaryMDLController::register_goal_outcome(Fact *goal, 
bool success, _Fact *evidence)
 const {
 
 2437   _Fact *f_success_object;
 
 2441     Code *success_object = 
new Success(goal, evidence, 1);
 
 2442     f_success_object = 
new Fact(success_object, now, now, 1, 1);
 
 2446     Code *success_object;
 
 2449       absentee = goal->get_goal()->get_target()->get_absentee();
 
 2450       success_object = 
new Success(goal, absentee, 1);
 
 2451       OUTPUT_LINE(PRED_MON, Utils::RelativeTime(now) << 
" " << goal->get_oid() << 
" goal success (Primary)");
 
 2455       success_object = 
new Success(goal, evidence, 1);
 
 2456       OUTPUT_LINE(PRED_MON, Utils::RelativeTime(now) << 
" " << goal->get_oid() << 
" goal failure (Primary)");
 
 2458     f_success_object = 
new AntiFact(success_object, now, now, 1, 1);
 
 2461   Group *primary_host = get_host();
 
 2465   uint16 out_group_count = get_out_group_count();
 
 2466   for (uint16 i = 0; i < out_group_count; ++i) { 
 
 2468     Group *out_group = (Group *)get_out_group(i);
 
 2469     int32 resilience = _Mem::Get()->get_goal_pred_success_res(out_group, now, seconds(0));
 
 2470     View *view = 
new View(View::SYNC_ONCE, now, 1, resilience, out_group, primary_host, f_success_object);
 
 2471     _Mem::Get()->inject(view);
 
 2475       view = 
new View(View::SYNC_ONCE, now, 1, 1, out_group, primary_host, absentee);
 
 2476       _Mem::Get()->inject(view);
 
 2481 void PrimaryMDLController::register_simulated_goal_outcome(Fact *goal, 
bool success, _Fact *evidence)
 const {
 
 2483   Code *success_object = 
new Success(goal, evidence, 1);
 
 2488     f_success = 
new Fact(success_object, now, now, 1, 1);
 
 2490     f_success = 
new AntiFact(success_object, now, now, 1, 1);
 
 2492   Pred *pred = 
new Pred(f_success, 1);
 
 2493   Fact *f_pred = 
new Fact(pred, now, now, 1, 1);
 
 2495   Group *primary_host = get_host();
 
 2496   int32 resilience = _Mem::Get()->get_goal_pred_success_res(primary_host, now, seconds(0));
 
 2497   View *view = 
new View(View::SYNC_ONCE, now, 1, resilience, primary_host, primary_host, f_pred);
 
 2501 void PrimaryMDLController::rate_model(
bool success) {
 
 2503   Code *model = get_core_object();
 
 2506   if (is_invalidated()) {
 
 2512   float32 strength = model->code(MDL_STRENGTH).asFloat();
 
 2513   float32 evidence_count = model->code(MDL_CNT).asFloat();
 
 2514   float32 success_count = model->code(MDL_SR).asFloat()*evidence_count;
 
 2517   model->code(MDL_DSR) = model->code(MDL_SR);
 
 2519   float32 success_rate;
 
 2520   bool is_phased_out = 
false;
 
 2521   bool is_deleted = 
false;
 
 2525     success_rate = success_count / evidence_count;
 
 2526     uint32 evidence_count_base = _Mem::Get()->get_mdl_inertia_cnt_thr();
 
 2527     if (success_rate >= _Mem::Get()->get_mdl_inertia_sr_thr() && evidence_count >= evidence_count_base) { 
 
 2529       evidence_count = (uint32)(1 / success_rate);
 
 2531       float32 new_strength = 1;
 
 2532       if (new_strength != strength) {
 
 2533         strength = new_strength;
 
 2534         model->code(MDL_STRENGTH) = Atom::Float(strength);
 
 2535         OUTPUT_LINE(MDL_REV, Utils::RelativeTime(Now()) << 
" mdl " << model->get_oid() << 
" strength:" << strength);
 
 2539     model->code(MDL_CNT) = Atom::Float(evidence_count);
 
 2540     model->code(MDL_SR) = Atom::Float(success_rate);
 
 2541     get_view()->set_act(success_rate);
 
 2545     success_rate = success_count / evidence_count;
 
 2546     if (success_rate > get_host()->get_act_thr()) { 
 
 2548       model->code(MDL_CNT) = Atom::Float(evidence_count);
 
 2549       model->code(MDL_SR) = Atom::Float(success_rate);
 
 2550       get_view()->set_act(success_rate);
 
 2552     } 
else if (strength == 1) { 
 
 2555       get_view()->set_act(0);
 
 2556       secondary_->get_view()->set_act(success_rate); 
 
 2558       is_phased_out = 
true;
 
 2562       ModelBase::Get()->register_mdl_failure(model);
 
 2567   OUTPUT_LINE(MDL_REV, Utils::RelativeTime(Now()) << 
" mdl " << model->get_oid() << 
" cnt:" << evidence_count << 
" sr:" << success_rate);
 
 2571     OUTPUT_LINE(MDL_REV, Utils::RelativeTime(Now()) << 
" mdl " << model->get_oid() << 
" phased out");
 
 2573     OUTPUT_LINE(MDL_REV, Utils::RelativeTime(Now()) << 
" mdl " << model->get_oid() << 
" deleted");
 
 2576 void PrimaryMDLController::assume(_Fact *input) {
 
 2578   if (is_requirement() || is_reuse() || is_cmd())
 
 2581   Code *model = get_core_object();
 
 2582   if (model->code(MDL_STRENGTH).asFloat() == 0) 
 
 2585   if (input->get_pred()) 
 
 2589   Code *host = get_host();
 
 2590   if (confidence <= host->code(GRP_SLN_THR).asFloat()) 
 
 2594   bm->reset_bwd_timings(input);
 
 2595   bool opposite = 
false;
 
 2596   MatchResult match_result = bm->match_bwd_lenient(input, rhs_);
 
 2597   switch (match_result) {
 
 2598   case MATCH_SUCCESS_NEGATIVE:
 
 2600   case MATCH_SUCCESS_POSITIVE:
 
 2601     assume_lhs(bm, opposite, input, confidence);
 
 2608 void PrimaryMDLController::assume_lhs(HLPBindingMap *bm, 
bool opposite, _Fact *input, float32 confidence) { 
 
 2615   P<Fact> f_imdl = get_f_ihlp(bm, 
false);
 
 2617   Fact *strong_requirement_ground;
 
 2618   switch (retrieve_imdl_bwd(bm, f_imdl, ground, strong_requirement_ground)) {
 
 2619   case WEAK_REQUIREMENT_ENABLED:
 
 2620   case NO_REQUIREMENT:
 
 2628   _Fact *bound_lhs = (_Fact *)bm->bind_pattern(lhs_); 
 
 2629   bound_lhs->set_cfd(confidence);
 
 2631     bound_lhs->set_opposite();
 
 2633   assumptionsCS_.enter();
 
 2634   assumptions_.push_back(bound_lhs);
 
 2635   assumptionsCS_.leave();
 
 2638   Timestamp before = bound_lhs->get_before();
 
 2639   Group *primary_host = get_host();
 
 2640   Timestamp::duration time_to_live;
 
 2642     time_to_live = before - now;
 
 2644     time_to_live = seconds(0);
 
 2645   int32 resilience = _Mem::Get()->get_goal_pred_success_res(primary_host, now, time_to_live);
 
 2646   View *view = 
new View(View::SYNC_ONCE, now, confidence, resilience, primary_host, primary_host, bound_lhs); 
 
 2647   _Mem::Get()->inject(view);
 
 2648   OUTPUT_LINE(MDL_OUT, Utils::RelativeTime(Now()) << 
" mdl " << get_object()->get_oid() << 
" -> " << bound_lhs->get_oid() << 
" asmp");
 
 2651 void PrimaryMDLController::kill_views() {
 
 2653   reductionCS_.enter();
 
 2654   if (is_invalidated()) {
 
 2656     reductionCS_.leave();
 
 2660   remove_requirement_from_rhs();
 
 2661   secondary_->remove_requirement_from_rhs();
 
 2663   get_view()->force_res(0);
 
 2664   secondary_->get_view()->force_res(0);
 
 2665   reductionCS_.leave();
 
 2668 void PrimaryMDLController::check_last_match_time(
bool match) {
 
 2673     last_match_timeCS_.enter();
 
 2675     last_match_time_ = now;
 
 2676     last_match_timeCS_.leave();
 
 2680     if (now - last_match_time_ > _Mem::Get()->get_primary_thz())
 
 2681       get_view()->set_act(0); 
 
 2685 bool PrimaryMDLController::abduction_allowed(HLPBindingMap *bm) { 
 
 2691   if (bm->get_fwd_before() <= Now())
 
 2693   if (!HLPOverlay::ScanBWDGuards(
this, bm))
 
 2700   Code* model = get_core_object();
 
 2701   auto template_set_index = model->code(MDL_TPL_ARGS).asIndex();
 
 2702   auto template_set_count = model->code(template_set_index).getAtomCount();
 
 2703   auto template_ti_index = template_set_index + template_set_count;
 
 2704   if (!(template_set_count >= 1 && model->code(template_ti_index).getDescriptor() == Atom::I_PTR &&
 
 2705         model->code(model->code(template_ti_index).asIndex()).asOpcode() == Opcodes::TI))
 
 2707   auto ti_index = model->code(template_ti_index).asIndex();
 
 2708   auto after_code_index = ti_index + 1;
 
 2709   auto before_code_index = ti_index + 2;
 
 2710   if (model->code(after_code_index).getDescriptor() != Atom::VL_PTR ||
 
 2711       model->code(before_code_index).getDescriptor() != Atom::VL_PTR)
 
 2714   auto after_map_index = model->code(after_code_index).asIndex();
 
 2715   auto before_map_index = model->code(before_code_index).asIndex();
 
 2717   if (bm->get_code(after_map_index) != NULL && bm->get_code(before_map_index) != NULL) {
 
 2723     after = Utils::GetTimestamp(bm->get_code(after_map_index));
 
 2724     before = Utils::GetTimestamp(bm->get_code(before_map_index));
 
 2733   if (!bm_copy->is_timestamp(after_map_index) || !bm_copy->is_timestamp(before_map_index))
 
 2736   after = Utils::GetTimestamp(bm_copy->get_code(after_map_index));
 
 2737   before = Utils::GetTimestamp(bm_copy->get_code(before_map_index));
 
 2746 void SecondaryMDLController::set_primary(PrimaryMDLController *primary) {
 
 2751 void SecondaryMDLController::take_input(
r_exec::View *input) {
 
 2753   if (become_invalidated())
 
 2756   if (input->object_->code(0).asOpcode() == Opcodes::Fact ||
 
 2757     input->object_->code(0).asOpcode() == Opcodes::AntiFact) 
 
 2758     Controller::__take_input<SecondaryMDLController>(input);
 
 2761 void SecondaryMDLController::reduce(
r_exec::View *input) { 
 
 2766   if (input->object_->is_invalidated())
 
 2769   SecondaryMDLOverlay o(
this, bindings_);
 
 2770   bool match = o.reduce((_Fact *)input->object_, NULL, NULL); 
 
 2773     monitor_predictions((_Fact *)input->object_);
 
 2775   check_last_match_time(match);
 
 2778 void SecondaryMDLController::reduce_batch(Fact *f_p_f_imdl, MDLController *controller) {
 
 2783   reduce_cache<EvidenceEntry>(&evidences_, f_p_f_imdl, controller);
 
 2784   reduce_cache<PredictedEvidenceEntry>(&predicted_evidences_, f_p_f_imdl, controller);
 
 2787 void SecondaryMDLController::predict(HLPBindingMap *bm, _Fact *input, Fact *f_imdl, 
bool chaining_was_allowed, RequirementsPair &r_p, Fact *ground,
 
 2788   MkRdx* ground_mk_rdx, vector<
P<_Fact> >& already_predicted) { 
 
 2790   _Fact *bound_rhs = (_Fact *)bm->bind_pattern(rhs_); 
 
 2791   Pred *_prediction = 
new Pred(bound_rhs, 1);
 
 2793   Fact *production = 
new Fact(_prediction, now, now, 1, 1);
 
 2795   register_requirement(production, r_p);
 
 2797   if (is_requirement()) { 
 
 2799     if (is_invalidated())
 
 2802     ((MDLController *)controllers_[RHSController])->store_requirement(production, NULL, 
this, chaining_was_allowed);
 
 2806   PMonitor *m = 
new PMonitor(
this, bm, production, NULL, 
false); 
 
 2813   if (((
_Fact*)f_p_f_imdl->get_reference(0)->get_reference(0))->is_fact()) {
 
 2815     _store_requirement(&requirements_.positive_evidences_, e);
 
 2816     reduce_cache<SecondaryMDLController>((
Fact *)f_p_f_imdl, controller);
 
 2818     _store_requirement(&requirements_.negative_evidences_, e);
 
 2821 void SecondaryMDLController::rate_model() { 
 
 2823   Code *model = get_core_object();
 
 2826   if (is_invalidated()) {
 
 2832   float32 evidence_count = model->code(MDL_CNT).asFloat();
 
 2833   float32 success_count = model->code(MDL_SR).asFloat()*evidence_count;
 
 2836   model->code(MDL_DSR) = model->code(MDL_SR);
 
 2837   model->code(MDL_CNT) = Atom::Float(evidence_count);
 
 2840   float32 success_rate = success_count / evidence_count; 
 
 2841   model->code(MDL_SR) = Atom::Float(success_rate);
 
 2843   bool is_phased_in = 
false;
 
 2844   if (success_rate > primary_->get_view()->get_host()->get_act_thr()) {
 
 2846     get_view()->set_act(0);
 
 2847     primary_->get_view()->set_act(success_rate); 
 
 2849     is_phased_in = 
true;
 
 2852     if (success_rate > get_view()->get_host()->get_act_thr()) 
 
 2853       get_view()->set_act(success_rate);
 
 2856   OUTPUT_LINE(MDL_REV, Utils::RelativeTime(Now()) << 
" mdl " << model->get_oid() << 
" cnt:" << evidence_count << 
" sr:" << success_rate);
 
 2860     OUTPUT_LINE(MDL_REV, Utils::RelativeTime(Now()) << 
" mdl " << model->get_oid() << 
" phased in");
 
 2863 void SecondaryMDLController::register_pred_outcome(Fact *f_pred, 
Code* mk_rdx, 
bool success, _Fact *evidence, float32 confidence, 
bool rate_failures) { 
 
 2865   register_req_outcome(f_pred, success, rate_failures);
 
 2868 void SecondaryMDLController::register_req_outcome(Fact *f_imdl, 
bool success, 
bool rate_failures) {
 
 2874     active_requirementsCS_.enter();
 
 2875     unordered_map<P<_Fact>, RequirementsPair, 
PHash<_Fact> >::const_iterator r = active_requirements_.find(f_imdl);
 
 2876     if (r != active_requirements_.end()) { 
 
 2878       for (
auto c = r->second.weak_requirements_.controllers.begin(); c != r->second.weak_requirements_.controllers.end(); ++c) {
 
 2880         if (!(*c)->is_invalidated())
 
 2881           (*c)->register_req_outcome(
 
 2882             r->second.weak_requirements_.f_imdl, success, r->second.weak_requirements_.chaining_was_allowed);
 
 2884       active_requirements_.erase(r);
 
 2886     active_requirementsCS_.leave();
 
 2890 void SecondaryMDLController::kill_views() {
 
 2892   remove_requirement_from_rhs();
 
 2893   primary_->remove_requirement_from_rhs();
 
 2895   get_view()->force_res(0);
 
 2896   primary_->get_view()->force_res(0);
 
 2899 void SecondaryMDLController::check_last_match_time(
bool match) {
 
 2904     last_match_timeCS_.enter();
 
 2906     last_match_time_ = now;
 
 2907     last_match_timeCS_.leave();
 
 2911     if (now - last_match_time_ > _Mem::Get()->get_secondary_thz()) {
 
 2913       ModelBase::Get()->register_mdl_timeout(get_core_object());