Sandbox.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007-2008 by Antonello Lobianco                              *
00003  *   antonello@regmas.org                                                  *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Library General Public License as       *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU Library General Public     *
00016  *   License along with this program; if not, write to the                 *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <algorithm>
00021 
00022 #include "Sandbox.h"
00023 #include "ThreadManager.h"
00024 #include <cmath>
00025 
00026 
00027 
00028 // Testing zip library...
00029 //#include "zip.h"
00030 //#include "unzip.h"
00031 //#include <QFile>
00032 //#include <QFileInfo>
00033 //#include <QString>
00034 //#include <QStringList>
00035 //#include <QList>
00036 //#include <iostream>
00037 //#include <iomanip>
00038 
00039 
00040 using namespace std;
00041 
00042 Sandbox::Sandbox(ThreadManager* MTHREAD_h){
00043     MTHREAD=MTHREAD_h;
00044 }
00045 
00046 
00047 Sandbox::~Sandbox(){
00048 
00049 }
00050 
00051 // ---------------------------------------------
00052 struct GccTest
00053 {
00054 
00055     GccTest(string name_h){
00056         nameMember = name_h;
00057     };
00058 
00059     string nameMember;
00060 
00061    operator string ()
00062    {
00063     
00064         cout << "the first function\n";
00065         cout << nameMember << endl;
00066         return "42";
00067    }
00068 
00069    operator int ()
00070    {
00071         cout << "its \"underload\"\n";
00072       return 42;
00073    }
00074 
00075     operator vector<int> ()
00076     {
00077         cout << "within vector <int>" << endl;
00078         vector <int> toReturn;
00079         toReturn.push_back(3);
00080         toReturn.push_back(4);
00081         toReturn.push_back(5);
00082         return toReturn;
00083     }
00084 
00085 };
00086 
00087 // --------------------------------------
00088 
00089 void
00090 Sandbox::test(){
00091 
00092 
00093     /*
00094     int a;
00095     a = getSetting<int>("myIntData", TYPE_INT);
00096 
00097     string b;
00098     b = getSetting<string>("myStringData", TYPE_STRING);
00099 
00100     bool c;
00101     c = getSetting<bool>("myBoolData", TYPE_BOOL);
00102 
00103 
00104     cout << "A is: " << a << endl;
00105 
00106     cout << "B is: " << b << endl;
00107 
00108     cout << "C is: " << c << endl;
00109 
00110     //vector<string> getVectorSetting <string> ("test", TYPE_STRING);
00111     //template <class T> vector <T> getVectorSetting(string name_h, int type);
00112 
00113     //vector <string> myStrings = getVectorSetting <vector<string> > ("test", TYPE_STRING);
00114 
00115     string s = GccTest("test");
00116     int i = GccTest("test");
00117     vector <int> iVector = GccTest("test");
00118 
00119     for (int i=0; i< iVector.size(); i++){
00120         cout << "iVector: " << iVector.at(i) << endl;
00121     }
00122     */
00123     
00124 
00125     /* // I learned: how to access elements - both objects and pointers - of a vector using pointers
00126     // testing how to operate with iterators over a pointer element in an array:
00127 
00128     cout << "Starting iterator test..." << endl;
00129 
00130     TestStructure a,b,c,d;
00131     a.i=0; b.i=1; c.i=2; d.i=3;
00132     TestStructure* ap;
00133     TestStructure* bp;
00134     TestStructure* cp;
00135     TestStructure* dp;
00136 
00137     ap = &a;
00138     bp = &b;
00139     cp = &c;
00140     dp = &d;
00141 
00142     vector <TestStructure>  objects;
00143     vector <TestStructure*> pointers;
00144 
00145     objects.push_back(a);
00146     objects.push_back(b);
00147     objects.push_back(c);
00148     objects.push_back(d);
00149 
00150     pointers.push_back(ap);
00151     pointers.push_back(bp);
00152     pointers.push_back(cp);
00153     pointers.push_back(dp);
00154     
00155     vector<TestStructure>::iterator pi;
00156     vector<TestStructure*>::iterator pp;
00157 
00158      //ok it works
00159     //for ( pi = objects.begin() ; pi != objects.end();){
00160     //  if(pi->i==2){
00161     //      objects.erase(pi);
00162     //  }
00163     //  else {
00164     //      ++pi;
00165     //  }
00166     //}
00167 
00168     //for (int j=0;j<objects.size();j++){
00169     //  cout << j << " object is: " << objects[j].i << endl;
00170     //}
00171     
00172 
00173     // works as well ;-))
00174     for ( pp = pointers.begin() ; pp != pointers.end();){
00175         if( (*pp)->i==2){
00176             //delete (*pp);
00177             pointers.erase(pp);
00178         }
00179         else {
00180             ++pp;
00181         }
00182     }
00183 
00184     for (int j=0;j<pointers.size();j++){
00185         cout << j << " pointers is: " << pointers[j]->i << endl;
00186     }
00187 
00188     // c is not destructed if we don't explicitelly call delete over the pointer...
00189     cout << c.i << endl; // this go in seg-frag if we call delete (*pp)..
00190     */
00191 
00192     // ------------------------------------------------------------------
00193     /* test on how to remove from a map.. deletable
00194     map <int, string> test;
00195     test.insert(pair<int, string>(2, "pippo"));
00196     test.insert(pair<int, string>(1, "pluto"));
00197     test.insert(pair<int, string>(5, "minni"));
00198     test.insert(pair<int, string>(3, "topolino"));
00199 
00200 
00201     map <int, string>::iterator p;
00202     p=test.find(3);
00203     if(p != test.end()){
00204         cout << p->second <<endl;
00205         test.erase(p);
00206     }
00207     else {
00208         cout << "not found " << endl;
00209     }
00210 
00211     map <int, string>::iterator p2;
00212     p2=test.find(3);
00213     if(p2 != test.end()){
00214         cout << p2->second <<endl;
00215         test.erase(p2);
00216     }
00217     else {
00218         cout << "not found " << endl;
00219     }
00220     */
00221 
00222     /*vector<int> test;
00223     for (int i=0;i<5;i++) test.push_back(i);
00224     cout << "test.." << endl;
00225     for (uint i=0;i<test.size();i++){
00226         cout << "Test "<<i<<": "<<test.at(i) << endl;
00227     }
00228     //test.erase(2);
00229 
00230     vector<int>::iterator p;
00231     for ( p = test.begin() ; p != test.end();){
00232         if(*p == 1 || *p == 2 || *p==4){
00233             test.erase(p);
00234         }
00235         else {
00236             ++p;
00237         }
00238     }
00239     
00240 
00241     for (uint i=0;i<test.size();i++){
00242         cout << "Test "<<i<<": "<<test.at(i) << endl;
00243     }
00244 
00245 //  test.erase(remove_if(test.begin(), test.end(), FindMatchingString(&fs))
00246 
00247 //  for (int i=0;i<test.size();i++) cout << "TEST: "<<i<< " " << test.at(i) << endl;
00248     */
00249 
00250     /*
00251     // On this test I am showing how to "move" one pointer from a vector of pointers to an other one. The real case is used to move Agent_farmer* pointers from the managedAgents vector to the removedVector.
00252 
00253     double* myDouble1 = new double(1);
00254     double* myDouble2 = new double(2);
00255     double* myDouble3 = new double(3);
00256     
00257     vector <double*> origin;
00258     vector <double*> destination;
00259 
00260     origin.push_back(myDouble1);
00261     origin.push_back(myDouble2);
00262     origin.push_back(myDouble3);
00263 
00264     cout << "MyDouble2: "<< *myDouble2 << endl;
00265     vector<double*>::iterator doublePIterator;
00266 
00267     for (int i=0;i<origin.size();i++){
00268         cout << i << " origin is: " << *origin[i] << endl;
00269     }
00270 
00271     for ( doublePIterator = origin.begin() ; doublePIterator !=origin.end();){
00272         if(*doublePIterator == myDouble2){
00273             destination.push_back(myDouble2);
00274             origin.erase(doublePIterator);
00275         }
00276         else {
00277             ++doublePIterator;
00278         }
00279     }
00280 
00281     for (int i=0;i<origin.size();i++){
00282         cout << i << " origin is now: " << *origin[i] << endl;
00283     }
00284 
00285     for (int i=0;i<destination.size();i++){
00286         cout << i << " destination is: " << *destination[i] << endl;
00287     } */
00288     
00289     // ------------------------------------------------------------------
00290     /*
00291     // Test on how to return a vector of pointers from a member vector of data
00292     TestStructure a,b,c,d;
00293     a.i=0; b.i=1; c.i=2; d.i=3;
00294     testVector.push_back(a);
00295     testVector.push_back(b);
00296     testVector.push_back(c);
00297     testVector.push_back(d);
00298 
00299     vector<TestStructure*>  myVector=getTestStructure();
00300 
00301     for(uint i=0;i<myVector.size();i++){
00302         msgOut(MSG_DEBUG, i2s(myVector[i]->i));
00303     }   
00304     */
00305 
00306     /*
00307     // Deleting an object and inserting a new one on a vector of objects.. it doesn't works.. problems with the last element..
00308     vector<BasicData>::iterator p;
00309     for(p=programSettingsVector.begin();p!=programSettingsVector.end();p++){
00310         if(p->name == SETT.name){
00311             programSettingsVector.erase(p);
00312             programSettingsVector.insert(p,1,SETT);
00313             cout << SETT.name <<endl;
00314             break;
00315         }
00316     }
00317     */
00318 
00319     /*double test = -987654321.987654321;
00320     double result;
00321     result = fabs(test);
00322     cout << "Test: "<< result << endl;*/
00323 
00324 
00325     /*
00326     // Testing the zip library:
00327 
00328     cout <<"Hello world Zip!" << endl;
00329 
00330     QString file = "data/testInput.ods";
00331     QString out = "data/tempInput";
00332     QString pwd = "";
00333     if (!QFile::exists(file))
00334     {
00335         cout << "File does not exist." << endl << endl;
00336         //return false;
00337     }
00338 
00339     UnZip::ErrorCode ec;
00340     UnZip uz;
00341 
00342     if (!pwd.isEmpty())
00343         uz.setPassword(pwd);
00344 
00345     ec = uz.openArchive(file);
00346     if (ec != UnZip::Ok)
00347     {
00348         cout << "Failed to open archive: " << uz.formatError(ec).toAscii().data() << endl << endl;
00349         //return false;
00350     }
00351 
00352     ec = uz.extractAll(out);
00353     if (ec != UnZip::Ok)
00354     {
00355         cout << "Extraction failed: " << uz.formatError(ec).toAscii().data() << endl << endl;
00356         uz.closeArchive();
00357         //return false;
00358     }
00359     */
00360 
00361     /*
00362     // How to : delete an element from an array from its position
00363     cout << "How to : delete an element from an array from its position" << endl;
00364 
00365     vector <string> headers;
00366     vector < vector <string> > records;
00367     vector <string> firstrecord;
00368     vector <string> secondrecord;
00369     records.push_back(firstrecord);
00370     records.push_back(secondrecord);
00371 
00372     headers.push_back("a");
00373     headers.push_back("b");
00374     headers.push_back("");
00375     headers.push_back("d");
00376     headers.push_back("e");
00377     headers.push_back("");
00378     
00379     records[0].push_back("0");
00380     records[0].push_back("1");
00381     records[0].push_back("2");
00382     records[0].push_back("3");
00383     records[0].push_back("4");
00384     records[0].push_back("5");
00385     records[1].push_back("00");
00386     records[1].push_back("11");
00387     records[1].push_back("22");
00388     records[1].push_back("33");
00389     records[1].push_back("44");
00390     records[1].push_back("55");
00391 
00392     for (int i=headers.size()-1;i>=0;i--){
00393         if(headers[i] == ""){
00394             headers.erase(headers.begin()+i);
00395             for (int j=0;j<records.size();j++){
00396                 records[j].erase(records[j].begin()+i);
00397             }
00398         }
00399     }
00400     for(uint i=0;i<headers.size();i++){
00401         cout << headers.at(i) << " - " << records[0].at(i) << " - " << records[1].at(i) << endl;
00402     }
00403     cout << "done!" << endl;
00404     */
00405 
00406     //testThreads();
00407     /*vector<double> numbers;
00408     double cumNumbers = 0.00;
00409     numbers.push_back(0.40);
00410     numbers.push_back(0.10);
00411     numbers.push_back(0.20);
00412     numbers.push_back(0.08);
00413     numbers.push_back(0.22);
00414     
00415     for (uint i=0;i<numbers.size();i++){
00416         cumNumbers += numbers[i];
00417     }
00418     
00419     if (cumNumbers <= 0.99999999 || cumNumbers >= 1.00000001) {
00420         cout <<"Bastardo!"<<endl;
00421     } else {
00422         cout <<"qui funzia!"<<endl;
00423     }*/
00424 
00425 }
00426 
00427 template <class T> vector <T> getVectorSetting(string name_h, int type) {
00428 
00429         vector <string> myStringDatas;
00430         myStringDatas.push_back("aaaaa");
00431         myStringDatas.push_back("bbbbb");
00432         myStringDatas.push_back("ccccc");   
00433         vector <T> xVector;
00434 
00435         for (int i=0;i<myStringDatas.size();i++){
00436             istringstream iss(myStringDatas[i]);
00437             T x;
00438             iss >> x;
00439             xVector.push_back(x);
00440         }
00441 
00442         return xVector;
00443 }
00444 
00445 
00446 
00447 
00448 template <class T> T
00449 Sandbox::getSetting(string name_h, int type){
00450 
00451     string myIntData;
00452     myIntData = "34";
00453     string myStringData;
00454     myStringData = "abcdefg";
00455 
00456     string myBoolData;
00457     myBoolData = "false";
00458     
00459     if(type==TYPE_INT){
00460         istringstream iss(myIntData);
00461         T x;
00462         iss >> x;
00463         return x;
00464     }
00465 
00466     if(type==TYPE_STRING){
00467         istringstream iss(myStringData);
00468         T x;
00469         iss >> x;
00470         return x;
00471     }
00472 
00473     if(type==TYPE_BOOL){
00474         string tempBoolString;
00475         if (myBoolData == "1" || myBoolData == "true" || myBoolData == "True" || myBoolData == "TRUE" || myBoolData == "vero" || myBoolData == "Vero"|| myBoolData == "VERO"){
00476             tempBoolString = "1";
00477         }
00478         else if (myBoolData == "0" || myBoolData == "false" || myBoolData == "False" || myBoolData == "FALSE" || myBoolData == "falso" || myBoolData == "falso"|| myBoolData == "FALSO"){
00479             tempBoolString = "0";
00480         }
00481         else {
00482             msgOut(MSG_CRITICAL_ERROR, "Impossible conversion of "+myBoolData+" to bool!. Aborted.");
00483         }
00484         istringstream iss(tempBoolString);
00485         T x;
00486         iss >> x;
00487         return x;
00488     }
00489 
00490 
00491 }
00492 
00493 template<typename T> T 
00494 Sandbox::test2(const std::string& s) {
00495   std::istringstream iss(s);
00496   T x;
00497   iss >> x;
00498   return x;
00499 }
00500 
00501 
00502 vector <TestStructure*>
00503 Sandbox::getTestStructure(){
00504     vector <TestStructure*> toReturn;
00505     for (uint i=0;i<testVector.size();i++){
00506         //TestStructure* tempTest = new TestStructure;
00507         toReturn.push_back(&testVector[i]);
00508     }
00509     return toReturn;
00510 
00511 }
00512 
00513 
00514 
00515 void
00516 Sandbox::testThreads(){
00517 
00518                 /*
00519                 PSEUDOCODE
00520                 - attivo i vari thread
00521                 - per ogni closestAgent itero fra i vari thread e se "รจ libero" gli assegno il closestAgent
00522                 - quando ho finito i closestAgent aspetto che tutti i threads abbiano finito il lavoro
00523                 - chiudo i threads
00524                 - vado avanti
00525                 */
00526                 int nAgents= 50;
00527                 vector<TestStructure*> myAgents;
00528                 vector<double> myResults (nAgents, (double) 0);
00529                 //int nThreads = MTHREAD->RD->getIntSetting("nThreads");
00530                 int nThreads= 5;
00531 
00532                 for (int i=0; i < nAgents; i++){
00533                     TestStructure* myAgent = new TestStructure;
00534                     myAgent->i = i;
00535                     myAgent->random =  (0+( (double)rand() / ((double)(RAND_MAX)+(double)(1)) )*(10-0+1))/ (double)100;
00536                     myAgents.push_back(myAgent);
00537                 }
00538 
00539                 vector <testThread*> myThreads ;
00540 
00541                 for (int i=0; i < nThreads; i++){
00542                     testThread* myThread = new testThread;
00543                     myThreads.push_back(myThread);
00544                 }
00545 
00546                 for (uint i=0;i<myAgents.size();i++){
00547                     bool assigned = false;
00548                     while(!assigned) {
00549                         for (uint j=0;j<myThreads.size();j++){
00550                             if (!myThreads[j]->isRunning()){
00551                                 cout << "Assigning agent " << i << " to thread " << j << endl;
00552                                 myThreads[j]->assignJob(myAgents[i]);
00553                                 myThreads[j]->start();
00554                                 assigned = true;
00555                                 break;
00556                             }
00557                             else {
00558                                 cout << "Thread " << j << " is busy" << endl;
00559                             }
00560                         }       
00561                     }   
00562                 }
00563                 /*
00564                 volatile bool somethingStopping = true;
00565                 while (somethingStopping){
00566                     somethingStopping = false;
00567                     for (uint i=0;i<myThreads.size();i++){
00568                         if(myThreads[i]->isRunning()){
00569                             somethingStopping = true;
00570                             //cout << "somethingStopping is true" << endl;
00571                         }
00572                     }
00573                 }
00574 
00575                 if (somethingStopping) {
00576                     cout << "somethingStopping is true" << endl;
00577                 }
00578                 else {
00579                     cout << "somethingStopping is false" << endl;
00580                 }
00581                 cout << "pinco pallo sono nel mezzo dei threads..."<<endl;
00582                 */
00583                 for (int i=0; i < nThreads; i++){
00584                     myThreads[i]->wait();
00585                 }
00586 
00587 
00588                 for (int i=0; i < nThreads; i++){
00589                     delete myThreads[i];
00590                 }
00591 
00592                 for (uint i=0;i<myAgents.size();i++){
00593                     //cout <<myAgents[i]->cachedOffer<<endl;
00594 
00595                     double random =  (0+( (double)rand() / ((double)(RAND_MAX)+(double)(1)) )*(10-0+1))/ (double)100;
00596                     //cout <<random<<endl;
00597                 }
00598 
00599                 //thread1.stop();
00600                 cout << "FINITO"<<endl;
00601                 
00602 
00603 }
00604 
00605 testThread::testThread(){
00606 
00607 }
00608 
00609 void
00610 testThread::run(){
00611 
00612     cout << agent->i << endl;
00613 
00614     double randChange =  (0+( (double)rand() / ((double)(RAND_MAX)+(double)(1)) )*(10-0+1))/ (double)100; //rand() must be not thread safe !!!!
00615 
00616     int justn = 10000;
00617     vector <double> takeTimeVector (justn, 0);
00618     for (int i =0; i< justn;i++){
00619         takeTimeVector.at(i)=i*2;
00620     }
00621     agent->cachedOffer = agent->random;
00622 }
00623 
00624 void
00625 testThread::assignJob(TestStructure* agent_h){
00626     agent = agent_h;
00627     agent->cachedOffer = 0;
00628 }
00629