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
00026
00027
00028
00029
00030
00031 #define _TMPLTDEF_SOURCE_
00032
00033 #include "setup.h"
00034
00035 #if DEFTEMPLATE_CONSTRUCT
00036
00037 #include <stdio.h>
00038 #define _STDIO_INCLUDED_
00039
00040 #include "memalloc.h"
00041 #include "exprnops.h"
00042 #include "cstrccom.h"
00043 #include "network.h"
00044 #include "tmpltpsr.h"
00045 #include "tmpltbsc.h"
00046 #include "tmpltutl.h"
00047 #include "tmpltfun.h"
00048 #include "router.h"
00049 #include "modulpsr.h"
00050 #include "modulutl.h"
00051 #include "cstrnchk.h"
00052 #include "envrnmnt.h"
00053
00054 #if BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE
00055 #include "bload.h"
00056 #include "tmpltbin.h"
00057 #endif
00058
00059 #if CONSTRUCT_COMPILER && (! RUN_TIME)
00060 #include "tmpltcmp.h"
00061 #endif
00062
00063 #include "tmpltdef.h"
00064
00065
00066
00067
00068
00069 static void *AllocateModule(void *);
00070 static void ReturnModule(void *,void *);
00071 static void ReturnDeftemplate(void *,void *);
00072 static void InitializeDeftemplateModules(void *);
00073 static void DeallocateDeftemplateData(void *);
00074 static void DestroyDeftemplateAction(void *,struct constructHeader *,void *);
00075 static void DestroyDeftemplate(void *,void *);
00076 #if RUN_TIME
00077 static void RuntimeDeftemplateAction(void *,struct constructHeader *,void *);
00078 static void SearchForHashedPatternNodes(void *,struct factPatternNode *);
00079 #endif
00080
00081
00082
00083
00084 globle void InitializeDeftemplates(
00085 void *theEnv)
00086 {
00087 globle struct entityRecord deftemplatePtrRecord = { "DEFTEMPLATE_PTR",
00088 DEFTEMPLATE_PTR,1,0,0,
00089 NULL,
00090 NULL,NULL,
00091 NULL,
00092 NULL,
00093 DecrementDeftemplateBusyCount,
00094 IncrementDeftemplateBusyCount,
00095 NULL,NULL,NULL,NULL,NULL };
00096 AllocateEnvironmentData(theEnv,DEFTEMPLATE_DATA,sizeof(struct deftemplateData),DeallocateDeftemplateData);
00097
00098 memcpy(&DeftemplateData(theEnv)->DeftemplatePtrRecord,&deftemplatePtrRecord,sizeof(struct entityRecord));
00099
00100 InitializeFacts(theEnv);
00101
00102 InitializeDeftemplateModules(theEnv);
00103
00104 DeftemplateBasicCommands(theEnv);
00105
00106 DeftemplateFunctions(theEnv);
00107
00108 DeftemplateData(theEnv)->DeftemplateConstruct =
00109 AddConstruct(theEnv,"deftemplate","deftemplates",ParseDeftemplate,EnvFindDeftemplate,
00110 GetConstructNamePointer,GetConstructPPForm,
00111 GetConstructModuleItem,EnvGetNextDeftemplate,SetNextConstruct,
00112 EnvIsDeftemplateDeletable,EnvUndeftemplate,ReturnDeftemplate);
00113
00114 InstallPrimitive(theEnv,(ENTITY_RECORD_PTR) &DeftemplateData(theEnv)->DeftemplatePtrRecord,DEFTEMPLATE_PTR);
00115 }
00116
00117
00118
00119
00120
00121 static void DeallocateDeftemplateData(
00122 void *theEnv)
00123 {
00124 #if ! RUN_TIME
00125 struct deftemplateModule *theModuleItem;
00126 void *theModule;
00127 #endif
00128 #if BLOAD || BLOAD_AND_BSAVE
00129 if (Bloaded(theEnv)) return;
00130 #endif
00131
00132 DoForAllConstructs(theEnv,DestroyDeftemplateAction,DeftemplateData(theEnv)->DeftemplateModuleIndex,FALSE,NULL);
00133
00134 #if ! RUN_TIME
00135 for (theModule = EnvGetNextDefmodule(theEnv,NULL);
00136 theModule != NULL;
00137 theModule = EnvGetNextDefmodule(theEnv,theModule))
00138 {
00139 theModuleItem = (struct deftemplateModule *)
00140 GetModuleItem(theEnv,(struct defmodule *) theModule,
00141 DeftemplateData(theEnv)->DeftemplateModuleIndex);
00142 rtn_struct(theEnv,deftemplateModule,theModuleItem);
00143 }
00144 #endif
00145 }
00146
00147
00148
00149
00150
00151 #if WIN_BTC
00152 #pragma argsused
00153 #endif
00154 static void DestroyDeftemplateAction(
00155 void *theEnv,
00156 struct constructHeader *theConstruct,
00157 void *buffer)
00158 {
00159 #if MAC_MCW || WIN_MCW || MAC_XCD
00160 #pragma unused(buffer)
00161 #endif
00162 struct deftemplate *theDeftemplate = (struct deftemplate *) theConstruct;
00163
00164 if (theDeftemplate == NULL) return;
00165
00166 DestroyDeftemplate(theEnv,theDeftemplate);
00167 }
00168
00169
00170
00171
00172
00173
00174 static void InitializeDeftemplateModules(
00175 void *theEnv)
00176 {
00177 DeftemplateData(theEnv)->DeftemplateModuleIndex = RegisterModuleItem(theEnv,"deftemplate",
00178 AllocateModule,
00179 ReturnModule,
00180 #if BLOAD_AND_BSAVE || BLOAD || BLOAD_ONLY
00181 BloadDeftemplateModuleReference,
00182 #else
00183 NULL,
00184 #endif
00185 #if CONSTRUCT_COMPILER && (! RUN_TIME)
00186 DeftemplateCModuleReference,
00187 #else
00188 NULL,
00189 #endif
00190 EnvFindDeftemplate);
00191
00192 #if (! BLOAD_ONLY) && (! RUN_TIME) && DEFMODULE_CONSTRUCT
00193 AddPortConstructItem(theEnv,"deftemplate",SYMBOL);
00194 #endif
00195 }
00196
00197
00198
00199
00200 static void *AllocateModule(
00201 void *theEnv)
00202 {
00203 return((void *) get_struct(theEnv,deftemplateModule));
00204 }
00205
00206
00207
00208
00209 static void ReturnModule(
00210 void *theEnv,
00211 void *theItem)
00212 {
00213 FreeConstructHeaderModule(theEnv,(struct defmoduleItemHeader *) theItem,DeftemplateData(theEnv)->DeftemplateConstruct);
00214 rtn_struct(theEnv,deftemplateModule,theItem);
00215 }
00216
00217
00218
00219
00220
00221 globle struct deftemplateModule *GetDeftemplateModuleItem(
00222 void *theEnv,
00223 struct defmodule *theModule)
00224 {
00225 return((struct deftemplateModule *) GetConstructModuleItemByIndex(theEnv,theModule,DeftemplateData(theEnv)->DeftemplateModuleIndex));
00226 }
00227
00228
00229
00230
00231
00232
00233 globle void *EnvFindDeftemplate(
00234 void *theEnv,
00235 char *deftemplateName)
00236 {
00237 return(FindNamedConstruct(theEnv,deftemplateName,DeftemplateData(theEnv)->DeftemplateConstruct));
00238 }
00239
00240
00241
00242
00243
00244
00245 globle void *EnvGetNextDeftemplate(
00246 void *theEnv,
00247 void *deftemplatePtr)
00248 {
00249 return((void *) GetNextConstructItem(theEnv,(struct constructHeader *) deftemplatePtr,DeftemplateData(theEnv)->DeftemplateModuleIndex));
00250 }
00251
00252
00253
00254
00255
00256 globle intBool EnvIsDeftemplateDeletable(
00257 void *theEnv,
00258 void *vTheDeftemplate)
00259 {
00260 struct deftemplate *theDeftemplate = (struct deftemplate *) vTheDeftemplate;
00261
00262 if (! ConstructsDeletable(theEnv))
00263 { return FALSE; }
00264
00265 if (theDeftemplate->busyCount > 0) return(FALSE);
00266 if (theDeftemplate->patternNetwork != NULL) return(FALSE);
00267
00268 return(TRUE);
00269 }
00270
00271
00272
00273
00274
00275 static void ReturnDeftemplate(
00276 void *theEnv,
00277 void *vTheConstruct)
00278 {
00279 #if (MAC_MCW || WIN_MCW) && (RUN_TIME || BLOAD_ONLY)
00280 #pragma unused(theEnv,vTheConstruct)
00281 #endif
00282
00283 #if (! BLOAD_ONLY) && (! RUN_TIME)
00284 struct deftemplate *theConstruct = (struct deftemplate *) vTheConstruct;
00285 struct templateSlot *slotPtr;
00286
00287 if (theConstruct == NULL) return;
00288
00289
00290
00291
00292
00293 #if DEBUGGING_FUNCTIONS
00294 DeftemplateData(theEnv)->DeletedTemplateDebugFlags = 0;
00295 if (theConstruct->watch) BitwiseSet(DeftemplateData(theEnv)->DeletedTemplateDebugFlags,0);
00296 #endif
00297
00298
00299
00300
00301
00302 slotPtr = theConstruct->slotList;
00303 while (slotPtr != NULL)
00304 {
00305 DecrementSymbolCount(theEnv,slotPtr->slotName);
00306 RemoveHashedExpression(theEnv,slotPtr->defaultList);
00307 slotPtr->defaultList = NULL;
00308 RemoveHashedExpression(theEnv,slotPtr->facetList);
00309 slotPtr->facetList = NULL;
00310 RemoveConstraint(theEnv,slotPtr->constraints);
00311 slotPtr->constraints = NULL;
00312 slotPtr = slotPtr->next;
00313 }
00314
00315 ReturnSlots(theEnv,theConstruct->slotList);
00316
00317
00318
00319
00320
00321 DeinstallConstructHeader(theEnv,&theConstruct->header);
00322
00323 rtn_struct(theEnv,deftemplate,theConstruct);
00324 #endif
00325 }
00326
00327
00328
00329
00330
00331 static void DestroyDeftemplate(
00332 void *theEnv,
00333 void *vTheConstruct)
00334 {
00335 #if (MAC_MCW || WIN_MCW) && (RUN_TIME || BLOAD_ONLY)
00336 #pragma unused(vTheConstruct)
00337 #endif
00338
00339 struct deftemplate *theConstruct = (struct deftemplate *) vTheConstruct;
00340 #if (! BLOAD_ONLY) && (! RUN_TIME)
00341 struct templateSlot *slotPtr, *nextSlot;
00342 #endif
00343 if (theConstruct == NULL) return;
00344
00345 #if (! BLOAD_ONLY) && (! RUN_TIME)
00346 slotPtr = theConstruct->slotList;
00347
00348 while (slotPtr != NULL)
00349 {
00350 nextSlot = slotPtr->next;
00351 rtn_struct(theEnv,templateSlot,slotPtr);
00352 slotPtr = nextSlot;
00353 }
00354 #endif
00355
00356 DestroyFactPatternNetwork(theEnv,theConstruct->patternNetwork);
00357
00358
00359
00360
00361
00362 #if (! BLOAD_ONLY) && (! RUN_TIME)
00363 DeinstallConstructHeader(theEnv,&theConstruct->header);
00364
00365 rtn_struct(theEnv,deftemplate,theConstruct);
00366 #endif
00367 }
00368
00369
00370
00371
00372
00373 globle void ReturnSlots(
00374 void *theEnv,
00375 struct templateSlot *slotPtr)
00376 {
00377 #if (MAC_MCW || WIN_MCW) && (RUN_TIME || BLOAD_ONLY)
00378 #pragma unused(theEnv,slotPtr)
00379 #endif
00380
00381 #if (! BLOAD_ONLY) && (! RUN_TIME)
00382 struct templateSlot *nextSlot;
00383
00384 while (slotPtr != NULL)
00385 {
00386 nextSlot = slotPtr->next;
00387 ReturnExpression(theEnv,slotPtr->defaultList);
00388 ReturnExpression(theEnv,slotPtr->facetList);
00389 RemoveConstraint(theEnv,slotPtr->constraints);
00390 rtn_struct(theEnv,templateSlot,slotPtr);
00391 slotPtr = nextSlot;
00392 }
00393 #endif
00394 }
00395
00396
00397
00398
00399
00400 globle void DecrementDeftemplateBusyCount(
00401 void *theEnv,
00402 void *vTheTemplate)
00403 {
00404 struct deftemplate *theTemplate = (struct deftemplate *) vTheTemplate;
00405
00406 if (! ConstructData(theEnv)->ClearInProgress) theTemplate->busyCount--;
00407 }
00408
00409
00410
00411
00412
00413 #if WIN_BTC
00414 #pragma argsused
00415 #endif
00416 globle void IncrementDeftemplateBusyCount(
00417 void *theEnv,
00418 void *vTheTemplate)
00419 {
00420 struct deftemplate *theTemplate = (struct deftemplate *) vTheTemplate;
00421 #if MAC_MCW || WIN_MCW || MAC_XCD
00422 #pragma unused(theEnv)
00423 #endif
00424
00425 theTemplate->busyCount++;
00426 }
00427
00428
00429
00430
00431
00432
00433 #if WIN_BTC
00434 #pragma argsused
00435 #endif
00436 globle void *EnvGetNextFactInTemplate(
00437 void *theEnv,
00438 void *theTemplate,
00439 void *factPtr)
00440 {
00441 #if MAC_MCW || WIN_MCW || MAC_XCD
00442 #pragma unused(theEnv)
00443 #endif
00444 if (factPtr == NULL)
00445 { return((void *) ((struct deftemplate *) theTemplate)->factList); }
00446
00447 if (((struct fact *) factPtr)->garbage) return(NULL);
00448
00449 return((void *) ((struct fact *) factPtr)->nextTemplateFact);
00450 }
00451
00452 #if ! RUN_TIME
00453
00454
00455
00456
00457 globle void *CreateDeftemplateScopeMap(
00458 void *theEnv,
00459 struct deftemplate *theDeftemplate)
00460 {
00461 unsigned scopeMapSize;
00462 char *scopeMap;
00463 char *templateName;
00464 struct defmodule *matchModule, *theModule;
00465 int moduleID,count;
00466 void *theBitMap;
00467
00468 templateName = ValueToString(theDeftemplate->header.name);
00469 matchModule = theDeftemplate->header.whichModule->theModule;
00470
00471 scopeMapSize = (sizeof(char) * ((GetNumberOfDefmodules(theEnv) / BITS_PER_BYTE) + 1));
00472 scopeMap = (char *) gm2(theEnv,scopeMapSize);
00473
00474 ClearBitString((void *) scopeMap,scopeMapSize);
00475 SaveCurrentModule(theEnv);
00476 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL) ;
00477 theModule != NULL ;
00478 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,(void *) theModule))
00479 {
00480 EnvSetCurrentModule(theEnv,(void *) theModule);
00481 moduleID = (int) theModule->bsaveID;
00482 if (FindImportedConstruct(theEnv,"deftemplate",matchModule,
00483 templateName,&count,TRUE,NULL) != NULL)
00484 SetBitMap(scopeMap,moduleID);
00485 }
00486 RestoreCurrentModule(theEnv);
00487 theBitMap = EnvAddBitMap(theEnv,scopeMap,scopeMapSize);
00488 IncrementBitMapCount(theBitMap);
00489 rm(theEnv,(void *) scopeMap,scopeMapSize);
00490 return(theBitMap);
00491 }
00492
00493 #endif
00494
00495 #if RUN_TIME
00496
00497
00498
00499
00500
00501
00502 #if WIN_BTC
00503 #pragma argsused
00504 #endif
00505 static void RuntimeDeftemplateAction(
00506 void *theEnv,
00507 struct constructHeader *theConstruct,
00508 void *buffer)
00509 {
00510 #if MAC_MCW || WIN_MCW || MAC_XCD
00511 #pragma unused(buffer)
00512 #endif
00513 struct deftemplate *theDeftemplate = (struct deftemplate *) theConstruct;
00514
00515 SearchForHashedPatternNodes(theEnv,theDeftemplate->patternNetwork);
00516 }
00517
00518
00519
00520
00521 static void SearchForHashedPatternNodes(
00522 void *theEnv,
00523 struct factPatternNode *theNode)
00524 {
00525 while (theNode != NULL)
00526 {
00527 if ((theNode->lastLevel != NULL) && (theNode->lastLevel->header.selector))
00528 { AddHashedPatternNode(theEnv,theNode->lastLevel,theNode,theNode->networkTest->type,theNode->networkTest->value); }
00529
00530 SearchForHashedPatternNodes(theEnv,theNode->nextLevel);
00531
00532 theNode = theNode->rightNode;
00533 }
00534 }
00535
00536
00537
00538
00539 globle void DeftemplateRunTimeInitialize(
00540 void *theEnv)
00541 {
00542 DoForAllConstructs(theEnv,RuntimeDeftemplateAction,DeftemplateData(theEnv)->DeftemplateModuleIndex,TRUE,NULL);
00543 }
00544
00545 #endif
00546
00547 #endif
00548
00549