ERAV: Entity Relation Attribute Value:
test_ERAV.cpp
Go to the documentation of this file.
00001 // test_ERAV.cpp (C) 2010 adolfo@di-mare.com
00002 
00003 /** \file  test_ERAV.cpp
00004     \brief Test -> ERAV -> erav2xml() -> xml2erav().
00005 
00006     THIS IS PUBLIC DOMAIN SOFTWARE.
00007     - Use at you own risk
00008     - yes: it´s free.
00009 
00010     \author Adolfo Di Mare <adolfo@di-mare.com>
00011     \date   2010.
00012 */
00013 
00014 #include "ERAV.h"
00015 
00016 #include <fstream>
00017 #include <iomanip>    // setw()
00018 #include <iostream>
00019 
00020 const char* ERAV::token::name(int tk) {
00021     // centered string when tk is ASCII
00022     static char ch[] = { '?',' ',' ',' ', 0 };
00023     switch (tk) {
00024         case 0:      return "ZERO ";
00025         case LTQ:    return "LTQ ";
00026         case QGT:    return "QGT ";
00027         case LTS:    return "LTS ";
00028         case SGT:    return "SGT ";
00029         case ID:     return "ID  ";
00030         case STRING: return "STRING";
00031         default:     *ch=tk; return ch;
00032     }
00033 }
00034 
00035 // These are the 3 variables that yylex() needs to keep its context.
00036 struct yylex_state {
00037     const char *cursor;
00038     const char *text;
00039     int         leng;
00040     int         line;
00041 };
00042 
00043 // token output in 1 line.
00044 void lexeme_line( int token, const yylex_state& yy ) {
00045     std::cout << "token(" << std::setw(6) << ERAV::token::name(token) << ") ";
00046     std::cout << " len("  << std::setw(2) << yy.leng << ") ";
00047     std::cout << " #ln( "  << std::setw(3) << yy.line << " )  lexeme == ";
00048     const char *y=yy.text; int len15 = (yy.leng>15 ? 15 : yy.leng);
00049     for (int i=0; i<len15; ++i,++y) { std::cout << (*y); }
00050     if ( yy.leng!=len15 ) { std::cout << " ....<"<< yy.leng << ">.... "; }
00051     std::cout << std::endl;
00052 }
00053 
00054 // Store into std::cin all tokens in file
00055 int test_yylex( const char *file ) {
00056     std::fstream cin( file );
00057     std::string buf;
00058     while ( !cin.fail() ) {
00059         char ch;
00060         cin.get(ch);
00061         if (!cin.fail()) { buf += ch; }
00062     }
00063     char *tmp = new char[buf.length()+1];
00064     buf.copy( tmp, buf.length(),0 );
00065     tmp[buf.length()] = 0;
00066     if (false) { std::cout << tmp << std::endl; }
00067     int token;
00068     yylex_state yy;  yy.cursor = tmp; yy.line=1;
00069     do {
00070         token = ERAV::yylex( &yy.cursor, &yy.text, &yy.leng, &yy.line );
00071         lexeme_line( token , yy );
00072     } while (token != 0);
00073     return 0;
00074 }
00075 
00076 // Create tuple <id,rel,sub,t,attrib,val>.
00077 // See http://www.di-mare.com/adolfo/cursos/2009-1/ERAV.jpg
00078 ERAV::tuple make_tuple(
00079     int id, int rel , int sub, char t, int attrib, std::string val
00080 ) {
00081     ERAV::tuple T;
00082     T.ID_ENT   = id;
00083     T.REL.ID   = rel;
00084     T.REL.SUB  = sub;
00085     T.REL.TYPE = tolower(t);
00086     T.ATTRIB   = attrib;
00087     T.VAL.BLOB = val;
00088     return T;
00089 }
00090 
00091 // Load a complete ERAV list<> with it´s words and strings
00092 void megaload(
00093     std::list<ERAV::tuple>& L ,
00094     ERAV::WORD&             W ,
00095     ERAV::WORD&             S
00096 ) {
00097     erav_clear(L,W,S);
00098 
00099     #define mktuple(id,rel,sub,t,a,v) L.push_back( make_tuple(id,rel,sub,t,a,v) )
00100 
00101     //       ID_ENT   REL_ID REL_SUB  T    ATTRIB      VAL
00102     mktuple( 1110111 , 0000 , 0000 , 'e' , 0001111 , ""         );
00103     mktuple( 1110111 , 0000 , 0000 , 's' , 2220000 , "7700002"  );
00104     mktuple( 1110111 , 0000 , 0000 , 'i' , 0001234 , "2"        );
00105     mktuple( 1110111 , 0000 , 0000 , 'a' , 1110111 , "1110123"  );
00106     mktuple( 2220222 , 0000 , 0000 , 'e' , 0001111 , ""         );
00107     mktuple( 2220222 , 0000 , 0000 , 's' , 2220000 , "7700001"  );
00108     mktuple( 1110111 , 0101 , 0000 , 'e' , 2220021 , ""         );
00109     mktuple( 1110111 , 0101 , 0000 , 's' , 5000002 , "7700021"  );
00110     mktuple( 1110111 , 0101 , 0000 , 'i' , 2220011 , "88887753" );
00111     mktuple( 1110111 , 0101 , 0000 , 's' , 5000005 , "7700022"  );
00112     mktuple( 1110111 , 0201 , 0101 , 'e' , 2220022 , ""         );
00113     mktuple( 1110111 , 0201 , 0101 , 'd' , 5000003 , "20080923" );
00114     mktuple( 1110111 , 0201 , 0101 , 'f' , 5000004 , "250.55"   );
00115     mktuple( 1110111 , 0202 , 0101 , 'e' , 2220022 , ""         );
00116     mktuple( 1110111 , 0202 , 0101 , 'd' , 5000003 , "20081025" );
00117     mktuple( 1110111 , 0202 , 0101 , 'f' , 2220024 , "386.12"   );
00118     mktuple( 1110111 , 0202 , 0101 , 'f' , 5000004 , "256.55"   );
00119     mktuple( 1110111 , 0203 , 0101 , 'e' , 2220022 , ""         );
00120     mktuple( 1110111 , 0203 , 0101 , 'd' , 5000003 , "20081112" );
00121     mktuple( 1110111 , 0203 , 0101 , 'f' , 5000004 , "260.33"   );
00122     mktuple( 1110111 , 0102 , 0000 , 'e' , 2220021 , ""         );
00123     mktuple( 1110111 , 0102 , 0000 , 's' , 5000002 , "7700024"  );
00124     mktuple( 1110111 , 0102 , 0000 , 'i' , 2220011 , "77538888" );
00125     mktuple( 1110111 , 0102 , 0000 , 's' , 5000005 , "7700025"  );
00126     mktuple( 1110111 , 0205 , 0102 , 'e' , 2220022 , ""         );
00127     mktuple( 1110111 , 0205 , 0102 , 'd' , 5000003 , "20090615" );
00128     mktuple( 1110111 , 0205 , 0102 , 'f' , 5000004 , "325.12"   );
00129     mktuple( 1110111 , 0301 , 0000 , 'e' , 2220023 , ""         );
00130     mktuple( 1110111 , 0301 , 0000 , 'd' , 5000003 , "20051231" );
00131     mktuple( 1110111 , 0302 , 0000 , 's' , 2220023 , "7700023"  );
00132     mktuple( 2220222 , 0101 , 0000 , 'e' , 2220023 , ""         );
00133     mktuple( 2220222 , 0101 , 0000 , 's' , 5000002 , "7700024"  );
00134     mktuple( 2220222 , 0101 , 0000 , 'i' , 2220011 , "77538888" );
00135     mktuple( 2220222 , 0101 , 0000 , 's' , 5000005 , "7700025"  );
00136     mktuple( 2220222 , 0201 , 0000 , 'e' , 2220024 , ""         );
00137     mktuple( 2220222 , 0201 , 0000 , 'd' , 5000003 , "20051302" );
00138 
00139     #undef mktuple
00140 
00141     //        ID_WORD   LANG   DESCR
00142     W.update( 0001111 , "es", "automotor"        );
00143     W.update( 0001234 , "es", "cantidad puertas" );
00144     W.update( 0001234 , "en", "number doors"     );
00145     W.update( 1110111 , "es", "color"            );
00146     W.update( 1110123 , "es", "azul"             );
00147     W.update( 2220000 , "es", "marca"            );
00148     W.update( 2220000 , "en", "brand"            );
00149     W.update( 2220011 , "es", "teléfono"         );
00150     W.update( 2220011 , "en", "phone"            );
00151     W.update( 2220021 , "es", "mecánico"         );
00152     W.update( 2220022 , "es", "revisión"         );
00153     W.update( 2220023 , "es", "dueño"            );
00154     W.update( 2220024 , "es", "aceite"           );
00155     W.update( 5000001 , "es", "alógeno"          );
00156     W.update( 5000002 , "es", "nombre"           );
00157     W.update( 5000003 , "es", "fecha"            );
00158     W.update( 5000004 , "es", "monto"            );
00159     W.update( 5000005 , "es", "dirección"        );
00160     W.update( 6660011 , "es", "foto"             );
00161     W.update( 2220021 , "en", "mechanic"         );
00162     W.update( 2220022 , "en", "revision"         );
00163     W.update( 2220023 , "en", "owner"            );
00164     W.update( 2220024 , "en", "oil"              );
00165     W.update( 5000001 , "en", "halogen"          );
00166     W.update( 5000002 , "en", "name"             );
00167     W.update( 5000003 , "en", "date"             );
00168     W.update( 5000004 , "en", "amount"           );
00169     W.update( 5000005 , "en", "address"          );
00170     W.update( 6660011 , "en", "photo"            );
00171 
00172     //       ID_STRIN   LANG   DESCR
00173     S.update( 7700001 , "**", "hyundai"                       );
00174     S.update( 7700002 , "**", "toyota"                        );
00175     S.update( 7700021 , "**", "Juan Mecánico"                 );
00176     S.update( 7700022 , "es", "200 Sur del Palo de Guayaba"   );
00177     S.update( 7700022 , "en", "200 South of the Guayaba tree" );
00178     S.update( 7700023 , "**", "Pepe Lerdeando"                );
00179     S.update( 7700024 , "**", "Pedro Mecánico"                );
00180     S.update( 7700025 , "es", "3 kms al este del Guayabero"   );
00181     S.update( 7700025 , "en", "2 miles east from Guayabero"   );
00182 }
00183 
00184 extern void dump_tuples( const std::list<ERAV::tuple>& L );
00185 
00186 void dump_tuples( const std::list<ERAV::tuple>& L ) {
00187     using namespace std;
00188     if ( L.empty() ) { std::cout << " [ L.empty() ]\n"; }
00189     std::list<ERAV::tuple>::const_iterator it;
00190     for ( it=L.begin(); it!=L.end() ; ++it ) {
00191         cout << setw(8) << it->ID_ENT << " [" << it->REL.ID << ',' << it->REL.SUB << "](";
00192         cout << it->REL.TYPE << ") " << setw(8) << it->ATTRIB << ' ' << it->VAL.BLOB << endl;
00193     }
00194 }
00195 
00196 void dump_words( const char* name , const ERAV::WORD& W ) {
00197     std::list<ERAV::WORD_tuple> L;
00198     W.toList(L);
00199     if ( L.empty() ) { std::cout << " [ " << name << ".empty() ]\n"; }
00200     std::list<ERAV::WORD_tuple>::const_iterator it;
00201     for ( it=L.begin(); it!=L.end() ; ++it ) {
00202         std::cout << std::setw(8) << it->ID_WORD;
00203         std::cout << " [" << it->LANG[0] << it->LANG[1] << "] ";
00204         std::cout << it->DESCR << std::endl;
00205     }
00206 }
00207 
00208 // TD is the test subdirectory for test_ERAV.cpp
00209 const std::string TD = "test/";
00210 
00211 void do_megaload() {
00212     std::fstream COUT;
00213     std::string XML_adolfo;
00214     std::list<ERAV::tuple> L;
00215     ERAV::WORD W,S;
00216     COUT.open( (TD+"XML_adolfo.bak").c_str() , std::ios_base::out);
00217     megaload(L,W,S);
00218     erav2xml(L,W,S,   XML_adolfo );
00219     erav2xml_enclose( XML_adolfo );
00220     COUT << XML_adolfo;
00221     COUT.close();
00222 }
00223 
00224 int do_file( const char* name ) {
00225     int ret=0;
00226     std::string file = name;
00227     file = TD + file;
00228     std::list<ERAV::tuple> L;
00229     ERAV::WORD W,S;
00230     std::fstream COUT;
00231     std::string XML, XML_bak, XML_bak_bak;
00232 
00233     if (false) { ret=test_yylex( file.c_str() ); }
00234     std::fstream cin( file.c_str() );
00235     char ch;
00236     while ( !cin.fail() ) {
00237         cin.get(ch);
00238         if (!cin.fail()) { XML += ch; }
00239     }
00240     if (false) { std::cout << XML << std::endl; }
00241     else { std::cout << '\n' << file << " ==============\n\n"; }
00242     try {
00243         erav_clear(     L,W,S );
00244         xml2erav( XML , L,W,S );           // append to L && W && S
00245         erav2xml(       L,W,S , XML_bak ); // append to XML
00246         erav2xml_enclose(       XML_bak ); // put header and <erav> tags
00247         COUT.open(   (file+".bak").c_str() , std::ios_base::out);
00248         std::cout << (file+".bak").c_str() << std::endl;
00249         COUT<<XML_bak;
00250         COUT.close();
00251 
00252         erav_clear(         L,W,S );
00253         xml2erav( XML_bak , L,W,S );              // append to L && W && S
00254         erav2xml(           L,W,S, XML_bak_bak ); // append to XML_bak
00255         erav2xml_enclose(          XML_bak_bak ); // put header and <erav> tags
00256         COUT.open(   (file+".bak"+".bak").c_str() , std::ios_base::out);
00257         std::cout << (file+".bak"+".bak").c_str() << std::endl;
00258         COUT<<XML_bak_bak;
00259         COUT.close();
00260     }
00261     catch ( ERAV::exception e ) { std::cout << e.what(); }
00262     catch ( ... ) { }
00263     dump_words( "WORD"   , W );
00264     dump_words( "STRING" , S );
00265     dump_tuples( L );
00266     return ( L.empty() || W.empty() || S.empty() ? 0 : 1 );
00267 }
00268 
00269 void load_XML(
00270     std::string file          , // file to read from
00271     std::list<ERAV::tuple>& L , // append to current value
00272     ERAV::WORD&             W , // append to current value
00273     ERAV::WORD&             S   // append to current value
00274 ) {
00275     std::string  XML;
00276     std::fstream cin( (TD+file).c_str() );
00277     char ch;
00278     while ( !cin.fail() ) {
00279         cin.get(ch);
00280         if (!cin.fail()) { XML += ch; }
00281     }
00282     try {
00283         xml2erav( XML , L,W,S ); // append to L && W && S
00284     }
00285     catch ( ERAV::exception e ) { std::cout << e.what(); }
00286     catch ( ... ) { }
00287 }
00288 
00289 void do_tuple() {
00290     std::list<ERAV::tuple> L;
00291     ERAV::WORD W,S;
00292     assert( L.empty() && W.empty() && S.empty() );
00293     for ( int i=0; i<5; ++i ) {
00294         load_XML( "STRING.xml"   , L , W , S );
00295         load_XML( "2220222.xml"  , L , W , S );
00296 
00297         load_XML( "ERAV-base.xml", L , W , S );
00298 
00299         load_XML( "WORD.xml"     , L , W , S );
00300         load_XML( "1110111.xml"  , L , W , S );
00301     }
00302     std::string XML_multiple, XML_base;
00303     erav2xml(   L , W , S , XML_multiple );
00304     erav_clear( L , W , S );
00305     xml2erav(   XML_multiple , L , W , S );
00306     erav2xml(   L , W , S , XML_base );
00307     if ( XML_base != XML_multiple ) {
00308         std::cout << "do_tuple() error\n";
00309     }
00310     assert( XML_base == XML_multiple );
00311 
00312     erav_clear( L , W , S );
00313     XML_multiple.clear();
00314     XML_base.clear();
00315     std::list<ERAV::tuple>     FROM;
00316     load_XML( "WORD.xml"     , FROM , W , S ); ERAV::merge_into(L,FROM);
00317     load_XML( "1110111.xml"  , FROM , W , S ); ERAV::merge_into(L,FROM);
00318     load_XML( "STRING.xml"   , FROM , W , S ); ERAV::merge_into(L,FROM);
00319     load_XML( "2220222.xml"  , FROM , W , S ); ERAV::merge_into(L,FROM);
00320     erav2xml( L , W , S , XML_multiple );
00321 
00322     XML_base.clear();
00323     erav_clear( L , W , S );
00324     load_XML( "WORD.xml"     , L , W , S ); erav2xml( L , W , S , XML_base );
00325     load_XML( "1110111.xml"  , L , W , S ); erav2xml( L , W , S , XML_base );
00326     load_XML( "STRING.xml"   , L , W , S ); erav2xml( L , W , S , XML_base );
00327     load_XML( "2220222.xml"  , L , W , S ); erav2xml( L , W , S , XML_base );
00328 
00329     erav_clear( L , W , S );   xml2erav( XML_base , L , W , S );
00330     XML_base.clear();          erav2xml( L , W , S , XML_base );
00331 
00332     if ( XML_base != XML_multiple ) {
00333         std::cout << "do_tuple() error\n";
00334         std::cout << XML_base;
00335     }
00336     assert( XML_base == XML_multiple );
00337 }
00338 
00339 // Main program.
00340 int main(int argc, char **argv ) {
00341     char* files[] = {  "noerav.xml",    "test_ERAV.cpp",
00342                        "ERAV-base.xml", "test_ERAV.cbp", "Adolfo.xml", // 0 1 2
00343                        "sierav.xml",   "noerav.xml",    "Erav.h",     // 3 4 5
00344                        "1110111.XML",  "WORD.XML"                  }; // 6 7
00345     int ret=0;
00346     do_megaload();
00347 
00348     #define DIM(V) (sizeof(V)/sizeof(*V))
00349     for ( unsigned i=0; i<DIM(files);++i ) {
00350         int r = do_file( files[i] );
00351         ret = ( ret==0 ? r : ret );
00352     }
00353     do_tuple();
00354     return ret;  // ( ! L.empty() ? 0 : 1 )
00355 }
00356 
00357 // EOF: test_ERAV.cpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines