#include "pla.h"
#include "hub.h"
using namespace std;
using namespace hub;
//---------------------------------------------------------------------------
std::string PlaDescr::getDescrName()
{
  std::string text = "PLA ";
  if( f_ ) text += "f";
  if( d_ ) text += "d";
  if( r_ ) text += "r";

  return text;
}
//---------------------------------------------------------------------------
void PlaDescr::type(std::string type)
{
  for( unsigned i = 0; i < type.length(); i++ ) {
    if( type[i] == 'f' ) f_ = true;
    if( type[i] == 'd' ) d_ = true;
    if( type[i] == 'r' ) r_ = true;
  }
}
//---------------------------------------------------------------------------
std::string PlaDescr::type(void) const
{
  std::string text;
  if( f_ ) text += "f";
  if( d_ ) text += "d";
  if( r_ ) text += "r";

  return text;
}
//---------------------------------------------------------------------------
void PlaDescr::setMatrices(std::list<std::string>& m)
{
  std::list<std::string>::iterator it = m.begin();
  std::string iv;
  std::string ov;
  int in;
  int on;
  int j;
  char c;
  for( unsigned i = 0; i < m.size(); i++ ) {
    in = 0;
    on = 0;
    j = 0;
    iv = "";
    while( in != inputNo_ ) {
      c = (*it)[j];
      if( c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '-' || c == '~' ) {
        iv += c;
        in++;
      }
      j++;
    }
    inputMatrix.push_back(iv);

    ov = "";
    while( on != outputNo_ ) {
      c = (*it)[j];
      if( c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '-' || c == '~' ) {
        ov += c;
        on++;
      }
      j++;
    }
    outputMatrix.push_back(ov);

    it++;
  }
}

//---------------------------------------------------------------------------
bool PlaAgent :: Eq (const BehavDescr* other) const
{
  const PlaDescr* that = dynamic_cast <const PlaDescr*> (other);
  if (!that) return false;
  if( (that->inputNo() != repr_.inputNo()) || (that->outputNo() != repr_.outputNo()) ) return false;

  //matrices comparison
  bool found;
  std::list<std::string> inputMatrix;
  std::list<std::string> outputMatrix;
  PlaDescr::MatrixIter i1 = that->inpBeg();
  PlaDescr::MatrixIter i2 = repr_.inpBeg();
  PlaDescr::MatrixIter o1 = that->outpBeg();
  PlaDescr::MatrixIter o2 = repr_.outpBeg();

  for(; i2 != repr_.inpEnd(); i2++, o2++ ) {
    inputMatrix.push_back(*i2);
    outputMatrix.push_back(*o2);
  }

  for(; i1 != that->inpEnd(); i1++, o1++ ) {
    std::list<std::string>::iterator i2 = inputMatrix.begin();
    std::list<std::string>::iterator o2 = outputMatrix.begin();
    found = false;
    for(; i2 != inputMatrix.end(); i2++, o2++ ) {
      if( ((*i1) == (*i2)) && ((*o1) == (*o2)) ) {
        //remove string from lists
        inputMatrix.erase(i2);
        outputMatrix.erase(o2);
        found = true;
        break;
      }
    }
    if( !found ) return false;
     
  }
  if( inputMatrix.empty() ) return true;
  else return true;
}
//---------------------------------------------------------------------------
BehavDescr* PlaAgent :: CreateDescr(void) const
{
  return new PlaDescr(repr_);
}

