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

testId.cxx

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/xmlUtil/src/id/testId.cxx,v 1.5 2002/04/05 18:26:44 jrb Exp $
00002 
00005 #include "xml/XmlParser.h"
00006 #include "xml/Dom.h"
00007 #include "xmlUtil/Substitute.h"
00008 #include "xmlUtil/id/DictValidVisitor.h"
00009 #include "xmlUtil/id/IdDict.h"
00010 #include "xmlUtil/id/NamedId.h"
00011 #include "xmlUtil/id/IdDictMan.h"
00012 #include "xmlUtil/id/IdConverter.h"
00013 #include "xmlUtil/id/IdConversion.h"  // maybe don't need this
00014 
00015 #include <dom/DOM_Element.hpp>
00016 #include <dom/DOM_DocumentType.hpp>
00017 
00018 #include <iostream>
00019 // #include <fstream>
00020 #include <string>
00021 
00022 std::ostream *openOut(char * outfile);
00023 
00024 void testQuery(xmlUtil::IdDict* dict);
00025 
00026 int testConverter(xmlUtil::IdConverter* converter);
00027 
00032 int main(int argc, char* argv[]) {
00033   char* xmlInput = "../xml/test-dict.xml";
00034   if (argc >= 2) {
00035     xmlInput = argv[1];
00036     if (!(strcmp(xmlInput, "-help"))) {  // instructions
00037       std::cout << 
00038       "First argument is xml file (id dictionary) to be parsed; defaults to test-dict.xml" 
00039                 << std::endl;
00040       exit(0);
00041     }
00042   }
00043 
00044   xml::XmlParser* parser = new xml::XmlParser();
00045   DOM_Document doc = parser->parse(xmlInput);
00046   DOM_Element  docElt = doc.getDocumentElement();
00047 
00048   if (doc == 0) {
00049     std::cout << "Document failed to parse correctly" << std::endl;
00050     exit(0);
00051   }
00052 
00053   std::cout << "Document was parsed" << std::endl;
00054 
00055   // Evaluate and substitute in case constants were used
00056   xmlUtil::Substitute* sub = new xmlUtil::Substitute(doc);
00057 
00058   // Look for IdDict element
00059   DOM_Element dictElt = xml::Dom::findFirstChildByName(docElt, "idDict");
00060   if (dictElt == DOM_Element()) {
00061     std::cout << "No identifier dictionary found " << std::endl;
00062     exit(0);
00063   }
00064   std::cout << "Dictionary element found" << std::endl;
00065 
00066   int nSub = sub->execute(dictElt);
00067   std::cout << " # elements substituted: " << nSub << std::endl;
00068   
00069   // Make the dictionary
00070   xmlUtil::IdDict  *dict = new xmlUtil::IdDict(dictElt);
00071   std::cout << "Dictionary " << dict->getDictName() << " made" << std::endl;
00072 
00073   
00074   // Attempt to verify it
00075   bool valid = dict->isValid();
00076   std::cout << "Dictionary is " << ( (valid) ? "VALID" : "INVALID" );
00077   std::cout << std::endl;
00078 
00079   // Test query functions
00080   testQuery(dict);
00081 
00082   // Register dictionary
00083   xmlUtil::IdDictMan *man = xmlUtil::IdDictMan::getPointer();
00084   xmlUtil::IdDictMan::RetCode ret = man->registerDict(dict);
00085 
00086   switch(ret) {
00087   case xmlUtil::IdDictMan::SUCCESS: 
00088     std::cout << "Dictionary successfully registered" << std::endl;
00089     break;
00090   case xmlUtil::IdDictMan::DUPLICATE: 
00091     std::cout << "Duplicate dictionary registration" << std::endl;
00092     break;
00093   default: 
00094     std::cout << "Dictionary registration failed" << std::endl;
00095     exit(ret);
00096   }
00097 
00098   // Check that we can find it again
00099   xmlUtil::IdDict *dictAgain = man->findDict(dict->getDictName());
00100   std::cout << ((dictAgain == dict) ? "Found dict" : "Failed to find dict") <<
00101     std::endl;
00102 
00103   // Look for IdConverter
00104   DOM_Element converterElt = 
00105     xml::Dom::findFirstChildByName(docElt, "idConverter");
00106   if (converterElt == DOM_Element()) {
00107     std::cout << "No id converter found " << std::endl;
00108     exit(0);
00109   }
00110   std::cout << "Id converter found" << std::endl;
00111 
00112   // Build representation
00113   xmlUtil::IdConverter *converter = new xmlUtil::IdConverter(converterElt);
00114 
00115   // What's it got?
00116   converter->displayConversions(std::cout);
00117 
00118   // Check it out
00119   return (testConverter(converter));
00120 }
00121 
00122 void testQuery(xmlUtil::IdDict* dict) {
00123 
00124   using namespace xmlUtil;  // for diagnostic output operators
00125 
00126   xmlUtil::Identifier okId(3);
00127   xmlUtil::Identifier badId(4);
00128 
00129   okId[0] = 0;
00130   okId[1] = 1;
00131   okId[2] = 1;
00132 
00133   badId[0] = 0;
00134   badId[1] = 0;
00135   badId[2] = 27;
00136   badId[3] = 1;
00137 
00138   bool valid = dict->idOk(okId);
00139   std::cout << "okId is " <<  ( (valid) ? "VALID" : "INVALID" );
00140   std::cout << std::endl;
00141 
00142   std::cout << "Original: " << okId << std::endl;
00143   std::cout << "Reconstituted: " << std::endl << (*(dict->getNamedId(okId)));
00144 
00145   valid = dict->idOk(badId);
00146   std::cout << "badId is " <<  ( (valid) ? "VALID" : "INVALID" );
00147   std::cout << std::endl;
00148 
00149   xmlUtil::NamedId nIdBadVal(4); 
00150   xmlUtil::NamedId nIdOk(2);
00151   xmlUtil::NamedId nIdBadName(3);
00152 
00153   nIdBadVal.addField("fLATObjects", 0);
00154   nIdBadVal.addField("fTowerY", 2);
00155   nIdBadVal.addField("fTowerX", 14);
00156   nIdBadVal.addField("fTowerObjects", 0);
00157 
00158   nIdOk.addField("fLATObjects", 0);
00159   nIdOk.addField("fTowerY", 2);
00160 
00161   nIdBadName.addField("fLATObjects", 0);
00162   nIdBadName.addField("fTower", 2);
00163   nIdBadName.addField("fTowerX", 1);
00164 
00165   valid = dict->idOk(nIdBadVal);
00166   std::cout << "nIdBadVal is " <<  ( (valid) ? "VALID" : "INVALID" );
00167   std::cout << std::endl;
00168 
00169   valid = dict->idOk(nIdOk);
00170   std::cout << "nIdOk is " <<  ( (valid) ? "VALID" : "INVALID" );
00171   std::cout << std::endl;
00172 
00173   std::cout << "Original named Id: " << nIdOk;
00174   Identifier *stripped = nIdOk.stripNames();
00175   std::cout << "..stripped: " << (*stripped);
00176   std::cout << "..reconstitued: " << std::endl 
00177             << (*(dict->getNamedId(*stripped)));
00178 
00179   valid = dict->idOk(nIdBadName);
00180   std::cout << "nIdBadName is " <<  ( (valid) ? "VALID" : "INVALID" );
00181   std::cout << std::endl;
00182 }
00183 
00184 int testConverter(xmlUtil::IdConverter* converter) {
00185 
00186   using namespace xmlUtil;  // to get diagnostic output functions
00187 
00188   if (!(converter->isConsistent()) ) {
00189     std::cout << "Converter is inconsistent" << std::endl;
00190     return (0);
00191   }
00192   std::cout << "Converter OK" << std::endl;
00193 
00194   // Check out some particular Identifier and NamedId objects
00195   xmlUtil::NamedId nId;
00196   nId.addField("fLATObjects", 0);   // Towers
00197   nId.addField("fTowerY", 3);
00198 
00199   std::cout << "Before conversion:" << std::endl << nId;
00200 
00201   std::cout << "After conversion (shouldn't change):" << std::endl;
00202   
00203   std::cout << (*(converter->convert(&nId)));
00204 
00205   nId.addField("fTowerX", 2);
00206   nId.addField("fTowerObjects", 0);    // CAL
00207   nId.addField("fLayer", 3);
00208   nId.addField("fMeasure", 0);
00209   nId.addField("fCALLog", 4);
00210   nId.addField("fCALLogCmp", 2);
00211 
00212   std::cout << "Before conversion:" << std::endl << nId;
00213 
00214   std::cout << "After conversion (should truncate):" << std::endl;
00215   
00216   std::cout << (*(converter->convert(&nId)));
00217 
00218   nId.popField(5);
00219   nId.addField("fTowerObjects", 1);   //TKR
00220   nId.addField("fTKRTray", 6);
00221   nId.addField("fMeasure", 1);
00222   nId.addField("fTKRTrayCmp", 3);
00223 
00224   std::cout << "Before conversion:" << std::endl << nId;
00225 
00226   std::cout << "After conversion (should compress):" << std::endl;
00227   
00228   std::cout << (*(converter->convert(&nId)));
00229 
00230   nId.popField(7);
00231   nId.addField("fLATObjects", 1);   // ACD
00232   nId.addField("fACDFace", 3);
00233   nId.addField("fRow", 2);
00234   nId.addField("fCol", 3);
00235 
00236   std::cout << "Before conversion:" << std::endl << nId;
00237 
00238   std::cout << "After conversion (should disappear):" << std::endl;
00239   
00240   std::cout << (*(converter->convert(&nId)));
00241 
00242   // Try last one again, but on Identifier rather than named id
00243   xmlUtil::Identifier *id = nId.stripNames();
00244 
00245   std::cout << "Before conversion (Identifier):" << std::endl << (*id);
00246   std::cout << "After conversion (should disappear):" << std::endl;
00247   std::cout << (*(converter->convert(id)));
00248 
00249   return 1;
00250 }
00251 

Generated on Wed Oct 16 14:02:48 2002 by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001