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
00032
00033
00034
00035
00036
00037
00038
00039
00040 #define _RULEDEF_SOURCE_
00041
00042 #include "setup.h"
00043
00044 #if DEFRULE_CONSTRUCT
00045
00046 #include <stdio.h>
00047 #define _STDIO_INCLUDED_
00048
00049 #include "agenda.h"
00050 #include "drive.h"
00051 #include "engine.h"
00052 #include "envrnmnt.h"
00053 #include "memalloc.h"
00054 #include "pattern.h"
00055 #include "retract.h"
00056 #include "reteutil.h"
00057 #include "rulebsc.h"
00058 #include "rulecom.h"
00059 #include "rulepsr.h"
00060 #include "ruledlt.h"
00061
00062 #if BLOAD || BLOAD_AND_BSAVE || BLOAD_ONLY
00063 #include "bload.h"
00064 #include "rulebin.h"
00065 #endif
00066
00067 #if CONSTRUCT_COMPILER && (! RUN_TIME)
00068 #include "rulecmp.h"
00069 #endif
00070
00071 #include "ruledef.h"
00072
00073
00074
00075
00076
00077 static void *AllocateModule(void *);
00078 static void ReturnModule(void *,void *);
00079 static void InitializeDefruleModules(void *);
00080 static void DeallocateDefruleData(void *);
00081 static void DestroyDefruleAction(void *,struct constructHeader *,void *);
00082 #if RUN_TIME
00083 static void AddBetaMemoriesToRule(void *,struct joinNode *);
00084 #endif
00085
00086
00087
00088
00089 globle void InitializeDefrules(
00090 void *theEnv)
00091 {
00092 unsigned long i;
00093 AllocateEnvironmentData(theEnv,DEFRULE_DATA,sizeof(struct defruleData),DeallocateDefruleData);
00094
00095 InitializeEngine(theEnv);
00096 InitializeAgenda(theEnv);
00097 InitializePatterns(theEnv);
00098 InitializeDefruleModules(theEnv);
00099
00100 AddReservedPatternSymbol(theEnv,"and",NULL);
00101 AddReservedPatternSymbol(theEnv,"not",NULL);
00102 AddReservedPatternSymbol(theEnv,"or",NULL);
00103 AddReservedPatternSymbol(theEnv,"test",NULL);
00104 AddReservedPatternSymbol(theEnv,"logical",NULL);
00105 AddReservedPatternSymbol(theEnv,"exists",NULL);
00106 AddReservedPatternSymbol(theEnv,"forall",NULL);
00107
00108 DefruleBasicCommands(theEnv);
00109
00110 DefruleCommands(theEnv);
00111
00112 DefruleData(theEnv)->DefruleConstruct =
00113 AddConstruct(theEnv,"defrule","defrules",
00114 ParseDefrule,EnvFindDefrule,
00115 GetConstructNamePointer,GetConstructPPForm,
00116 GetConstructModuleItem,EnvGetNextDefrule,SetNextConstruct,
00117 EnvIsDefruleDeletable,EnvUndefrule,ReturnDefrule);
00118
00119 DefruleData(theEnv)->AlphaMemoryTable = (ALPHA_MEMORY_HASH **)
00120 gm3(theEnv,sizeof (ALPHA_MEMORY_HASH *) * ALPHA_MEMORY_HASH_SIZE);
00121
00122 for (i = 0; i < ALPHA_MEMORY_HASH_SIZE; i++) DefruleData(theEnv)->AlphaMemoryTable[i] = NULL;
00123
00124 DefruleData(theEnv)->BetaMemoryResizingFlag = TRUE;
00125
00126 DefruleData(theEnv)->RightPrimeJoins = NULL;
00127 DefruleData(theEnv)->LeftPrimeJoins = NULL;
00128 }
00129
00130
00131
00132
00133
00134 static void DeallocateDefruleData(
00135 void *theEnv)
00136 {
00137 struct defruleModule *theModuleItem;
00138 void *theModule;
00139 struct activation *theActivation, *tmpActivation;
00140 struct salienceGroup *theGroup, *tmpGroup;
00141
00142 #if BLOAD || BLOAD_AND_BSAVE
00143 if (Bloaded(theEnv))
00144 { return; }
00145 #endif
00146
00147 DoForAllConstructs(theEnv,DestroyDefruleAction,DefruleData(theEnv)->DefruleModuleIndex,FALSE,NULL);
00148
00149 for (theModule = EnvGetNextDefmodule(theEnv,NULL);
00150 theModule != NULL;
00151 theModule = EnvGetNextDefmodule(theEnv,theModule))
00152 {
00153 theModuleItem = (struct defruleModule *)
00154 GetModuleItem(theEnv,(struct defmodule *) theModule,
00155 DefruleData(theEnv)->DefruleModuleIndex);
00156
00157 theActivation = theModuleItem->agenda;
00158 while (theActivation != NULL)
00159 {
00160 tmpActivation = theActivation->next;
00161
00162 rtn_struct(theEnv,activation,theActivation);
00163
00164 theActivation = tmpActivation;
00165 }
00166
00167 theGroup = theModuleItem->groupings;
00168 while (theGroup != NULL)
00169 {
00170 tmpGroup = theGroup->next;
00171
00172 rtn_struct(theEnv,salienceGroup,theGroup);
00173
00174 theGroup = tmpGroup;
00175 }
00176
00177 #if ! RUN_TIME
00178 rtn_struct(theEnv,defruleModule,theModuleItem);
00179 #endif
00180 }
00181
00182 rm3(theEnv,DefruleData(theEnv)->AlphaMemoryTable,sizeof (ALPHA_MEMORY_HASH *) * ALPHA_MEMORY_HASH_SIZE);
00183 }
00184
00185
00186
00187
00188
00189 #if WIN_BTC
00190 #pragma argsused
00191 #endif
00192 static void DestroyDefruleAction(
00193 void *theEnv,
00194 struct constructHeader *theConstruct,
00195 void *buffer)
00196 {
00197 #if MAC_MCW || WIN_MCW || MAC_XCD
00198 #pragma unused(buffer)
00199 #endif
00200 struct defrule *theDefrule = (struct defrule *) theConstruct;
00201
00202 DestroyDefrule(theEnv,theDefrule);
00203 }
00204
00205
00206
00207
00208
00209 static void InitializeDefruleModules(
00210 void *theEnv)
00211 {
00212 DefruleData(theEnv)->DefruleModuleIndex = RegisterModuleItem(theEnv,"defrule",
00213 AllocateModule,
00214 ReturnModule,
00215 #if BLOAD_AND_BSAVE || BLOAD || BLOAD_ONLY
00216 BloadDefruleModuleReference,
00217 #else
00218 NULL,
00219 #endif
00220 #if CONSTRUCT_COMPILER && (! RUN_TIME)
00221 DefruleCModuleReference,
00222 #else
00223 NULL,
00224 #endif
00225 EnvFindDefrule);
00226 }
00227
00228
00229
00230
00231 static void *AllocateModule(
00232 void *theEnv)
00233 {
00234 struct defruleModule *theItem;
00235
00236 theItem = get_struct(theEnv,defruleModule);
00237 theItem->agenda = NULL;
00238 theItem->groupings = NULL;
00239 return((void *) theItem);
00240 }
00241
00242
00243
00244
00245 static void ReturnModule(
00246 void *theEnv,
00247 void *theItem)
00248 {
00249 FreeConstructHeaderModule(theEnv,(struct defmoduleItemHeader *) theItem,DefruleData(theEnv)->DefruleConstruct);
00250 rtn_struct(theEnv,defruleModule,theItem);
00251 }
00252
00253
00254
00255
00256
00257 globle struct defruleModule *GetDefruleModuleItem(
00258 void *theEnv,
00259 struct defmodule *theModule)
00260 {
00261 return((struct defruleModule *) GetConstructModuleItemByIndex(theEnv,theModule,DefruleData(theEnv)->DefruleModuleIndex));
00262 }
00263
00264
00265
00266
00267
00268 globle void *EnvFindDefrule(
00269 void *theEnv,
00270 char *defruleName)
00271 {
00272 return(FindNamedConstruct(theEnv,defruleName,DefruleData(theEnv)->DefruleConstruct));
00273 }
00274
00275
00276
00277
00278
00279
00280
00281 globle void *EnvGetNextDefrule(
00282 void *theEnv,
00283 void *defrulePtr)
00284 {
00285 return((void *) GetNextConstructItem(theEnv,(struct constructHeader *) defrulePtr,DefruleData(theEnv)->DefruleModuleIndex));
00286 }
00287
00288
00289
00290
00291
00292 globle intBool EnvIsDefruleDeletable(
00293 void *theEnv,
00294 void *vTheDefrule)
00295 {
00296 struct defrule *theDefrule;
00297
00298 if (! ConstructsDeletable(theEnv))
00299 { return FALSE; }
00300
00301 for (theDefrule = (struct defrule *) vTheDefrule;
00302 theDefrule != NULL;
00303 theDefrule = theDefrule->disjunct)
00304 { if (theDefrule->executing) return(FALSE); }
00305
00306 if (EngineData(theEnv)->JoinOperationInProgress) return(FALSE);
00307
00308 return(TRUE);
00309 }
00310
00311 #if RUN_TIME
00312
00313
00314
00315
00316
00317 globle void DefruleRunTimeInitialize(
00318 void *theEnv,
00319 struct joinLink *rightPrime,
00320 struct joinLink *leftPrime)
00321 {
00322 struct defmodule *theModule;
00323 struct defrule *theRule;
00324
00325 DefruleData(theEnv)->RightPrimeJoins = rightPrime;
00326 DefruleData(theEnv)->LeftPrimeJoins = leftPrime;
00327
00328 SaveCurrentModule(theEnv);
00329
00330 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00331 theModule != NULL;
00332 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00333 {
00334 EnvSetCurrentModule(theEnv,(void *) theModule);
00335 for (theRule = EnvGetNextDefrule(theEnv,NULL);
00336 theRule != NULL;
00337 theRule = EnvGetNextDefrule(theEnv,theRule))
00338 { AddBetaMemoriesToRule(theEnv,theRule->lastJoin); }
00339 }
00340
00341 RestoreCurrentModule(theEnv);
00342 }
00343
00344
00345
00346
00347
00348 static void AddBetaMemoriesToRule(
00349 void *theEnv,
00350 struct joinNode *theNode)
00351 {
00352 AddBetaMemoriesToJoin(theEnv,theNode);
00353
00354 if (theNode->lastLevel != NULL)
00355 { AddBetaMemoriesToRule(theEnv,theNode->lastLevel); }
00356
00357 if (theNode->joinFromTheRight)
00358 { AddBetaMemoriesToRule(theEnv,theNode->rightSideEntryStructure); }
00359 }
00360
00361 #endif
00362
00363 #if RUN_TIME || BLOAD_ONLY || BLOAD || BLOAD_AND_BSAVE
00364
00365
00366
00367
00368 globle void AddBetaMemoriesToJoin(
00369 void *theEnv,
00370 struct joinNode *theNode)
00371 {
00372 if ((theNode->leftMemory != NULL) || (theNode->rightMemory != NULL))
00373 { return; }
00374
00375
00376
00377 if ((! theNode->firstJoin) || theNode->patternIsExists || theNode-> patternIsNegated || theNode->joinFromTheRight)
00378 {
00379 if (theNode->leftHash == NULL)
00380 {
00381 theNode->leftMemory = get_struct(theEnv,betaMemory);
00382 theNode->leftMemory->beta = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *));
00383 theNode->leftMemory->beta[0] = NULL;
00384 theNode->leftMemory->size = 1;
00385 theNode->leftMemory->count = 0;
00386 theNode->leftMemory->last = NULL;
00387 }
00388 else
00389 {
00390 theNode->leftMemory = get_struct(theEnv,betaMemory);
00391 theNode->leftMemory->beta = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *) * INITIAL_BETA_HASH_SIZE);
00392 memset(theNode->leftMemory->beta,0,sizeof(struct partialMatch *) * INITIAL_BETA_HASH_SIZE);
00393 theNode->leftMemory->size = INITIAL_BETA_HASH_SIZE;
00394 theNode->leftMemory->count = 0;
00395 theNode->leftMemory->last = NULL;
00396 }
00397
00398
00399 if (theNode->firstJoin && (theNode->patternIsExists || theNode-> patternIsNegated || theNode->joinFromTheRight))
00400 {
00401 theNode->leftMemory->beta[0] = CreateEmptyPartialMatch(theEnv);
00402 theNode->leftMemory->beta[0]->owner = theNode;
00403 }
00404 }
00405 else
00406 { theNode->leftMemory = NULL; }
00407
00408 if (theNode->joinFromTheRight)
00409 {
00410 if (theNode->leftHash == NULL)
00411 {
00412 theNode->rightMemory = get_struct(theEnv,betaMemory);
00413 theNode->rightMemory->beta = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *));
00414 theNode->rightMemory->last = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *));
00415 theNode->rightMemory->beta[0] = NULL;
00416 theNode->rightMemory->last[0] = NULL;
00417 theNode->rightMemory->size = 1;
00418 theNode->rightMemory->count = 0;
00419 }
00420 else
00421 {
00422 theNode->rightMemory = get_struct(theEnv,betaMemory);
00423 theNode->rightMemory->beta = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *) * INITIAL_BETA_HASH_SIZE);
00424 theNode->rightMemory->last = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *) * INITIAL_BETA_HASH_SIZE);
00425 memset(theNode->rightMemory->beta,0,sizeof(struct partialMatch **) * INITIAL_BETA_HASH_SIZE);
00426 memset(theNode->rightMemory->last,0,sizeof(struct partialMatch **) * INITIAL_BETA_HASH_SIZE);
00427 theNode->rightMemory->size = INITIAL_BETA_HASH_SIZE;
00428 theNode->rightMemory->count = 0;
00429 }
00430 }
00431
00432 else if (theNode->firstJoin && (theNode->rightSideEntryStructure == NULL))
00433 {
00434 theNode->rightMemory = get_struct(theEnv,betaMemory);
00435 theNode->rightMemory->beta = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *));
00436 theNode->rightMemory->last = (struct partialMatch **) genalloc(theEnv,sizeof(struct partialMatch *));
00437 theNode->rightMemory->beta[0] = CreateEmptyPartialMatch(theEnv);
00438 theNode->rightMemory->beta[0]->owner = theNode;
00439 theNode->rightMemory->beta[0]->rhsMemory = TRUE;
00440 theNode->rightMemory->last[0] = theNode->rightMemory->beta[0];
00441 theNode->rightMemory->size = 1;
00442 theNode->rightMemory->count = 1;
00443 }
00444
00445 else
00446 { theNode->rightMemory = NULL; }
00447 }
00448
00449 #endif
00450
00451 #endif
00452
00453