/****************************************************************** * * * Nest version 2 - 3D Wasp Nest Simulator * * Copyright (C) 1997 Sylvain GUERIN * * LIASC, ENST Bretagne & Santa Fe Institute * * * ****************************************************************** E-mail: Sylvain.Guerin@enst-bretagne.fr or: Sylvain Guerin 13 rue des Monts Lorgeaux, 51460 L'EPINE, FRANCE ****************************************************************** This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ****************************************************************** Name : MainInt.c Component : main interface Fonctionality : contains methods for objects refering to the main interface ******************************************************************/ #include "nest2.h" #include "MainInt.h" #include "ViewInt.h" #include "file.h" #include "TitleInt.h" #include "config.h" #ifdef XPM_INC_X11 # include #else # include #endif #include #include #include #include #include "WaspIcon.xpm" #include "WaspIconBW.xpm" void MainInterface::manageGeneticAlarm () { alarm (0); if (geneticRunning) { if (!geneticSimulation->isRunning) { /* la simu 'sait' que c'est fini, mais pas l'interface */ geneticRunning=0; } else { geneticSimulation->simulationControl (); } } else { if (deferedSimuList->getNumber () > 0) { /* s'il y a des simulations en differe, forcement l'interface d'algos genetiques est lancee, voila, c'est tout ce que j'ai a dire et puis c'est tout */ /* printf ("je regarde des fois que y'aurait des simus a lancer\n"); */ geneticInterface->testAndManageDeferedSimulations (); } } alarm (3); signal (SIGALRM, manage_genetic_alarm); } void MainInterface::manageWindow () { XEvent report; int end=0; actionType action; long mask; mask = ExposureMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask; while (!end) { /* modif gorette vite faite, il faut envoyer des signaux de temps a autres */ /* voir message 3 de nestum.X11 */ for (;;) { alarm (3); signal (SIGALRM, manage_genetic_alarm); XMaskEvent (display, mask, &report); alarm (0); if (report.type == Expose || report.type == ButtonPress || report.type == ButtonRelease || report.type == KeyPress || report.type == KeyRelease ) break; } manageGeneticAlarm (); alarm (0); if (report.type == Expose || isFileSelectorInterfaceDisplayed == 0) { if (exitButton->manageEvent (report) == buttonPressed) end = manageExit (); if (configButton->manageEvent (report) == buttonPressed) manageOptions (); if (netButton->manageEvent (report) == buttonPressed) manageNet (); if (geneticButton->manageEvent (report) == buttonPressed) manageGenetic (); if (infosButton->manageEvent (report) == buttonPressed) { if (!isTitleInterfaceDisplayed) { titleInterface = new TitleInterface (display, width, height); isTitleInterfaceDisplayed = 1; } else { displayDialogBox (this, "Error", "Nest2", "Infos window already opened.", "Continue"); } } if (loadButton->manageEvent (report) == buttonPressed) manageLoadOpen (); if (saveButton->manageEvent (report) == buttonPressed) manageSave (); if (loadStepButton->manageEvent (report) == buttonPressed) manageLoadStep (); if (cleanButton->manageEvent (report) == buttonPressed) manageClean (); if (editButton->manageEvent (report) == buttonPressed) manageEdit (); if (createButton->manageEvent (report) == buttonPressed) manageCreate (); if (simulationButton->manageEvent (report) == buttonPressed) manageSimulation (); if (viewButton->manageEvent (report) == buttonPressed) manageView (); if (evaluateButton->manageEvent (report) == buttonPressed) manageEvaluate (); if (parametersButton->manageEvent (report) == buttonPressed) manageSimuParameters (); simulationsArrayTitle->manageEvent (report); action = simulationsArray->manageEvent (report); if (action == selectionChanged) { /* Change selection for selection simulationsArray->getSelectedLine () */ displayPreview (-1); } else if (action == doubleClick) { displayPreview (-1); } if (previewWindow->manageEvent (report) == mustBeRefreshed) { /* et hop, on se reaffiche la preview */ displayPreview (-1); } dialogPanel->manageEvent (report); dialogText1->manageEvent (report); dialogText2->manageEvent (report); dialogText3->manageEvent (report); dialogText4->manageEvent (report); dialogText5->manageEvent (report); dialogText6->manageEvent (report); if (isTitleInterfaceDisplayed) { if (titleInterface->manageInterface (report) == endInterface) { delete titleInterface; isTitleInterfaceDisplayed = 0; } } if (netInterfaceDisplayed == 1) { if (netInterface->manageInterface(report) == endInterface) { delete netInterface; netInterfaceDisplayed = 0; } } if (geneticInterfaceDisplayed == 1) { if (geneticInterface->manageInterface(report) == endInterface) { delete geneticInterface; geneticInterfaceDisplayed = 0; } } if (viewInterfacesDisplayedNumber > 0) { int i; for (i=0; imanageInterface (report) == endInterface) { viewInterfacesDisplayedNumber--; simulations[viewSimulationIndex[i]]->isDisplayed = BFalse; simulations[viewSimulationIndex[i]]->isPreviewed = BTrue; viewSimulationIndex[i] = -1; delete viewInterface[i]; displayPreview (-1); } } } } if (evalInterfacesDisplayedNumber > 0) { int i; Simulation* simu; Graph* graph; FitnessType fitness; for (i=0; imanageInterface (report); if (action == endInterface) { evalInterfacesDisplayedNumber--; //simulations[evalSimulationIndex[i]]->isDisplayed = BFalse; evalSimulationIndex[i] = -1; delete evalInterface[i]; } else if (action == simpleSignal) { /* mettre eventuellement a jour la nouvelle fitness */ simu = simulations[evalSimulationIndex[i]]; graph = simu->simulationArchitecture->graph; fitness = (FitnessType)fitnessTypeEnum.defaultValue (); if ((fitness == Complexity) && (graph->isComplexityComputed)) { simu->mark = simu->evaluate (Complexity); simu->isSaved = BFalse; } else if ((fitness == Coherence) && (graph->isCoherenceComputed)) { simu->mark = simu->evaluate (Coherence); simu->isSaved = BFalse; } else if ((fitness == HybridMethod) && (graph->isCoherenceComputed)) { simu->mark = simu->evaluate (HybridMethod); simu->isSaved = BFalse; } simu->arrayUpdate (simulationsArray); } } } } if (editInterfacesDisplayedNumber > 0) { int i; for (i=0; imanageInterface (report); if (action == endInterface) { editInterfacesDisplayedNumber--; simulations[editSimulationIndex[i]]->isEdited = BFalse; editSimulationIndex[i] = -1; delete editInterface[i]; } else if (action == endInterfaceAndSave) { editInterfacesDisplayedNumber--; simulations[editSimulationIndex[i]]->isEdited = BFalse; simulations[editSimulationIndex[i]]->arrayUpdate (simulationsArray); editSimulationIndex[i] = -1; delete editInterface[i]; /* the same as before but update the array */ } } } } } /* fin de test Expose */ if (isFileSelectorInterfaceDisplayed == 1) { unsigned int result = fileSelector->manageInterface (report) ; if ( result != 3) manageLoadClose (result); } } /* while(!end) */ } void MainInterface::refresh () { int i; exitButton->refresh (); configButton->refresh (); netButton->refresh (); geneticButton->refresh (); infosButton->refresh (); loadButton->refresh (); saveButton->refresh (); loadStepButton->refresh (); cleanButton->refresh (); editButton->refresh (); createButton->refresh (); simulationButton->refresh (); viewButton->refresh (); evaluateButton->refresh (); parametersButton->refresh (); simulationsArrayTitle->refresh (); simulationsArray->refresh (); if ((previewWindow->shouldBeRefreshed) && (!showLoadedPreviews)) displayPreview (-1); dialogPanel->refresh (); dialogText1->refresh (); dialogText2->refresh (); dialogText3->refresh (); dialogText4->refresh (); dialogText5->refresh (); dialogText6->refresh (); if (isTitleInterfaceDisplayed) titleInterface->refresh (); if (isFileSelectorInterfaceDisplayed == 1) fileSelector->refresh (); if (netInterfaceDisplayed == 1) netInterface->refresh (); if (geneticInterfaceDisplayed == 1) geneticInterface->refresh (); if (viewInterfacesDisplayedNumber > 0) { for (i=0; irefresh (); } } if (evalInterfacesDisplayedNumber > 0) { for (i=0; irefresh (); } } if (editInterfacesDisplayedNumber > 0) { for (i=0; irefresh (); } } } MainInterface::MainInterface (int x0, int y0) : Interface (x0, y0, 830, 600, 1, VERSION, "Nest2") { int i; char temp[256]; timeOut = simulationTimeOut; geneticRunning = 0; geneticSimulationSet = 0; net = new Net(MaxHostsNumber); displayInfoBox (NULL, "Net initialisation", "Nest2", "Reading netconfig file and checking hosts, please wait..."); net->loadFromConfigFile (anInfoBox); freeInfoBox (); strcpy (scrollText[0], " "); sprintf (temp, " %s ", VERSION); strcpy (scrollText[1], temp); strcpy (scrollText[2], " 3D Wasp Nest Simulator "); strcpy (scrollText[3], " Copyright (C) 1997 Sylvain GUERIN "); strcpy (scrollText[4], " LIASC ENST Bretagne & Santa Fe Institute "); strcpy (scrollText[5], " "); exitButton = new Button (this, 700, 10, 110, 25, "Exit", centerJustified, enabled); configButton = new Button (this, 700, 40, 110, 25, "Options", centerJustified, enabled); netButton = new Button (this, 700, 70, 110, 25, "Net", centerJustified, enabled); geneticButton = new Button (this, 700, 100, 110, 25, "Genetic", centerJustified, enabled); infosButton = new Button (this, 700, 130, 110, 25, "Infos", centerJustified, enabled); dialogPanel = new RoundPanel (this, 20+PIXMAP_WIDTH, 10, 660-PIXMAP_WIDTH, 110); dialogText1 = new TextWidget (dialogPanel, 10, 10, scrollText[0]); dialogText2 = new TextWidget (dialogPanel, 10, 25, scrollText[1]); dialogText3 = new TextWidget (dialogPanel, 10, 40, scrollText[2]); dialogText4 = new TextWidget (dialogPanel, 10, 55, scrollText[3]); dialogText5 = new TextWidget (dialogPanel, 10, 70, scrollText[4]); dialogText6 = new TextWidget (dialogPanel, 10, 85, scrollText[5]); loadButton = new Button (this, 50, 530, 130, 25, "Load", centerJustified, enabled); saveButton = new Button (this, 50, 560, 130, 25, "Save", centerJustified, enabled); loadStepButton = new Button (this, 200, 530, 130, 25, "Generation", centerJustified, enabled); cleanButton = new Button (this, 200, 560, 130, 25, "Remove all", centerJustified, enabled); editButton = new Button (this, 350, 530, 130, 25, "Edit", centerJustified, enabled); createButton = new Button (this, 350, 560, 130, 25, "Create", centerJustified, enabled); simulationButton = new Button (this, 500, 530, 130, 25, "Simulation", centerJustified, enabled); viewButton = new Button (this, 500, 560, 130, 25, "View", centerJustified, enabled); evaluateButton = new Button (this, 650, 530, 130, 25, "Evaluate", centerJustified, enabled); parametersButton = new Button (this, 650, 560, 130, 25, "Parameters", centerJustified, enabled); simulationsContent = new ArrayContent(8, maxSimulations); simulationsContent->addColumn ("Nb", IntType, 3); simulationsContent->addColumn ("Name", CharType, 23); simulationsContent->addColumn ("Type", CharType, 7); simulationsContent->addColumn ("Rules", IntType, 6); simulationsContent->addColumn ("Simul.", CharType, 6); simulationsContent->addColumn ("Size", IntType, 5); simulationsContent->addColumn ("Fitness", CharType, 7); simulationsContent->addColumn ("File name", CharType, 22); simulationsArray = new ArrayWindow (this, 20, 170, 790, 340, enabled, Grey, simulationsContent); simulationsArrayTitle = new TextWidget (this, 225, 152, " Memorized simulations "); simulationsNb = 0; /* fenetre de previsualisation */ previewWindow = new GraphicWindow (this, 10, 10, PIXMAP_WIDTH, PIXMAP_HEIGHT, enabled, ExposureMask, LightGrey); viewInterfacesDisplayedNumber = 0; for (i=0; iisPreviewed) { if (simulations[i]->simulationArchitecture->pixmapInitialized == BTrue) { /* Liberation du pixmap de la simulation i */ simulations[i]->simulationArchitecture->freePixmap (display); } } /* liberations des differentes simu */ delete simulations[i]; } if (isTitleInterfaceDisplayed) { isTitleInterfaceDisplayed = 0; delete titleInterface; } /* ensuite on libere les objets graphiques */ XFreePixmap(display, waspPixmap); delete dialogText1; delete dialogText2; delete dialogText3; delete dialogText4; delete dialogText5; delete dialogText6; delete dialogPanel; delete exitButton; delete configButton; delete netButton; delete geneticButton; delete infosButton; delete simulationsArray; delete simulationsArrayTitle; delete loadButton; delete saveButton; delete loadStepButton; delete cleanButton; delete editButton; delete createButton; delete simulationButton; delete viewButton; delete evaluateButton; delete parametersButton; delete simulationsContent; delete net; delete deferedSimuList; delete previewWindow; } void MainInterface::displayNewText (char* aText) { strcpy (scrollText[0], scrollText[1]); strcpy (scrollText[1], scrollText[2]); strcpy (scrollText[2], scrollText[3]); strcpy (scrollText[3], scrollText[4]); strcpy (scrollText[4], scrollText[5]); strcpy (scrollText[5], aText); dialogText1->change (scrollText[0]); dialogText2->change (scrollText[1]); dialogText3->change (scrollText[2]); dialogText4->change (scrollText[3]); dialogText5->change (scrollText[4]); dialogText6->change (scrollText[5]); } void MainInterface::manageOptions () { int oldTimeOut; oldTimeOut = timeOut; displayInputBox (this, "Options", "nest2", 6, 35); addIntInput ( "Simulation time out ", 4, &timeOut); addEnumInput ( "Default file format ", 11, &fileFormatEnum); addEnumInput ( "Architecture saving ", 10, &archMustBeSavedEnum); addEnumInput ( " Preview saving ", 10, &previewMustBeSavedEnum); addEnumInput ( " Compression ", 5, &mustBeCompressedEnum); if (manageInputBox ()) /* cancelled operation */ { timeOut = oldTimeOut; } else { // printf ("File format: %d\n", fileFormatEnum.defaultValue ()); displayNewText ("Parameters have been changed"); } } void MainInterface::manageNet () { if (netInterfaceDisplayed) displayDialogBox (this, "Error", "Nest2", "Net configuration window already opened.", "Continue"); else { netInterface = new NetInterface (this, 300, 200, display, net); netInterfaceDisplayed = 1; } } void MainInterface::manageGenetic () { if (geneticInterfaceDisplayed) { displayDialogBox (this, "Error", "Nest2", "Genetic algorithm window already opened.", "Continue"); } else { geneticInterface = new GeneticInterface (50, 10, "Genetic simulation", "Genetic", display, this); geneticInterfaceDisplayed = 1; } } void MainInterface::manageLoadOpen () { if (isFileSelectorInterfaceDisplayed) { displayDialogBox (this, "Error", "Nest2", "File Selector window already opened.", "Continue"); } else { fileSelector = new FileSelector (this, 200, 130, display, fileDatas); isFileSelectorInterfaceDisplayed = 1; } } void MainInterface::manageLoadClose (unsigned int res) { int i; char pathName[90]; char fileName[30]; delete fileSelector; isFileSelectorInterfaceDisplayed = 0; if (res == 0) return; else if (res == 1) /* load all */ { showLoadedPreviews = 1; strcpy (pathName, fileDatas->returnCompleteDirectory ()); displayAbortableInfoBox (this, "Load", "Nest2", "Loading file(s), please wait..."); for (i=0; iselectedFilesNumber (); i++) { strcpy (fileName, fileDatas->selectedFile (i)); loadNewFile (pathName, fileName); anInfoBox->setStatWindow ((double)(i+1) /(double)(fileDatas->selectedFilesNumber ())); if (anInfoBox->refreshStatWindow () == 1) break; } freeInfoBox (); simulationsArrayTitle ->change (" Memorized simulations "); displayPreview (-1); showLoadedPreviews = 0; return; } else if (res == 2) /* load file */ { strcpy (pathName, fileDatas->returnCompleteDirectory ()); strcpy (fileName, fileDatas->selectedFile (fileDatas->returnFileIndex ())); loadNewFile (pathName, fileName); simulationsArrayTitle ->change (" Memorized simulations "); displayPreview (-1); return; } else { printf ("Error: value indefined\n"); return; } } /* ---------------------------------------------------------------------- Methode de gestion pour le chargement d'une simulation ---------------------------------------------------------------------- */ void MainInterface::loadNewFile (char* pathName, char* fileName) { int i, res, res2; char displayedText[50]; char temp[100]; int isCompressed; FileFormat format; isCompressed = 0; res = -1; if (simulationsContent->getLinesNumber () < maxSimulations) { i = simulationsArray->addLine (); res2 = returnSimuType (pathName, fileName, &isCompressed, &format); if ((res2 == 0) || (res2 == 1)) /* ok, we continue */ { if (res2 == 0) { simulations[i] = new Simulation(i, maxRules, cubic); simulations[i]->setDisplayAndWindow (display, defaultVisual, gc, &(previewWindow->win), color, fontInfo); if (isCompressed) simulations[i]->isCompressed = BTrue; res = simulations[i]->load (pathName, fileName, BTrue); } else if (res2 == 1) { simulations[i] = new Simulation(i, maxRules, hexa); simulations[i]->setDisplayAndWindow (display, defaultVisual, gc, &(previewWindow->win), color, fontInfo); if (isCompressed) simulations[i]->isCompressed = BTrue; res = simulations[i]->load (pathName, fileName, BTrue); } if (res == 0) /* file not found or opening error */ { simulationsArray->removeLine (i); delete simulations[i]; printf ("Error: unable to open file %s\n", fileName); sprintf (displayedText, "Error: unable to open %s", fileName); displayNewText (displayedText); } else if (res == 1) /* loading ok */ { simulations[i]->arrayUpdate (simulationsArray); simulationsNb++; sprintf (displayedText, "File %s loaded.", fileName); displayNewText (displayedText); } else /* wrong format */ { sprintf (temp, "File %s has a wrong format, unable to be loaded.", fileName); displayDialogBox (this, "Error", "Nest2", temp, "Continue"); simulationsArray->removeLine (i); delete simulations[i]; sprintf (displayedText, "Error: wrong format for file %s.", fileName); displayNewText (displayedText); } } else if (res2 == -1) /* wrong format */ { sprintf (temp, "File %s has a wrong format, unable to be loaded.", fileName); displayDialogBox (this, "Error", "Nest2", temp, "Continue"); simulationsArray->removeLine (i); sprintf (displayedText, "Error: wrong format for file %s.", fileName); displayNewText (displayedText); } else if (res2 == -2) /* file not found or opening error */ { sprintf (temp, "Error: unable to open file %s", fileName); displayDialogBox (this, "Error", "Nest2", temp, "Continue"); simulationsArray->removeLine (i); sprintf (displayedText, "Error: unable to open %s.", fileName); displayNewText (displayedText); } } else { displayDialogBox (this, "Error", "Nest2", " No more memory to store a new simulation. ", "Continue"); } } /* ---------------------------------------------------------------------- Methode de gestion de la sauvegarde des simulations ---------------------------------------------------------------------- */ void MainInterface::manageSave () { int i; char displayedText[50]; char newPath[256]; char newFile[256]; char newName[256]; Enumeration compressionEnum; /* on cherche deja si une simulation est selectionnee */ i = simulationsArray->getSelectedLine (); if (i == -1) /* pas de ligne selectionee */ { displayDialogBox (this, "Error", "Nest2", "No selected simulation.", "Continue"); } else { /* init of compression option enumeration */ compressionEnum.add ("no"); compressionEnum.add ("yes"); compressionEnum.setDefault (simulations[i]->isCompressed); /* affichage de la boite de dialogue pour la sauvegarde */ if (strncmp (simulations[i]->pathName, homeDirectory, strlen(homeDirectory)) == 0) strcpy (newPath, simulations[i]->pathName); else { strcpy (newPath, homeDirectory); strcat (newPath, "/"); } strcpy (newFile, simulations[i]->fileName); strcpy (newName, simulations[i]->name); displayInputBox (this, "Save simulation", "nest2", 7, 53); addCharInput ( " Simulation name ", 23, newName); addCharInput ( " File name ", 23, newFile); addCharInput ( " Path name ", 23, newPath); addEnumInput ( " Architecture saving ", 10, &archMustBeSavedEnum); addEnumInput ( " Preview saving ", 10, &previewMustBeSavedEnum); addEnumInput ( " Compression ", 5, &compressionEnum); if (manageInputBox ()) { /* cancelled operation */ } else { /* OK: on tente la sauvegarde alors */ /* mise a jour des nouvelles valeurs */ strcpy (simulations[i]->pathName, newPath); strcpy (simulations[i]->fileName, newFile); strcpy (simulations[i]->name, newName); /* mise a jour de simulationsArray */ simulations[i]->arrayUpdate (simulationsArray); /* et on sauve... */ if (simulations[i] ->save ((FileFormat)fileFormatEnum.defaultValue (), (ArchMustBeSavedType)archMustBeSavedEnum.defaultValue (), (PreviewMustBeSavedType)previewMustBeSavedEnum.defaultValue (), (MustBeCompressedType)compressionEnum.defaultValue ()) == 0) { displayDialogBox (this, "Error", "Nest2", " Unable to save current simulation. ", "Continue"); } else { sprintf (displayedText, "File %s has been saved.", simulations[i]->fileName); displayNewText (displayedText); } } } } /* ---------------------------------------------------------------------- Methode de gestion du chargement d'une generation (simu. genetique) ---------------------------------------------------------------------- */ void MainInterface::manageLoadStep () { int generation; char pathName[256]; char currentFile[256]; int nbFiles = 0; char temp[100]; int i; char newTitle1[256]; char newTitle2[256]; int blank; DIR *dp; struct dirent *ep; struct stat stats; if (!geneticSimulationSet) { displayDialogBox (this, "Error", "Nest2", " No genetic simulation loaded. ", "Continue"); } else { if (geneticSimulation->totalSteps == 0) { displayDialogBox (this, "Error", "Nest2", " No generation computed. ", "Continue"); } else if ((viewInterfacesDisplayedNumber > 0) || (editInterfacesDisplayedNumber > 0) || (evalInterfacesDisplayedNumber > 0) ) { displayDialogBox (this, "Error", "Nest2", "Please close all refering windows before.", "Continue"); } else { generation = 1; displayInputBox (this, "Load a generation", "nest2", 1, 30); addIntInput (" Generation : ", 6, &generation); if (manageInputBox () == 0) { if ((generation > (int)geneticSimulation->totalSteps) || (generation < 1)) { displayDialogBox (this, "Error", "Nest2", " Bad generation number. ", "Continue"); } else { manageClean (); /* " Memorized simulations " */ /* 41 colonnes */ sprintf (newTitle1, "Generation %d of %s", generation, geneticSimulation->name); if (strlen(newTitle1) < 41) { blank = (int) ((double)(41-strlen(newTitle1))/2.0); if (blank>0) { strcpy (newTitle2, " "); for (i=1; ichange (newTitle2); strcpy (pathName, geneticSimulation->pathName); strcat (pathName, geneticSimulation->dirName); strcat (pathName, "/"); strcat (pathName, geneticSimulation->currentSimulationStep->getStepName (geneticSimulation->dirName, generation)); strcat (pathName, "/"); /* strcat (pathName, "*.rul.zhuf"); */ dp = opendir (pathName); if (dp != NULL) { while ((ep = readdir (dp))) { if (strcmp (&(ep->d_name[strlen(ep->d_name)-9]), ".rul.zhuf") == 0) { strcpy (currentFile, pathName); strcat (currentFile, ep->d_name); if (stat (currentFile, &stats) == 0 && S_ISDIR (stats.st_mode)) { sprintf (temp, "%s is a directory.", currentFile); displayDialogBox (this, "Warning", "Nest2", temp, "Continue"); } else { loadNewFile (pathName, ep->d_name); nbFiles++ ; } } } } else { sprintf (temp, "Could not open %s.", geneticSimulation->dirName ); displayDialogBox (this, "Warning", "Nest2", temp, "Continue"); } if (nbFiles == 0) { displayDialogBox (this, "Error", "Nest2", " File(s) not found. ", "Continue"); } } } else { /* operation cancelled */ } } } } void MainInterface::manageClean () { int i; if ((viewInterfacesDisplayedNumber > 0) || (editInterfacesDisplayedNumber > 0) || (evalInterfacesDisplayedNumber > 0) ) displayDialogBox (this, "Error", "Nest2", "Please close all refering windows before.", "Continue"); else { manageNotSaved (); for (i=0; i<(int)simulationsNb; i++) { simulationsContent->removeLine (i); delete simulations[i]; } simulationsArray->reset (); simulationsArray->refreshAll (); simulationsNb = 0; displayPreview (-1); } } void MainInterface::manageEdit () { int i; char temp[256]; i = simulationsArray->getSelectedLine (); if (i == -1) { /* pas de ligne selectionee */ displayDialogBox (this, "Error", "Nest2", "No selected simulation.", "Continue"); } else { if (editInterfacesDisplayedNumber >= maxEditInterfaces) { displayDialogBox (this, "Error", "Nest2", "Please close some other editions before.", "Continue"); } else { int emptyEditInterface = 0; displayNewText ("Edition has been launched."); sprintf (temp, "Edition of %s", simulations[i]->name); if (simulations[i]->simuType == cubic) strcat (temp, " (cubic)"); else strcat (temp, " (hexa)"); while (editSimulationIndex[emptyEditInterface] != -1) { emptyEditInterface++; } if (emptyEditInterface < maxEditInterfaces) { editInterface[emptyEditInterface] = new EditInterface (this, 100, 50, temp, simulations[i]->name, this->display, simulations[i]); editSimulationIndex[emptyEditInterface] = i; editInterfacesDisplayedNumber++; } else { printf ("Error: maxInterface reached\n"); } } } } void MainInterface::manageCreate () { Rule* aRule; int emptyEditInterface; int i; int x,y,z; char name[256]; char fileName[256]; char pathName[256]; SimulationType selectedType; if (simulationsContent->getLinesNumber () < maxSimulations) { if (editInterfacesDisplayedNumber >= maxEditInterfaces) { displayDialogBox (this, "Error", "Nest2", "Please close some other colony editors before.", "Continue"); } else { strcpy (name, "none"); strcpy (fileName, "noname.rul"); strcpy (pathName, homeDirectory); strcat (pathName, "/rules/"); displayInputBox (this, "Creation parameters", "nest2", 5, 50); addEnumInput ( " Simulation type", 10, &simulationTypeEnum); addCharInput ( " Simulation name", 20, name); addCharInput ( "Simulation file name", 20, fileName); addCharInput ( "Simulation path name", 27, pathName); if (manageInputBox ()) { /* cancelled operation */ } else { selectedType = (SimulationType)(simulationTypeEnum.defaultValue ()); aRule = new Rule (selectedType); i = simulationsArray->addLine (); simulations[i] = new Simulation(i, maxRules, selectedType); simulations[i]->setDisplayAndWindow (display, defaultVisual, gc, &(previewWindow->win), color, fontInfo); strcpy (simulations[i]->name, name); strcpy (simulations[i]->fileName, fileName); strcpy (simulations[i]->pathName, pathName); simulationsNb++; //simulations[i]->simuType = selectedType; /* deja fait dans le constructeur */ for (x=0; x<3; x++) { for (y=0; y<3; y++) { for (z=0; z<3; z++) { aRule->set (x,y,z,EMPTY); } } } aRule->set (1,1,1,RED); simulations[i]->simulationRuleArray->add (*aRule); simulations[i]->ruleNumber = 1; simulations[i]->arrayUpdate (simulationsArray); simulations[i]->isSaved = BFalse; emptyEditInterface = 0; displayNewText ("New edition has been launched."); while (editSimulationIndex[emptyEditInterface] != -1) { emptyEditInterface++; } if (emptyEditInterface < maxEditInterfaces) { editInterface[emptyEditInterface] = new EditInterface (this, 100, 50, "Creation of a new colony", simulations[i]->name, this->display, simulations[i]); editSimulationIndex[emptyEditInterface] = i; editInterfacesDisplayedNumber++; } else { printf ("Error: maxInterface reached\n"); } delete aRule; } } } else { displayDialogBox (this, "Error", "Nest2", " No more memory to store a new simulation. ", "Continue"); } } void MainInterface::manageSimulation () { int i; char temp[100]; ViewInterface* simulationViewInterface = NULL; learningMode typeOfLearning; int allowedFails; int mustBeEvaluated; i = simulationsArray->getSelectedLine (); if (i == -1) { /* pas de ligne selectionee */ displayDialogBox (this, "Error", "Nest2", "No selected simulation.", "Continue"); } else { simulations[i]->archSize = editedArchSize; if (simulations[i]->isDisplayed) { displayDialogBox (this, "Error", "Nest2", "Please close the visualisation before.", "Continue"); } else { if (editedShowConstructionEnum.defaultValue () == 1) { simulationViewInterface = openNewViewInterface (i); if (simulationViewInterface == NULL) { displayDialogBox (this, "Error", "Nest2", "Can't open the visualisation.", "Continue"); return; } displayAbortableInfoBox (this, 10, 10, "Simulation", "Nest2", "Simulation is running, please wait..."); } else { simulationViewInterface = NULL; displayAbortableInfoBox (this, "Simulation", "Nest2", "Simulation is running, please wait..."); } if (editedAutoLearning.defaultValue () == 0) { /* pas d'auto-learning */ typeOfLearning = noLearning; allowedFails = -1; } else { typeOfLearning = learnWhileSimulate; allowedFails = editedAllowedFails; } if (((FitnessType)fitnessTypeEnum.defaultValue () != Complexity) && ((FitnessType)fitnessTypeEnum.defaultValue () != Coherence) && ((FitnessType)fitnessTypeEnum.defaultValue () != HybridMethod)) { mustBeEvaluated = 1; } else { mustBeEvaluated = 0; } simulations[i] ->simulateShowsStats (editedArchSize, editedIterationsNumber, editedMaxCells, (FitnessType)fitnessTypeEnum.defaultValue (), typeOfLearning, allowedFails, mustBeEvaluated, editedNoiseRate, anInfoBox, simulationViewInterface, editedShowCellsSteps); simulations[i]->arrayUpdate (simulationsArray); freeInfoBox (); if (simulationViewInterface == NULL) { simulations[i]->simulationArchitecture ->loadPreviewFromArch (display, &(previewWindow->win), gc, color, fontInfo); simulations[i]->isPreviewed = BTrue; } sprintf (temp, "Simulation complete (%ld cells built).", simulations[i]->simulationArchitecture->cellsNumber); displayNewText (temp); if (editedShowConstructionEnum.defaultValue () == 1) { simulationViewInterface->alreadyComputed = 0; simulationViewInterface->displayView3d (); } } } } ViewInterface* MainInterface::openNewViewInterface (int simulationIndex) { /* cette methode ouvre une nouvelle interface de visualisation pour voir la construction en cours, retourne l'interface si tout va bien, sinon NULL */ char temp[256]; if (viewInterfacesDisplayedNumber >= maxViewInterfaces) { displayDialogBox (this, "Error", "Nest2", "Please close some visualisations before.", "Continue"); return NULL; } else { int emptyViewInterface = 0; displayNewText ("Visualisation has been launched."); simulations[simulationIndex]->isDisplayed = BTrue; while (viewSimulationIndex[emptyViewInterface] != -1) { emptyViewInterface++; } if (emptyViewInterface < maxViewInterfaces) { sprintf (temp, "%s %dx%d", simulations[simulationIndex]->name, simulations[simulationIndex]->archSize, simulations[simulationIndex]->archSize); if (simulations[simulationIndex]->simuType == cubic) { strcat (temp, " (cubic)"); } else { strcat (temp, " (hexa)"); } viewInterface[emptyViewInterface] = new ViewInterface (this, 410, 30, 600, 500, 1, temp, simulations[simulationIndex]->name, this->display, NULL, simulations[simulationIndex]->simuType, previewWindow->win, simulations[simulationIndex]->archSize); viewSimulationIndex[emptyViewInterface] = simulationIndex; viewInterfacesDisplayedNumber++; /* et on retourne l'interface en question */ return viewInterface[emptyViewInterface]; } else { printf ("Error: maxInterface reached\n"); return NULL; } } } void MainInterface::manageView () { int i; char temp[256]; i = simulationsArray->getSelectedLine (); if (i == -1) { /* pas de ligne selectionee */ displayDialogBox (this, "Error", "Nest2", "No selected simulation.", "Continue"); } else { if (simulations[i]->isSimulated == BTrue) { if (viewInterfacesDisplayedNumber >= maxViewInterfaces) { displayDialogBox (this, "Error", "Nest2", "Please close some visualisations before.", "Continue"); } else { int emptyViewInterface = 0; displayNewText ("Visualisation has been launched."); simulations[i]->isDisplayed = BTrue; while (viewSimulationIndex[emptyViewInterface] != -1) { emptyViewInterface++; } if (emptyViewInterface < maxViewInterfaces) { sprintf (temp, "%s %dx%d", simulations[i]->name, simulations[i]->archSize, simulations[i]->archSize); if (simulations[i]->simuType == cubic) { strcat (temp, " (cubic)"); } else { strcat (temp, " (hexa)"); } viewInterface[emptyViewInterface] = new ViewInterface (this, 300, 10, 720, 720, 1, temp, simulations[i]->name, this->display, simulations[i]->simulationArchitecture, simulations[i]->simuType, previewWindow->win, simulations[i]->archSize); viewSimulationIndex[emptyViewInterface] = i; viewInterfacesDisplayedNumber++; } else { printf ("Error: maxInterface reached\n"); } } } else { displayDialogBox (this, "Error", "Nest2", "Please run the simulation before.", "Continue"); } } } void MainInterface::manageEvaluate () { int i; char temp[256]; i = simulationsArray->getSelectedLine (); if (i == -1) /* pas de ligne selectionee */ { displayDialogBox (this, "Error", "Nest2", "No selected simulation.", "Continue"); } else { if (evalInterfacesDisplayedNumber >= maxEvalInterfaces) { displayDialogBox (this, "Error", "Nest2", "Please close some other evaluations before.", "Continue"); } else { if (simulations[i]->isSimulated == BTrue) { if (simulations[i]->simulationArchitecture-> graph->getNodesNumber () != 0 || simulations[i]->simulationArchitecture-> graph->getEdgesNumber () != 0) { int emptyEvalInterface = 0; sprintf (temp, "Evaluation for %s", simulations[i]->name); if (simulations[i]->simuType == cubic) strcat (temp, " (cubic)"); else strcat (temp, " (hexa)"); while (evalSimulationIndex[emptyEvalInterface] != -1) emptyEvalInterface++; if (emptyEvalInterface < maxEvalInterfaces) { evalInterface[emptyEvalInterface] = new EvalInterface (this, 50, 10, temp, simulations[i]->name, this->display, simulations[i]); evalSimulationIndex[emptyEvalInterface] = i; evalInterfacesDisplayedNumber++; } else { printf ("Error: maxInterface reached\n"); } } else { displayDialogBox (this, "Error", "Nest2", "No graph associated with simulation. Please re-run simulation.", "Continue"); } } else { displayDialogBox (this, "Error", "Nest2", "Please run the simulation before.", "Continue"); } } } } /* reglage des parametres de simulation */ void MainInterface::manageSimuParameters () { displayInputBox (this, "Simulation parameters", "nest2", 11, 40); addIntInput ( " Architecture size ", 8, &editedArchSize); addDoubleInput (" Noise tolerance ", 8, &editedNoiseRate); addIntInput ( " Max iterations ", 8, &editedIterationsNumber); addIntInput ( " Max cells ", 8, &editedMaxCells); addEnumInput ( " Fitness function ", 17, &fitnessTypeEnum); addEnumInput ( " Auto learning ", 6, &editedAutoLearning); addIntInput ( " Allowed fails ", 8, &editedAllowedFails); addEnumInput ( " Show construction ", 6, &editedShowConstructionEnum); addIntInput ( " Cells steps ", 8, &editedShowCellsSteps); if (manageInputBox ()) { /* cancelled operation */ } else { //printf ("auto learning: %d\n", editedAutoLearning.defaultValue ()); //printf ("fitness: %d\n", fitnessTypeEnum.defaultValue ()); //printf ("show construction: %d\n", editedShowConstructionEnum.defaultValue ()); displayNewText ("Simulation parameters have been changed."); } } /* methode de fin */ int MainInterface::manageExit () { /* USELESS int i; */ /* USELESS char temp[256]; */ /* USELESS char displayedText[256]; */ if (displayDialogBox (this, "Exit", "Nest2", " Really quit Nest2 ? ", "Exit", "Cancel") == 0) { manageNotSaved (); return 1; } else { return 0; } } /* Traitement des cas ou on decide de quitter et que toutes les simulations ne sont pas sauvees (apres avoir ete editees) */ void MainInterface::manageNotSaved () { int i; char temp[256]; char displayedText[256]; char newPath[256]; char newFile[256]; char newName[256]; Enumeration compressionEnum; for (i=0; i<(int)simulationsNb; i++) { if (simulations[i]->isSaved == BFalse) { sprintf (temp, " File %s not saved. Please confirm saving. ", simulations[i]->fileName); if (displayDialogBox (this, "Save", "Nest2", temp, "Save", "Don't save") == 0) { /* init of compression option enumeration */ compressionEnum.add ("no"); compressionEnum.add ("yes"); compressionEnum.setDefault (simulations[i]->isCompressed); /* affichage de la boite de dialogue pour la sauvegarde */ if (strncmp (simulations[i]->pathName, homeDirectory, strlen(homeDirectory)) == 0) strcpy (newPath, simulations[i]->pathName); else { strcpy (newPath, homeDirectory); strcat (newPath, "/"); } strcpy (newFile, simulations[i]->fileName); strcpy (newName, simulations[i]->name); displayInputBox (this,"Save simulation", "nest2", 7, 53); addCharInput ( " Simulation name ", 23, newName); addCharInput ( " File name ", 23, newFile); addCharInput ( " Path name ", 23, newPath); addEnumInput ( " Architecture saving ", 10, &archMustBeSavedEnum); addEnumInput ( " Preview saving ", 10, &previewMustBeSavedEnum); addEnumInput ( " Compression ", 5, &compressionEnum); if (manageInputBox ()) { /* cancelled operation */ } else { /* OK: on tente la sauvegarde alors */ /* mise a jour des nouvelles valeurs */ strcpy (simulations[i]->pathName, newPath); strcpy (simulations[i]->fileName, newFile); strcpy (simulations[i]->name, newName); /* mise a jour de simulationsArray */ simulations[i]->arrayUpdate (simulationsArray); /* et on sauve... */ if (simulations[i] ->save ((FileFormat)fileFormatEnum.defaultValue (), (ArchMustBeSavedType)archMustBeSavedEnum.defaultValue (), (PreviewMustBeSavedType)previewMustBeSavedEnum.defaultValue (), (MustBeCompressedType)compressionEnum.defaultValue ()) == 0) { displayDialogBox (this, "Error", "Nest2", " Unable to save current simulation. ", "Continue"); } else { sprintf (displayedText, "File %s has been saved.", simulations[i]->fileName); displayNewText (displayedText); } } } } } } /* Cette methode vient lire le fichier considere, et le teste pour savoir si le type de simulation est plutot cubic ou plutot hexa. Ceci est tres utile pour savoir comment on va declarer les differentes simulations */ int MainInterface::returnSimuType (char* aPathName, char* aFileName, int* isCompressed, FileFormat* aFileFormat) { /* return 0 when cubic, return 1 when hexa, -1 when wrong format, -2 when reading error */ char fname[256]; FILE *pf; char temp[256]; int lenght; lenght = strlen (aFileName); if ( (aFileName[lenght-5] == '.') && (aFileName[lenght-4] == 'z') && (aFileName[lenght-3] == 'h') && (aFileName[lenght-2] == 'u') && (aFileName[lenght-1] == 'f')) { /* ok, compressed file */ CompressAlgorithm algorithm; *isCompressed = 1; lenght = strlen (aFileName); if (lenght < 10) return -2; if ((aFileName[lenght-9] == '.') && (aFileName[lenght-8] == 'r') && (aFileName[lenght-7] == 'u') && (aFileName[lenght-6] == 'l')) { *aFileFormat = rulFile; strcpy (fname, aPathName); strcat (fname, tempRulesFile1); algorithm.decompress (aPathName, aFileName, tempRulesFile1); aFileName[lenght-5] = '\0'; } else if ((aFileName[lenght-9] == '.') && (aFileName[lenght-8] == 't') && (aFileName[lenght-7] == 'h') && (aFileName[lenght-6] == 'r')) { *aFileFormat = thrFile; strcpy (fname, aPathName); strcat (fname, tempRulesFile2); algorithm.decompress (aPathName, aFileName, tempRulesFile2); aFileName[lenght-5] = '\0'; } else { *aFileFormat = undefined; printf ("Error: undefined format\n"); } } else { strcpy (fname, aPathName); strcat (fname, aFileName); } pf = fopen(fname,"r"); if (pf == NULL) { return -2; } else { fgets (temp, 256, pf); /* lecture du nom de l'architecture */ fgets (temp, 256, pf); /* lecture du type de la simulation */ if (strncmp (temp, "type = cubic", 11) == 0) { fclose (pf); if (*isCompressed) remove (fname); return 0; } else if (strncmp (temp, "type = hexa", 10) == 0) { fclose (pf); if (*isCompressed) remove (fname); return 1; } else { fclose (pf); if (*isCompressed) remove (fname); return -1; /* wrong format */ } } } /* Cette methode permet d'afficher l'icone de previsualisation de la vue quand c'est possible (pixmap initialise). Sinon, elle efface la fenetre */ void MainInterface::displayPreview (const int j) { int i; i = j; Pixmap toBeDisplayed; if (i == -1) /* we don't know what to do let's decide :) */ i = simulationsArray->getSelectedLine (); if (i == -1) { /* pas de ligne selectionne, on affiche la jolie guepe */ toBeDisplayed = waspPixmap; } else { /* si l'icone existe bel et bien, c'est cool, on l'affiche */ if (simulations[i]->isSimulated) { if (simulations[i]->simulationArchitecture->pixmapInitialized && simulations[i]->isPreviewed) { toBeDisplayed = simulations[i]-> simulationArchitecture->preview; } else { toBeDisplayed = waspPixmap; } } else /* sinon meme punition, on affiche la jolie guepe*/ toBeDisplayed = waspPixmap; } XCopyArea (display, toBeDisplayed, previewWindow->win, *gc, 0, 0, PIXMAP_WIDTH, PIXMAP_HEIGHT , 0, 0); } /* methode de chargement de l'icone de la guepe */ void MainInterface::loadPixmap () { XpmAttributes xpma; xpma.valuemask = XpmExactColors; if (defaultDepth == 1) { if (XpmCreatePixmapFromData (display, previewWindow->win, WaspIconBW, &waspPixmap, NULL, &xpma) != 0) printf ("ERROR >> unable to create WaspIcon\n"); } else { if (XpmCreatePixmapFromData (display, previewWindow->win, WaspIcon, &waspPixmap, NULL, &xpma) != 0) printf ("ERROR >> unable to create WaspIcon\n"); } XpmFreeAttributes (&xpma); }