machine.cc 6.23 KB
Newer Older
TOUZE Francois's avatar
TOUZE Francois committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
#include "machine.h"
#include "dataManager.h"
#include "mixedTools.h"
#include "INIReader.h"

machine::machine(dataManager* dmg) : 
  expMachine_(NULL),
  machineName_("dummy") 
{
  INIReader reader("../../pspa_config.ini");
  debug = reader.GetInteger("Debugging", "debug_mode", -1);

 dataManager_ = dmg;
}

machine::~machine()
{
  if ( expMachine_ ) delete expMachine_;
}

void machine::addSector(sector* sor)
{
  containedSectors_.push_back( sor );
}

void machine::addSector(string s)
{
  sector* sor= dataManager_->getSectorPointerByLabel( s );
  addSector( sor );
}

void machine::eraseSector(sector* sor) 
{
  std::vector<sector*>::iterator it;
  it= find(containedSectors_.begin(),containedSectors_.end(),sor);
  if( it != containedSectors_.end() ) {
    containedSectors_.erase( it );
    if ( expMachine_ ) {
      delete expMachine_;
      expMachine_= NULL;
    }
  }
}

void machine::eraseSector(string s)
{
  sector* sor= dataManager_->getSectorPointerByLabel( s );
  eraseSector( sor );
}

void machine::clearSectors()
{
  if( !containedSectors_.empty() ) containedSectors_.clear();
}

void machine::expandMachine() 
{ 
  if(debug > 0) cout << "machine::expandMachine()\n";

  if ( expMachine_ ) {
    delete expMachine_;
    expMachine_ = NULL;
  }

  expMachine_= new expandedMachine( dataManager_ );
  //expMachine_->fromSectors( cointainedSectors_ );
  expMachine_->update(containedSectors_,computingBlocks_);
}

void machine::FileAMLOutput(UAPNode *root) 
{
  if(debug > 0) cout << "machine::FileAMLOutput()\n";

  cout << "machine::FileAMLOutput()\n";

  UAPNode *node= root->addChild("machine");
  node->addAttribute("name",machineName_);
  UAPNode *nd1= node->addChild("machineSectors");
  for (unsigned k = 0; k < containedSectors_.size(); k++)
    {
      string str= containedSectors_.at(k)->getLabel();
      nd1->addChild("msector")->addAttribute("name",str);
    }
  
  if ( expMachine_ ) expMachine_->FileAMLOutput( node );
}

bool machine::FileAMLInput(UAPNode *nde) 
{
  if(debug > 0) cout << "machine::FileAMLInput()\n";

  if ( !nde ) return false;
  containedSectors_.clear();
  UAPNode* listSect= nde->getChildByName("machineSectors");
  if ( !listSect ) {
    if(debug > 0) 
      cout << "machine::FileAMLInput : no sector in the machine\n";
  } else {
    NodeVec lesSecteurs= listSect->getSubNodesByName("msector");
    for (unsigned k = 0; k < lesSecteurs.size(); k++) 
      {
	sector* sect= dataManager_->getSectorPointerByLabel(lesSecteurs.at(k)->getAttribute("name")->getValue()); 
	if ( sect ) containedSectors_.push_back(sect);
      }
  }

  if(debug > 0) cout << "machine:: " << containedSectors_.size() << " sector(s)\n";

  computingBlocks_.clear();
  UAPNode *nd1= nde->getChildByName(string("expandedMachine"));  
  //cout << nd1->toStringTree() << endl;

  if ( nd1 ) {
    expandMachine();
    if (!expMachine_->FileAMLInput( nd1 )) return false;
  } else {
    expMachine_= NULL;
    return false;
  }
  
  vector<computingBlock*>& blks= expMachine_->getComputingBlocks();
  for (unsigned k = 0; k < blks.size(); k++) 
    {
      computingBlock* ptr= blks.at(k);
      if ( ptr ) computingBlocks_.push_back(ptr);
    }
  
  return true;
}

expandedMachine* machine::getExpandedMachine() const
{ 
  return expMachine_;
}

