Skip to content
Snippets Groups Projects
Select Git revision
  • e00aa24d36e9085787a840462d396bd02fda07c1
  • master default protected
  • loss
  • producer
4 results

Strategy.cpp

Blame
  • Strategy.cpp 3.45 KiB
    #include "Strategy.hpp"
    #include "Config.hpp"
    
    Strategy::Strategy(std::vector<std::string> definition)
    {
      std::regex blockRegex("(?:(?:\\s|\\t)*)Block(?:(?:\\s|\\t)*):(?:(?:\\s|\\t)*)End\\{(.*)\\}(?:(?:\\s|\\t)*)");
    
      for (auto & line : definition)
        if (!util::doIfNameMatch(blockRegex, line, [this](auto sm)
          {
            blocks.emplace_back(util::split(sm.str(1), ' '));
          }))
        {
          if (blocks.empty())
            util::myThrow(fmt::format("invalid line '{}', expeced 'Block : End{}'",line,"{...}"));
          blocks.back().addMovement(line);
        }
    
      if (blocks.empty())
        util::myThrow("empty strategy");
      for (auto & block : blocks)
        if (block.empty())
          util::myThrow("there is an empty block");
    }
    
    Strategy::Movement Strategy::getMovement(const Config & c, const std::string & transition)
    {
      auto movement = blocks[currentBlock].getMovement(c, transition);
    
      if (blocks[currentBlock].isFinished(c, movement))
      {
        currentBlock++;
        if (currentBlock >= blocks.size())
          return endMovement;
        movement.first = blocks[currentBlock].getInitialState();
        movement.second = -c.getWordIndex();
      }
    
      return movement;
    }
    
    Strategy::Movement Strategy::Block::getMovement(const Config & c, const std::string & transition)
    {
      std::string transitionPrefix(util::split(transition, ' ')[0]);
      auto currentState = c.getState();
    
      for (auto & movement : movements)
      {
        auto fromState = std::get<0>(movement);
        auto toState = std::get<1>(movement);
        auto trans = std::get<2>(movement);
        auto mov = std::get<3>(movement);
    
        if (fromState == currentState and (trans == transitionPrefix or trans == "*"))
          return std::make_pair(toState, mov);
      }
    
      util::myThrow(fmt::format("no movement found for state '{}' and transitionPrefix '{}'", currentState, transitionPrefix));
      return endMovement;
    }
    
    const std::string Strategy::getInitialState() const
    {
      return blocks.at(0).getInitialState();
    }
    
    void Strategy::reset()
    {
      currentBlock = 0;
    }