dataManager.cpp 19.9 KB
Newer Older
TOUZE Francois's avatar
TOUZE Francois committed
1 2 3 4 5 6 7 8 9 10 11 12

#include <iostream>

#include "dataManager.h"
#include "mixedTools.h"
#include "abstractSoftware.h"
#include "computingBlock.h"

#include "UAP/UAPUtilities.hpp"
#include "AML/AMLReader.hpp"
#include "Translate/AMLParser.hpp"
#include "Translate/MADXParser.hpp"
13
#include "BETAParser.h"
TOUZE Francois's avatar
TOUZE Francois committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

#include "INIReader.h"
namespace BU = BasicUtilities;

dataManager::dataManager() : 
  workingDir_(),
  userDir_(),
  nameOfCase_(),
  machineCourante_(NULL),
  sectorCounter_(0), 
  runIndex_(0)
{
  // FOR DEBBUGING
  INIReader reader("../../pspa_config.ini");
  debug= reader.GetInteger("Debugging", "debug_mode", -1);
  if(debug > 0) cout << "1-dataManager::dataManager()\n";
}

dataManager::dataManager(string userDir,string wrkDir,string s) :
33 34
  workingDir_(wrkDir),
  userDir_(userDir),
TOUZE Francois's avatar
TOUZE Francois committed
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  nameOfCase_(s),
  machineCourante_(NULL),
  sectorCounter_(0),
  runIndex_(0)
{
  // FOR DEBBUGING
  INIReader reader("../../pspa_config.ini");
  debug = reader.GetInteger("Debugging", "debug_mode", -1);
  if(debug > 0) cout << "2-dataManager::dataManager()\n";

  machineCourante_= new machine(this);
}

dataManager::~dataManager()
{
  if(debug > 0) cout << "dataManager::~dataManager()\n";
  
  for (unsigned k=0; k < sectors_.size();k++) 
    {
      if ( sectors_[k] != NULL ) delete sectors_[k];
    }
  if ( machineCourante_ ) delete machineCourante_;
}

bool dataManager::areDataCoherent() 
{
  if(debug > 0) cout << "dataManager::areDataCoherent()\n";
  
  if ( !machineCourante_->getExpandedMachine() ) return false;
  bool resultat = false;
  resultat = machineCourante_->getExpandedMachine()->areDataCoherent();
  return resultat;
}

void dataManager::initializeExecution()
{
  if(debug > 0)
    cout << "dataManager::initializeExecution: nameOfCase = " << getnameOfCase() << endl;
  
  runIndex_= 0; // run number
  diagnosticBeam_.clear();
  indexElementToIndexDiag_.clear();
}

bool dataManager::executeAll()
{
  if(debug > 0) cout << "dataManager::executeAll()\n";
  
  bool success = true;
  expandedMachine *exma= machineCourante_->getExpandedMachine();
  vector<computingBlock*>& blks= exma->getComputingBlocks();
  particleBeam *computedBeam= NULL;
  for(unsigned int k = 0; k < blks.size(); ++k) 
    {
      computingBlock *ptr= blks.at(k);
      abstractSoftware *softw= blks.at(k)->getSoftware();
      if( softw == NULL ) {
	success= false;
	consoleMessage("dataManager::executeAll: unknown software\n");
	break;
      }
					 
      runIndex_++;      
      success= softw->createInputFile(computedBeam);
      
      if ( success ) {
	success= softw->execute();
	if ( success ) {
	  success= softw->buildBeamAfterElements();
	}
      }
      
      if( !success ) {
	cout << "dataManager::executeAll: " << softw->getName() << " has failed..." << "ABORT" << endl;
	break;
      } else {
	if( diagnosticBeam_.size() > 0 ) {
	  computedBeam = &diagnosticBeam_.at(indexElementToIndexDiag_.back());
	} else {
	  computedBeam = NULL;
	}
	
	cout << "dataManager::executeAll => " << softw->getName() <<" ended successfully" << " (#" << diagnosticBeam_.size() << ")\n" << endl;
      }
    } // block k
  
  if(debug > 0) cout << "dataManager::executeAll #diagnosticBeam = " << diagnosticBeam_.size() << endl;
  return success;
}

int dataManager::getRunIndex() const
{
  return runIndex_;
}

