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

Last change on this file since 336 was 336, checked in by garnier, 11 years ago

bug #43 fixed

File size: 10.1 KB
Line 
1#include "dataManager.h"
2#include "mathematicalConstants.h"
3#include "PhysicalConstants.h"
4#include "softwareParmela.h"
5#include "softwareTransport.h"
6#include "GWt_pspaApplication.h"
7#include "GWt_console.h"
8#include "softwareGenerator.h"
9#include "softwareTest.h"
10
11#include <stdio.h>
12#include <fstream>
13//#include <algorithm>
14
15
16dataManager::dataManager(PspaApplication* pspa) :
17currentBeam_(NULL)
18,workingDir_("")
19,pspa_ ( pspa)
20{
21}
22
23
24dataManager::~dataManager() {
25    unsigned k;
26    for (k=0; k < jobList_.size();k++) {
27        if ( jobList_[k] != NULL ) delete jobList_[k];
28    }
29    if ( currentBeam_ == NULL ) delete currentBeam_;
30}
31
32void dataManager::consoleMessage(string message) {
33   GWt_console* console = static_cast<GWt_console*> (wApp->findWidget ("console"));
34   if (console) console->addConsoleMessage(message);
35   pspa_->processEvents();
36}
37
38string dataManager::getLabelFromElementNumero(int numero)
39{
40    abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(numero-1);
41    if ( ptr == NULL ) return "";
42    return ptr->getLabel();
43}
44
45
46int dataManager::getNumeroFromElementLabel(string label){
47    int index = -1;
48    unsigned int k;
49   
50    for (k = 0; k < getBeamLineSize() ; k++)
51    {
52        if (pspa_->getBeamLine()->getAbstractElement(k) != NULL){
53            if ( pspa_->getBeamLine()->getAbstractElement(k)->getLabel() == label )
54            {
55                index = (int)k + 1;
56                return index;
57            }
58        }
59    }
60    return index;
61}
62
63
64abstractElement* dataManager::getElementPointerFromNumero(int k){
65    return pspa_->getBeamLine()->getAbstractElement(k-1);
66}
67
68
69void dataManager::addSectionToExecute(int debut, int fin, nomDeLogiciel prog)
70{
71    jobList_.push_back(new sectionToExecute);
72    jobList_.back()->firstElement = debut;
73    jobList_.back()->lastElement = fin;
74    jobList_.back()->software  = prog;
75}
76
77void dataManager::clearSectionToExecute()
78{
79    unsigned k;
80    for(k = 0; k < jobList_.size(); k++)
81    {
82        if ( jobList_[k] != NULL ) delete jobList_[k];
83    }
84    jobList_.clear();
85}
86
87trivaluedBool dataManager::checkExecute(string& diagnostic)
88{
89   cout << "dataManager::checkExecute()" << endl;
90   consoleMessage(" VERIFICATION AVANT EXECUTION ");
91   //   console_ = static_cast<GWt_console*> (wApp->findWidget ("console"));
92   //   if (console_) console_->addConsoleMessage(" VERIFICATION AVANT EXECUTION ");
93
94  trivaluedBool resul = ok;
95  unsigned k,j;
96  diagnostic.clear();
97  unsigned numeroDeb, numeroFin;
98  cout << "controle execution : " << endl;
99  for(k = 0; k < jobList_.size(); k++) {
100    numeroDeb = jobList_[k]->firstElement;
101    numeroFin = jobList_[k]->lastElement;
102    if ( numeroFin  < numeroDeb )
103      {
104        diagnostic += " last element before first  for job " + (jobList_[k]->software).getString() + " \n";
105        resul = error;
106        break;
107      }
108    cout << " numeroDeb= " << numeroDeb << " numeroFin= " << numeroFin << endl;
109    // indexDeb--;
110    // indexFin--;
111   
112    abstractElement* elPtr;
113    string checkCompatibility;
114    resul = checkElementsForExec(jobList_[k]->software, numeroDeb, numeroFin, checkCompatibility);
115    diagnostic += checkCompatibility;
116  }
117  cout << " data manager renvoie resul = " << resul << endl;
118  return resul;
119}
120
121
122trivaluedBool dataManager::checkElementsForExec(nomDeLogiciel logiciel, unsigned numeroDeb, unsigned numeroFin, string& diagnostic)
123{
124    trivaluedBool resul = ok;
125    diagnostic.clear();
126    trivaluedBool accepted;
127    abstractElement* elPtr;
128    for(unsigned j = numeroDeb; j <= numeroFin; j++) {
129        elPtr = pspa_->getBeamLine()->getAbstractElement(j-1);
130        accepted = elPtr->is_accepted_by_software(logiciel);
131        if(accepted  == error  ) {
132            diagnostic += " the element " + elPtr->getNomdElement().getElementName() + " is not allowed with " + logiciel.getString() + " \n";
133            resul = error;
134        } else if ( accepted  == warning ) {
135            diagnostic += " the element " + elPtr->getNomdElement().getElementName() + " will be ignored by " + logiciel.getString() + " \n";
136            if ( resul != error ) resul = warning;
137        }
138    }
139    return resul;
140}
141
142
143
144
145void dataManager::initializeExecution()
146{
147    if (workingDir_ == "") {
148        return;
149    }
150    removeFile(workingDir_ + "parmdesz");
151    removeFile(workingDir_ + "parmin");
152    removeFile(workingDir_ + "parin.input0");
153    removeFile(workingDir_ + "transport.input");
154    removeFile(workingDir_ + "transport.output");
155    removeFile(workingDir_ + "generator.in");
156    removeFile(workingDir_ + "faisceau.ini");
157    removeFile(workingDir_ + "generator.output");
158    diagnosticBeam_.clear();
159    currentBeam_ = NULL;
160    clearSectionToExecute();
161}
162
163void dataManager::removeFile(string nameOfFile)
164{
165    ifstream fileExists;
166    fileExists.open(nameOfFile.c_str(), ios::in);
167    if (fileExists) {
168        fileExists.close();
169        remove(nameOfFile.c_str());
170    }
171}
172
173bool dataManager::executeAll()
174{
175    bool success = true;
176    //    resul.clear();
177    //    string resultatPartiel;
178    unsigned k;
179    abstractSoftware* softw =NULL;
180   
181    for(k = 0; k < jobList_.size(); k++)
182    {
183      int debut = jobList_[k]->firstElement;
184      int fin = jobList_[k]->lastElement;
185      cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->software).getString() << endl;
186
187      softw = createSoftwareConnexion(jobList_[k]->software );
188
189      if ( softw == NULL ) {
190        success = false;
191        consoleMessage("dataManager::executeAll : unknown software");
192        //      resultatPartiel =  " unknown software -- STOP \n ";     
193        break;
194      }
195
196      success = softw->createInputFile(currentBeam_, debut, fin, workingDir_);
197      if ( success ) {
198        success = softw->execute(debut,fin,workingDir_);
199        if ( success ) {
200          success = softw->buildBeamAfterElements(debut, fin, diagnosticBeam_, workingDir_);
201        }
202      }
203      delete softw;
204      if ( success ) {
205        currentBeam_ = &diagnosticBeam_.back();
206        cout << " execute termine avec succes " << endl;
207      } else {
208        currentBeam_ = NULL;
209        cout << " execute termine en ECHEC " << endl;
210      }
211      if ( !success ) break;
212    }
213    return success;
214}
215
216
217void dataManager::saveConfiguration( string nameOfFile)
218{
219    ofstream outfile;
220    string name = workingDir_ + nameOfFile + ".save";
221    outfile.open(name.c_str(), ios::out);
222    if (!outfile) {
223        cerr << " error opening output file for persistency " << name << endl;
224    }
225   
226    outfile << globParam_.FileOutputFlow();
227    unsigned k;
228    abstractElement* elPtr;
229    for ( k=0 ; k < getBeamLineSize() ; k++)
230    {
231        elPtr = pspa_->getBeamLine()->getAbstractElement(k);
232        outfile << elPtr->FileOutputFlow();
233    }
234    outfile.close();
235}
236
237
238bool dataManager::restoreElements( string inputFileName)
239{
240    cout << "dataManager::restoreElements() fichier " << inputFileName << endl;
241    ifstream infile;
242    string name = inputFileName;
243    infile.open(name.c_str(), ios::in);
244    if (!infile) {
245        cerr << " error opening input stream " << name << endl;
246        return false;
247    }
248    else cout << " successful opening input stream " << name << endl;
249   
250    string globalTitle;
251    if ( infile >> globalTitle ) {
252        if ( globalTitle == string("globals") ) {
253            globParam_.raz();
254            globParam_.FileInput(infile);
255        } else {
256            cerr << " dataManager::restoreElements ERROR : global parameters seems to be missing" << endl;
257            return false;
258        }
259    } else {
260        cerr << " dataManager::restoreElements ERROR : reading data save file" << endl;
261        return false;
262    }
263   
264    pspa_->getBeamLine()->clear();
265   
266    typedElement elementType;
267    string elementLabel;
268    while (infile >> elementLabel) {
269        elementType = nomdElements::getTypeFromLabel(elementLabel);
270       
271        GWt_abstractElement* nouveau = pspa_->getBeamLine()->addElement(elementType);
272       
273        if ( nouveau == NULL ) {
274            cerr << " dataManager::restoreElements ERROR : restoring element " << elementLabel << " failed " << endl;
275            return false;
276        }
277        nouveau->FileInput(infile);
278    }
279    infile.close();
280
281    // debug
282    unsigned k;
283    for(k = 0; k < getBeamLineSize(); k++) {
284        abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(k);
285       
286        cout << "reupere " << ptr->getLabel() << endl;
287    }
288    return true;
289}
290
291
292particleBeam* dataManager::getDiagnosticBeam(unsigned index)
293{
294   
295   
296    if (  index >= diagnosticBeam_.size() ) {
297        return NULL;
298    }
299    else {
300        return &diagnosticBeam_.at(index);
301    }
302}
303
304
305void dataManager::donneesRmsEnveloppe( string type, unsigned int numeroDeb, unsigned int numeroFin, vector<double>& xcor, vector<double>& ycor)
306{
307    unsigned k;
308    if ( numeroDeb < 1 ) numeroDeb = 1;
309    if ( numeroFin > diagnosticBeam_.size() ) numeroFin = diagnosticBeam_.size();
310       
311    double longueur = 0.0;
312    double valeur = 0.0;
313    xcor.clear();
314    ycor.clear();
315    if ( type == "x" )
316    {
317        for (k= numeroDeb; k <= numeroFin; k++)
318        {
319            if ( !diagnosticBeam_.at(k-1).momentRepresentationOk() ) diagnosticBeam_.at(k-1).buildMomentRepresentation();
320            longueur += pspa_->getBeamLine()->getAbstractElement(k-1)->getLenghtOfElement();
321            valeur = diagnosticBeam_.at(k-1).getXmaxRms();
322            xcor.push_back(longueur);
323            ycor.push_back(valeur);
324        }
325    }
326    else
327    {
328        cerr << " dataManager::donneesRmsEnveloppe type " << type << " not programmed " << endl;
329        return;
330    }
331}
332
333int dataManager::getBeamLineSize() {
334    return pspa_->getBeamLine()->getBeamLineSize();
335}
336
337abstractSoftware* dataManager::createSoftwareConnexion(nomDeLogiciel logi) {
338
339      string inputFileName;
340      if(logi == nomDeLogiciel::parmela) {
341        inputFileName = "parmin";
342        return new softwareParmela(inputFileName, &globParam_, this);
343      } else if (logi == nomDeLogiciel::transport) {
344        inputFileName = "transport.input";
345        return new softwareTransport(inputFileName, &globParam_, this);
346      } else if (logi == nomDeLogiciel::generator) {
347        inputFileName = "generator.in";
348        return new softwareGenerator(inputFileName, &globParam_, this);
349      } else if (logi == nomDeLogiciel::test) {
350        return new softwareTest(inputFileName, &globParam_, this);
351      } else {
352        return NULL;
353      }
354
355}
Note: See TracBrowser for help on using the repository browser.