Skip to content
Snippets Groups Projects
Transition.cpp 1.44 KiB
Newer Older
#include "Transition.hpp"
#include <regex>
Transition::Transition(const std::string & name)
{
  this->name = name;

  std::regex writeRegex("WRITE ([bs])\\.(.+) (.+) (.+)");

  try
  {

  if (util::doIfNameMatch(writeRegex, name, [this](auto sm){initWrite(sm[3], sm[1], sm[2], sm[4]);}))
  throw std::invalid_argument("no match");

  } catch (std::exception & e) {util::myThrow(fmt::format("Invalid name '{}' ({})", name, e.what()));}

}

void Transition::apply(Config & config)
{
  for (Action & action : sequence)
    action.apply(config, action);
}

bool Transition::appliable(const Config & config) const
{
  for (const Action & action : sequence)
    if (!action.appliable(config, action))
      return false;

  return true;
}

int Transition::getCost(const Config & config) const
{
  return cost(config);
}

void Transition::initWrite(std::string colName, std::string object, std::string index, std::string value)
{
  auto objectValue = Action::str2object(object);
  int indexValue = std::stoi(index);

  sequence.emplace_back(Action::addHypothesisRelative(colName, objectValue, indexValue, value));

  cost = [colName, objectValue, indexValue, value](const Config & config)
  {
    int lineIndex = 0;
    if (objectValue == Action::Object::Buffer)
      lineIndex = config.getWordIndex() + indexValue;
    else
      lineIndex = config.getStack(indexValue);
    if (config.getConst(colName, lineIndex, 0) == value)
      return 0;