void dataManager::writeToAMLFile(string fileName)
{
  if(debug > 0) cout << "dataManager::writeToAMLFile()\n";

  cout << "dataManager::writeToAMLFile()\n";
  
  UAPNode *UAPRoot= NULL;
  UAPRoot= new UAPNode("UAP_root");
  UAPNode *rep= UAPRoot->addChild("AML_representation");
  
  // root node in the hierarchy
  UAPNode *lab= rep->addChild("laboratory");
  lab->addAttribute("name","PSPA lab");
  
  // sequence of sectors with elements
  for (unsigned k = 0; k < sectors_.size(); k++) 
    {
      sector *secteur= sectors_.at(k);
      secteur->InputRep( lab );
    }

  if ( machineCourante_ ) machineCourante_->FileAMLOutput( lab );
  if(debug > 0) cout << "!Create the AML file ---------------------------" << endl;
 
  // stored in your own private workspace
  // string AMLFile = userDir_ + "/" + fileName + ".aml";
  string AMLFile= userDir_ + fileName + ".aml";
  AMLReader reader;
  reader.AMLRepToAMLFile(UAPRoot,AMLFile);
}

bool dataManager::parseAMLFile( string fileName )
{
  if(debug > 0) cout << "dataManager::parseAMLFile()\n";
  
  UAPNode *UAPRoot= NULL; 
  AMLReader reader;
  fileName= userDir_ + fileName;
  UAPRoot= reader.AMLFileToAMLRep( fileName );
  
  NodeVec lst1= UAPRoot->getSubNodesByName( "laboratory" );
  UAPNode *nd1= *lst1.begin();
  if ( !nd1 ) {
    cout << "dataManager::parseAMLFile: can't find the <laboratory> node\n";
    return false;
  }

  sectors_.clear(); // clear the beamLine
  NodeVec lst2= nd1->getSubNodesByName( "sector" );
  if ( lst2.empty() ) {
    cout << "dataManager::parseAMLFile: can't find <sector> nodes\n";
    return false;
  }
  
  NodeVecIter ie;
  for (ie = lst2.begin(); ie != lst2.end(); ie++) 
    {
      if ( (*ie)->getAttribute("ref") ) {
	continue;
      } else {
	UAPAttribute* att;
	att= (*ie)->getAttribute("name");
	if ( !att ) continue;
	string s= att->getValue();
	sectors_.push_back( new sector(this,att->getValue()) );
	sectors_.back()->FileAMLInput(*ie);
      } 
    }

   UAPNode *nd3= nd1->getChildByName( "machine" );
   if ( !nd3 ) {
     cout << "dataManager::parseAMLFile:: no machine ?...\n";
     return false;
   }
   
   if ( !machineCourante_ ) machineCourante_= new machine( this );
   if (!machineCourante_->FileAMLInput( nd3 )) {
     cout << "dataManager::parseAMLFile: failure to parse the file\n";
     return false;
   } else {
     return true;
   }
}

