00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #define _TMPLTBIN_SOURCE_
00026
00027 #include "setup.h"
00028
00029 #if DEFTEMPLATE_CONSTRUCT && (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE) && (! RUN_TIME)
00030
00031 #include <stdio.h>
00032 #define _STDIO_INCLUDED_
00033
00034 #include "memalloc.h"
00035 #include "bload.h"
00036 #include "bsave.h"
00037 #include "factbin.h"
00038 #include "cstrnbin.h"
00039 #include "factmngr.h"
00040 #include "tmpltpsr.h"
00041 #include "tmpltdef.h"
00042 #include "tmpltutl.h"
00043 #include "envrnmnt.h"
00044
00045 #include "tmpltbin.h"
00046
00047
00048
00049
00050
00051 #if BLOAD_AND_BSAVE
00052 static void BsaveFind(void *);
00053 static void BsaveStorage(void *,FILE *);
00054 static void BsaveBinaryItem(void *,FILE *);
00055 #endif
00056 static void BloadStorage(void *);
00057 static void BloadBinaryItem(void *);
00058 static void UpdateDeftemplateModule(void *,void *,long);
00059 static void UpdateDeftemplate(void *,void *,long);
00060 static void UpdateDeftemplateSlot(void *,void *,long);
00061 static void ClearBload(void *);
00062 static void DeallocateDeftemplateBloadData(void *);
00063
00064
00065
00066
00067
00068 globle void DeftemplateBinarySetup(
00069 void *theEnv)
00070 {
00071 AllocateEnvironmentData(theEnv,TMPLTBIN_DATA,sizeof(struct deftemplateBinaryData),DeallocateDeftemplateBloadData);
00072 #if BLOAD_AND_BSAVE
00073 AddBinaryItem(theEnv,"deftemplate",0,BsaveFind,NULL,
00074 BsaveStorage,BsaveBinaryItem,
00075 BloadStorage,BloadBinaryItem,
00076 ClearBload);
00077 #endif
00078 #if (BLOAD || BLOAD_ONLY)
00079 AddBinaryItem(theEnv,"deftemplate",0,NULL,NULL,NULL,NULL,
00080 BloadStorage,BloadBinaryItem,
00081 ClearBload);
00082 #endif
00083 }
00084
00085
00086
00087
00088
00089 static void DeallocateDeftemplateBloadData(
00090 void *theEnv)
00091 {
00092 size_t space;
00093
00094 space = DeftemplateBinaryData(theEnv)->NumberOfTemplateModules * sizeof(struct deftemplateModule);
00095 if (space != 0) genfree(theEnv,(void *) DeftemplateBinaryData(theEnv)->ModuleArray,space);
00096
00097 space = DeftemplateBinaryData(theEnv)->NumberOfDeftemplates * sizeof(struct deftemplate);
00098 if (space != 0) genfree(theEnv,(void *) DeftemplateBinaryData(theEnv)->DeftemplateArray,space);
00099
00100 space = DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots * sizeof(struct templateSlot);
00101 if (space != 0) genfree(theEnv,(void *) DeftemplateBinaryData(theEnv)->SlotArray,space);
00102 }
00103
00104 #if BLOAD_AND_BSAVE
00105
00106
00107
00108
00109
00110
00111 static void BsaveFind(
00112 void *theEnv)
00113 {
00114 struct deftemplate *theDeftemplate;
00115 struct templateSlot *theSlot;
00116 struct defmodule *theModule;
00117
00118
00119
00120
00121
00122
00123
00124 SaveBloadCount(theEnv,DeftemplateBinaryData(theEnv)->NumberOfDeftemplates);
00125 SaveBloadCount(theEnv,DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots);
00126 SaveBloadCount(theEnv,DeftemplateBinaryData(theEnv)->NumberOfTemplateModules);
00127
00128
00129
00130
00131
00132
00133 DeftemplateBinaryData(theEnv)->NumberOfDeftemplates = 0;
00134 DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots = 0;
00135 DeftemplateBinaryData(theEnv)->NumberOfTemplateModules = 0;
00136
00137
00138
00139
00140
00141 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00142 theModule != NULL;
00143 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00144 {
00145
00146
00147
00148
00149
00150
00151 EnvSetCurrentModule(theEnv,(void *) theModule);
00152 DeftemplateBinaryData(theEnv)->NumberOfTemplateModules++;
00153
00154
00155
00156
00157
00158 for (theDeftemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,NULL);
00159 theDeftemplate != NULL;
00160 theDeftemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,theDeftemplate))
00161 {
00162
00163
00164
00165
00166 MarkConstructHeaderNeededItems(&theDeftemplate->header,
00167 DeftemplateBinaryData(theEnv)->NumberOfDeftemplates++);
00168
00169
00170
00171
00172
00173
00174 for (theSlot = theDeftemplate->slotList;
00175 theSlot != NULL;
00176 theSlot = theSlot->next)
00177 {
00178 DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots++;
00179 theSlot->slotName->neededSymbol = TRUE;
00180 }
00181 }
00182
00183 }
00184 }
00185
00186
00187
00188
00189
00190 static void BsaveStorage(
00191 void *theEnv,
00192 FILE *fp)
00193 {
00194 size_t space;
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 space = sizeof(long) * 3;
00205 GenWrite(&space,sizeof(size_t),fp);
00206 GenWrite(&DeftemplateBinaryData(theEnv)->NumberOfDeftemplates,sizeof(long int),fp);
00207 GenWrite(&DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots,sizeof(long int),fp);
00208 GenWrite(&DeftemplateBinaryData(theEnv)->NumberOfTemplateModules,sizeof(long int),fp);
00209 }
00210
00211
00212
00213
00214
00215 static void BsaveBinaryItem(
00216 void *theEnv,
00217 FILE *fp)
00218 {
00219 size_t space;
00220 struct deftemplate *theDeftemplate;
00221 struct bsaveDeftemplate tempDeftemplate;
00222 struct templateSlot *theSlot;
00223 struct bsaveTemplateSlot tempTemplateSlot;
00224 struct bsaveDeftemplateModule tempTemplateModule;
00225 struct defmodule *theModule;
00226 struct deftemplateModule *theModuleItem;
00227
00228
00229
00230
00231
00232
00233
00234 space = (DeftemplateBinaryData(theEnv)->NumberOfDeftemplates * sizeof(struct bsaveDeftemplate)) +
00235 (DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots * sizeof(struct bsaveTemplateSlot)) +
00236 (DeftemplateBinaryData(theEnv)->NumberOfTemplateModules * sizeof(struct bsaveDeftemplateModule));
00237 GenWrite(&space,sizeof(size_t),fp);
00238
00239
00240
00241
00242
00243 DeftemplateBinaryData(theEnv)->NumberOfDeftemplates = 0;
00244 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00245 theModule != NULL;
00246 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00247 {
00248 EnvSetCurrentModule(theEnv,(void *) theModule);
00249
00250 theModuleItem = (struct deftemplateModule *)
00251 GetModuleItem(theEnv,NULL,FindModuleItem(theEnv,"deftemplate")->moduleIndex);
00252 AssignBsaveDefmdlItemHdrVals(&tempTemplateModule.header,
00253 &theModuleItem->header);
00254 GenWrite(&tempTemplateModule,sizeof(struct bsaveDeftemplateModule),fp);
00255 }
00256
00257
00258
00259
00260
00261 DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots = 0;
00262 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00263 theModule != NULL;
00264 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00265 {
00266 EnvSetCurrentModule(theEnv,(void *) theModule);
00267
00268 for (theDeftemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,NULL);
00269 theDeftemplate != NULL;
00270 theDeftemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,theDeftemplate))
00271 {
00272 AssignBsaveConstructHeaderVals(&tempDeftemplate.header,
00273 &theDeftemplate->header);
00274 tempDeftemplate.implied = theDeftemplate->implied;
00275 tempDeftemplate.numberOfSlots = theDeftemplate->numberOfSlots;
00276 tempDeftemplate.patternNetwork = BsaveFactPatternIndex(theDeftemplate->patternNetwork);
00277
00278 if (theDeftemplate->slotList != NULL)
00279 { tempDeftemplate.slotList = DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots; }
00280 else tempDeftemplate.slotList = -1L;
00281
00282 GenWrite(&tempDeftemplate,sizeof(struct bsaveDeftemplate),fp);
00283
00284 DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots += theDeftemplate->numberOfSlots;
00285 }
00286 }
00287
00288
00289
00290
00291
00292 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00293 theModule != NULL;
00294 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00295 {
00296 EnvSetCurrentModule(theEnv,(void *) theModule);
00297
00298 for (theDeftemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,NULL);
00299 theDeftemplate != NULL;
00300 theDeftemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,theDeftemplate))
00301 {
00302 for (theSlot = theDeftemplate->slotList;
00303 theSlot != NULL;
00304 theSlot = theSlot->next)
00305 {
00306 tempTemplateSlot.constraints = ConstraintIndex(theSlot->constraints);
00307 tempTemplateSlot.slotName = theSlot->slotName->bucket;
00308 tempTemplateSlot.multislot = theSlot->multislot;
00309 tempTemplateSlot.noDefault = theSlot->noDefault;
00310 tempTemplateSlot.defaultPresent = theSlot->defaultPresent;
00311 tempTemplateSlot.defaultDynamic = theSlot->defaultDynamic;
00312 tempTemplateSlot.defaultList = HashedExpressionIndex(theEnv,theSlot->defaultList);
00313 tempTemplateSlot.facetList = HashedExpressionIndex(theEnv,theSlot->facetList);
00314
00315 if (theSlot->next != NULL) tempTemplateSlot.next = 0L;
00316 else tempTemplateSlot.next = -1L;
00317
00318 GenWrite(&tempTemplateSlot,sizeof(struct bsaveTemplateSlot),fp);
00319 }
00320 }
00321 }
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 RestoreBloadCount(theEnv,&DeftemplateBinaryData(theEnv)->NumberOfDeftemplates);
00332 RestoreBloadCount(theEnv,&DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots);
00333 RestoreBloadCount(theEnv,&DeftemplateBinaryData(theEnv)->NumberOfTemplateModules);
00334 }
00335
00336 #endif
00337
00338
00339
00340
00341
00342 static void BloadStorage(
00343 void *theEnv)
00344 {
00345 size_t space;
00346
00347
00348
00349
00350
00351
00352 GenReadBinary(theEnv,&space,sizeof(size_t));
00353 GenReadBinary(theEnv,&DeftemplateBinaryData(theEnv)->NumberOfDeftemplates,sizeof(long int));
00354 GenReadBinary(theEnv,&DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots,sizeof(long int));
00355 GenReadBinary(theEnv,&DeftemplateBinaryData(theEnv)->NumberOfTemplateModules,sizeof(long int));
00356
00357
00358
00359
00360
00361
00362 if (DeftemplateBinaryData(theEnv)->NumberOfTemplateModules == 0)
00363 {
00364 DeftemplateBinaryData(theEnv)->DeftemplateArray = NULL;
00365 DeftemplateBinaryData(theEnv)->SlotArray = NULL;
00366 DeftemplateBinaryData(theEnv)->ModuleArray = NULL;
00367 return;
00368 }
00369
00370 space = DeftemplateBinaryData(theEnv)->NumberOfTemplateModules * sizeof(struct deftemplateModule);
00371 DeftemplateBinaryData(theEnv)->ModuleArray = (struct deftemplateModule *) genalloc(theEnv,space);
00372
00373
00374
00375
00376
00377
00378 if (DeftemplateBinaryData(theEnv)->NumberOfDeftemplates == 0)
00379 {
00380 DeftemplateBinaryData(theEnv)->DeftemplateArray = NULL;
00381 DeftemplateBinaryData(theEnv)->SlotArray = NULL;
00382 return;
00383 }
00384
00385 space = DeftemplateBinaryData(theEnv)->NumberOfDeftemplates * sizeof(struct deftemplate);
00386 DeftemplateBinaryData(theEnv)->DeftemplateArray = (struct deftemplate *) genalloc(theEnv,space);
00387
00388
00389
00390
00391
00392
00393 if (DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots == 0)
00394 {
00395 DeftemplateBinaryData(theEnv)->SlotArray = NULL;
00396 return;
00397 }
00398
00399 space = DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots * sizeof(struct templateSlot);
00400 DeftemplateBinaryData(theEnv)->SlotArray = (struct templateSlot *) genalloc(theEnv,space);
00401 }
00402
00403
00404
00405
00406
00407 static void BloadBinaryItem(
00408 void *theEnv)
00409 {
00410 size_t space;
00411
00412
00413
00414
00415
00416
00417
00418 GenReadBinary(theEnv,&space,sizeof(size_t));
00419
00420
00421
00422
00423
00424
00425 BloadandRefresh(theEnv,DeftemplateBinaryData(theEnv)->NumberOfTemplateModules,sizeof(struct bsaveDeftemplateModule),
00426 UpdateDeftemplateModule);
00427
00428
00429
00430
00431
00432
00433 BloadandRefresh(theEnv,DeftemplateBinaryData(theEnv)->NumberOfDeftemplates,sizeof(struct bsaveDeftemplate),
00434 UpdateDeftemplate);
00435
00436
00437
00438
00439
00440
00441 BloadandRefresh(theEnv,DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots,sizeof(struct bsaveTemplateSlot),
00442 UpdateDeftemplateSlot);
00443 }
00444
00445
00446
00447
00448
00449 static void UpdateDeftemplateModule(
00450 void *theEnv,
00451 void *buf,
00452 long obji)
00453 {
00454 struct bsaveDeftemplateModule *bdmPtr;
00455
00456 bdmPtr = (struct bsaveDeftemplateModule *) buf;
00457 UpdateDefmoduleItemHeader(theEnv,&bdmPtr->header,&DeftemplateBinaryData(theEnv)->ModuleArray[obji].header,
00458 (int) sizeof(struct deftemplate),
00459 (void *) DeftemplateBinaryData(theEnv)->DeftemplateArray);
00460 }
00461
00462
00463
00464
00465
00466 static void UpdateDeftemplate(
00467 void *theEnv,
00468 void *buf,
00469 long obji)
00470 {
00471 struct deftemplate *theDeftemplate;
00472 struct bsaveDeftemplate *bdtPtr;
00473
00474 bdtPtr = (struct bsaveDeftemplate *) buf;
00475 theDeftemplate = (struct deftemplate *) &DeftemplateBinaryData(theEnv)->DeftemplateArray[obji];
00476
00477 UpdateConstructHeader(theEnv,&bdtPtr->header,&theDeftemplate->header,
00478 (int) sizeof(struct deftemplateModule),(void *) DeftemplateBinaryData(theEnv)->ModuleArray,
00479 (int) sizeof(struct deftemplate),(void *) DeftemplateBinaryData(theEnv)->DeftemplateArray);
00480
00481 if (bdtPtr->slotList != -1L)
00482 { theDeftemplate->slotList = (struct templateSlot *) &DeftemplateBinaryData(theEnv)->SlotArray[bdtPtr->slotList]; }
00483 else
00484 { theDeftemplate->slotList = NULL; }
00485
00486 if (bdtPtr->patternNetwork != -1L)
00487 { theDeftemplate->patternNetwork = (struct factPatternNode *) BloadFactPatternPointer(bdtPtr->patternNetwork); }
00488 else
00489 { theDeftemplate->patternNetwork = NULL; }
00490
00491 theDeftemplate->implied = bdtPtr->implied;
00492 #if DEBUGGING_FUNCTIONS
00493 theDeftemplate->watch = FactData(theEnv)->WatchFacts;
00494 #endif
00495 theDeftemplate->inScope = FALSE;
00496 theDeftemplate->numberOfSlots = (unsigned short) bdtPtr->numberOfSlots;
00497 theDeftemplate->factList = NULL;
00498 theDeftemplate->lastFact = NULL;
00499 }
00500
00501
00502
00503
00504
00505 static void UpdateDeftemplateSlot(
00506 void *theEnv,
00507 void *buf,
00508 long obji)
00509 {
00510 struct templateSlot *theSlot;
00511 struct bsaveTemplateSlot *btsPtr;
00512
00513 btsPtr = (struct bsaveTemplateSlot *) buf;
00514 theSlot = (struct templateSlot *) &DeftemplateBinaryData(theEnv)->SlotArray[obji];
00515
00516 theSlot->slotName = SymbolPointer(btsPtr->slotName);
00517 IncrementSymbolCount(theSlot->slotName);
00518 theSlot->defaultList = HashedExpressionPointer(btsPtr->defaultList);
00519 theSlot->facetList = HashedExpressionPointer(btsPtr->facetList);
00520 theSlot->constraints = ConstraintPointer(btsPtr->constraints);
00521
00522 theSlot->multislot = btsPtr->multislot;
00523 theSlot->noDefault = btsPtr->noDefault;
00524 theSlot->defaultPresent = btsPtr->defaultPresent;
00525 theSlot->defaultDynamic = btsPtr->defaultDynamic;
00526
00527 if (btsPtr->next != -1L)
00528 { theSlot->next = (struct templateSlot *) &DeftemplateBinaryData(theEnv)->SlotArray[obji + 1]; }
00529 else
00530 { theSlot->next = NULL; }
00531 }
00532
00533
00534
00535
00536
00537 static void ClearBload(
00538 void *theEnv)
00539 {
00540 size_t space;
00541 int i;
00542
00543
00544
00545
00546
00547
00548 for (i = 0; i < DeftemplateBinaryData(theEnv)->NumberOfDeftemplates; i++)
00549 { UnmarkConstructHeader(theEnv,&DeftemplateBinaryData(theEnv)->DeftemplateArray[i].header); }
00550
00551
00552
00553
00554
00555
00556 for (i = 0; i < DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots; i++)
00557 { DecrementSymbolCount(theEnv,DeftemplateBinaryData(theEnv)->SlotArray[i].slotName); }
00558
00559
00560
00561
00562
00563 space = DeftemplateBinaryData(theEnv)->NumberOfTemplateModules * sizeof(struct deftemplateModule);
00564 if (space != 0) genfree(theEnv,(void *) DeftemplateBinaryData(theEnv)->ModuleArray,space);
00565 DeftemplateBinaryData(theEnv)->NumberOfTemplateModules = 0;
00566
00567
00568
00569
00570
00571 space = DeftemplateBinaryData(theEnv)->NumberOfDeftemplates * sizeof(struct deftemplate);
00572 if (space != 0) genfree(theEnv,(void *) DeftemplateBinaryData(theEnv)->DeftemplateArray,space);
00573 DeftemplateBinaryData(theEnv)->NumberOfDeftemplates = 0;
00574
00575
00576
00577
00578
00579 space = DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots * sizeof(struct templateSlot);
00580 if (space != 0) genfree(theEnv,(void *) DeftemplateBinaryData(theEnv)->SlotArray,space);
00581 DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots = 0;
00582
00583
00584
00585
00586
00587 #if (! BLOAD_ONLY)
00588 CreateImpliedDeftemplate(theEnv,(SYMBOL_HN *) EnvAddSymbol(theEnv,"initial-fact"),FALSE);
00589 #endif
00590 }
00591
00592
00593
00594
00595
00596 globle void *BloadDeftemplateModuleReference(
00597 void *theEnv,
00598 int theIndex)
00599 {
00600 return ((void *) &DeftemplateBinaryData(theEnv)->ModuleArray[theIndex]);
00601 }
00602
00603 #endif
00604
00605