Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

test.cpp

00001 #include <stdlib.h>
00002 #include <iostream>
00003 #include <string>
00004 #include <ShockGraph.h>
00005 #include "matchingsystem.hpp"
00006 #include <Debug.h>
00007 
00008 using namespace std;
00009 
00010 const string error_msgs[] = {
00011         /*0*/"Not enough arguments", 
00012         /*1*/"Error creating the database", 
00013         /*2*/"Error occured during reading of the database",
00014         /*3*/"Parameters are incorrect. Use -h for help", 
00015         /*4*/"Could not open the command file",
00016         /*5*/"Error occured during writing of the database",
00017         /*6*/"Error reading the command file",
00018         /*7*/"Expect <DTCF_root>",
00019         /*8*/"Expect <types>",
00020         /*9*/"Expect </DTCF_root>",
00021         /*10*/"Expect <type>",
00022         /*11*/"Expect </type>",
00023         /*12*/"Expect <name>",
00024         /*13*/"Expect </name>",
00025         /*14*/"Expect <regions>",
00026         /*15*/"Expect <region>",
00027         /*16*/"Expect </region>",
00028         /*17*/"Expect <prototype>",
00029         /*18*/"Expect </prototype>",
00030         /*19*/"Expect <id>",
00031         /*20*/"Expect </id>",
00032         /*21*/"Expect <dag>",
00033         /*22*/"Expect </dag>",
00034         /*23*/"Expect <views>",
00035         /*24*/"Expect </DTCF root>",
00036         /*25*/"Cannot find type with the given name",
00037         /*26*/"Cannot open output file",
00038         /*27*/"Cannot write to output file",
00039         /*28*/"Cannot find a view with an id",
00040         /*29*/"Cannot find a dag with an id",
00041         /*30*/"Cannot find a prototype with a given name",
00042         /*31*/"Cannot find a prototype with a given id",
00043         /*32*/"Cannot find a type with a given id",
00044         /*33*/"The dimension of the database should be between one and fourty",
00045         /*34*/"Cannot open shockgraph file",
00046         /*35*/"Expect <view>",
00047         /*36*/"Expect </view>"
00048 };
00049 
00050 istream& read_command(istream& is, string& s)
00051 {
00052         char p;
00053         char c[256];
00054         int i;
00055 
00056         do {
00057                 is.read(&p, sizeof(p)); 
00058         } while(p != '<' && p != '>');
00059 
00060         i=-1;
00061         do {
00062                 i++;
00063                 is.read(&c[i], sizeof(char));
00064         }
00065         while((i < 255) && (c[i] != '<') && (c[i] != '>'));
00066 
00067         c[i] = '\0';
00068         s = string(c);
00069         return is;
00070 }
00071 
00072 ostream& PrintHelp(ostream& os)
00073 {
00074         os << "Usage:   test {-c|-r|-h} [dimensions] [commandfile] [outputfile] [datafile] [file] ..." << endl << endl;
00075         os << "-c creates new database" << endl;
00076         os << "-r reads database from config file" << endl;
00077         os << "-h print out this help" << endl;
00078 
00079         return os;
00080 }
00081 
00082 void error_exit(int i)
00083 {
00084         cerr << "Error " << i << ": " << error_msgs[i] << endl;
00085         exit(3);
00086 }
00087 
00088 int main(int argc, char *argv[])
00089 {
00090         int i;
00091         bool q;
00092         list< pair<string, ID> > view_map;
00093         map<ID, string> prototype_map;
00094         int dim;
00095         string idstr;
00096         string outfile;
00097         string cfile;
00098         string param;
00099         string names[7];
00100         ID prid;
00101         
00102         names[0] = "database.cnf";
00103         names[1] = "dags.dat";
00104         names[2] = "index.dat";
00105         names[3] = "prototypes.dat";
00106         names[4] = "regions.dat";
00107         names[5] = "types.dat";
00108         names[6] = "views.dat";
00109         
00110 
00111         DAGMatchingDatabase<ShockGraph> dmd;
00112         DAGMatcher<ShockGraph> matcher(dmd);
00113 
00114 
00115         //Check whether program parameters where indicated correctly
00116         if(argc < 2) 
00117         {
00118                 cerr << error_msgs[0] << endl;
00119                 PrintHelp(cout);
00120                 exit(3);
00121         }
00122 
00123         param = string(argv[1]);
00124 
00125         if(param == "-c" || param== "-r")
00126         {
00127 
00128                 //Check whether program parameters where indicated correctly
00129                 if(argc < 5) 
00130                 {
00131                         cerr << error_msgs[0] << endl;
00132                         PrintHelp(cout);
00133                         exit(3);
00134                 }
00135 
00136                 dim = atoi(argv[2]);
00137                 cout << "Dim is " << dim << endl;
00138                 if(dim < 1 || dim > 40) error_exit(33);
00139 
00140                 //Read command file name
00141                 cfile = string(argv[3]);
00142 
00143                 //Read output file
00144                 outfile = string(argv[4]);
00145         
00146                 for(i=5; i < argc && i < 12; i++)
00147                 {
00148                         names[i-5] = string(argv[i]);
00149                 }
00150 
00151                 if(param == "-c")
00152                 {
00153                         q = dmd.DAGs.Create(names[1]);
00154 //                      if(q) cout << "good1" << endl; else cout << "bad1" << endl;
00155                         q = q && dmd.prototypes.Create(names[3]);
00156 //                      if(q) cout << "good3" << endl; else cout << "bad3" << endl;
00157                         q = q && dmd.regions.Create(names[4]);
00158 //                      if(q) cout << "good4" << endl; else cout << "bad4" << endl;
00159                         q = q && dmd.types.Create(names[5]);
00160 //                      if(q) cout << "good5" << endl; else cout << "bad5" << endl;
00161                         q = q && dmd.views.Create(names[6]); 
00162 //                      if(q) cout << "good6" << endl; else cout << "bad6" << endl;
00163                         q = q && dmd.index.Create(names[2], dim);
00164 //                      if(q) cout << "good2" << endl; else cout << "bad2" << endl;
00165 
00166                         if(q)
00167                         {
00168                                 cout << "Databases was created successfuly" << endl;
00169                         }
00170                         else
00171                         {
00172                                 error_exit(1);
00173                         }
00174                 }
00175                 else
00176                 {
00177                         if(dmd.Read(names[0]) == false)
00178                         {
00179                                 error_exit(2);
00180                         }
00181                         else
00182                         {
00183                                 cout << "Reading was performed successfuly" << endl;
00184                         }
00185                 }
00186         }
00187         else if(param == "-h")
00188         {
00189                 PrintHelp(cout);
00190                 exit(3);
00191         }
00192         else
00193         {
00194                 cerr << error_msgs[3] << endl;
00195                 PrintHelp(cout);
00196 
00197                 exit(3);
00198         }
00199 
00200 
00201         //If we got to here than database has been created successfuly
00202         //Now we start creating the database based on command file
00203         fstream f;
00204         string instrn;
00205         ID regid;
00206         ID tid;
00207         ID dagid = 0;
00208         ShockGraph dag;
00209 
00210         f.open(cfile.c_str(), ios::in | ios::nocreate);
00211         if(!f) error_exit(4);
00212 
00213         if(!read_command(f, instrn)) error_exit(6);
00214         if(instrn != "DTCF_root")       error_exit(7);
00215         
00216         if(!read_command(f, instrn)) error_exit(6);
00217         if(instrn != "types") error_exit(8);
00218 
00219         if(!read_command(f, instrn)) error_exit(6);
00220         do
00221         {
00222                 if(instrn != "type") error_exit(10);
00223 
00224                         if(!read_command(f, instrn)) error_exit(6);
00225                         if(instrn != "name") error_exit(12);
00226 
00227                                 if(!read_command(f, instrn)) error_exit(6);
00228                                 dmd.types.Add(DAGType(instrn));
00229 
00230                         if(!read_command(f, instrn)) error_exit(6);
00231                         if(instrn != "/name") error_exit(13);
00232 
00233                 if(!read_command(f, instrn)) error_exit(6);
00234                 if(instrn != "/type") error_exit(11);
00235 
00236                 if(!read_command(f, instrn)) error_exit(6);
00237         }
00238         while(instrn != "/types");
00239 
00240         if(!read_command(f, instrn)) error_exit(6);
00241         if(instrn != "regions") error_exit(14);
00242 
00243         if(!read_command(f, instrn)) error_exit(6);
00244         do
00245         {
00246                 if(instrn != "region") error_exit(15);
00247 
00248                 regid = dmd.regions.Add(DAGRegion());
00249 
00250                         if(!read_command(f, instrn)) error_exit(6);
00251                         if(instrn != "prototype") error_exit(17);
00252 
00253                                 if(!read_command(f, instrn)) error_exit(6);
00254                                 if(instrn != "id") error_exit(19);
00255                                 
00256                                         if(!read_command(f, idstr)) error_exit(6);
00257 
00258                                 if(!read_command(f, instrn)) error_exit(6);
00259                                 if(instrn != "/id") error_exit(20);
00260 
00261                                 if(!read_command(f, instrn)) error_exit(6);
00262                                 if(instrn != "type") error_exit(10);
00263                                 
00264                                         if(!read_command(f, instrn)) error_exit(6);
00265                                         tid = dmd.types.Find(instrn);
00266                                         if(tid == NULLID) error_exit(25);
00267 
00268                                 if(!read_command(f, instrn)) error_exit(6);
00269                                 if(instrn != "/type") error_exit(11);
00270 
00271                                 if(!read_command(f, instrn)) error_exit(6);
00272                                 if(instrn != "dag") error_exit(21);
00273                                 
00274                                         if(!read_command(f, instrn)) error_exit(6);
00275                                         if(!dag.ReadFromXMLFile(instrn.c_str())) error_exit(34);
00276                                         dagid = dmd.DAGs.Add(dag);
00277                                 if(!read_command(f, instrn)) error_exit(6);
00278                                 if(instrn != "/dag") error_exit(22);
00279                         
00280                                 prid = dmd.prototypes.Add(DAGPrototype(DAGView(tid, regid, dagid)));
00281                                 dmd.index.Add(prid, dag);
00282                                 prototype_map.insert(pair<ID, string>(prid, idstr));
00283 
00284                         if(!read_command(f, instrn)) error_exit(6);
00285                         if(instrn != "/prototype") error_exit(18);
00286 
00287                         if(!read_command(f, instrn)) error_exit(6);
00288                         if(instrn != "views") error_exit(23);
00289 
00290                         if(!read_command(f, instrn)) error_exit(6);
00291                         do
00292                         {
00293                                 if(instrn != "view") error_exit(35);
00294 
00295                                 if(!read_command(f, instrn)) error_exit(6);
00296                                 if(instrn != "id") error_exit(19);
00297                                 
00298                                         if(!read_command(f,idstr)) error_exit(6);
00299 
00300                                 if(!read_command(f, instrn)) error_exit(6);
00301                                 if(instrn != "/id") error_exit(20);
00302 
00303                                 if(!read_command(f, instrn)) error_exit(6);
00304                                 if(instrn != "type") error_exit(10);
00305                                 
00306                                         if(!read_command(f, instrn)) error_exit(6);
00307                                         tid = dmd.types.Find(instrn);
00308                                         if(tid == NULLID) error_exit(25);
00309 
00310                                 if(!read_command(f, instrn)) error_exit(6);
00311                                 if(instrn != "/type") error_exit(11);
00312 
00313                                 if(!read_command(f, instrn)) error_exit(6);
00314                                 if(instrn != "dag") error_exit(21);
00315                                 
00316                                         if(!read_command(f, instrn)) error_exit(6);
00317                                         if(!dag.ReadFromXMLFile(instrn.c_str())) error_exit(34);
00318                                         dagid = dmd.DAGs.Add(dag);
00319 
00320                                 if(!read_command(f, instrn)) error_exit(6);
00321                                 if(instrn != "/dag") error_exit(22);
00322 
00323                                 view_map.push_back(pair<string, ID>(idstr, dmd.views.Add(DAGView(tid, regid, dagid))));
00324 
00325                                 if(!read_command(f, instrn)) error_exit(6);
00326                                 if(instrn != "/view") error_exit(36);
00327 
00328                                 if(!read_command(f, instrn)) error_exit(6);
00329                         }
00330                         while(instrn != "/views");
00331 
00332                 if(!read_command(f, instrn)) error_exit(6);
00333                 if(instrn != "/region") error_exit(16);
00334 
00335                 if(!read_command(f, instrn)) error_exit(6);
00336         }
00337         while(instrn != "/regions");
00338 
00339 
00340         if(!read_command(f, instrn)) error_exit(6);
00341         if(instrn != "/DTCF_root")      error_exit(9);
00342 
00343         f.close();
00344 
00345         //By now we finished creating the database from command file
00346         //Here is where we match each view and record the results
00347         typename list< pair<string, ID> >::iterator p;
00348         vector< MatchData<ShockGraph> > results;
00349         int k = 2;
00350         pair<DAGView, bool> temp_a;
00351         pair<ShockGraph, bool> temp_b;
00352         pair<DAGType, bool> temp_c;
00353         pair<DAGPrototype, bool> temp_d;
00354         typename map<ID, string>::iterator mi;
00355 
00356         f.open(outfile.c_str(), ios::out);
00357         if(!f) error_exit(26);
00358 
00359         if(!(f << "<results>" << endl)) error_exit(27);
00360 
00361         for(p = view_map.begin(); p!=view_map.end(); p++)
00362         {
00363 
00364                 if(!(f << "<view>" << endl)) error_exit(27);
00365                 if(!(f << "<name>")) error_exit(27);
00366                 if(!(f << p->first)) error_exit(27);
00367                 if(!(f << "</name>" << endl)) error_exit(27);
00368 
00369                 temp_a = dmd.views[p->second];
00370                 if(!temp_a.second) error_exit(28);
00371 
00372                 temp_b = dmd.DAGs[temp_a.first.graph];
00373                 if(!temp_b.second) error_exit(29);
00374                 
00375                 matcher.GetSimilar(temp_b.first, results, k);
00376 
00377                 if(!(f << "<index matches>" << endl)) error_exit(27);
00378 
00379                 for(i = 0; i<results.size(); i++)
00380                 {
00381                         if(!(f << "<prototype>" << endl)) error_exit(27);
00382                         
00383                                 if(!(f << "<name>")) error_exit(27);
00384                                         mi = prototype_map.find(results[i].prototype);
00385                                         if(mi == prototype_map.end()) error_exit(30);
00386                                         if(!(f << mi->second)) error_exit(27);
00387                                 if(!(f << "</name>" << endl)) error_exit(27);
00388 
00389                                 temp_d = dmd.prototypes[results[i].prototype];
00390                                 if(!temp_d.second) error_exit(31);
00391 
00392                                 temp_c = dmd.types[temp_d.first.view.type];
00393                                 if(!temp_c.second) error_exit(32);
00394 
00395                                 if(!(f << "<type>")) error_exit(27);
00396                                         if(!(f << temp_c.first.name)) error_exit(27);
00397                                 if(!(f << "</type>" << endl)) error_exit(27);
00398 
00399                                 if(!(f << "<distance>")) error_exit(27);
00400                                 if(!(f << results[i].distance)) error_exit(27);
00401                                 if(!(f << "</distance>" << endl)) error_exit(27);
00402                         
00403                         if(!(f << "</prototype>" << endl)) error_exit(27);
00404                 }
00405 
00406                 if(!(f << "</index matches>" << endl)) error_exit(27);
00407 
00408                 matcher.SortByClosest(temp_b.first, results);
00409 
00410                 if(!(f << "<final matches>" << endl)) error_exit(27);
00411 
00412                 for(i = 0; i<results.size(); i++)
00413                 {
00414                         if(!(f << "<prototype>" << endl)) error_exit(27);
00415                         
00416                                 if(!(f << "<name>")) error_exit(27);
00417                                         mi = prototype_map.find(results[i].prototype);
00418                                         if(mi == prototype_map.end()) error_exit(30);
00419                                         if(!(f << mi->second)) error_exit(27);
00420                                 if(!(f << "</name>" << endl)) error_exit(27);
00421 
00422                                 temp_d = dmd.prototypes[results[i].prototype];
00423                                 if(!temp_d.second) error_exit(31);
00424 
00425                                 temp_c = dmd.types[temp_d.first.view.type];
00426                                 if(!temp_c.second) error_exit(32);
00427 
00428                                 if(!(f << "<type>")) error_exit(27);
00429                                         if(!(f << temp_c.first.name)) error_exit(27);
00430                                 if(!(f << "</type>" << endl)) error_exit(27);
00431 
00432                                 if(!(f << "<distance>")) error_exit(27);
00433                                 if(!(f << results[i].distance)) error_exit(27);
00434                                 if(!(f << "</distance>" << endl)) error_exit(27);
00435                         
00436                         if(!(f << "</prototype>" << endl)) error_exit(27);
00437                 }
00438 
00439                 if(!(f << "</final matches>" << endl)) error_exit(27);
00440         }
00441 
00442         if(!(f << "</results>" << endl)) error_exit(27);
00443 
00444         //Here we save the database
00445         if(dmd.Write(names[0]) == false)
00446                 error_exit(5);
00447         else
00448                 cout << "Operation proceeded succesfuly" << endl;
00449 
00450         return 0;
00451 }

Generated on Sat Nov 13 11:21:26 2004 for Noisy DAG Matcher by doxygen1.2.18