/sources/conservative/manager.cpp

https://github.com/Dadomba/IGEM_2011_ENSPS_Final_Folder · C++ · 341 lines · 257 code · 47 blank · 37 comment · 53 complexity · 607a0e1674a15f14766fbfcee21b3b04 MD5 · raw file

  1. #include "manager.h"
  2. Manager::Manager(MainWindow *target_ui){
  3. this->ui = target_ui;
  4. this->species_list=NULL;
  5. this->species_list_size=0;
  6. this->reaction_list=NULL;
  7. this->reaction_list_size=0;
  8. // //TEST species
  9. // Species A("A",0);
  10. // Species B("B",0);
  11. // Species C("C",1);
  12. // Species D("D",0);
  13. // Species E("E",0);
  14. // Species F("F",1);
  15. // Species X("X",0);
  16. // Species Y("Y",1);
  17. // Species tab_1[2];
  18. // Species tab_2[2];
  19. // tab_1[0] = A;
  20. // tab_1[1] = B;
  21. // tab_2[0] = C;
  22. // tab_2[1] = D;
  23. // // Instanciation des 3 blocs
  24. // Inh inh_1("inh_1", "inh_1","inh_1",D,F);
  25. // Complex cplx_1("cplx_1", "cplx_1","cplx_1",tab_1,2,X);
  26. // Synth synth_1("synth_1", "synth_1","synth_1",tab_1, tab_2, 2, 2,Y);
  27. // inh_1.file_creation();
  28. // cplx_1.file_creation();
  29. // synth_1.file_creation();
  30. // p_Reaction* test = new p_Reaction[3];
  31. // test[0] = &inh_1;
  32. // test[1] = &synth_1;
  33. // test[2] = &cplx_1;
  34. // System syst_1(test,3,"test","test","test");
  35. // syst_1.test_bench_generation();
  36. connect(this->ui,SIGNAL(addSpeciesRequestFromUi(string,float)),this,SLOT(speciesAddRequest(string,float)));
  37. connect(this,SIGNAL(speciesAddingSuccess(bool,string,float)),this->ui,SLOT(speciesAddingResult(bool,string,float)));
  38. connect(this->ui,SIGNAL(removeSpeciesRequestFromUi(string)),this,SLOT(speciesRemoveRequest(string)));
  39. connect(this,SIGNAL(speciesRemovingSuccess(bool,string)),this->ui,SLOT(speciesRemovingResult(bool,string)));
  40. connect(this->ui,SIGNAL(modifySpeciesRequestFromUi(string,float)),this,SLOT(speciesModifyRequest(string,float)));
  41. connect(this,SIGNAL(speciesModifiingSuccess(bool,string,float)),this->ui,SLOT(speciesModifiingResult(bool,string,float)));
  42. connect(this->ui,SIGNAL(addReactionRequestFromUi(QString,int,QStringList,QString,QStringList)),this,SLOT(reactionAddRequest(QString,int,QStringList,QString,QStringList)));
  43. connect(this,SIGNAL(reactionAddingSuccess(bool,QString,int,QStringList,QString,QStringList)),this->ui,SLOT(reactionAddingResult(bool,QString,int,QStringList,QString,QStringList)));
  44. connect(this->ui,SIGNAL(removeReactionRequestFromUi(string)),this,SLOT(reactionRemoveRequest(string)));
  45. connect(this,SIGNAL(reactionRemovingSuccess(bool,string)),this->ui,SLOT(reactionRemovingResult(bool,string)));
  46. connect(this->ui,SIGNAL(createSystemRequestFromUi(QString,QStringList)),this,SLOT(systemCreationRequest(QString,QStringList)));
  47. connect(this,SIGNAL(systemCreationSuccess(bool,QString)),this->ui,SLOT(systemCreationResult(bool,QString)));
  48. }
  49. Species* Manager::newTabAllocation(int size){
  50. Species *new_tab = new Species[size];
  51. if(new_tab == NULL){
  52. std::cerr<<"Dynamic allocation error"<<std::endl;
  53. exit(-1);
  54. }
  55. else{
  56. return new_tab;
  57. }
  58. }
  59. p_Reaction* Manager::newTabAllocation_react(int size){
  60. p_Reaction *new_tab = new p_Reaction[size];
  61. if(new_tab == NULL){
  62. std::cerr<<"Dynamic allocation error"<<std::endl;
  63. exit(-1);
  64. }
  65. else{
  66. return new_tab;
  67. }
  68. }
  69. //*********************************************//
  70. // Species Part //
  71. //*********************************************//
  72. bool Manager::addSpeciesToList(string name, float initial_value){
  73. if(this->isSpeciesInList(name)){
  74. std::cerr<<"Species already exists in the list"<<std::endl;
  75. return false;
  76. }
  77. else{
  78. Species *new_tab= this->newTabAllocation(this->species_list_size+1);
  79. for(int i = 0; i<this->species_list_size; i++)
  80. {
  81. new_tab[i] = this->species_list[i];
  82. }
  83. new_tab[this->species_list_size] = Species(name,initial_value);
  84. this->species_list_size++;
  85. delete[] this->species_list;
  86. this->species_list = new_tab;
  87. return true;
  88. }
  89. }
  90. bool Manager::removeSpeciesFromList(string name){
  91. if(this->isSpeciesInList(name)){
  92. Species *new_tab= this->newTabAllocation(this->species_list_size-1);
  93. int j=0;
  94. for(int i=0;i<species_list_size;i++){
  95. if(this->species_list[i].Get_name() != name){
  96. new_tab[j]=this->species_list[i];
  97. j++;
  98. }
  99. }
  100. this->species_list_size--;
  101. this->species_list = new_tab;
  102. return true;
  103. }
  104. else{
  105. std::cerr<<"Species does not exists in the list"<<std::endl;
  106. return false;
  107. }
  108. }
  109. bool Manager::modifySpeciesToList(string name, float initial_value){
  110. if(this->isSpeciesInList(name)){
  111. for(int i=0;i<species_list_size;i++){
  112. if(this->species_list[i].Get_name() == name){
  113. this->species_list[i].Set_initial_state(initial_value);
  114. }
  115. }
  116. return true;
  117. }
  118. else{
  119. std::cerr<<"Species does not exists in the list"<<std::endl;
  120. return false;
  121. }
  122. }
  123. bool Manager::isSpeciesInList(string name){
  124. for(int i =0;i<this->species_list_size;i++){
  125. if(this->species_list[i].Get_name() == name){
  126. return true;
  127. }
  128. }
  129. return false;
  130. }
  131. void Manager::speciesAddRequest(string name, float initial_value){
  132. bool res = this->addSpeciesToList(name,initial_value);
  133. emit speciesAddingSuccess(res,name,initial_value);
  134. }
  135. void Manager::speciesRemoveRequest(string name){
  136. bool res = this->removeSpeciesFromList(name);
  137. emit speciesRemovingSuccess(res,name);
  138. }
  139. void Manager::speciesModifyRequest(string name, float initial_value){
  140. bool res = this->modifySpeciesToList(name,initial_value);
  141. emit speciesModifiingSuccess(res,name,initial_value);
  142. }
  143. //*********************************************//
  144. // Reaction Part //
  145. //*********************************************//
  146. bool Manager::addReactionToList(Reaction *react_to_add){
  147. if(this->isReactionInList(react_to_add->get_reaction_name())){
  148. std::cerr<<"Reaction already exists in the list"<<std::endl;
  149. return false;
  150. }
  151. else{
  152. p_Reaction *new_tab = this->newTabAllocation_react(this->reaction_list_size+1);
  153. for(int i = 0; i<this->reaction_list_size; i++)
  154. {
  155. new_tab[i] = this->reaction_list[i];
  156. }
  157. new_tab[this->reaction_list_size] = react_to_add;
  158. this->reaction_list_size++;
  159. delete[] this->reaction_list;
  160. this->reaction_list = new_tab;
  161. return true;
  162. }
  163. }
  164. bool Manager::removeReactionFromList(string name){
  165. if(this->isReactionInList(name)){
  166. p_Reaction *new_tab= this->newTabAllocation_react(this->reaction_list_size-1);
  167. int j=0;
  168. for(int i=0;i<reaction_list_size;i++){
  169. if(this->reaction_list[i]->get_reaction_name() != name){
  170. new_tab[j]=this->reaction_list[i];
  171. j++;
  172. }
  173. }
  174. this->reaction_list_size--;
  175. this->reaction_list = new_tab;
  176. return true;
  177. }
  178. else{
  179. std::cerr<<"Species does not exists in the list"<<std::endl;
  180. return false;
  181. }
  182. }
  183. bool Manager::isReactionInList(string name){
  184. for(int i =0;i<this->reaction_list_size;i++){
  185. if(this->reaction_list[i]->get_reaction_name() == name){
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. void Manager::reactionAddRequest(QString name, int type, QStringList speciesin, QString speciesout, QStringList speciesoptional){
  192. switch(type){
  193. case CST_COMPLEXATION:{
  194. Species *tab = NULL;
  195. Species out;
  196. int tab_size = 0;
  197. for(int i=0;i<speciesin.size();i++){
  198. for(int j=0;j<species_list_size;j++){
  199. if(species_list[j].Get_name() == speciesin.at(i).toStdString()){
  200. Species *new_tab = new Species[tab_size+1];
  201. for(int n=0;n<tab_size;n++){
  202. new_tab[n].Set_name(tab[n].Get_name());
  203. new_tab[n].Set_initial_state(tab[n].Get_initial_state());
  204. }
  205. new_tab[tab_size] = species_list[i];
  206. tab = new_tab;
  207. tab_size++;
  208. }
  209. }
  210. }
  211. for(int j=0;j<species_list_size;j++){
  212. if(species_list[j].Get_name() == speciesout.toStdString()){
  213. out = species_list[j];
  214. break;
  215. }
  216. }
  217. Complex *tmp = new Complex("E_" + name.toStdString(),name.toStdString(),name.toStdString(),tab, speciesin.size(),out);
  218. bool res = addReactionToList(tmp);
  219. emit reactionAddingSuccess(res, name, CST_COMPLEXATION, speciesin, speciesout);
  220. break;
  221. }
  222. case CST_SYNTHESIS:{
  223. Species *tab_act = NULL;
  224. int tab_act_size = 0;
  225. for(int i=0;i<speciesin.size();i++){
  226. for(int j=0;j<species_list_size;j++){
  227. if(species_list[j].Get_name() == speciesin.at(i).toStdString()){
  228. Species *new_tab = new Species[tab_act_size+1];
  229. for(int n=0;n<tab_act_size;n++){
  230. new_tab[n].Set_name(tab_act[n].Get_name());
  231. new_tab[n].Set_initial_state(tab_act[n].Get_initial_state());
  232. }
  233. new_tab[tab_act_size] = species_list[i];
  234. tab_act = new_tab;
  235. tab_act_size++;
  236. }
  237. }
  238. }
  239. Species *tab_rep = NULL;
  240. int tab_rep_size = 0;
  241. for(int i=0;i<speciesoptional.size();i++){
  242. for(int j=0;j<species_list_size;j++){
  243. if(species_list[j].Get_name() == speciesoptional.at(i).toStdString()){
  244. Species *new_tab_2 = new Species[tab_rep_size+1];
  245. for(int n=0;n<tab_rep_size;n++){
  246. new_tab_2[n].Set_name(tab_rep[n].Get_name());
  247. new_tab_2[n].Set_initial_state(tab_rep[n].Get_initial_state());
  248. }
  249. new_tab_2[tab_rep_size] = species_list[j];
  250. tab_rep = new_tab_2;
  251. tab_rep_size++;
  252. }
  253. }
  254. }
  255. Species out;
  256. for(int j=0;j<species_list_size;j++){
  257. if(species_list[j].Get_name() == speciesout.toStdString()){
  258. out = species_list[j];
  259. break;
  260. }
  261. }
  262. Synth *tmp = new Synth("E_" + name.toStdString(),name.toStdString(),name.toStdString(), tab_act, tab_rep, tab_act_size, tab_rep_size, out);
  263. bool res = addReactionToList(tmp);
  264. emit reactionAddingSuccess(res, name, CST_SYNTHESIS, speciesin, speciesout, speciesoptional);
  265. break;
  266. }
  267. }
  268. }
  269. void Manager::reactionRemoveRequest(string name){
  270. bool res = this->removeReactionFromList(name);
  271. emit reactionRemovingSuccess(res,name);
  272. }
  273. //*********************************************//
  274. // System Part //
  275. //*********************************************//
  276. void Manager::systemCreationRequest(QString name, QStringList reactions){
  277. p_Reaction reaction_in_system_tab[reactions.size()];
  278. int reaction_in_system_tab_size=reactions.size();
  279. for(int i=0; i<reactions.size();i++){
  280. for(int j=0;j<reaction_list_size;j++){
  281. if(reaction_list[j]->get_reaction_name() == ("E_"+reactions.at(i)).toStdString()){
  282. reaction_in_system_tab[i] = reaction_list[j];
  283. }
  284. }
  285. }
  286. for(int i=0;i<reaction_in_system_tab_size;i++){
  287. reaction_in_system_tab[i]->filereact_creation();
  288. reaction_in_system_tab[i]->fileresandcap_creation();
  289. reaction_in_system_tab[i]->filepkg_creation();
  290. }
  291. System new_system(reaction_in_system_tab,reaction_in_system_tab_size,"E_"+name.toStdString(),name.toStdString(),name.toStdString());
  292. bool res = new_system.test_bench_generation();
  293. emit systemCreationSuccess(res,name);
  294. }