bool dataManager::parseXFile( string fileName )
{
  if(debug > 0) cout << "dataManager::parseXFile()\n";

  UAPNode *UAPRoot= NULL;
  TranslateCore *reader= new MADXParser();
  fileName= userDir_ + fileName;
  UAPRoot= reader->XFileToAMLRep( fileName );

  UAPNode *nd1= UAPRoot->getChildByName("AML_representation");
  UAPNode *nd2= nd1->getChildByName( "laboratory" );
  if(debug > 0) cout << nd2->toStringTree() << endl;
   
  UAPNode *nd3= nd2->getChildByName( "machine" );
  UAPAttribute *a1= nd3->getChildByName( "sector" )->getAttribute( "ref" );
229 230

  // recovers the working beam line ( lower case )
TOUZE Francois's avatar
TOUZE Francois committed
231 232 233 234
  string nameOfMachine= a1->getValue();
  if(debug > 0) cout << "machine = " << nameOfMachine << endl;

  clearSectors(); // clear the BeamLine
235 236

  // from the <beam> node creates the sector::BUNCH
TOUZE Francois's avatar
TOUZE Francois committed
237 238
  UAPNode *nd4= nd2->getChildByName( "beam" );
  if( nd4 ) {
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

    UAPNode *nd5;
    // GeV => in MeV
    nd5= nd4->getChildByName( "total_energy" );
    if (nd5) {
      UAPAttribute *a= nd5->getAttribute( "design" );
      double EGev= BasicUtilities::string_to_double( a->getValue() );
      string sMev= BasicUtilities::double_to_string( 1000.*EGev );
      nd5->addAttribute("design",sMev);
    }
    // GeV => in MeV
    nd5= nd4->getChildByName( "pc" );
    if (nd5) {
      UAPAttribute *a= nd5->getAttribute( "design" );
      double EGev= BasicUtilities::string_to_double( a->getValue() );
      string sMev= BasicUtilities::double_to_string( 1000.*EGev );
      nd5->addAttribute("design",sMev);
    }

    sectors_.push_back( new sector(this,"BUNCH") );
TOUZE Francois's avatar
TOUZE Francois committed
259
    sectors_.back()->readAMLTree( nd4 );
260
    
TOUZE Francois's avatar
TOUZE Francois committed
261
  } else {
262
    
TOUZE Francois's avatar
TOUZE Francois committed
263 264
    cout << "dataManager::parseXFile:: no beam in the machine?...\n";
    return false;
265
    
TOUZE Francois's avatar
TOUZE Francois committed
266
  }
267 268

  // among <sector> nodes recovers the one which describes the beam line
TOUZE Francois's avatar
TOUZE Francois committed
269 270
  NodeVec lst1= nd2->getChildrenByName( "sector" );
  NodeVecIter where= AMLtools::find(lst1.begin(),lst1.end(),nameOfMachine);
271 272
  
  // creates the sector::nameOfMachine with its elements
TOUZE Francois's avatar
TOUZE Francois committed
273 274 275 276
  NodeVec lst2= nd2->getChildrenByName( "element" );
  machineElements(*where,lst1,lst2);
  if(debug > 0) cout << (*where)->toStringTree() << endl;
   
277
  // creates the machine made up sector::BUNCH and sector::nameOfMachine
TOUZE Francois's avatar
TOUZE Francois committed
278
  if ( !machineCourante_ ) machineCourante_ = new machine(this);
279 280 281 282 283 284 285

  // sector::BUNCH is the first of the list
  machineCourante_->addSector( sectors_.at(0) );
  
  // convert to upper case
  mixedTools::toupper( nameOfMachine );
  // among the others sectors find the one whose name is nameOfMachine
TOUZE Francois's avatar
TOUZE Francois committed
286 287
  for (unsigned k = 0; k < sectors_.size(); k++) 
    {
288 289
      string s= sectors_.at( k )->getLabel();
      if (s.find( nameOfMachine ) != std::string::npos) {
TOUZE Francois's avatar
TOUZE Francois committed
290
	machineCourante_->addSector( sectors_.at(k) );
291
      }
TOUZE Francois's avatar
TOUZE Francois committed
292 293
    }
  
294
  return true;
TOUZE Francois's avatar
TOUZE Francois committed
295 296
}

297
void dataManager::machineElements(UAPNode *nde,NodeVec& sList,NodeVec& eList,bool reflx)
TOUZE Francois's avatar
TOUZE Francois committed
298 299 300 301
{
  if(debug > 0) cout << "dataManager::machineElements()\n";
  
  NodeVec v;
302
  NodeVec& children= nde->getChildren();
TOUZE Francois's avatar
TOUZE Francois committed
303
  NodeVecIter it;
304
  for (it = children.begin(); it != children.end(); ++it)
TOUZE Francois's avatar
TOUZE Francois committed
305 306
    {
      v.push_back(*it);
307 308 309 310 311
      
      if ((*it)->getName() == "sector") {
	
	UAPAttribute *a2= (*it)->getAttribute("ref");
	UAPAttribute *a3= (*it)->getAttribute("reflection");
TOUZE Francois's avatar
TOUZE Francois committed
312 313
	NodeVecIter kt; 
	kt= AMLtools::find(sList.begin(),sList.end(),a2->getValue());
314
	if ( a3 ) machineElements(*kt,sList,eList,true);
TOUZE Francois's avatar
TOUZE Francois committed
315
	else machineElements(*kt,sList,eList,false);
316
	
TOUZE Francois's avatar
TOUZE Francois committed
317 318 319
      }
    }

320
  string s= nde->getAttributeString("name");
TOUZE Francois's avatar
TOUZE Francois committed
321 322 323 324 325 326 327 328 329
  string upper= BasicUtilities::str_to_upper( s );
  if( reflx ) {
    upper.insert(0,"-");
    std::reverse(v.begin(),v.end()); // reverse of the sequence
  }

  if(debug > 0) { 
    cout<<"dataManager::machineElements(): new sector "<< upper << endl;
  }
330
  
TOUZE Francois's avatar
TOUZE Francois committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
  sector *sor= new sector(this,upper);
  sectors_.push_back( sor );
  sor->sectorTree(v,eList);
  return;
}

