Discrete2d.m
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:5k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1. #import <simtools.h>
  2. #import <space.h>
  3. #import <space/Discrete2d.h>
  4. #include <swarmconfig.h>
  5. #define OBJNAME "myDiscrete2d"
  6. #define STRVAL "Hello World"
  7. #define OTHERSTRVAL "Other World"
  8. #define ULONGVAL 1000
  9. #define OTHERULONGVAL 10
  10. #define XSIZE 4
  11. #define YSIZE 3
  12. #define OTHERX 2
  13. #define OTHERY 2
  14. @interface MyClass: CreateDrop
  15. {
  16.   const char *strVal;
  17. }
  18. - (BOOL)checkObject;
  19. @end
  20. @interface MyClassOther: MyClass
  21. {
  22. }
  23. @end
  24. @implementation MyClass
  25. + createBegin: aZone
  26. {
  27.   MyClass *obj = [super createBegin: aZone];
  28.   obj->strVal = STRVAL;
  29.   return obj;
  30. }
  31. - (BOOL)checkObject
  32. {
  33.   if (strcmp (strVal, STRVAL) != 0)
  34.     return NO;
  35.   return YES;
  36. }
  37. @end
  38. @implementation MyClassOther
  39. + createBegin: aZone
  40. {
  41.   MyClassOther *obj = [super createBegin: aZone];
  42.   obj->strVal = OTHERSTRVAL;
  43.   return obj;
  44. }
  45. - (BOOL)checkObject
  46. {
  47.   if (strcmp (strVal, OTHERSTRVAL) != 0)
  48.     return NO;
  49.   return YES;
  50. }
  51. @end
  52. @interface MyDiscrete2d: Discrete2d
  53. {
  54.   BOOL updateFlag;
  55. }
  56. - setUpdateFlag: (BOOL)updateFlag;
  57. - updateArchiver: archiver;
  58. - (BOOL)checkObject;
  59. @end
  60. @implementation MyDiscrete2d
  61. + createBegin: aZone
  62. {
  63.   MyDiscrete2d *obj = [super createBegin: aZone];
  64.   obj->updateFlag = YES;
  65.   return obj;
  66. }
  67. - setUpdateFlag: (BOOL)theUpdateFlag
  68. {
  69.   updateFlag = theUpdateFlag;
  70.   return self;
  71. }
  72. - updateArchiver: archiver
  73. {
  74.   if (updateFlag)
  75.     {
  76.       if (objectFlag)
  77.         [archiver putDeep: OBJNAME object: self];
  78.       else
  79.         [archiver putShallow: OBJNAME object: self];
  80.     }
  81.   return self;
  82. }
  83. - (BOOL)checkObject
  84. {
  85.   if (!objectFlag)
  86.     {
  87.       unsigned i, lcount;
  88.       
  89.       lcount = xsize * ysize;
  90.       for (i = 0; i < lcount; i++)
  91.         {
  92.           if (i == (OTHERY * xsize + OTHERX))
  93.             {
  94.               if (lattice[i] != (id) OTHERULONGVAL)
  95.                 return NO;
  96.             }
  97.           else if (lattice[i] != (id) ULONGVAL)
  98.             return NO;
  99.         }
  100.     }
  101.   else
  102.     {
  103.       unsigned x, y;
  104.       
  105.       for (x = 0; x < xsize; x++) 
  106.         for (y = 0; y < ysize; y++)
  107.           if ([*discrete2dSiteAt(lattice, offsets, x, y) checkObject] == NO)
  108.             return NO;
  109.     }
  110.   return YES;
  111. }
  112. @end
  113. static id
  114. createArchiver (id aZone, BOOL hdf5Flag, BOOL inhibitLoadFlag, BOOL deepFlag)
  115. {
  116.   if (hdf5Flag)
  117.     return [[[[HDF5Archiver createBegin: aZone]
  118.                setPath: (deepFlag ? "objects.hdf" : "values.hdf")]
  119.               setInhibitLoadFlag: inhibitLoadFlag]
  120.              createEnd];
  121.   
  122.   else
  123.     return [[[[LispArchiver createBegin: aZone]
  124.                setPath: (deepFlag ? "objects.scm" : "values.scm")]
  125.               setInhibitLoadFlag: inhibitLoadFlag]
  126.              createEnd];
  127. }
  128. static BOOL
  129. checkArchiverDiscrete2d (id aZone, BOOL hdf5Flag, BOOL deepFlag, 
  130.                          BOOL updateFlag)
  131. {
  132.   id obj;
  133.   BOOL ret;
  134.   id archiver;
  135.   archiver = createArchiver (aZone, hdf5Flag, (updateFlag ? YES : NO), 
  136.                              deepFlag);
  137.   if (!deepFlag)
  138.     {
  139.       obj = [[[[[MyDiscrete2d createBegin: aZone]
  140.                  setSizeX: XSIZE Y: YSIZE]
  141.                 setObjectFlag: NO]
  142.                setUpdateFlag: updateFlag]
  143.               createEnd];
  144.       [obj fastFillWithValue: ULONGVAL];
  145.       
  146.       // make one cell different from the others
  147.       [obj putValue: OTHERULONGVAL atX: OTHERX Y: OTHERY];
  148.     }
  149.   else
  150.     {
  151.       id latticeObj = [MyClass create: aZone];
  152.       obj = [[[[[MyDiscrete2d createBegin: aZone]
  153.                  setSizeX: XSIZE Y: YSIZE]
  154.                 setObjectFlag: YES]
  155.                setUpdateFlag: updateFlag]
  156.               createEnd];
  157.       [obj fastFillWithObject: latticeObj];
  158.       
  159.       // make one cell different from the others
  160.       [obj putObject: [MyClassOther create: aZone] atX: OTHERX Y: OTHERY];
  161.     }
  162.   [archiver registerClient: obj];
  163.   [archiver sync];
  164.   [obj drop];
  165.   [archiver drop];
  166.       
  167.   archiver = createArchiver (aZone, hdf5Flag, NO, deepFlag);
  168.   obj = [archiver getObject: OBJNAME];
  169.   [archiver drop];
  170.   ret = [obj checkObject];
  171.   [obj drop];
  172.   
  173.   return ret;
  174. }
  175. int
  176. main (int argc, const char **argv)
  177. {
  178.   initSwarmBatch (argc, argv);
  179.   if (checkArchiverDiscrete2d (globalZone, NO, YES, YES) == NO)
  180.     raiseEvent (InternalError, 
  181.                 "Deep Lisp serialization of Discrete2d with objects" 
  182.                 " and update failed");
  183.   if (checkArchiverDiscrete2d (globalZone, NO, YES, NO) == NO)
  184.     raiseEvent (InternalError, 
  185.                 "Deep Lisp serialization of Discrete2d with objects" 
  186.                 " and no update failed");
  187.   if (checkArchiverDiscrete2d (globalZone, NO, NO, YES) == NO)
  188.     raiseEvent (InternalError, 
  189.                 "Shallow Lisp serialization of Discrete2d with values"
  190.                 " and update failed");
  191.   if (checkArchiverDiscrete2d (globalZone, NO, NO, YES) == NO)
  192.     raiseEvent (InternalError, 
  193.                 "Shallow Lisp serialization of Discrete2d with values"
  194.                 " and no update failed");
  195. #ifdef HAVE_HDF5
  196.   if (checkArchiverDiscrete2d (globalZone, YES, YES, YES) == NO)
  197.     raiseEvent (InternalError, 
  198.                 "Deep HDF5 serialization of Discrete2d with objects failed");
  199.   if (checkArchiverDiscrete2d (globalZone, YES, NO, YES) == NO)
  200.     raiseEvent (InternalError, 
  201.                 "Shallow HDF5 serialization of Discrete2d with values failed");
  202. #endif
  203.   return 0;
  204. }