openModeller  Version 1.5.0
om_algorithm.cpp
Go to the documentation of this file.
1 #include <openmodeller/om.hh>
3 #include <openmodeller/Log.hh>
5 
6 #include "getopts/getopts.h"
7 
8 #include "om_cmd_utils.hh"
9 
10 #include <string>
11 #include <iostream> // I/O
12 
13 #include <stdexcept>
14 
15 using namespace std;
16 
17 int main( int argc, char **argv ) {
18 
19  Options opts;
20  int option;
21 
22  // command-line parameters (short name, long name, description, take args)
23  opts.addOption( "" , "log-level" , "Set the log level (debug, warn, info, error)", true );
24  opts.addOption( "v", "version" , "Display version info", false );
25  opts.addOption( "l", "list" , "List available algorithms (id and name)", false );
26  opts.addOption( "d", "dump-xml" , "Dump algorithms' metadata in XML", false );
27  opts.addOption( "i", "id" , "Algorithm id", true );
28  opts.addOption( "c", "config-file", "Configuration file for openModeller", true );
29 
30  std::string log_level("info");
31  bool list_algs = false;
32  bool dump_algs = false;
33  std::string alg_id;
34  std::string config_file;
35 
36  if ( ! opts.parse( argc, argv ) ) {
37 
38  opts.showHelp( argv[0] );
39  exit(0);
40  }
41 
42  // Set up any related external resources
44 
45  OpenModeller om;
46 
47  while ( ( option = opts.cycle() ) >= 0 ) {
48 
49  switch ( option ) {
50 
51  case 0:
52  log_level = opts.getArgs( option );
53  break;
54  case 1:
55  printf( "om_algorithm %s\n", om.getVersion().c_str() );
56  printf("This is free software; see the source for copying conditions. There is NO\n");
57  printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
58  exit(0);
59  break;
60  case 2:
61  list_algs = true;
62  break;
63  case 3:
64  dump_algs = true;
65  break;
66  case 4:
67  alg_id = opts.getArgs( option );
68  break;
69  case 5:
70  config_file = opts.getArgs( option );
71  break;
72  default:
73  break;
74  }
75  }
76 
77  // Check parameters
78 
79  if ( ( ! list_algs ) && ( ! dump_algs ) && alg_id.empty() ) {
80 
81  printf( "Please specify one of the parameters: --list, --dump-xml or --id\n");
82  exit(1);
83  }
84 
85  // Log stuff
86 
87  Log::Level level_code = getLogLevel( log_level );
88 
89  Log::instance()->setLevel( level_code );
90 
91  // om configuration
92  if ( ! config_file.empty() ) {
93 
94  Settings::loadConfig( config_file );
95  }
96 
97  // Real work
98 
99  try {
100 
102 
103  if ( dump_algs ) {
104 
106  Configuration::writeXml( cfg, cout );
107 
108  return 0;
109  }
110 
111  if ( list_algs ) {
112 
113  AlgMetadata const **availables = om.availableAlgorithms();
114 
115  if ( ! *availables ) {
116 
117  printf( "Could not find any algorithm.\n" );
118  exit(1);
119  }
120 
121  AlgMetadata const *metadata;
122 
123  printf( "Available algorithms (id: name)\n" );
124 
125  while ( ( metadata = *availables++ ) ) {
126 
127  printf( " %s: %s\n", metadata->id.c_str(), metadata->name.c_str() );
128  }
129 
130  return 0;
131  }
132 
133  if ( ! alg_id.empty() ) {
134 
135  AlgMetadata const *alg_metadata = AlgorithmFactory::algorithmMetadata( alg_id.c_str() );
136 
137  printf( "Algorithm metadata:\n" );
138  printf( " Name: %s\n", alg_metadata->name.c_str() );
139  printf( " Version: %s\n", alg_metadata->version.c_str() );
140  printf( " Overview:\n\n%s\n\n", alg_metadata->overview.c_str() );
141  printf( " Description:\n\n%s\n\n", alg_metadata->description.c_str() );
142  printf( " Author(s): %s\n", alg_metadata->author.c_str() );
143  printf( " Bibliography:\n\n%s\n\n", alg_metadata->biblio.c_str() );
144  printf( " Developer(s): %s (%s)\n", alg_metadata->code_author.c_str(), alg_metadata->contact.c_str() );
145 
146  std::string const accepts_categorical = ( alg_metadata->categorical ) ? "yes" : "no";
147  printf( " Accepts categorical data: %s\n", accepts_categorical.c_str() );
148 
149  std::string const requires_absences = ( alg_metadata->absence ) ? "yes" : "no";
150  printf( " Requires absence points: %s\n", requires_absences.c_str() );
151 
152  AlgParamMetadata *param = alg_metadata->param;
153 
154  // Include parameters metadata
155  for ( int i = 0 ; i < alg_metadata->nparam; param++, i++ ) {
156 
157  printf( "\n Parameter: %s\n", param->id.c_str() );
158  printf( " Name: %s\n", param->name.c_str() );
159 
160  string datatype("?");
161 
162  if ( param->type == Integer ) {
163 
164  datatype = "Integer";
165  }
166  else if ( param->type == Real ) {
167 
168  datatype = "Real";
169  }
170  else if ( param->type == String ) {
171 
172  datatype = "String";
173  }
174 
175  printf( " Datatype: %s\n", datatype.c_str() );
176 
177  printf( " Overview: %s\n", param->overview.c_str() );
178 
179  printf( " Description: %s\n", param->description.c_str() );
180 
181  if ( param->has_min || param->has_max ) {
182 
183  if ( param->has_min ) {
184 
185  printf( " Minimum value: %f\n", param->min_val );
186  }
187  if ( param->has_max ) {
188 
189  printf( " Maximum value: %f\n", param->max_val );
190  }
191  }
192 
193  printf( " Typical value: %s\n", param->typical.c_str() );
194  }
195  }
196  }
197  catch ( runtime_error e ) {
198 
199  printf( "om_algorithm: %s\n", e.what() );
200  exit(1);
201  }
202 
203  return 0;
204 }
int categorical
If not zero accept categorical maps.
Definition: AlgMetadata.hh:87
static void loadConfig(const std::string configFile)
Definition: Settings.cpp:100
std::string author
Algorithm's author.
Definition: AlgMetadata.hh:81
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
int main(int argc, char **argv)
Log::Level getLogLevel(std::string level)
std::string id
Identifier to be used by programmers.
Definition: AlgMetadata.hh:75
Level
Definition: Log.hh:54
void setLevel(Level level)
Definition: Log.hh:107
AlgParamMetadata * param
Definition: AlgMetadata.hh:91
std::string code_author
Who implemented.
Definition: AlgMetadata.hh:84
std::string contact
code_author contact (eg e-mail).
Definition: AlgMetadata.hh:85
static ConfigurationPtr getConfiguration()
void setupExternalResources()
Definition: os_specific.cpp:95
AlgMetadata const ** availableAlgorithms()
static void writeXml(const ConstConfigurationPtr &config, char const *fileaname)
static int searchDefaultDirs()
int nparam
Number of parameters.
Definition: AlgMetadata.hh:89
std::string name
Name to be shown to end users.
Definition: AlgMetadata.hh:76
std::string getVersion()
static AlgMetadata const * algorithmMetadata(std::string const algorithm_id)
std::string biblio
Bibliography reference.
Definition: AlgMetadata.hh:82
std::string version
Built version.
Definition: AlgMetadata.hh:77
std::string description
Detailed description.
Definition: AlgMetadata.hh:79
int absence
Needs absence points to run.
Definition: AlgMetadata.hh:88
AlgMetadata metadata
Definition: garp.cpp:134
std::string overview
Short description.
Definition: AlgMetadata.hh:78