void dataManager::addNewSector() 
{
  if(debug > 0) cout << "dataManager::addNewSector()\n";
  
  std::ostringstream ostr;
  ostr << ++sectorCounter_;
  string s("SECTOR ");
  if(sectorCounter_ < 10) s.append("0");
  s.append( ostr.str() );

  sectors_.push_back( new sector(this,s) );
  //machineCourante_->addSector( s );
  machineCourante_->addSector( sectors_.back() );
}

void dataManager::eraseSector(string sectName)
{
  if(debug > 0) cout << "dataManager::eraseSector()\n";

  sector *sor= getSectorPointerByLabel( sectName );
  std::vector<sector*>::iterator it;
  it= find(sectors_.begin(),sectors_.end(),sor);
  if( it != sectors_.end() ) sectors_.erase( it );
  
  //machineCourante_->eraseSector( sectName );
  machineCourante_->eraseSector( *it );
}

void dataManager::setSecteurCourant(string etiq)
{
  secteurCourant_= getSectorPointerByLabel(etiq);
}

sector* dataManager::getSecteurCourant() const 
{
  return secteurCourant_;
}

void dataManager::clearSectors()
{
  sectorCounter_= 0;
  if( !sectors_.empty() ) sectors_.clear();
  machineCourante_->clearSectors();
}

void dataManager::consoleMessage(string message)
{
  if(debug > 0) cout << "dataManager::consoleMessage()\n";
  cout << "consoleMessage:: " << message;
}

void dataManager::consoleOutput(string out)
{
  if(debug > 0) cout << "dataManager::consoleOutput()\n";
  //cout << "consoleOutput:: " << out << endl;
}

void dataManager::messageEcran(string niveau,string msg)
{
  if(debug > 0) cout << "dataManager::messageEcran()\n";
  cout << "dataManager:: " << niveau << " : " << msg << endl;
}

string dataManager::getnameOfCase() const
{
  if(debug > 0) cout << "dataManager::getnameOfCase()\n";
  return nameOfCase_;
}

void dataManager::setnameOfCase(string s)
{
  nameOfCase_= s;
}

string dataManager::getUserDir() const
{
  return userDir_;
}

string dataManager::getSoftwareDir() const
{
  return workingDir_;
}

machine* dataManager::getCurrentMachine() const
{
  return machineCourante_;
}

expandedMachine* dataManager::getCurrentExpandedMachine() const
{
  return machineCourante_->getExpandedMachine();
}
  
// si newBeam = true, on cree un nouveau diag
// si newBeam = false, on utilise le dernier
particleBeam* dataManager::updateCurrentDiagnostic(bool newBeam) 
{
  if ( newBeam ) {
    diagnosticBeam_.push_back( particleBeam() );
  }
					  
  indexElementToIndexDiag_.push_back(diagnosticBeam_.size() - 1);
  return &diagnosticBeam_.back();
}

particleBeam* dataManager::getDiagnosticBeam(unsigned index)
{
  if(debug > 0) cout << "dataManager::getDiagnosticBeam()\n";

  if (index >= indexElementToIndexDiag_.size() ) {
    return NULL;
  } else {
    int indDiag = indexElementToIndexDiag_.at(index);
    return &diagnosticBeam_.at(indDiag);
  }
}

particleBeam* dataManager::getDiagnosticBeam_deprecated(string elementLabel)
{
  if(debug > 0) cout << "dataManager::getDiagnosticBeam_deprecated()\n";
  
  if ( !machineCourante_->getExpandedMachine() ) return NULL;
  // FIXME : Devra etre changé par une récupération par "abstractElement" et non pas par label
  unsigned int number = 0;
  abstractElement* elPtr;
  vector<computingBlock*> blks= machineCourante_->getExpandedMachine()->getComputingBlocks();
  for (unsigned k = 0; k < blks.size(); k++) {
    computingBlock* block= blks.at(k);
    for (unsigned m = 0; m < block->getNumberOfElements(); m++) {
      elPtr = block->getElement(m);
      if (elPtr->getLabel() == elementLabel) {
	return getDiagnosticBeam(number);
      }
      number ++;
    }
  }
  return NULL;
}

