source: PSPA/Interface_Web/trunk/pspaWT/sources/controler/src/dataManager.cc @ 297

Last change on this file since 297 was 297, checked in by lemeur, 11 years ago

include <algorithm> dans softwareTransport

File size: 7.9 KB
Line 
1#include "dataManager.h"
2#include "mathematicalConstants.h"
3#include "PhysicalConstants.h"
4#include "softwareParmela.h"
5#include "softwareTransport.h"
6
7#include <stdio.h>
8#include <fstream>
9//#include <algorithm>
10
11abstractElement* dataManager::addElement(typedElement elemType)
12{
13  return elementsGallery_.addElement(elemType);
14}
15
16string dataManager::getLabelFromElementNumero(int numero)
17{
18  abstractElement* ptr = elementsGallery_.getElementPointerFromNumero(numero);
19  if ( ptr == NULL ) return "";
20  return ptr->getLabel();
21}
22
23
24void dataManager::addSectionToExecute(int debut, int fin, nomDeLogiciel prog)
25{
26  jobList_.push_back(new sectionToExecute);
27  jobList_.back()->firstElement = debut;
28  jobList_.back()->lastElement = fin;
29  jobList_.back()->software  = prog;
30}
31
32void dataManager::clearSectionToExecute()
33{
34  unsigned k;
35  for(k = 0; k < jobList_.size(); k++)
36    {
37      if ( jobList_[k] != NULL ) delete jobList_[k];
38    }
39  jobList_.clear();
40}
41
42trivaluedBool dataManager::checkExecute(string& diagnostic)
43{
44  cout << "dataManager::checkExecute()" << endl;
45  trivaluedBool resul = ok;
46  unsigned k,j;
47  diagnostic.clear();
48  unsigned indexDeb, indexFin;
49  cout << "controle execution : " << endl;
50  for(k = 0; k < jobList_.size(); k++) {
51    indexDeb = jobList_[k]->firstElement;
52    indexFin = jobList_[k]->lastElement;
53    if ( indexFin  <= indexDeb )
54      {
55        diagnostic += " first and last elements are the same for job " + (jobList_[k]->software).getString() + " \n";
56        resul = error;
57        break;
58      }
59    cout << " indexDeb= " << indexDeb << " indexFin= " << indexFin << endl;
60    indexDeb--;
61    indexFin--;
62   
63    abstractElement* elPtr;
64    string checkCompatibility;
65    resul = checkElementsForExec(jobList_[k]->software, indexDeb, indexFin, checkCompatibility);
66    diagnostic += checkCompatibility;
67  }
68  cout << " data manager renvoie resul = " << resul << endl;
69  return resul;
70}
71
72trivaluedBool dataManager::checkElementsForExec(nomDeLogiciel logiciel, unsigned indexDeb, unsigned indexFin, string& diagnostic)
73{
74  trivaluedBool resul = ok;
75  diagnostic.clear();
76  trivaluedBool accepted;
77  abstractElement* elPtr;
78  for(unsigned j = indexDeb; j <= indexFin; j++) {
79    elPtr = elementsGallery_.getElementPointerFromIndex(j);
80    accepted = elPtr->is_accepted_by_software(logiciel);
81    if(accepted  == error  ) {
82      diagnostic += " the element " + elPtr->getNomdElement().getElementName() + " is not allowed with " + logiciel.getString() + " \n";
83      resul = error;
84    } else if ( accepted  == warning ) {
85      diagnostic += " the element " + elPtr->getNomdElement().getElementName() + " will be ignored by " + logiciel.getString() + " \n"; 
86      if ( resul != error ) resul = warning;
87    }
88  }
89  return resul;
90}
91
92
93
94
95void dataManager::initializeExecution(string workingDir)
96{
97  removeFile(workingDir + "parmdesz");
98  removeFile(workingDir + "parmin");
99  removeFile(workingDir + "transport.input");
100  removeFile(workingDir + "transport.output");
101  diagnosticBeam_.clear();
102  currentBeam_ = NULL;
103  clearSectionToExecute();
104}
105
106void dataManager::removeFile(string nameOfFile) 
107{
108  ifstream fileExists;
109  fileExists.open(nameOfFile.c_str(), ios::in);
110  if (fileExists) {
111    fileExists.close();
112    remove(nameOfFile.c_str());
113  }
114}
115
116bool dataManager::executeAll(string workingDir, string &resul)
117{
118  bool success = true;
119  resul.clear();
120  string resultatPartiel;
121  unsigned k;
122  abstractSoftware* softw =NULL;
123
124  for(k = 0; k < jobList_.size(); k++)
125    {
126      int debut = jobList_[k]->firstElement;
127      int fin = jobList_[k]->lastElement;
128      resultatPartiel.clear();
129      cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->software).getString() << endl;
130
131      if(jobList_[k]->software == nomDeLogiciel::parmela) {
132        softw = new softwareParmela(&globParam_, &elementsGallery_);
133      } else if (jobList_[k]->software == nomDeLogiciel::transport) {
134        softw = new softwareTransport(&globParam_, &elementsGallery_);
135      } else {
136        success = false;
137        resultatPartiel =  " unknown software -- STOP \n ";     
138        break;
139      }
140
141      success = softw->createInputFile(currentBeam_, debut, fin, workingDir);
142      if ( !success ) {
143        resultatPartiel += " error creating input file ";
144      } else {
145        success = softw->execute(diagnosticBeam_, debut,fin,workingDir,resultatPartiel);
146      }
147      delete softw;
148      if ( success ) currentBeam_ = &diagnosticBeam_.back();
149      else currentBeam_ = NULL;
150      resul += resultatPartiel;
151      if ( !success ) break;
152    }
153  return success;
154}
155
156void dataManager::saveConfiguration(string workingDir, string nameOfFile)
157{
158  ofstream outfile;
159  string name = workingDir + nameOfFile + ".save";
160  outfile.open(name.c_str(), ios::out);
161  if (!outfile) {
162    cerr << " error opening output file for persistency " << name << endl;
163  }
164
165  outfile << globParam_.FileOutputFlow();
166  unsigned k;
167  abstractElement* elPtr;
168  for ( k=0 ; k < elementsGallery_.size() ; k++)
169    {
170      elPtr = elementsGallery_.getElementPointerFromIndex(k);
171      outfile << elPtr->FileOutputFlow();
172    }
173  outfile.close();   
174}
175
176bool dataManager::restoreElements(string workingDir, string inputFileName)
177{
178  cout << "dataManager::restoreElements() fichier " << inputFileName << endl;
179  ifstream infile;
180   string name = workingDir + inputFileName + ".save";
181  infile.open(name.c_str(), ios::in);
182  if (!infile) {
183    cerr << " error opening input stream " << name << endl;
184    return false;
185  }
186  else cout << " successful opening input stream " << name << endl;
187
188  string globalTitle;
189  if ( infile >> globalTitle ) {
190    if ( globalTitle == string("globals") ) {
191      globParam_.raz();
192      globParam_.FileInput(infile);
193    } else {
194      cerr << " dataManager::restoreElements ERROR : global parameters seems to be missing" << endl;
195      return false;
196    }
197  } else {
198    cerr << " dataManager::restoreElements ERROR : reading data save file" << endl;
199    return false;
200  }
201
202  elementsGallery_.raz();
203  //  typedElement elem;
204 typedElement elementType;
205  string elementLabel;
206  //  while (infile >> ielem) {
207  while (infile >> elementLabel) {
208    //    elem = (typedElement)ielem;
209    //    abstractElement* nouveau = addElement(elem);
210    elementType = nomdElements::getTypeFromLabel(elementLabel);
211    abstractElement* nouveau = addElement( elementType);
212    if ( nouveau == NULL ) {
213      cerr << " dataManager::restoreElements ERROR : restoring element " << elementLabel << " failed " << endl;
214      return false;
215    }
216    nouveau->FileInput(infile);
217  } 
218  infile.close();
219
220  // debug
221  unsigned k;
222  for(k = 0; k <  elementsGallery_.size(); k++) {
223    abstractElement* ptr= elementsGallery_.getElementPointerFromIndex(k);
224    cout << "reupere " << ptr->getLabel() << endl;
225  }
226
227
228  return true;
229}
230
231particleBeam* dataManager::getDiagnosticBeam(unsigned index) 
232{
233  cout << " particleBeam* dataManager je vais dessiner " << getLabelFromElementNumero(index+1) << endl;
234
235
236  if (  index >= diagnosticBeam_.size() ) {
237      return NULL;
238    }
239    else {
240      return &diagnosticBeam_.at(index);
241    }
242}
243
244void dataManager::donneesRmsEnveloppe( string type, unsigned int numeroDeb, unsigned int numeroFin, vector<double>& xcor, vector<double>& ycor)
245{
246  unsigned k;
247  if ( numeroDeb < 1 ) numeroDeb = 1;
248  if ( numeroFin > diagnosticBeam_.size() ) numeroFin = diagnosticBeam_.size();
249
250  unsigned indexDeb = numeroDeb -1;
251  unsigned indexFin = numeroFin -1;
252
253  double longueur = 0.0;
254  double valeur = 0.0;
255  xcor.clear();
256  ycor.clear();
257  if ( type == "x" )
258    {
259      for (k= indexDeb; k <= indexFin; k++)
260        {
261          if ( !diagnosticBeam_.at(k).momentRepresentationOk() ) diagnosticBeam_.at(k).buildMomentRepresentation();
262          longueur += elementsGallery_.getElementPointerFromIndex(k)->getLenghtOfElement();
263          valeur = diagnosticBeam_.at(k).getXmaxRms();
264          xcor.push_back(longueur);
265          ycor.push_back(valeur);
266        }
267    }
268  else
269    {
270      cerr << " dataManager::donneesRmsEnveloppe type " << type << " not programmed " << endl;
271      return;
272    }
273}
274
Note: See TracBrowser for help on using the repository browser.