Added eoParser
This commit is contained in:
parent
46b6a9e17a
commit
0d8648c0e6
8 changed files with 1038 additions and 7 deletions
|
|
@ -13,7 +13,7 @@ LDADDS = $(top_builddir)/src/libeo.a
|
|||
|
||||
###############################################################################
|
||||
|
||||
noinst_PROGRAMS = t-eobreeder t-eoinclusion t-eoinsertion t-eo t-eofitness t-eoproblem t-eobin t-eolottery t-eo2dVector t-eogeneration t-eoEasyEA t-eoNonUniform t-eoUniform t-eoRandom
|
||||
noinst_PROGRAMS = t-eobreeder t-eoinclusion t-eoinsertion t-eo t-eofitness t-eoproblem t-eobin t-eolottery t-eo2dVector t-eogeneration t-eoEasyEA t-eoNonUniform t-eoUniform t-eoRandom t-parser
|
||||
|
||||
###############################################################################
|
||||
|
||||
|
|
@ -109,3 +109,10 @@ t_eo2dVector_LDFLAGS = -lm
|
|||
t_eo2dVector_LDADD = $(LDADDS)
|
||||
|
||||
###############################################################################
|
||||
|
||||
t_parser_SOURCES = t-parser.cpp
|
||||
t_parser_DEPENDENCIES = $(DEPS)
|
||||
t_parser_LDFLAGS = -lm
|
||||
t_parser_LDADD = $(LDADDS)
|
||||
|
||||
###############################################################################
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ LDADDS = $(top_builddir)/src/libeo.a
|
|||
|
||||
###############################################################################
|
||||
|
||||
noinst_PROGRAMS = t-eobreeder t-eoinclusion t-eoinsertion t-eo t-eofitness t-eoproblem t-eobin t-eolottery t-eo2dVector t-eogeneration t-eoEasyEA t-eoNonUniform t-eoUniform t-eoRandom
|
||||
noinst_PROGRAMS = t-eobreeder t-eoinclusion t-eoinsertion t-eo t-eofitness t-eoproblem t-eobin t-eolottery t-eo2dVector t-eogeneration t-eoEasyEA t-eoNonUniform t-eoUniform t-eoRandom t-parser
|
||||
|
||||
###############################################################################
|
||||
|
||||
|
|
@ -180,6 +180,13 @@ t_eo2dVector_SOURCES = t-eo2dVector.cc
|
|||
t_eo2dVector_DEPENDENCIES = $(DEPS)
|
||||
t_eo2dVector_LDFLAGS = -lm
|
||||
t_eo2dVector_LDADD = $(LDADDS)
|
||||
|
||||
###############################################################################
|
||||
|
||||
t_parser_SOURCES = t-parser.cpp
|
||||
t_parser_DEPENDENCIES = $(DEPS)
|
||||
t_parser_LDFLAGS = -lm
|
||||
t_parser_LDADD = $(LDADDS)
|
||||
mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
|
||||
CONFIG_CLEAN_FILES =
|
||||
PROGRAMS = $(noinst_PROGRAMS)
|
||||
|
|
@ -208,6 +215,7 @@ t_eoEasyEA_OBJECTS = t-eoEasyEA.o
|
|||
t_eoNonUniform_OBJECTS = t-eoNonUniform.o
|
||||
t_eoUniform_OBJECTS = t-eoUniform.o
|
||||
t_eoRandom_OBJECTS = t-eoRandom.o
|
||||
t_parser_OBJECTS = t-parser.o
|
||||
CXXFLAGS = @CXXFLAGS@
|
||||
CXXCOMPILE = $(CXX) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
|
||||
LTCXXCOMPILE = $(LIBTOOL) --mode=compile $(CXX) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
|
||||
|
|
@ -229,9 +237,9 @@ DEP_FILES = .deps/t-eo.P .deps/t-eo2dVector.P .deps/t-eoEasyEA.P \
|
|||
.deps/t-eoNonUniform.P .deps/t-eoRandom.P .deps/t-eoUniform.P \
|
||||
.deps/t-eobin.P .deps/t-eobreeder.P .deps/t-eofitness.P \
|
||||
.deps/t-eogeneration.P .deps/t-eoinclusion.P .deps/t-eoinsertion.P \
|
||||
.deps/t-eolottery.P .deps/t-eoproblem.P
|
||||
SOURCES = $(t_eobreeder_SOURCES) $(t_eoinclusion_SOURCES) $(t_eoinsertion_SOURCES) $(t_eo_SOURCES) $(t_eofitness_SOURCES) $(t_eoproblem_SOURCES) $(t_eobin_SOURCES) $(t_eolottery_SOURCES) $(t_eo2dVector_SOURCES) $(t_eogeneration_SOURCES) $(t_eoEasyEA_SOURCES) $(t_eoNonUniform_SOURCES) $(t_eoUniform_SOURCES) $(t_eoRandom_SOURCES)
|
||||
OBJECTS = $(t_eobreeder_OBJECTS) $(t_eoinclusion_OBJECTS) $(t_eoinsertion_OBJECTS) $(t_eo_OBJECTS) $(t_eofitness_OBJECTS) $(t_eoproblem_OBJECTS) $(t_eobin_OBJECTS) $(t_eolottery_OBJECTS) $(t_eo2dVector_OBJECTS) $(t_eogeneration_OBJECTS) $(t_eoEasyEA_OBJECTS) $(t_eoNonUniform_OBJECTS) $(t_eoUniform_OBJECTS) $(t_eoRandom_OBJECTS)
|
||||
.deps/t-eolottery.P .deps/t-eoproblem.P .deps/t-parser.P
|
||||
SOURCES = $(t_eobreeder_SOURCES) $(t_eoinclusion_SOURCES) $(t_eoinsertion_SOURCES) $(t_eo_SOURCES) $(t_eofitness_SOURCES) $(t_eoproblem_SOURCES) $(t_eobin_SOURCES) $(t_eolottery_SOURCES) $(t_eo2dVector_SOURCES) $(t_eogeneration_SOURCES) $(t_eoEasyEA_SOURCES) $(t_eoNonUniform_SOURCES) $(t_eoUniform_SOURCES) $(t_eoRandom_SOURCES) $(t_parser_SOURCES)
|
||||
OBJECTS = $(t_eobreeder_OBJECTS) $(t_eoinclusion_OBJECTS) $(t_eoinsertion_OBJECTS) $(t_eo_OBJECTS) $(t_eofitness_OBJECTS) $(t_eoproblem_OBJECTS) $(t_eobin_OBJECTS) $(t_eolottery_OBJECTS) $(t_eo2dVector_OBJECTS) $(t_eogeneration_OBJECTS) $(t_eoEasyEA_OBJECTS) $(t_eoNonUniform_OBJECTS) $(t_eoUniform_OBJECTS) $(t_eoRandom_OBJECTS) $(t_parser_OBJECTS)
|
||||
|
||||
all: all-redirect
|
||||
.SUFFIXES:
|
||||
|
|
@ -340,6 +348,10 @@ t-eoUniform: $(t_eoUniform_OBJECTS) $(t_eoUniform_DEPENDENCIES)
|
|||
t-eoRandom: $(t_eoRandom_OBJECTS) $(t_eoRandom_DEPENDENCIES)
|
||||
@rm -f t-eoRandom
|
||||
$(CXXLINK) $(t_eoRandom_LDFLAGS) $(t_eoRandom_OBJECTS) $(t_eoRandom_LDADD) $(LIBS)
|
||||
|
||||
t-parser: $(t_parser_OBJECTS) $(t_parser_DEPENDENCIES)
|
||||
@rm -f t-parser
|
||||
$(CXXLINK) $(t_parser_LDFLAGS) $(t_parser_OBJECTS) $(t_parser_LDADD) $(LIBS)
|
||||
.cc.o:
|
||||
$(CXXCOMPILE) -c $<
|
||||
.cc.lo:
|
||||
|
|
|
|||
165
eo/test/t-parser.cpp
Normal file
165
eo/test/t-parser.cpp
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/* parser.cpp
|
||||
example of use of Parser.h
|
||||
|
||||
(c) geneura team, 1999
|
||||
-----------------------------------------------------------------------------*/
|
||||
|
||||
#include <eoParser.h>
|
||||
#include <eoRNG.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
void GetOutputParam(Parser & parser,
|
||||
string & _string) {
|
||||
|
||||
try {
|
||||
parser.AddTitle("Separate parameter: the output file name");
|
||||
_string = parser.getString("-O", "--OutputFile", "", "The output file name" );
|
||||
} catch (UException & e) {
|
||||
cout << e.what() << endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
} catch (exception & e) {
|
||||
cout << e.what() << endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void sub(Parser & parser) {
|
||||
int i;
|
||||
cout << "Function sub:" << endl;
|
||||
|
||||
try {
|
||||
parser.AddTitle("Private parameters of subroutine sub");
|
||||
i = parser.getInt("-j", "--sint", "5", "private integer of subroutine" );
|
||||
} catch (UException & e) {
|
||||
cout << e.what() << endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
} catch (exception & e) {
|
||||
cout << e.what() << endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
cout << "Read " << i << endl;
|
||||
}
|
||||
|
||||
|
||||
/// Uses the parser and returns param values
|
||||
void getParams( Parser & parser,
|
||||
unsigned & _integer,
|
||||
float & _floating,
|
||||
string & _string,
|
||||
vector<string> & _array,
|
||||
bool & _boolean) {
|
||||
|
||||
try {
|
||||
_integer = parser.getInt("-i", "--int", "2", "interger number" );
|
||||
_floating = parser.getFloat("-f", "--float", "0.2", "floating point number" );
|
||||
_string = parser.getString("-s", "--string", "string", "a string" );
|
||||
_array = parser.getArray("-a", "--array", "a b", "an array enclosed within < >" );
|
||||
_boolean = parser.getBool("-b","--bool", "a bool value" );
|
||||
}
|
||||
catch (UException & e)
|
||||
{
|
||||
cout << e.what() << endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
catch (exception & e)
|
||||
{
|
||||
cout << e.what() << endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// Uses the parser and returns param values
|
||||
void InitRandom( Parser & parser) {
|
||||
unsigned long _seed;
|
||||
try {
|
||||
_seed = parser.getUnsignedLong("-S", "--seed", "0", "Seed for Random number generator" );
|
||||
}
|
||||
catch (UException & e)
|
||||
{
|
||||
cout << e.what() << endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
catch (exception & e)
|
||||
{
|
||||
cout << e.what() << endl;
|
||||
exit(1);
|
||||
}
|
||||
if (_seed == 0) { // use clock to get a "random" seed
|
||||
struct timeval tval;
|
||||
struct timezone tzp;
|
||||
|
||||
gettimeofday (&tval, &tzp); // time since midnight January 1, 1970.
|
||||
_seed = tval.tv_usec ; // micro seconds
|
||||
char s[32];
|
||||
sprintf(s,"%ld", _seed);
|
||||
parser.setParamValue("--seed", s); // so it will be printed out in the status file, and canbe later re-used to re-run EXACTLY the same run
|
||||
}
|
||||
rng.reseed(_seed);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int main( int argc, char* argv[]) {
|
||||
|
||||
unsigned in;
|
||||
float f;
|
||||
string s;
|
||||
vector<string> a;
|
||||
bool b;
|
||||
|
||||
// Create the command-line parser
|
||||
Parser parser( argc, argv, "Parser example");
|
||||
InitRandom(parser);
|
||||
parser.AddTitle("General parameters");
|
||||
getParams(parser, in, f, s, a, b);
|
||||
|
||||
cout << "\n integer: " << in << endl
|
||||
<< " float: "<< f << endl
|
||||
<< " string: /"<< s << "/" << endl
|
||||
<< " boolean: "<< b << endl
|
||||
<< " array: < ";
|
||||
vector<string>::const_iterator i;
|
||||
for (i=a.begin() ; i<a.end() ; i++) {
|
||||
cout << *i << " ";
|
||||
}
|
||||
cout << ">" << endl << endl ;
|
||||
|
||||
// call to the subroutine that also needs some parameters
|
||||
sub(parser);
|
||||
|
||||
// writing all parameters
|
||||
//
|
||||
// if programmer wishes, the name of the output file can be set as a parameter itself
|
||||
// otherwise it will be argv[0].status
|
||||
string OutputFileName;
|
||||
GetOutputParam(parser, OutputFileName);
|
||||
|
||||
parser.outputParam(OutputFileName);
|
||||
if( parser.getBool("-h" , "--help" , "Shows this help")) {
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// but progrmamer should be careful to write the parser parameters
|
||||
// after the last bit that uses it has finished
|
||||
|
||||
|
||||
// Now the main body of the program
|
||||
|
||||
for (int i=0; i<20; i++) {
|
||||
cout << rng.normal() << endl;
|
||||
}
|
||||
cout << "C'est fini" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in a new issue