abstractElement* dataManager::getElementInSectorsByLabel(string etiq) 
{
  if(debug > 0) cout << "dataManager::getElementInSectorsByLabel()\n";

  for (unsigned k = 0; k < sectors_.size(); k++) 
    {
      vector<abstractElement*>& elts= sectors_.at(k)->getElements();
      for (unsigned j = 0; j < elts.size(); j++)
	{
	  if ( etiq == elts.at(j)->getLabel() ) 
	    return elts.at(j);
	}
    }
  return NULL;
}

sector* dataManager::getSectorPointerByLabel(string etiq) 
{
  if(debug > 0) cout << "dataManager::getSectorPointerByLabel()\n";
  
  for (unsigned k = 0; k < sectors_.size(); k++) 
    {
      if(debug > 0) cout << "dataManager::getSectorPointerByLabel label du secteur de la liste  : " << sectors_.at(k)->getLabel() << endl;
      
      if( etiq == sectors_.at(k)->getLabel() ) {
	if(debug > 0) cout << "dataManager::getSectorPointerByLabel secteur " << etiq << ", retrouve " << endl;
	return sectors_.at( k );
      }
    }

  cout << "dataManager::getSectorPointerByLabel: ECHEC le secteur " << etiq << " est introuvable" << endl;
  return NULL;
}
 
const vector<sector*>& dataManager::getSectors() const
{
  return sectors_;
}

globalParameters* dataManager::getGlobalParameters()  
{
  // seulement pour softwareParmela
  return &globParam_;
}

json dataManager::toJSONFormat()
{
  json j;
  toJSON(j);
  return j;
}
  
void dataManager::toJSON(json& j)
{
531 532
  if(debug > 0) cout << "\n\ndataManager::toJSON()\n";
  
TOUZE Francois's avatar
TOUZE Francois committed
533 534 535 536 537 538 539 540 541 542
  if ( !machineCourante_ ) {
    cout << "dataManager::toJSON:: no machine ?...\n";
    return;
  }

  vector<sector*>& sors= machineCourante_->getSectors();
  for (unsigned k = 0; k < sors.size(); k++)
    {
      string s1= "sector-" + mixedTools::alea(); 
      string s2= sors.at(k)->getLabel();
543
      mixedTools::toupper( s2 );
TOUZE Francois's avatar
TOUZE Francois committed
544 545 546 547 548 549 550 551 552
      
      json x;
      x["id"] = s1;
      x["displayName"] = s2;
      x["category"] = "sector";
      sors.at(k)->toJSON( x );
      j["machine"].push_back( x );
    }

553 554
  // cout << j.dump(4) << endl;
  
TOUZE Francois's avatar
TOUZE Francois committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
  cout << "dataManager::toJSON nameOfCase => " << nameOfCase_ << endl;
  j["name"]= nameOfCase_;

  machineCourante_->toJSON( j );
}

bool dataManager::parseJSONFile( string fileName )
{
  fileName= userDir_ + fileName;
  if( !mixedTools::exists( fileName ) ) {
    cout << "ERROR in dataManager::parseJSONFile:: " << fileName << " is unkwown\n";
    return false;
  } else {
    cout << "dataManager::parseJSONFile:: open " << fileName << "\n";
  }
    
  // read the JSON file
  std::ifstream ifs( fileName );
  json j;
  ifs >> j;
  ifs.close();
    
  std::cout << std::boolalpha;
  auto mac= j.find( "machine" );
  if( mac == j.end() ) {
    cout << "dataManager::parseJSONFile: can't find the <machine> key\n";
    return false;
  }

  
  //cout << mac->dump(4) << endl;
  
  /**
  cout << "1- ############ Liste du/des secteur(s): ############\n";
  sectors_.clear();
  for (auto kt = mac->begin(); kt != mac->end(); ++kt)
    {
      auto key= kt->find( "displayName" ).value();
      sectors_.push_back( new sector(this,key) );
      sectors_.back()->fromJSON( *kt );
    }
  */

  map<string, vector<json> > cmap;
  int count= 0;
  for (auto kt = mac->begin(); kt != mac->end(); ++kt)
    {
      string key= "KEY" + mixedTools::intToString( count );
      cmap[ key ].push_back(*kt);
      string category= kt->find( "category" ).value();
      if (category == "sector") count++;
    }
  
  cout << "1- ############ Liste du/des secteur(s): ############\n";
  sectors_.clear();
  for(int i = 0; i < cmap.size(); ++i)
    {
      string key= "KEY" + mixedTools::intToString( i );
      map<string, vector<json> >::iterator it= cmap.find( key );
      if ( it == cmap.end() ) continue;

      json jk= it->second.at( 0 );
      string category= jk.find( "category" ).value();
      
      if ( category == "sector" ) {
	
	string str= jk.find( "displayName" ).value();
	sectors_.push_back( new sector(this,str) );
	auto xe= jk.find( "elements" );
	sectors_.back()->fromJSON( *xe );
	
      } else {

	sectors_.push_back( new sector(this,key) );
	sectors_.back()->fromJSON( it->second );
	
      }
      
    }
  
  auto pname= j.find( "name" );
  string s= *pname;
  if (nameOfCase_ != s) {
    cout << "dataManager::parseJSONFile => nameOfCase " << nameOfCase_ << " with " << s << endl;
    nameOfCase_= s;
  }
 
  if ( !machineCourante_ ) machineCourante_= new machine( this );
   auto jt= j.find( "simulations" );
   json exp;
   if( jt != j.end() ) exp= *jt;
   bool status= machineCourante_->fromJSON( cmap,exp );
   return status;
}

