<commit_before>// SourceFacility.cpp
// Implements the SourceFacility class
#include "SourceFacility.h"

#include "QueryEngine.h"
#include "Logger.h"
#include "RecipeLibrary.h"
#include "GenericResource.h"
#include "CycException.h"
#include "CycLimits.h"
#include "MarketModel.h"

#include <sstream>
#include <limits>

#include <boost/lexical_cast.hpp>

using namespace std;
using boost::lexical_cast;

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
SourceFacility::SourceFacility() : 
  out_commod_(""), 
  recipe_name_(""), 
  commod_price_(0), 
  capacity_(numeric_limits<double>::max()) {
  ordersWaiting_ = deque<msg_ptr>();
  inventory_ = MatBuff();
  setMaxInventorySize(numeric_limits<double>::max());
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
SourceFacility::~SourceFacility() {}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
void SourceFacility::initModuleMembers(QueryEngine* qe) {
  QueryEngine* output = qe->queryElement("output");

  setRecipe(output->getElementContent("recipe"));

  string data = output->getElementContent("outcommodity");
  setCommodity(data);
  Commodity commod(data);
  CommodityProducer::addCommodity(commod);

  data = output->getElementContent("output_capacity"); 
  CommodityProducer::setCapacity(commod,lexical_cast<double>(data));
  setCapacity(lexical_cast<double>(data));  

  try
    {
      data = output->getElementContent("inventorysize"); 
      setMaxInventorySize(lexical_cast<double>(data));
    } 
  catch (CycNullQueryException e) 
    {
      setMaxInventorySize(numeric_limits<double>::max());
    }
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
std::string SourceFacility::str() {
  std::stringstream ss;
  ss << FacilityModel::str()
     << " supplies commodity '"
     << out_commod_ << "' with recipe '" 
     << recipe_name_ << "' at a capacity of "
     << capacity_ << " kg per time step "
     << " with max inventory of " << inventory_.capacity() << " kg.";
  return "" + ss.str();
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
void SourceFacility::cloneModuleMembersFrom(FacilityModel* sourceModel) {
  SourceFacility* source = dynamic_cast<SourceFacility*>(sourceModel);
  setCommodity(source->commodity());
  setCapacity(source->capacity());
  setRecipe(source->recipe());
  setMaxInventorySize(source->maxInventorySize());
  copyProducedCommoditiesFrom(source);
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
void SourceFacility::handleTick(int time){
  LOG(LEV_INFO3, "SrcFac") << facName() << " is ticking {";

  generateMaterial();
  Transaction trans = buildTransaction();

  LOG(LEV_INFO4, "SrcFac") << "offers "<< trans.resource()->quantity() << " kg of "
                           << out_commod_ << ".";

  sendOffer(trans);

  LOG(LEV_INFO3, "SrcFac") << "}";
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
void SourceFacility::handleTock(int time){
  LOG(LEV_INFO3, "SrcFac") << facName() << " is tocking {";

  // check what orders are waiting,
  // send material if you have it now
  while (!ordersWaiting_.empty()) {
    Transaction order = ordersWaiting_.front()->trans();
    if (order.resource()->quantity() - inventory_.quantity() > cyclus::eps()) {
      LOG(LEV_INFO3, "SrcFac") << "Not enough inventory. Waitlisting remaining orders.";
      break;
    } else {
      order.approveTransfer();
      ordersWaiting_.pop_front();
    }
  }
  // For now, lets just print out what we have at each timestep.
  LOG(LEV_INFO4, "SrcFac") << "SourceFacility " << this->ID()
                  << " is holding " << this->inventory_.quantity()
                  << " units of material at the close of month " << time
                  << ".";

  LOG(LEV_INFO3, "SrcFac") << "}";
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
void SourceFacility::receiveMessage(msg_ptr msg) {
  // is this a message from on high? 
  if(msg->trans().supplier() == this) {
    // file the order
    ordersWaiting_.push_front(msg);
    LOG(LEV_INFO5, "SrcFac") << name() << " just received an order.";
  } else {
    throw CycException("SourceFacility is not the supplier of this msg.");
  }
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
vector<rsrc_ptr> SourceFacility::removeResource(Transaction order) {
  return MatBuff::toRes(inventory_.popQty(order.resource()->quantity()));
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
void SourceFacility::setCommodity(std::string name) {
  out_commod_ = name;
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
std::string SourceFacility::commodity() {
  return out_commod_;
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
void SourceFacility::setCapacity(double capacity) {
  capacity_ = capacity;
}
<commit_msg>added more log output from the source facility<commit_after>// SourceFacility.cpp
// Implements the SourceFacility class
#include "SourceFacility.h"

#include "QueryEngine.h"
#include "Logger.h"
#include "RecipeLibrary.h"
#include "GenericResource.h"
#include "CycException.h"
#include "CycLimits.h"
#include "MarketModel.h"

#include <sstream>
#include <limits>

#include <boost/lexical_cast.hpp>

using namespace std;
using boost::lexical_cast;

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
SourceFacility::SourceFacility() : 
  out_commod_(""), 
  recipe_name_(""), 
  commod_price_(0), 
  capacity_(numeric_limits<double>::max()) {
  ordersWaiting_ = deque<msg_ptr>();
  inventory_ = MatBuff();
  setMaxInventorySize(numeric_limits<double>::max());
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
SourceFacility::~SourceFacility() {}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
void SourceFacility::initModuleMembers(QueryEngine* qe) {
  QueryEngine* output = qe->queryElement("output");

  setRecipe(output->getElementContent("recipe"));

  string data = output->getElementContent("outcommodity");
  setCommodity(data);
  Commodity commod(data);
  CommodityProducer::addCommodity(commod);

  data = output->getElementContent("output_capacity"); 
  CommodityProducer::setCapacity(commod,lexical_cast<double>(data));
  setCapacity(lexical_cast<double>(data));  

  try
    {
      data = output->getElementContent("inventorysize"); 
      setMaxInventorySize(lexical_cast<double>(data));
    } 
  catch (CycNullQueryException e) 
    {
      setMaxInventorySize(numeric_limits<double>::max());
    }
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
std::string SourceFacility::str() {
  std::stringstream ss;
  ss << FacilityModel::str()
     << " supplies commodity '"
     << out_commod_ << "' with recipe '" 
     << recipe_name_ << "' at a capacity of "
     << capacity_ << " kg per time step "
     << " with max inventory of " << inventory_.capacity() << " kg.";
  return "" + ss.str();
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
void SourceFacility::cloneModuleMembersFrom(FacilityModel* sourceModel) {
  SourceFacility* source = dynamic_cast<SourceFacility*>(sourceModel);
  setCommodity(source->commodity());
  setCapacity(source->capacity());
  setRecipe(source->recipe());
  setMaxInventorySize(source->maxInventorySize());
  copyProducedCommoditiesFrom(source);
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
void SourceFacility::handleTick(int time){
  LOG(LEV_INFO3, "SrcFac") << facName() << " is ticking {";

  generateMaterial();
  Transaction trans = buildTransaction();

  LOG(LEV_INFO4, "SrcFac") << "offers "<< trans.resource()->quantity() << " kg of "
                           << out_commod_ << ".";

  sendOffer(trans);

  LOG(LEV_INFO3, "SrcFac") << "}";
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
void SourceFacility::handleTock(int time){
  LOG(LEV_INFO3, "SrcFac") << facName() << " is tocking {";

  // check what orders are waiting,
  // send material if you have it now
  while (!ordersWaiting_.empty()) {
    Transaction order = ordersWaiting_.front()->trans();
    LOG(LEV_INFO3, "SrcFac") << "Order is for: " << order.resource()->quantity();
    LOG(LEV_INFO3, "SrcFac") << "Inventory is: " << inventory_.quantity();
    if (order.resource()->quantity() - inventory_.quantity() > cyclus::eps()) {
      LOG(LEV_INFO3, "SrcFac") << "Not enough inventory. Waitlisting remaining orders.";
      break;
    } else {
      LOG(LEV_INFO3, "SrcFac") << "Satisfying order.";
      order.approveTransfer();
      ordersWaiting_.pop_front();
    }
  }
  // For now, lets just print out what we have at each timestep.
  LOG(LEV_INFO4, "SrcFac") << "SourceFacility " << this->ID()
                  << " is holding " << this->inventory_.quantity()
                  << " units of material at the close of month " << time
                  << ".";

  LOG(LEV_INFO3, "SrcFac") << "}";
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
void SourceFacility::receiveMessage(msg_ptr msg) {
  // is this a message from on high? 
  if(msg->trans().supplier() == this) {
    // file the order
    ordersWaiting_.push_front(msg);
    LOG(LEV_INFO5, "SrcFac") << name() << " just received an order.";
    LOG(LEV_INFO5, "SrcFac") << "for " << msg->trans().resource()->quantity() 
                             << " of " << msg->trans().commod();
  } else {
    throw CycException("SourceFacility is not the supplier of this msg.");
  }
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
vector<rsrc_ptr> SourceFacility::removeResource(Transaction order) {
  return MatBuff::toRes(inventory_.popQty(order.resource()->quantity()));
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
void SourceFacility::setCommodity(std::string name) {
  out_commod_ = name;
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
std::string SourceFacility::commodity() {
  return out_commod_;
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
void SourceFacility::setCapacity(double capacity) {
  capacity_ = capacity;
}

{'fraction_non_alphanumeric': 0.21523920880834063, 'fraction_numerical': 0.0021436227224008574, 'mean_word_length': 2.073053892215569, 'pattern_counts': {'":': 0, '<': 173, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 97, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5624236', 'n_tokens_mistral': 3657, 'n_tokens_neox': 3574, 'n_words': 538}