#include "dataManager.h" #include "expandedMachine.h" #include "softwareUnknown.h" #include "softwareParmela.h" #include "softwareTransport.h" #include "softwareGenerator.h" #include "softwareGenerator.h" #include "softwareUsersprogram.h" #include "softwareTest.h" #include "softwareMadx.h" //xx void expandedMachine::clearComputingBlock(int a) { cout << " expandedMachine::clearComputingBlock : effacement du bloc d'index = " << a << endl; if (a < 0) return; if (a >= (int)computingBlocks_.size()) return; // lors de la creation de la section on a fait un 'new' d'un // softwareXXXX : on fait ici le 'delete' // const abstractSoftware* soft = computingBlocks_.at(a)->getSoftware(); // if ( soft != NULL ) delete soft; computingBlocks_.at(a)->clearAll(); delete computingBlocks_[a]; computingBlocks_.erase (computingBlocks_.begin()+a); } bool expandedMachine::eraseComputingBlock(computingBlock* cpbl) { for ( unsigned k=0; k < computingBlocks_.size(); k++) { if ( cpbl == computingBlocks_.at(k) ) { cout << " expandedMachine::eraseComputingBlock : J'AI TROUVE LE BLOC A DETRUIRE " << endl; unsigned nbElem = computingBlocks_.at(k)->getNumberOfElements(); if ( k != 0 ) { computingBlocks_.at(k-1)->extendByTheEnd(nbElem); } else { if ( computingBlocks_.size() == 1 ) return false; computingBlocks_.at(k+1)->reinitialiseElements(computingBlocks_.at(k)->getRankOfFirstElement(), computingBlocks_.at(k+1)->getNumberOfElements() + nbElem); } clearComputingBlock(int(k) ); return true; } } return false; } abstractSoftware* expandedMachine::getSoftwarePointer(std::string logiciel, computingBlock* cpblk) { cout << " expandedMachine::getSoftwarePointer .... logiciel " << logiciel << endl; abstractSoftware* prog; string inputFileName; if(logiciel == "parmela") { inputFileName = "parmin"; prog = new softwareParmela(inputFileName, cpblk,dataManager_ ); } else if (logiciel == "transport") { inputFileName = "transport.input"; prog = new softwareTransport(inputFileName, cpblk,dataManager_ ); } else if (logiciel == "generator") { inputFileName = "generator.in"; prog = new softwareGenerator(inputFileName, cpblk,dataManager_ ); } else if (logiciel == "madx") { inputFileName = "madx.input"; prog = new softwareMadx(inputFileName,cpblk,dataManager_ ); } else if (logiciel == "usersprogram") { inputFileName = "dummy"; prog = new softwareUsersprogram(inputFileName, cpblk,dataManager_ ); } else if (logiciel == "test") { prog = new softwareTest(inputFileName, cpblk,dataManager_ ); } else { prog = new softwareUnknown(); } return prog; } void expandedMachine::setSoftware(std::string logiciel, computingBlock* cpblk) { int a = -1; for (unsigned k=0; k < computingBlocks_.size(); k++ ) { if ( cpblk == computingBlocks_.at(k) ) a = k; } if ( a < 0) return; abstractSoftware* prog = getSoftwarePointer(logiciel, computingBlocks_.at(a)); // string inputFileName; // if(logiciel == "parmela") { // inputFileName = "parmin"; // prog = new softwareParmela(inputFileName, computingBlocks_.at(a),dataManager_ ); // } else if (logiciel == "transport") { // inputFileName = "transport.input"; // prog = new softwareTransport(inputFileName, computingBlocks_.at(a),dataManager_ ); // } else if (logiciel == "generator") { // inputFileName = "generator.in"; // prog = new softwareGenerator(inputFileName, computingBlocks_.at(a),dataManager_ ); // } else if (logiciel == "madx") { // inputFileName = "madx.input"; // prog = new softwareMadx(inputFileName,computingBlocks_.at(a),dataManager_ ); // } else if (logiciel == "usersprogram") { // inputFileName = "dummy"; // prog = new softwareUsersprogram(inputFileName, computingBlocks_.at(a),dataManager_ ); // } else if (logiciel == "test") { // prog = new softwareTest(inputFileName, computingBlocks_.at(a),dataManager_ ); // } else { // prog = new softwareUnknown(); // } computingBlocks_.at(a)->setSoftware(prog); } // methode provisoire void expandedMachine::fromSectors( const vector& secteurs ) { cout << " methode expandedMachine::fromSectors " << endl; unsigned k,j,m; elements_.clear(); for (k=0; k < secteurs.size() ; k++) { sector* sect = secteurs.at(k); // vector vecsec = sect->getSectionsToExecute(); // for (j=0; j < vecsec.size() ; j++) { vector< abstractElement* > lesElements = sect->getElements(); for ( m=0; m < lesElements.size(); m++) elements_.push_back(lesElements.at(m)); // } } cout << " expandedMachine::fromSectors la machine a " << elements_.size() << " elements " << endl; computingBlocks_.clear(); computingBlocks_.push_back(new computingBlock(0, elements_.size(),NULL, this)); } bool expandedMachine::openNewLastBlock() { cout << " expandedMachine::openNewLastBlock() " << endl; abstractElement* s = computingBlocks_.back()->getLastElement(); if ( !s ) { cout << " expandedMachine::openNewLastBlock() dernier element non reconnu " << endl; return false; } computingBlocks_.back()->removeLastElement(); computingBlocks_.push_back(new computingBlock(getElementRankFromPointer(s), 1,NULL, this)); } bool expandedMachine::openNewBlockAfter(computingBlock* cpbl) { cout << " expandedMachine::openNewBlockAfter() " << endl; int preceedingIndex = -1; int compteur=0; vector::iterator iter; vector::iterator iterFound; for ( iter = computingBlocks_.begin(); iter != computingBlocks_.end(); iter++, compteur++) { if ( cpbl == *iter ) { cout << " expandedMachine::openNewBlockAfter : J'AI TROUVE LE BLOC APRES LEQUEL INSERER " << endl; iterFound = iter; preceedingIndex = compteur; break; } } if ( preceedingIndex < 0 ) return false; unsigned dernierPrecedent = computingBlocks_.at(preceedingIndex)->getRankOfLastElement(); computingBlocks_.at(preceedingIndex)->removeLastElement(); unsigned nbElemNew; if ( preceedingIndex < computingBlocks_.size() - 1 ) { computingBlocks_.at(preceedingIndex+1)->removeFirstElement(); nbElemNew = 2; } else { nbElemNew = 1; } computingBlocks_.insert(iter + 1, new computingBlock(dernierPrecedent, nbElemNew,NULL, this)); cout << " expandedMachine::openNewBlockAfter il y a maintenant " << computingBlocks_.size() << " blocs " << endl; for ( unsigned j=0; j < computingBlocks_.size(); j++) { cout << " bloc " << j << " : " << endl; for ( unsigned k=0; k < computingBlocks_.at(j)->getNumberOfElements(); k++) { cout << computingBlocks_.at(j)->getElement(k)->getLabel() << endl; } } } void expandedMachine::FileAMLOutput(UAPNode* root) { UAPNode* node = root->addChild("machine"); node->addAttribute("name",machineName_); UAPNode* lesElements = node->addChild("machineElements"); for ( unsigned k=0; k < elements_.size(); k++) { lesElements->addChild("melement")->addAttribute("name",elements_.at(k)->getLabel()); } UAPNode* lesBlocs = node->addChild("mcomputingBlocks"); for ( unsigned k=0; k < computingBlocks_.size(); k++) { computingBlocks_.at(k)->FileAMLOutput(lesBlocs ); } } bool expandedMachine::FileAMLInput( UAPNode* entree) { if ( !entree ) return false; elements_.clear(); computingBlocks_.clear(); UAPNode* listElem = entree->getChildByName("machineElements"); if ( !listElem ) { cout << " expandedMachine::FileAMLInput PAS D'ELEMENTS DANS LA MACHINE " << endl; } else { NodeVec lesElements = listElem->getSubNodesByName("melement"); for (unsigned k=0; k < lesElements.size(); k++ ) { abstractElement* elem = dataManager_->getElementInSectorsByLabel(lesElements.at(k)->getAttribute("name")->getValue()); if ( elem ) elements_.push_back(elem); } } UAPNode* lesBlocks = entree->getChildByName("mcomputingBlocks"); if ( !lesBlocks ) { cout << " expandedMachine::FileAMLInput PAS DE BLOCS DANS LA MACHINE " << endl; } else { NodeVec listBlocks = lesBlocks->getSubNodesByName("computingBlock"); for ( unsigned k=0; k < listBlocks.size(); k++ ) { computingBlocks_.push_back(new computingBlock(this)); computingBlocks_.back()->FileAMLInput( listBlocks.at(k)); } } } abstractElement* expandedMachine::getElementFromLabel(string lab) { abstractElement* resultat = NULL; for ( unsigned k=0; k < elements_.size(); k++) { if ( elements_.at(k)->getLabel() == lab ) { resultat = elements_.at(k); break; } } return resultat; } int expandedMachine::getElementRankFromLabel(string lab) { for ( unsigned k=0; k < elements_.size(); k++) { if ( elements_.at(k)->getLabel() == lab ) { return k; } } return -1; } int expandedMachine::getElementRankFromPointer(abstractElement* ptr) { for ( unsigned k=0; k < elements_.size(); k++) { if ( elements_.at(k) == ptr ) { return k; } } return -1; } void expandedMachine::setFirstElementOfBlock(computingBlock* bclk, int index) { // This will set the beginning of a block to the element with the given name // Possible cases : // // | section 1 | section 2 | section 3 | section 4 | section 5 | // | a b c d | e f g h | i j k l | m n o p | q r | Before (example) // Moved in a previous section : // | a b c d | e f | _g_ h i j k l | m n o p | q r | Case 1 : move beginning of section 3 to "g" // | a b c | | _d_ e f g h i j k l | m n o p | q r | Case 2 : move beginning of section 3 to "d" => section 2 will be removed ! // => all elements from "d"/"g" to "l" will be moved in the 3rd section // Moved in the same section : // | a b c d | e f g h i j | _k_ l | m n o p | q r | Case 3 : move beginning of section 3 to "k" // => all elements from "i"(begin of 3rd section) to before "k" will be moved in the 2nd (3-1) section // Moved in a next section : // | a b c d | e f g h i j k l m | _n_ | o p | q r | Case 4 : move beginning of section 3 to "n" // | a b c d | e f g h i j k l m n o p | _q_ | | r | Case 5 : move beginning of section 3 to "q" => remove section 4 ! // => all elements from "i"(begin of 3rd section) to before "n"/"q" will be moved in the 2nd (3-1) section // => "n"/"q" element is set to be alone in 3rd section // std::vector & mchVect = machine_->getComputingBlocks(); cout << " expandedMachine::setFirstElementOfBlock ENTREE " << endl; if ( index < 0 || index >= elements_.size() ) { cout << " expandedMachine::setFirstElementOfBlock index out of range " << index << endl; return; } unsigned int blockNumber = 0; int blockNumberOfNewBegin = 0; // get this block number // get the block number of the new begin element unsigned int curseur = 0; if ( computingBlocks_.size() ) curseur = computingBlocks_[0]->getRankOfFirstElement(); bool newFoundInBlocks = false; bool newFoundBeforeBlocks = false; if ( curseur > index ) { newFoundBeforeBlocks = true; } else { for (unsigned int a=0; a< computingBlocks_.size(); a++) { if (computingBlocks_[a] == bclk) blockNumber = a; curseur += computingBlocks_[a]->getNumberOfElements(); if ((curseur >index ) && (!newFoundInBlocks)) { blockNumberOfNewBegin = a; newFoundInBlocks = true; } } } if (!newFoundInBlocks ) { if ( newFoundBeforeBlocks ) { unsigned nombre = computingBlocks_[blockNumber]->getRankOfLastElement() - index + 1; computingBlocks_[blockNumber]->reinitialiseElements(index, nombre); // on vide les blocs initiaux. for (int a = 0 ; a < blockNumber; a++) computingBlocks_[a]->clearElements(); } else { // le nouveau debut est apres les blocks presents if ( blockNumber > 0 ) { unsigned debutPrecedent = computingBlocks_[blockNumber-1]->getRankOfFirstElement(); unsigned nombre = index - debutPrecedent; computingBlocks_[blockNumber-1]->reinitialiseElements(debutPrecedent, nombre); } unsigned nombre = elements_.size() - index; computingBlocks_[blockNumber]->reinitialiseElements(index, nombre); // on vide les blocs finaux for (int a = blockNumber + 1 ; a < computingBlocks_.size(); a++) computingBlocks_[a]->clearElements(); } } else if ( blockNumberOfNewBegin < blockNumber ) { // index trouve dans les blocs // Moved in a previous section cout << " expandedMachine::setFirstElementOfBlock Moved in a previous section new bloc = " << blockNumberOfNewBegin << endl; unsigned debutPrecedent = computingBlocks_[blockNumberOfNewBegin]->getRankOfFirstElement(); unsigned nombre = index - debutPrecedent; computingBlocks_[blockNumberOfNewBegin]->reinitialiseElements(debutPrecedent, nombre); unsigned lastIndex = computingBlocks_[blockNumber]->getRankOfLastElement(); computingBlocks_[blockNumber]->reinitialiseElements(index, lastIndex - index +1); // on vide les blocs intermediaires. for (int a = blockNumber-1 ; a > blockNumberOfNewBegin; a--) computingBlocks_[a]->clearElements(); } else if ( blockNumberOfNewBegin == blockNumber ) { // Moved in the same block : cout << " expandedMachine::setFirstElementOfBlock Moved in the same block new bloc = " << blockNumberOfNewBegin << endl; // les elements precedant index sont affectes au bloc precedent if ( blockNumber ) { unsigned debutPrecedent = computingBlocks_[blockNumber-1]->getRankOfFirstElement(); unsigned nombre = index - debutPrecedent; computingBlocks_[blockNumber-1]->reinitialiseElements(debutPrecedent, nombre); } unsigned ancienDebut = computingBlocks_[blockNumber]->getRankOfFirstElement(); unsigned offset = index - ancienDebut; computingBlocks_[blockNumber]->reinitialiseElements(index, computingBlocks_[blockNumber]->getNumberOfElements() - offset); } else if ( blockNumberOfNewBegin > blockNumber ) { // Moved in a next section : if ( blockNumber > 0 ) { unsigned debutPrecedent = computingBlocks_[blockNumber-1]->getRankOfFirstElement(); unsigned nombre = index - computingBlocks_[blockNumberOfNewBegin]->getRankOfFirstElement(); computingBlocks_[blockNumber-1]->reinitialiseElements(debutPrecedent, nombre); } unsigned offset = index - computingBlocks_[blockNumberOfNewBegin]->getRankOfFirstElement(); unsigned nombre = computingBlocks_[blockNumberOfNewBegin]->getNumberOfElements() - offset; computingBlocks_[blockNumberOfNewBegin]->reinitialiseElements(index, nombre); // on vide les blocs intermediaires. for (int a = blockNumber ; a < blockNumberOfNewBegin; a++) computingBlocks_[a]->clearElements(); } // Suppress the empty blocs for (unsigned int a=0; a< computingBlocks_.size(); a++) { if ( computingBlocks_[a]->isEmpty() ) { clearComputingBlock(a); } } cout << " expandedMachine::setFirstElementOfBlock Apres nettoyage, il reste " << computingBlocks_.size() << " blocs " << endl; for ( unsigned j=0; j < computingBlocks_.size(); j++) { cout << " bloc " << j << " : " << endl; for ( unsigned k=0; k < computingBlocks_.at(j)->getNumberOfElements(); k++) { cout << computingBlocks_.at(j)->getElement(k)->getLabel() << endl; } } } void expandedMachine::setLastElementOfBlock(computingBlock* bclk, int index) { // This will set the end of a block to the element with the given name if ( index < 0 || index >= elements_.size() ) { cout << " expandedMachine::setLastElementOfBlock index out of range " << index << endl; return; } unsigned int blockNumber = 0; int blockNumberOfNewEnd = 0; // get this block number // get the block number of the new begin element unsigned int curseur = 0; if ( computingBlocks_.size() ) curseur = computingBlocks_.back()->getRankOfLastElement(); bool newFoundInBlocks = false; bool newFoundAfterBlocks = false; if ( curseur < index ) { newFoundAfterBlocks = true; } else { // for ( int a = computingBlocks_.size() -1; a >= 0; a--) { // pour l'instant on ne peut que la fin du DERNIER BLOC // a revoir si necessaire if (computingBlocks_.back() != bclk) { cout << " expandedMachine::setLastElementOfBlock, le bloc a modifer n'est pas le dernier : revoir la programmation du module " << endl; return; } blockNumber = computingBlocks_.size() - 1; for ( int a = computingBlocks_.size() -1; a >= 0 ; a--) { curseur -= computingBlocks_[a]->getNumberOfElements(); if ((curseur < index ) && (!newFoundInBlocks)) { blockNumberOfNewEnd = a; newFoundInBlocks = true; } } } if (!newFoundInBlocks ) { if ( newFoundAfterBlocks ) { unsigned nombre = index - computingBlocks_.back()->getRankOfLastElement(); computingBlocks_.back()->extendByTheEnd(nombre); } else { // le nouveau debut est avant les blocks presents computingBlocks_[0]->reinitialiseElements(0, index + 1); // on vide les blocs restants for (int a = 1 ; a < computingBlocks_.size(); a++) computingBlocks_[a]->clearElements(); } } else { // index trouve dans les blocs unsigned nombre = computingBlocks_[blockNumberOfNewEnd]->getRankOfLastElement() - index; computingBlocks_[blockNumberOfNewEnd]->suppressByTheEnd(nombre); // on vide les blocs restants for (int a = blockNumberOfNewEnd + 1 ; a < computingBlocks_.size(); a++) computingBlocks_[a]->clearElements(); } // Suppress the empty blocs for (unsigned int a=0; a< computingBlocks_.size(); a++) { if ( computingBlocks_[a]->isEmpty() ) { clearComputingBlock(a); } } cout << " expandedMachine::setLastElementOfBlock Apres nettoyage, il reste " << computingBlocks_.size() << " blocs " << endl; for ( unsigned j=0; j < computingBlocks_.size(); j++) { cout << " bloc " << j << " : " << endl; for ( unsigned k=0; k < computingBlocks_.at(j)->getNumberOfElements(); k++) { cout << computingBlocks_.at(j)->getElement(k)->getLabel() << endl; } } } bool expandedMachine::areDataCoherent() { cout << "***********************************" << endl; cout << "** expandedMachine::areDataCoherent() **" << endl; cout << "***********************************" << endl; bool caMarche = true; string diagnosticErrors; string diagnosticWarnings; for (int a = 0; a < computingBlocks_.size(); a++) { if ( !computingBlocks_.at(a)->checkCoherence(diagnosticErrors, diagnosticWarnings) ) caMarche = false; }//a cout << " erreurs : " << diagnosticErrors << endl; if ( !diagnosticErrors.empty() ) dataManager_->messageEcran("ERROR", diagnosticErrors); if ( !diagnosticWarnings.empty() ) { cout << " y a des warnings " << diagnosticWarnings << endl; dataManager_->messageEcran("WARNING",diagnosticWarnings); } return caMarche; } abstractElement* expandedMachine::getElementPointer( unsigned rang) { if ( rang > elements_.size() ) return NULL; return elements_.at(rang); }