home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK1.toast / Development Kits / AIAT / Examples / Sources / DemoRouter.cpp < prev   
Encoding:
Text File  |  1998-04-16  |  9.2 KB  |  424 lines  |  [TEXT/CWIE]

  1. /// DemoRouter.cpp
  2. //    Copyright:    © 1997 - 1998 by Apple Computer, Inc., all rights reserved.
  3.  
  4. #include <stdio.h>
  5. #ifdef __mac_os
  6. #include <Files.h>
  7. #endif
  8.  
  9. #include "IARouter.h"
  10.  
  11. #include "SimpleAnalysis.h"
  12. #ifdef __mac_os
  13. #include "HFSCluster.h"
  14. #include "HFSStorage.h"
  15. #include "HFSIterator.h"
  16. #include "HFSCorpus.h"
  17. #include "HFSTextFolderCorpus.h"
  18. #else
  19. #include <sys/stat.h>
  20. #include <stdlib.h>
  21. #ifndef WIN32
  22. #include <limits.h>
  23. #include <unistd.h>
  24. #endif
  25.  
  26. #include "UFSCluster.h"
  27. #include "UFSStorage.h"
  28. #include "UFSIterator.h"
  29. #include "UFSCorpus.h"
  30. #include "UFSDirectoryCorpus.h"
  31. #endif
  32. #include "VectorIndex.h"
  33.  
  34.  
  35.  
  36. #ifdef DEBUG_NEW
  37. #include <DebugNew.h>
  38. #endif
  39.  
  40. #if __profile__
  41. #include <profiler.h>
  42. #endif
  43.  
  44. #ifdef __mac_os
  45. // enter the name of items to check for best router here
  46. StringPtr unSortedItems = "\pBike:Folders:Disorganized Press Releases";
  47. // enter name of the index here
  48. StringPtr singleIndexName = "\pBike:Folders:test.index";
  49.  
  50. // enter the names of the router folders here
  51.  
  52. StringPtr clusterFolders[] = {
  53.         "\pBike:Folders:untitled folder",
  54.         "\pBike:Folders:untitled folder 1",
  55.         "\pBike:Folders:untitled folder 2",
  56.         "\p" // empty string to mark end
  57. };
  58.  
  59. long    FSSpecToDirID( const FSSpec* inFSSpec );
  60. long FSSpecToDirID( const FSSpec* inFSSpec )
  61. {
  62.     CInfoPBRec    pb;
  63.     OSErr        err;
  64.         
  65.     pb.dirInfo.ioNamePtr = (StringPtr) inFSSpec->name;
  66.     pb.dirInfo.ioVRefNum = inFSSpec->vRefNum;
  67.     pb.dirInfo.ioFDirIndex = 0;
  68.     pb.dirInfo.ioDrDirID = inFSSpec->parID;
  69.     err = PBGetCatInfoSync(&pb);                
  70.     IAAssertion(err == noErr, "PBGetCatInfoSync() in FSSpecToDirID", IAAssertionFailure);
  71.  
  72.     return pb.dirInfo.ioDrDirID;
  73. }
  74.  
  75. char*    PToCStr(StringPtr source, char* dest);
  76. char* PToCStr(StringPtr source, char* dest)
  77. {
  78.     int len = source[0];
  79.     for (int i = 0; i < len; i++)
  80.         dest[i] = source[i+1];
  81.     dest[len] = 0;
  82.     
  83.     return dest;
  84. }
  85.  
  86. #else
  87.  
  88. typedef char* StringPtr;
  89.  
  90. // enter the name of items to check for best router here
  91. StringPtr unSortedItems = "/Farzin/Folders/Disorganized";
  92. // enter name of the index here
  93. StringPtr singleIndexName = "/Farzin/Folders/test.index";
  94.  
  95. // enter the names of the router folders here
  96.  
  97. StringPtr clusterFolders[] = {
  98.         "/Farzin/Folders/untitled",
  99.         "/Farzin/Folders/untitled1",
  100.         "/Farzin/Folders/untitled2",
  101.         "" // empty string to mark end
  102. };
  103.  
  104. #endif
  105.  
  106.  
  107. void    AddItemsToIndex(StringPtr    folderPathName, VectorIndex* inIndex);
  108. void    AddItemsToIndex(StringPtr    folderPathName, VectorIndex* inIndex)
  109. {        
  110. #ifdef __mac_os
  111.     FSSpec    fsSpec;
  112.     OSErr err = FSMakeFSSpec(0, 0, folderPathName, &fsSpec);
  113.     IAAssertion(err == noErr, "Can't get folder", IAAssertionFailure);    
  114.  
  115.     HFSIterator     folderIterator(fsSpec.vRefNum, FSSpecToDirID(&fsSpec));
  116.     IATry
  117.     {
  118.         while (folderIterator.Increment()) {
  119.             CInfoPBRec*    pb = folderIterator.GetPBRec();
  120.  
  121.             HFSDoc*    doc = new HFSDoc((HFSCorpus*)inIndex->GetCorpus(), pb->hFileInfo.ioVRefNum, pb->hFileInfo.ioFlParID, pb->hFileInfo.ioNamePtr);
  122.             inIndex->AddDoc(doc);
  123.         }
  124.     }
  125.     IACatch (const IAException& exception) 
  126.     {
  127.               printf("%s, %s\n", exception.What(), exception.GetLocation());
  128.     }
  129. #else
  130.     UFSIterator     folderIterator((const byte*)folderPathName);
  131.     byte name[PATH_MAX];
  132.     IATry
  133.     {
  134.       while (folderIterator.Increment()) {
  135.       name[0] = '\0';
  136.       strcpy(name, folderIterator.GetPath());
  137. #ifndef WIN32
  138.       strcat(name, "/");
  139. #else
  140.       strcat(name, "\\");
  141. #endif
  142.       strcat(name, folderIterator.GetFileName());
  143.         UFSDoc*    doc = new UFSDoc(name);
  144.         inIndex->AddDoc(doc);
  145.       }
  146.     }
  147.     IACatch (const IAException& exception) {
  148.         printf("got exception!\n");
  149.     }
  150. #endif
  151.  
  152. }
  153.  
  154. void DemoRouting();
  155. void DemoRouting() 
  156. {
  157.  
  158. #ifdef __mac_os
  159.   FSSpec    fsSpec;
  160.   char str[256];
  161.   // create/initialize our index
  162.   (void)FSMakeFSSpec(0, 0, singleIndexName, &fsSpec);
  163.         
  164.   IAStorage*    storage = MakeHFSStorage(fsSpec.vRefNum, fsSpec.parID, fsSpec.name);
  165.   storage->Initialize();
  166.  
  167.   HFSCorpus*        corpus = new HFSCorpus;
  168.   IAAnalysis*        an = new SimpleAnalysis();
  169.   
  170.   VectorIndex*     index = new VectorIndex(storage, corpus, an);
  171.   index->Initialize();
  172.  
  173.  
  174.         // setup clusters
  175.   uint32    clusterCount = 0;
  176.   for (clusterCount = 0; clusterFolders[clusterCount][0] != 0; clusterCount++ ) {}
  177.   HFSCluster** folders = new HFSCluster*[clusterCount];
  178.   
  179.   for (uint32 i  = 0; i < clusterCount; i++ ) {
  180.     folders[i] = new HFSCluster(index, clusterFolders[i]);
  181.   }
  182.         
  183.         // instantiate a router and initialize with the corpuses representing
  184.         // our clusters.
  185.  
  186.   IARouter myRouter (index);
  187.   myRouter.InitializeClusters((IACluster**)folders, clusterCount);
  188.  
  189.         
  190.   AddItemsToIndex(unSortedItems, index);
  191.  
  192.   index->Flush();
  193.         
  194.   HFSTextFolderCorpus* source = new HFSTextFolderCorpus(unSortedItems);
  195.  
  196.   IADocIterator* docs = source->GetDocIterator();
  197.   IADoc* doc = docs->GetNextDoc();
  198.   while (doc) {
  199.     uint32 clusterIndex = myRouter.WhichCluster(doc, false);
  200.     printf ("%s belongs in cluster %d\n",  PToCStr(((HFSDoc*)doc)->GetFileName(), str), ++clusterIndex);
  201.     delete doc;
  202.     doc = docs->GetNextDoc();
  203.   }
  204.         
  205.   delete docs;
  206.   delete source;
  207.                         
  208.   myRouter.Store();
  209.   index->Flush();
  210.   storage->Commit();
  211.         
  212.   delete index;
  213.   delete storage;
  214.         
  215.                 
  216.   for (uint32 i  = 0; i < clusterCount; i++ ) {
  217.     delete folders[i];
  218.   }
  219.   delete [] folders;
  220.  
  221. #else 
  222.  
  223.   IAStorage*    storage = MakeFileStorage(singleIndexName);
  224.   storage->Initialize();
  225.  
  226.   UFSCorpus*        corpus = new UFSCorpus;
  227.   IAAnalysis*        an = new SimpleAnalysis();
  228.  
  229.   VectorIndex*     index = new VectorIndex(storage, corpus, an);
  230.   index->Initialize();
  231.  
  232.  
  233.   // setup clusters
  234.   uint32    clusterCount = 0;
  235.   for (clusterCount = 0; clusterFolders[clusterCount][0] != 0; clusterCount++ ) {}
  236.   UFSCluster** folders = new UFSCluster*[clusterCount];
  237.   
  238.   for (uint32 i  = 0; i < clusterCount; i++ ) {
  239.     folders[i] = new UFSCluster(index, (const byte*)clusterFolders[i]);
  240.   }
  241.         
  242.   // instantiate a router and initialize with the corpuses representing
  243.   // our clusters.
  244.  
  245.   IARouter myRouter (index);
  246.   myRouter.InitializeClusters((IACluster**)folders, clusterCount);
  247.  
  248.         
  249.   AddItemsToIndex(unSortedItems, index);
  250.  
  251.   index->Flush();
  252.         
  253.   uint32 l;
  254.  
  255.   UFSIterator     folderIterator((const byte*)unSortedItems);
  256.   byte name[PATH_MAX];
  257.         
  258.   while (folderIterator.Increment()) {
  259.     name[0] = '\0';
  260.     strcpy(name, folderIterator.GetPath());
  261. #ifndef WIN32
  262.     strcat(name, "/");
  263. #else
  264.     strcat(name, "\\");
  265. #endif
  266.     strcat(name, folderIterator.GetFileName());
  267.     UFSDoc*    doc = new UFSDoc(name);
  268.     uint32 clusterIndex = myRouter.WhichCluster(doc, false);
  269.     printf ("%s belongs in cluster %d\n", doc->GetName(&l), ++clusterIndex);
  270.     delete doc;
  271.   }
  272.         
  273.   myRouter.Store();
  274.   index->Flush();
  275.   storage->Commit();
  276.  
  277.   delete index;
  278.   delete storage;
  279.         
  280.   for (uint32 i  = 0; i < clusterCount; i++ ) {
  281.     delete folders[i];
  282.   }
  283.   delete [] folders;
  284.  
  285. #endif
  286. }
  287.  
  288.  
  289. void DemoRestore();
  290. void DemoRestore() 
  291. {
  292.  
  293. #ifdef __mac_os
  294.   FSSpec    fsSpec;
  295.   char str[256];
  296.   // create/initialize our index
  297.   (void)FSMakeFSSpec(0, 0, singleIndexName, &fsSpec);
  298.         
  299.   IAStorage*    storage = MakeHFSStorage(fsSpec.vRefNum, fsSpec.parID, fsSpec.name);
  300.   storage->Open(true);
  301.  
  302.   HFSCorpus*        corpus = new HFSCorpus();
  303.   IAAnalysis*        an = new SimpleAnalysis();
  304.  
  305.   VectorIndex*     index = new VectorIndex(storage, corpus, an);
  306.   index->Open();
  307.  
  308.         
  309.         // instantiate a router and restore it
  310.  
  311.   IARouter myRouter (index);
  312.   myRouter.Restore();
  313.  
  314.   IADocIterator* docs = NULL;
  315.   IADoc* doc = NULL;
  316.   HFSTextFolderCorpus* source = new HFSTextFolderCorpus(unSortedItems);
  317.         
  318.   docs = source->GetDocIterator();        
  319.   doc = docs->GetNextDoc();
  320.   while (doc) {
  321.     uint32 clusterIndex = myRouter.WhichCluster(doc, false);
  322.     printf ("%s belongs in cluster %d\n",  PToCStr(((HFSDoc*)doc)->GetFileName(), str), ++clusterIndex);
  323.     delete doc;
  324.     doc = docs->GetNextDoc();
  325.   }
  326.         
  327.   delete docs;
  328.   delete source;
  329.   index->Flush();
  330.   storage->Commit();
  331.         
  332.   delete index;
  333.   delete storage;
  334.  
  335. #else
  336.  
  337.   IAStorage*    storage = MakeFileStorage(singleIndexName);
  338.   storage->Open(true);
  339.  
  340.   UFSCorpus*        corpus = new UFSCorpus;
  341.   IAAnalysis*        an = new SimpleAnalysis();
  342.  
  343.   VectorIndex*     index = new VectorIndex(storage, corpus, an);
  344.   index->Open();
  345.  
  346.   IARouter myRouter(index);
  347.   myRouter.Restore();
  348.  
  349.   uint32 l;
  350.  
  351.   UFSIterator     folderIterator((const byte*)unSortedItems);
  352.   byte name[PATH_MAX];
  353.         
  354.   while (folderIterator.Increment()) {
  355.     name[0] = '\0';
  356.     strcpy(name, folderIterator.GetPath());
  357. #ifndef WIN32
  358.     strcat(name, "/");
  359. #else
  360.     strcat(name, "\\");
  361. #endif
  362.     strcat(name, folderIterator.GetFileName());
  363.     UFSDoc*    doc = new UFSDoc(name);
  364.     uint32 clusterIndex = myRouter.WhichCluster(doc, false);
  365.     printf ("%s belongs in cluster %d\n", doc->GetName(&l), ++clusterIndex);
  366.     delete doc;
  367.   }
  368.  
  369.   index->Flush();
  370.   storage->Commit();
  371.   delete index;
  372.   delete storage;
  373.  
  374. #endif
  375.  
  376. }
  377.  
  378.  
  379. void main() {
  380.  
  381. #if __profile__
  382. #ifdef powerc
  383.     ProfilerInit(collectDetailed, PPCTimeBase, 1500, 50);
  384. #else
  385.     ProfilerInit(collectSummary, microsecondsTimeBase, 1500, 50);
  386. #endif    
  387. #endif
  388.  
  389.     // make this bigger to use more memory and index faster
  390.     IADiskBlockSize = 4096;
  391.  
  392.     IATry {
  393.       DemoRouting();
  394.       printf ("\n\n\n");
  395.       DemoRestore();
  396.     }
  397.     IACatch (const IAException& exception) {
  398. #ifdef __mac_os
  399.       printf("Caught exception: %s\n", exception.What());
  400. #else
  401.       printf ("Exception thrown\n");
  402. #endif
  403.     }
  404.  
  405.     IAReportMemoryUsage();
  406.  
  407.  
  408. #if __profile__
  409.     ProfilerDump("\pDemoRouting.prof");
  410.     ProfilerTerm();
  411. #endif
  412.  
  413. #ifdef DEBUG_NEW
  414.     DebugNewReportLeaks();
  415. #endif
  416. }
  417.  
  418. #ifndef __mac_os
  419. #ifndef WIN32
  420. void terminate();
  421. void terminate() {exit(0);}
  422. #endif
  423. #endif
  424.