|
| 1 | +/** |
| 2 | + * @file Graphitti_Main.cpp |
| 3 | + * |
| 4 | + * @ingroup Simulator/Core |
| 5 | + * |
| 6 | + * @brief Starting point of the Simulation - Main. |
| 7 | + * |
| 8 | + * The main functions calls the Core's runSimulation method which performs the following simulation steps: |
| 9 | + * 1) Instantiates Simulator object |
| 10 | + * 2) Parses command line to get configuration file and additional information if provided |
| 11 | + * 3) Loads global Simulator parameters from configuration file |
| 12 | + * 4) Instantiates all simulator objects (Layout, Connections, Synapases, Vertices) |
| 13 | + * 5) Reads simulator objects' parameters from configuration file |
| 14 | + * 6) Simulation setup (Deseralization, Initailizing values, etc.) |
| 15 | + * 7) Run Simulation |
| 16 | + * 8) Simulation shutdown (Save results, serialize) |
| 17 | + * |
| 18 | + * The Core is de-coupled from main to improve testability. |
| 19 | + */ |
| 20 | + |
| 21 | +#include "Core.h" |
| 22 | +#include "log4cplus/configurator.h" |
| 23 | +#include "log4cplus/logger.h" |
| 24 | +#include "log4cplus/loggingmacros.h" |
| 25 | +#include <cstdlib> |
| 26 | +#include <fstream> |
| 27 | +#include <iostream> |
| 28 | +#include <string> |
| 29 | +#include <unistd.h> |
| 30 | + |
| 31 | +using namespace std; |
| 32 | + |
| 33 | +// Function to check whether there is a file given at a specific path |
| 34 | +bool findFile(string path) |
| 35 | +{ |
| 36 | + // Opens the file at specified path |
| 37 | + ifstream newFile(path); |
| 38 | + |
| 39 | + // Checks if file is opened properly, otherwise results in an error and returns false |
| 40 | + if (newFile.is_open()) { |
| 41 | + // Use good() to check if the file exists |
| 42 | + bool found = newFile.good(); |
| 43 | + newFile.close(); |
| 44 | + return found; |
| 45 | + |
| 46 | + } else { |
| 47 | + cerr << "ERROR opening file." << endl; |
| 48 | + } |
| 49 | + |
| 50 | + return false; |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +/// Main function calls the Core's runSimulation method which |
| 55 | +/// handles command line arguments and running the simulation. |
| 56 | +/// |
| 57 | +/// @param argc argument count. |
| 58 | +/// @param argv arguments. |
| 59 | +/// @return -1 if error, else 0 if success. |
| 60 | +int main(int argc, char *argv[]) |
| 61 | +{ |
| 62 | + // Clear logging files at the start of each simulation |
| 63 | + fstream("Output/Debug/logging.txt", ios::out | ios::trunc); |
| 64 | + fstream("Output/Debug/vertices.txt", ios::out | ios::trunc); |
| 65 | + fstream("Output/Debug/edges.txt", ios::out | ios::trunc); |
| 66 | + |
| 67 | + // Initialize log4cplus and set properties based on configure file |
| 68 | + ::log4cplus::initialize(); |
| 69 | + |
| 70 | + // This is to find the absolute path of the home directory log4cplus file |
| 71 | + string absPath = getenv("HOME"); |
| 72 | + absPath = absPath + "/log4cplus_configure.ini"; |
| 73 | + |
| 74 | + // Checks whether the file is in the home directory |
| 75 | + // otherwise uses the file in RuntimeFiles |
| 76 | + if (findFile(absPath)) { |
| 77 | + ::log4cplus::PropertyConfigurator::doConfigure(absPath); |
| 78 | + } else { |
| 79 | + ::log4cplus::PropertyConfigurator::doConfigure("RuntimeFiles/log4cplus_configure.ini"); |
| 80 | + } |
| 81 | + |
| 82 | + // storing command line arguments as string |
| 83 | + // required to pass as an argument to setupSimulation |
| 84 | + string cmdLineArguments; |
| 85 | + string executableName = argv[0]; |
| 86 | + for (int i = 1; i < argc; i++) { |
| 87 | + cmdLineArguments = cmdLineArguments + argv[i] + " "; |
| 88 | + } |
| 89 | + |
| 90 | + // Creating an instance of core class |
| 91 | + Core core; |
| 92 | + return core.runSimulation(executableName, cmdLineArguments); |
| 93 | +}; |
0 commit comments