openModeller  Version 1.4.0
om_algorithm.cpp
Go to the documentation of this file.
00001 #include <openmodeller/om.hh>
00002 #include <openmodeller/om_defs.hh>
00003 #include <openmodeller/Log.hh>
00004 #include <openmodeller/os_specific.hh>
00005 
00006 #include "getopts/getopts.h"
00007 
00008 #include "om_cmd_utils.hh"
00009 
00010 #include <string>
00011 #include <iostream>  // I/O 
00012 
00013 #include <stdexcept>
00014  
00015 using namespace std;
00016 
00017 int main( int argc, char **argv ) {
00018 
00019   Options opts;
00020   int option;
00021 
00022   // command-line parameters (short name, long name, description, take args)
00023   opts.addOption( "" , "log-level"  , "Set the log level (debug, warn, info, error)", true );
00024   opts.addOption( "v", "version"    , "Display version info", false );
00025   opts.addOption( "l", "list"       , "List available algorithms (id and name)", false );
00026   opts.addOption( "d", "dump-xml"   , "Dump algorithms' metadata in XML", false );
00027   opts.addOption( "i", "id"         , "Algorithm id", true );
00028   opts.addOption( "c", "config-file", "Configuration file for openModeller", true );
00029 
00030   std::string log_level("info");
00031   bool        list_algs = false;
00032   bool        dump_algs = false;
00033   std::string alg_id;
00034   std::string config_file;
00035 
00036   if ( ! opts.parse( argc, argv ) ) {
00037 
00038     opts.showHelp( argv[0] ); 
00039     exit(0);
00040   }
00041 
00042   // Set up any related external resources
00043   setupExternalResources();
00044 
00045   OpenModeller om;
00046 
00047   while ( ( option = opts.cycle() ) >= 0 ) {
00048 
00049     switch ( option ) {
00050 
00051       case 0:
00052         log_level = opts.getArgs( option );
00053         break;
00054       case 1:
00055         printf( "om_algorithm %s\n", om.getVersion().c_str() );
00056         printf("This is free software; see the source for copying conditions. There is NO\n");
00057         printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
00058         exit(0);
00059         break;
00060       case 2:
00061         list_algs = true;
00062         break;
00063       case 3:
00064         dump_algs = true;
00065         break;
00066       case 4:
00067         alg_id = opts.getArgs( option );
00068         break;
00069       case 5:
00070         config_file = opts.getArgs( option );
00071         break;
00072       default:
00073         break;
00074     }
00075   }
00076 
00077   // Check parameters
00078 
00079   if ( ( ! list_algs ) && ( ! dump_algs ) && alg_id.empty() ) {
00080 
00081     printf( "Please specify one of the parameters: --list, --dump-xml or --id\n");
00082     exit(1);
00083   }
00084 
00085   // om configuration
00086   if ( ! config_file.empty() ) { 
00087 
00088     Settings::loadConfig( config_file );
00089   }
00090 
00091   // Log stuff
00092 
00093   Log::Level level_code = getLogLevel( log_level );
00094 
00095   Log::instance()->setLevel( level_code );
00096 
00097   // Real work
00098 
00099   try {
00100 
00101     AlgorithmFactory::searchDefaultDirs();
00102 
00103     if ( dump_algs ) {
00104 
00105       ConfigurationPtr cfg = AlgorithmFactory::getConfiguration();
00106       Configuration::writeXml( cfg, cout );
00107 
00108       return 0;
00109     }
00110 
00111     if ( list_algs ) {
00112 
00113       AlgMetadata const **availables = om.availableAlgorithms();
00114 
00115       if ( ! *availables ) {
00116 
00117         printf( "Could not find any algorithm.\n" );
00118         exit(1);
00119       }
00120 
00121       AlgMetadata const *metadata;
00122 
00123       printf( "Available algorithms (id: name)\n" );
00124 
00125       while ( ( metadata = *availables++ ) ) {
00126 
00127         printf( "  %s: %s\n", metadata->id.c_str(), metadata->name.c_str() );
00128       }
00129 
00130       return 0;
00131     }
00132 
00133     if ( ! alg_id.empty() ) {
00134 
00135       AlgMetadata const *alg_metadata = AlgorithmFactory::algorithmMetadata( alg_id.c_str() );
00136 
00137       printf( "Algorithm metadata:\n" );
00138       printf( "  Name: %s\n", alg_metadata->name.c_str() );
00139       printf( "  Version: %s\n", alg_metadata->version.c_str() );
00140       printf( "  Overview:\n\n%s\n\n", alg_metadata->overview.c_str() );
00141       printf( "  Description:\n\n%s\n\n", alg_metadata->description.c_str() );
00142       printf( "  Author(s): %s\n", alg_metadata->author.c_str() );
00143       printf( "  Bibliography:\n\n%s\n\n", alg_metadata->biblio.c_str() );
00144       printf( "  Developer(s): %s (%s)\n", alg_metadata->code_author.c_str(), alg_metadata->contact.c_str() );
00145 
00146       std::string const accepts_categorical = ( alg_metadata->categorical ) ? "yes" : "no";
00147       printf( "  Accepts categorical data: %s\n", accepts_categorical.c_str() );
00148 
00149       std::string const requires_absences = ( alg_metadata->absence ) ? "yes" : "no";
00150       printf( "  Requires absence points: %s\n", requires_absences.c_str() );
00151 
00152       AlgParamMetadata *param = alg_metadata->param;
00153 
00154       // Include parameters metadata
00155       for ( int i = 0 ; i < alg_metadata->nparam; param++, i++ ) {
00156 
00157         printf( "\n  Parameter: %s\n", param->id.c_str() );
00158         printf( "  Name: %s\n", param->name.c_str() );
00159 
00160         string datatype("?");
00161 
00162         if ( param->type == Integer ) {
00163 
00164           datatype = "Integer";
00165         }
00166         else if ( param->type == Real ) {
00167 
00168           datatype = "Real";
00169         }
00170         else if ( param->type == String ) {
00171 
00172           datatype = "String";
00173         }
00174 
00175         printf( "  Datatype: %s\n", datatype.c_str() );
00176 
00177         printf( "  Overview: %s\n", param->overview.c_str() );
00178 
00179         printf( "  Description: %s\n", param->description.c_str() );
00180 
00181         if ( param->has_min || param->has_max ) {
00182 
00183           if ( param->has_min ) {
00184 
00185             printf( "  Minimum value: %f\n", param->min_val );
00186           }
00187           if ( param->has_max ) {
00188 
00189             printf( "  Maximum value: %f\n", param->max_val );
00190           }
00191         }
00192 
00193         printf( "  Typical value: %s\n", param->typical.c_str() );
00194       }
00195     }
00196   }
00197   catch ( runtime_error e ) {
00198 
00199     printf( "om_algorithm: %s\n", e.what() );
00200     exit(1);
00201   }
00202 
00203   return 0;
00204 }