void machine::toJSON(json& j)
{
  if(debug > 0) std::cout << "machine::toJSON()\n";
  
  if ( !expMachine_ ) {
    cout << "machine::toJSON:: no expandMachine\n";
    return;
  }

  expMachine_->toJSON( j );
}

bool machine::fromJSON(json j)
{
150 151
  if(debug > 0) std::cout << "machine::fromJSON( 1 )\n";
   bool status= true;
TOUZE Francois's avatar
TOUZE Francois committed
152 153 154 155 156 157 158 159 160 161 162
  
  containedSectors_.clear();
  auto mac= j.find( "machine" );
  for (auto kt = mac->begin(); kt != mac->end(); ++kt)
    {
      auto key= kt->find( "displayName" ).value();
      sector *sor= dataManager_->getSectorPointerByLabel( key );
      if ( sor ) containedSectors_.push_back( sor );
    }

  if(debug > 0)
163
    cout << "=> machine has " << containedSectors_.size() << " sector(s)\n";
TOUZE Francois's avatar
TOUZE Francois committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

  computingBlocks_.clear();
  auto exp= j.find( "simulations" );
  if( exp == j.end() ) {
    expMachine_= NULL;
    status= false;
    cout << "machine::fromJSON: no expandMachine\n";
  } else {
    expandMachine();
    if (!expMachine_->fromJSON( *exp )) {
      status= false;
    } else {
      vector<computingBlock*>& blks= expMachine_->getComputingBlocks();
      for (unsigned k = 0; k < blks.size(); k++) 
	{
	  computingBlock *ptr= blks.at(k);
	  if ( ptr ) computingBlocks_.push_back( ptr );
	}
    }
  }

  return status;
}

bool machine::fromJSON(map< string,vector<json> > cmap,json exp)
{
190
  if(debug > 0) std::cout << "machine::fromJSON( 2 )\n";
TOUZE Francois's avatar
TOUZE Francois committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  bool status= true;
  
  containedSectors_.clear();
  for(int i = 0; i < cmap.size(); ++i)
    {
      string key= "KEY" + mixedTools::intToString( i );
      map< string,vector<json> >::iterator it= cmap.find( key );
      json jk= it->second.at( 0 );
      string category= jk.find( "category" ).value();
      if ( category == "sector" ) {
	string str= jk.find( "displayName" ).value();
	key= str;
      }
      sector *sor= dataManager_->getSectorPointerByLabel( key );
      if ( sor ) containedSectors_.push_back( sor );
    }

208 209
  if (debug > 0)
    cout << "machine has " << containedSectors_.size() << " sector(s)\n";
TOUZE Francois's avatar
TOUZE Francois committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  
  computingBlocks_.clear();
  if( exp.is_null() ) {
    expMachine_= NULL;
    status= false;
    cout << "machine::fromJSON: no expandMachine\n";
  } else {
    expandMachine();
    if (!expMachine_->fromJSON( exp )) {
      status= false;
    } else {
      vector<computingBlock*>& blks= expMachine_->getComputingBlocks();
      for (unsigned k = 0; k < blks.size(); k++) 
	{
	  computingBlock *ptr= blks.at(k);
	  if ( ptr ) computingBlocks_.push_back( ptr );
	}
    }
  }

  return status;
}

/**
void machine::fromSimulate(json j)
{
    cout << "machine::fromSimulate has " << containedSectors_.size() << " sector(s)\n";

  computingBlocks_.clear();
  auto exp= j.find( "simulations" );
  if( exp == j.end() ) {
    expMachine_= NULL;
    cout << "machine::fromSimulate: no expandMachine\n";
  } else {
    expandMachine();
    expMachine_->fromJSON( *exp );
    vector<computingBlock*>& blks= expMachine_->getComputingBlocks();
    for (unsigned k = 0; k < blks.size(); k++) 
      {
	computingBlock *ptr= blks.at(k);
	if ( ptr ) computingBlocks_.push_back( ptr );
      }
  }
}
*/