bool StandardInterventionDistributionEventCoordinator::visitIndividualCallback( IIndividualHumanEventContext *ihec, float & incrementalCostOut, ICampaignCostObserver * pICCO ) { { // Add real checks on demographics based on intervention demographic targetting. // Return immediately if we hit a non-distribute condition if( qualifiesDemographically( ihec ) == false ) { LOG_DEBUG("Individual not given intervention because not in target demographic\n"); return false; } LOG_DEBUG("Individual meets demographic targeting criteria\n"); if (!TargetedIndividualIsCovered(ihec)) { incrementalCostOut = 0; return false; } else { incrementalCostOut = 0; // instantiate and distribute intervention LOG_DEBUG_F( "Attempting to instantiate intervention of class %s\n", std::string(json::QuickInterpreter(intervention_config._json)["class"].As<json::String>()).c_str() ); IDistributableIntervention *di = _di->Clone(); release_assert(di); if (di) { di->AddRef(); di->Distribute( ihec->GetInterventionsContext(), pICCO ); di->Release(); // a bit wasteful for now, could cache it for the next fellow LOG_DEBUG_F("Distributed an intervention %p to individual %d at a cost of %f\n", di, ihec->GetSuid().data, incrementalCostOut); } } } return true; }
bool MultiInterventionEventCoordinator::visitIndividualCallback( IIndividualHumanEventContext *ihec, float & incrementalCostOut, ICampaignCostObserver * pICCO ) { // Less of this would need to be copied from the base class with a more thoughtful encapsulation of functions // In particular, only the give-intervention(s)-to-individual stuff inside the try statement is different. if( !demographic_restrictions.HasDefaultRestrictions() ) // don't waste any more time with checks if we're giving to everyone { if( qualifiesDemographically( ihec ) == false ) { LOG_DEBUG("Individual not given intervention because not in target demographic\n"); return false; } } LOG_DEBUG("Individual meets demographic targeting criteria\n"); if (!TargetedIndividualIsCovered(ihec)) { incrementalCostOut = 0; return false; } else { incrementalCostOut = 0; try { const json::Array & interventions_array = json::QuickInterpreter( intervention_config._json ).As<json::Array>(); LOG_DEBUG_F("interventions array size = %d\n", interventions_array.Size()); for( int idx=0; idx<interventions_array.Size(); idx++ ) { const json::Object& actualIntervention = json_cast<const json::Object&>(interventions_array[idx]); Configuration * tmpConfig = Configuration::CopyFromElement(actualIntervention); assert( tmpConfig ); // instantiate and distribute intervention LOG_DEBUG_F( "Attempting to instantiate intervention of class %s\n", std::string((*tmpConfig)["class"].As<json::String>()).c_str() ); IDistributableIntervention *di = InterventionFactory::getInstance()->CreateIntervention(tmpConfig); assert(di); delete tmpConfig; tmpConfig = nullptr; if (di) { if (!di->Distribute( ihec->GetInterventionsContext(), pICCO ) ) { di->Release(); // a bit wasteful for now, could cache it for the next fellow } LOG_DEBUG_F("Distributed an intervention %p to individual %d at a cost of %f\n", di, ihec->GetSuid().data, incrementalCostOut); } } } catch(json::Exception &e) { // ERROR: ::cerr << "exception casting intervention_config to array! " << e.what() << std::endl; throw GeneralConfigurationException( __FILE__, __LINE__, __FUNCTION__, e.what() ); // ( "InterventionConfigs json problem: intervention_config is valid json but needs to be an array." ); } } return true; }