bool dataManager::fromSimulate(json j)
{
  if(debug > 0) std::cout << "dataManager::fromSimulate()\n";
653

TOUZE Francois's avatar
TOUZE Francois committed
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
  auto mac= j.find( "machine" );
  if( mac == j.end() ) {
    cout << "dataManager::fromSimumate: can't find the <machine> key\n";
    return false;
  }
  
  sectors_.clear();
  for (auto kt = mac->begin(); kt != mac->end(); ++kt)
    {
      auto key= kt->find( "displayName" ).value();
      sectors_.push_back( new sector(this,key) );
      auto xe= kt->find( "elements" );
      sectors_.back()->fromJSON( *xe );
    }

  auto pname= j.find( "projectName" );
  if( pname == j.end() ) {
    nameOfCase_= "XXXX";
  } else {
    string s= *pname;
    nameOfCase_= s;
  }
  if(debug > 0) cout << "dataManager::fromSimulate: nameOfCase_ => " <<  nameOfCase_ << endl;
  
  if ( !machineCourante_ ) machineCourante_= new machine( this );
  bool status= machineCourante_->fromJSON( j );
  return status;
}

void dataManager::settingPlot()
{
  if(debug > 0) cout << "dataManager::settingPlot() => " << nameOfCase_ << endl;
  
  expandedMachine *exma= machineCourante_->getExpandedMachine();
  vector<computingBlock*>& blks= exma->getComputingBlocks();
  for(unsigned int k = 0; k < blks.size(); ++k) 
    {
      computingBlock *ptr= blks.at(k);
      abstractSoftware *softw= ptr->getSoftware();
      json obj= softw->readOpticalParameters();
    }
}
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749

bool dataManager::parseBETAFile( string fileName )
{
  if(debug > 0) cout << "dataManager::parseBETAFile()\n";

  BETAParser reader;
  UAPNode *upRoot= NULL;
  fileName= userDir_ + fileName;
  upRoot= reader.FileToAMLRep( fileName );

  if ( !upRoot ) {
    cout << "dataManager::parseBETAFile:: failure to parse " << fileName << "\n";
    return false;
  }
  
  if(debug > 0) cout << upRoot->toStringTree() << endl;
  
  // From ΒΕΤA sections there are two PSPA::sectors defined
  clearSectors();
    
  // The first one contains the element BEAM
  UAPNode *nd1= upRoot->getChildByName( "beam" );
  if( nd1 ) {
    sectors_.push_back( new sector(this,"BUNCH") );
    sectors_.back()->readAMLTree( nd1 );
  } else {
    cout << "dataManager::parseBETAFile:: no beam in the machine\n";
    return false;
  }

  // The second one contains the elements in the STRUCTURE
  NodeVec lst1= upRoot->getChildrenByName( "sector" );
  NodeVec lst2= upRoot->getChildrenByName( "element" );
  machineElements(lst1[0],lst1,lst2);

  if (!machineCourante_) machineCourante_= new machine( this );
  machineCourante_->addSector( sectors_.at(0) );
  machineCourante_->addSector( sectors_.at(1) );

  if(debug > 0) {
    for (unsigned k = 0; k < sectors_.size(); k++) 
      {
	cout << sectors_.at(k)->getLabel() << " =>\n";
      
	vector<abstractElement*>& elts= sectors_.at(k)->getElements();
	for (unsigned i = 0; i < elts.size(); i++) 
	  {
	    cout << elts.at(i)->getLabel() << endl;
	  }
      }
  }

  return true;
}