MASA-Core
Configs.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2010-2015   Edans Sandes
00004  *
00005  * This file is part of MASA-Core.
00006  * 
00007  * MASA-Core is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * MASA-Core is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with MASA-Core.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  ******************************************************************************/
00021 
00022 #include "Configs.hpp"
00023 
00024 #include "../exceptions/IllegalArgumentException.hpp"
00025 
00026 const char* DEFAULT_CONFIGS[] = {
00027         #include "default.h"
00028         NULL,
00029 };
00030 
00031 
00032 #define QUOTE(A) #A
00033 #define STRINGIFY(A) QUOTE(A)
00034 
00035 #define DEFAULT_SRA_STRATEGY_GLOBAL 0
00036 #define DEFAULT_SRA_STRATEGY_STAGE1 0
00037 #define DEFAULT_SRA_STRATEGY_STAGE2 0
00038 #define DEFAULT_SRA_STRATEGY_STAGE3 0
00039 
00040 #define DEFAULT_SRA_PATH_GLOBAL "work.tmp"
00041 #define DEFAULT_SRA_PATH_STAGE1 ""
00042 #define DEFAULT_SRA_PATH_STAGE2 ""
00043 #define DEFAULT_SRA_PATH_STAGE3 ""
00044 
00045 
00046 
00047 Configs::Configs() {
00048         static config_option_t options[] = {
00049                         {"[global]", "sra-strategy", &sra_strategy[0],
00050                                         STRINGIFY(DEFAULT_SRA_STRATEGY_GLOBAL),
00051                                         ConfigParser::parse_int_enum, SRA_STRATEGY_ENUM},
00052                         {"[stage1]", "sra-strategy",    &sra_strategy[1],
00053                                         STRINGIFY(DEFAULT_SRA_STRATEGY_STAGE1),
00054                                         ConfigParser::parse_int_enum, SRA_STRATEGY_ENUM},
00055                         {"[stage2]", "sra-strategy",    &sra_strategy[2],
00056                                         STRINGIFY(DEFAULT_SRA_STRATEGY_STAGE2),
00057                                         ConfigParser::parse_int_enum, SRA_STRATEGY_ENUM},
00058                         {"[stage3]", "sra-strategy",    &sra_strategy[3],
00059                                         STRINGIFY(DEFAULT_SRA_STRATEGY_STAGE3),
00060                                         ConfigParser::parse_int_enum, SRA_STRATEGY_ENUM},
00061 
00062                         {"[global]", "sra-compression", &sra_compression[0],
00063                                         STRINGIFY(DEFAULT_SRA_COMPRESSION_GLOBAL),
00064                                         ConfigParser::parse_int_enum, SRA_COMPRESSION_ENUM},
00065                         {"[stage1]", "sra-compression", &sra_compression[1],
00066                                         STRINGIFY(DEFAULT_SRA_COMPRESSION_STAGE1),
00067                                         ConfigParser::parse_int_enum, SRA_COMPRESSION_ENUM},
00068                         {"[stage2]", "sra-compression", &sra_compression[2],
00069                                         STRINGIFY(DEFAULT_SRA_COMPRESSION_STAGE2),
00070                                         ConfigParser::parse_int_enum, SRA_COMPRESSION_ENUM},
00071                         {"[stage3]", "sra-compression", &sra_compression[3],
00072                                         STRINGIFY(DEFAULT_SRA_COMPRESSION_STAGE3),
00073                                         ConfigParser::parse_int_enum, SRA_COMPRESSION_ENUM},
00074 
00075                         {"[global]", "sra-path", &sra_path[0],
00076                                         DEFAULT_SRA_PATH_GLOBAL,
00077                                         ConfigParser::parse_path, NULL},
00078                         {"[stage1]", "sra-path", &sra_path[1],
00079                                         DEFAULT_SRA_PATH_STAGE1,
00080                                         ConfigParser::parse_path, NULL},
00081                         {"[stage2]", "sra-path", &sra_path[2],
00082                                         DEFAULT_SRA_PATH_STAGE2,
00083                                         ConfigParser::parse_path, NULL},
00084                         {"[stage3]", "sra-path", &sra_path[3],
00085                                         DEFAULT_SRA_PATH_STAGE3,
00086                                         ConfigParser::parse_path, NULL},
00087 
00088                         {"[global]", "sra-disk-size", &disk_size[0],
00089                                         STRINGIFY(DEFAULT_SRA_DISK_SIZE_GLOBAL),
00090                                         ConfigParser::parse_longlong_size, NULL},
00091                         {"[stage1]", "sra-disk-size", &disk_size[1],
00092                                         STRINGIFY(DEFAULT_SRA_DISK_SIZE_STAGE1),
00093                                         ConfigParser::parse_longlong_size, NULL},
00094                         {"[stage2]", "sra-disk-size", &disk_size[2],
00095                                         STRINGIFY(DEFAULT_SRA_DISK_SIZE_STAGE2),
00096                                         ConfigParser::parse_longlong_size, NULL},
00097                         {"[stage3]", "sra-disk-size", &disk_size[3],
00098                                         STRINGIFY(DEFAULT_SRA_DISK_SIZE_STAGE3),
00099                                         ConfigParser::parse_longlong_size, NULL},
00100 
00101                         {"[global]", "sra-ram-size", &ram_size[0],
00102                                         STRINGIFY(DEFAULT_SRA_RAM_SIZE_GLOBAL),
00103                                         ConfigParser::parse_longlong_size, NULL},
00104                         {"[stage1]", "sra-ram-size", &ram_size[1],
00105                                         STRINGIFY(DEFAULT_SRA_RAM_SIZE_STAGE1),
00106                                         ConfigParser::parse_longlong_size, NULL},
00107                         {"[stage2]", "sra-ram-size", &ram_size[2],
00108                                         STRINGIFY(DEFAULT_SRA_RAM_SIZE_STAGE2),
00109                                         ConfigParser::parse_longlong_size, NULL},
00110                         {"[stage3]", "sra-ram-size", &ram_size[3],
00111                                         STRINGIFY(DEFAULT_SRA_RAM_SIZE_STAGE3),
00112                                         ConfigParser::parse_longlong_size, NULL},
00113 
00114                         {"[global]", "block-pruning", &block_pruning[0],
00115                                         "",
00116                                         ConfigParser::parse_bool, NULL},
00117                         {"[stage1]", "block-pruning", &block_pruning[1],
00118                                         "",
00119                                         ConfigParser::parse_bool, NULL},
00120                         {"[stage2]", "block-pruning", &block_pruning[2],
00121                                         "",
00122                                         ConfigParser::parse_bool, NULL},
00123                         {"[stage3]", "block-pruning", &block_pruning[3],
00124                                         "",
00125                                         ConfigParser::parse_bool, NULL},
00126 
00127                         {"[stage1]", "pruning-initial-score", &stage1_pruning_initial_score,
00128                                         "",
00129                                         ConfigParser::parse_int, NULL},
00130 
00131                         {"[stage3]", "max-partition-size", &max_partition_size[3],
00132                                         "",
00133                                         ConfigParser::parse_int, NULL},
00134                         {"[stage4]", "max-partition-size", &max_partition_size[4],
00135                                         "",
00136                                         ConfigParser::parse_int, NULL},
00137 
00138                         {"[stage3]", "max-iterations", &max_iterations[3],
00139                                         "",
00140                                         ConfigParser::parse_int, NULL},
00141                         {"[stage4]", "max-iterations", &max_iterations[4],
00142                                         "",
00143                                         ConfigParser::parse_int, NULL},
00144 
00145                         {"[stage4]", "execution-type", &stage4_execution_type,
00146                                         "",
00147                                         ConfigParser::parse_int_enum, STAGE_4_EXECUTION_TYPE_ENUM},
00148 
00149                         {"[global]", "work-path", (void*)&work_path,
00150                                         "",
00151                                         ConfigParser::parse_path, NULL},
00152 
00153                         {NULL, NULL, (void*)NULL, "", NULL, NULL}
00154         };
00155         parser = new ConfigParser(options);
00156 
00157         for (int i=0; i<7; i++) {
00158                 sra_strategy[i] = CONFIG_NOT_SET;
00159                 sra_compression[i] = CONFIG_NOT_SET;
00160                 disk_size[i] = CONFIG_NOT_SET;
00161                 ram_size[i] = CONFIG_NOT_SET;
00162                 block_pruning[i] = CONFIG_NOT_SET;
00163                 max_iterations[i] = CONFIG_NOT_SET;
00164                 max_partition_size[i] = CONFIG_NOT_SET;
00165                 sra_path[i] = "";
00166         }
00167         stage1_pruning_initial_score = CONFIG_NOT_SET;
00168         stage4_execution_type = CONFIG_NOT_SET;
00169         work_path = "";
00170 
00171         loadConfigs(DEFAULT_CONFIGS);
00172 }
00173 
00174 Configs::~Configs()
00175 {
00176         // TODO Auto-generated destructor stub
00177 }
00178 
00179 
00180 void Configs::loadFile(string filename) {
00181         FILE* file = fopen(filename.c_str(), "rt");
00182         if (file == NULL) {
00183                 //throw IllegalArgumentException("Configuration File not found.");
00184                 fprintf(stderr, "Configuration File not found. %s.\n", filename.c_str());
00185                 return;
00186         }
00187         char line[512];
00188         int linenum = 0;
00189 
00190         while (fgets(line, sizeof(line), file) != NULL) {
00191                 linenum++;
00192                 try {
00193                         parser->parseLine(line);
00194                 } catch (IllegalArgumentException &e) {
00195                         char msg[512];
00196                         sprintf(msg, "%s: Error in line %d: `%s'.", filename.c_str(), linenum, line);
00197                         throw IllegalArgumentException(e.getErr().c_str(), msg);
00198                 }
00199         }
00200 
00201         //parser->parseFile(file);
00202         //parser->printFile(stdout);
00203 }
00204 
00205 void Configs::loadConfigs(const char** configs) {
00206         int linenum = 0;
00207         while (configs[linenum] != NULL) {
00208                 try {
00209                         parser->parseLine(configs[linenum]);
00210                 } catch (IllegalArgumentException &e) {
00211                         char msg[512];
00212                         sprintf(msg, "Error in config `%s'.", configs[linenum]);
00213                         throw IllegalArgumentException(e.getErr().c_str(), msg);
00214                 }
00215                 linenum++;
00216         }
00217 
00218         //parser->printFile(stdout);
00219 }
00220 
00221 void Configs::printFile(FILE* file) {
00222         parser->printFile(file, true);
00223 }
00224 
00225 int Configs::getSRAStrategy(int stage) {
00226         if (sra_strategy[stage] == CONFIG_NOT_SET) {
00227                 return sra_strategy[GLOBAL];
00228         } else {
00229                 return sra_strategy[stage];
00230         }
00231 }
00232 
00233 int Configs::getSRACompression(int stage) {
00234         if (sra_compression[stage] == CONFIG_NOT_SET) {
00235                 return sra_compression[GLOBAL];
00236         } else {
00237                 return sra_compression[stage];
00238         }
00239 }
00240 
00241 long long Configs::getSRADiskSize(int stage) {
00242         if (disk_size[stage] == CONFIG_NOT_SET) {
00243                 return disk_size[GLOBAL];
00244         } else {
00245                 return disk_size[stage];
00246         }
00247 }
00248 
00249 long long Configs::getSRARamSize(int stage) {
00250         if (ram_size[stage] == CONFIG_NOT_SET) {
00251                 return ram_size[GLOBAL];
00252         } else {
00253                 return ram_size[stage];
00254         }
00255 }
00256 
00257 bool Configs::isBlockPruningEnabled(int stage) {
00258         if (block_pruning[stage] == CONFIG_NOT_SET) {
00259                 return block_pruning[GLOBAL];
00260         } else {
00261                 return block_pruning[stage];
00262         }
00263 }
00264 
00265 int Configs::getMaxIterations(int stage) {
00266         return max_iterations[stage];
00267 }
00268 
00269 int Configs::getMaxPartitionSize(int stage) {
00270         return max_partition_size[stage];
00271 }
00272 
00273 int Configs::getPruningInitialScore() {
00274         return stage1_pruning_initial_score;
00275 }
00276 
00277 int main_test(int argc, const char** argv) {
00278 
00279         Configs configs;
00280         configs.loadFile(string(argv[1]));
00281 
00282 }