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 #define _FACTBIN_SOURCE_
00027
00028 #include "setup.h"
00029
00030 #if DEFTEMPLATE_CONSTRUCT && (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE) && (! RUN_TIME)
00031
00032 #include <stdio.h>
00033 #define _STDIO_INCLUDED_
00034
00035 #include "memalloc.h"
00036 #include "tmpltdef.h"
00037 #include "bload.h"
00038 #include "bsave.h"
00039 #include "reteutil.h"
00040 #include "rulebin.h"
00041 #include "moduldef.h"
00042 #include "envrnmnt.h"
00043
00044 #include "factbin.h"
00045
00046
00047
00048
00049
00050 struct bsaveFactPatternNode
00051 {
00052 struct bsavePatternNodeHeader header;
00053 unsigned short whichSlot;
00054 unsigned short whichField;
00055 unsigned short leaveFields;
00056 long networkTest;
00057 long nextLevel;
00058 long lastLevel;
00059 long leftNode;
00060 long rightNode;
00061 };
00062
00063 #define BSAVE_FIND 0
00064 #define BSAVE_PATTERNS 1
00065
00066
00067
00068
00069
00070 #if BLOAD_AND_BSAVE
00071 static void BsaveDriver(void *,int,FILE *,struct factPatternNode *);
00072 static void BsaveFind(void *);
00073 static void BsaveStorage(void *,FILE *);
00074 static void BsaveFactPatterns(void *,FILE *);
00075 static void BsavePatternNode(void *,struct factPatternNode *,FILE *);
00076 #endif
00077 static void BloadStorage(void *);
00078 static void BloadBinaryItem(void *);
00079 static void UpdateFactPatterns(void *,void *,long);
00080 static void ClearBload(void *);
00081 static void DeallocateFactBloadData(void *);
00082
00083
00084
00085
00086
00087 globle void FactBinarySetup(
00088 void *theEnv)
00089 {
00090 AllocateEnvironmentData(theEnv,FACTBIN_DATA,sizeof(struct factBinaryData),DeallocateFactBloadData);
00091
00092 #if BLOAD_AND_BSAVE
00093 AddBinaryItem(theEnv,"facts",0,BsaveFind,NULL,
00094 BsaveStorage,BsaveFactPatterns,
00095 BloadStorage,BloadBinaryItem,
00096 ClearBload);
00097 #endif
00098 #if BLOAD || BLOAD_ONLY
00099 AddBinaryItem(theEnv,"facts",0,NULL,NULL,NULL,NULL,
00100 BloadStorage,BloadBinaryItem,
00101 ClearBload);
00102 #endif
00103 }
00104
00105
00106
00107
00108
00109 static void DeallocateFactBloadData(
00110 void *theEnv)
00111 {
00112 size_t space;
00113 int i;
00114
00115 for (i = 0; i < FactBinaryData(theEnv)->NumberOfPatterns; i++)
00116 { DestroyAlphaMemory(theEnv,&FactBinaryData(theEnv)->FactPatternArray[i].header,FALSE); }
00117
00118 space = FactBinaryData(theEnv)->NumberOfPatterns * sizeof(struct factPatternNode);
00119 if (space != 0) genfree(theEnv,(void *) FactBinaryData(theEnv)->FactPatternArray,space);
00120 }
00121
00122 #if BLOAD_AND_BSAVE
00123
00124
00125
00126
00127
00128
00129 static void BsaveFind(
00130 void *theEnv)
00131 {
00132 struct deftemplate *theDeftemplate;
00133 struct defmodule *theModule;
00134
00135
00136
00137
00138
00139
00140
00141 SaveBloadCount(theEnv,FactBinaryData(theEnv)->NumberOfPatterns);
00142
00143
00144
00145
00146
00147
00148 FactBinaryData(theEnv)->NumberOfPatterns = 0L;
00149
00150
00151
00152
00153
00154 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00155 theModule != NULL;
00156 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00157 {
00158
00159
00160
00161
00162
00163 EnvSetCurrentModule(theEnv,(void *) theModule);
00164
00165
00166
00167
00168
00169
00170
00171 for (theDeftemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,NULL);
00172 theDeftemplate != NULL;
00173 theDeftemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,theDeftemplate))
00174 { BsaveDriver(theEnv,BSAVE_FIND,NULL,theDeftemplate->patternNetwork); }
00175 }
00176 }
00177
00178
00179
00180
00181
00182
00183 static void BsaveDriver(
00184 void *theEnv,
00185 int action,
00186 FILE *fp,
00187 struct factPatternNode *thePattern)
00188 {
00189 while (thePattern != NULL)
00190 {
00191 switch(action)
00192 {
00193 case BSAVE_FIND:
00194 thePattern->bsaveID = FactBinaryData(theEnv)->NumberOfPatterns++;
00195 break;
00196
00197 case BSAVE_PATTERNS:
00198 BsavePatternNode(theEnv,thePattern,fp);
00199 break;
00200
00201 default:
00202 break;
00203 }
00204
00205 if (thePattern->nextLevel == NULL)
00206 {
00207 while (thePattern->rightNode == NULL)
00208 {
00209 thePattern = thePattern->lastLevel;
00210 if (thePattern == NULL) return;
00211 }
00212 thePattern = thePattern->rightNode;
00213 }
00214 else
00215 { thePattern = thePattern->nextLevel; }
00216 }
00217 }
00218
00219
00220
00221
00222
00223 static void BsaveStorage(
00224 void *theEnv,
00225 FILE *fp)
00226 {
00227 size_t space;
00228
00229 space = sizeof(long);
00230 GenWrite(&space,sizeof(size_t),fp);
00231 GenWrite(&FactBinaryData(theEnv)->NumberOfPatterns,sizeof(long int),fp);
00232 }
00233
00234
00235
00236
00237
00238 static void BsaveFactPatterns(
00239 void *theEnv,
00240 FILE *fp)
00241 {
00242 size_t space;
00243 struct deftemplate *theDeftemplate;
00244 struct defmodule *theModule;
00245
00246
00247
00248
00249
00250
00251
00252 space = FactBinaryData(theEnv)->NumberOfPatterns * sizeof(struct bsaveFactPatternNode);
00253 GenWrite(&space,sizeof(size_t),fp);
00254
00255
00256
00257
00258
00259 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00260 theModule != NULL;
00261 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00262 {
00263
00264
00265
00266
00267
00268 EnvSetCurrentModule(theEnv,(void *) theModule);
00269 for (theDeftemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,NULL);
00270 theDeftemplate != NULL;
00271 theDeftemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,theDeftemplate))
00272 { BsaveDriver(theEnv,BSAVE_PATTERNS,fp,theDeftemplate->patternNetwork); }
00273 }
00274
00275
00276
00277
00278
00279
00280
00281
00282 RestoreBloadCount(theEnv,&FactBinaryData(theEnv)->NumberOfPatterns);
00283 }
00284
00285
00286
00287
00288
00289 static void BsavePatternNode(
00290 void *theEnv,
00291 struct factPatternNode *thePattern,
00292 FILE *fp)
00293 {
00294 struct bsaveFactPatternNode tempNode;
00295
00296 AssignBsavePatternHeaderValues(theEnv,&tempNode.header,&thePattern->header);
00297
00298 tempNode.whichField = thePattern->whichField;
00299 tempNode.leaveFields = thePattern->leaveFields;
00300 tempNode.whichSlot = thePattern->whichSlot;
00301 tempNode.networkTest = HashedExpressionIndex(theEnv,thePattern->networkTest);
00302 tempNode.nextLevel = BsaveFactPatternIndex(thePattern->nextLevel);
00303 tempNode.lastLevel = BsaveFactPatternIndex(thePattern->lastLevel);
00304 tempNode.leftNode = BsaveFactPatternIndex(thePattern->leftNode);
00305 tempNode.rightNode = BsaveFactPatternIndex(thePattern->rightNode);
00306
00307 GenWrite(&tempNode,(unsigned long) sizeof(struct bsaveFactPatternNode),fp);
00308 }
00309
00310 #endif
00311
00312
00313
00314
00315
00316 static void BloadStorage(
00317 void *theEnv)
00318 {
00319 size_t space;
00320
00321
00322
00323
00324
00325
00326 GenReadBinary(theEnv,&space,sizeof(size_t));
00327 GenReadBinary(theEnv,&FactBinaryData(theEnv)->NumberOfPatterns,sizeof(long int));
00328
00329
00330
00331
00332
00333
00334 if (FactBinaryData(theEnv)->NumberOfPatterns == 0)
00335 {
00336 FactBinaryData(theEnv)->FactPatternArray = NULL;
00337 return;
00338 }
00339
00340 space = FactBinaryData(theEnv)->NumberOfPatterns * sizeof(struct factPatternNode);
00341 FactBinaryData(theEnv)->FactPatternArray = (struct factPatternNode *) genalloc(theEnv,space);
00342 }
00343
00344
00345
00346
00347
00348 static void BloadBinaryItem(
00349 void *theEnv)
00350 {
00351 size_t space;
00352 long i;
00353
00354
00355
00356
00357
00358
00359
00360 GenReadBinary(theEnv,&space,sizeof(size_t));
00361
00362
00363
00364
00365
00366
00367 BloadandRefresh(theEnv,FactBinaryData(theEnv)->NumberOfPatterns,(unsigned) sizeof(struct bsaveFactPatternNode),
00368 UpdateFactPatterns);
00369
00370 for (i = 0; i < FactBinaryData(theEnv)->NumberOfPatterns; i++)
00371 {
00372 if ((FactBinaryData(theEnv)->FactPatternArray[i].lastLevel != NULL) &&
00373 (FactBinaryData(theEnv)->FactPatternArray[i].lastLevel->header.selector))
00374 {
00375 AddHashedPatternNode(theEnv,FactBinaryData(theEnv)->FactPatternArray[i].lastLevel,
00376 &FactBinaryData(theEnv)->FactPatternArray[i],
00377 FactBinaryData(theEnv)->FactPatternArray[i].networkTest->type,
00378 FactBinaryData(theEnv)->FactPatternArray[i].networkTest->value);
00379 }
00380 }
00381 }
00382
00383
00384
00385
00386
00387 static void UpdateFactPatterns(
00388 void *theEnv,
00389 void *buf,
00390 long obji)
00391 {
00392 struct bsaveFactPatternNode *bp;
00393
00394 bp = (struct bsaveFactPatternNode *) buf;
00395
00396 UpdatePatternNodeHeader(theEnv,&FactBinaryData(theEnv)->FactPatternArray[obji].header,&bp->header);
00397
00398 FactBinaryData(theEnv)->FactPatternArray[obji].bsaveID = 0L;
00399 FactBinaryData(theEnv)->FactPatternArray[obji].whichField = bp->whichField;
00400 FactBinaryData(theEnv)->FactPatternArray[obji].leaveFields = bp->leaveFields;
00401 FactBinaryData(theEnv)->FactPatternArray[obji].whichSlot = bp->whichSlot;
00402
00403 FactBinaryData(theEnv)->FactPatternArray[obji].networkTest = HashedExpressionPointer(bp->networkTest);
00404 FactBinaryData(theEnv)->FactPatternArray[obji].rightNode = BloadFactPatternPointer(bp->rightNode);
00405 FactBinaryData(theEnv)->FactPatternArray[obji].nextLevel = BloadFactPatternPointer(bp->nextLevel);
00406 FactBinaryData(theEnv)->FactPatternArray[obji].lastLevel = BloadFactPatternPointer(bp->lastLevel);
00407 FactBinaryData(theEnv)->FactPatternArray[obji].leftNode = BloadFactPatternPointer(bp->leftNode);
00408 }
00409
00410
00411
00412
00413
00414 static void ClearBload(
00415 void *theEnv)
00416 {
00417 size_t space;
00418 long i;
00419
00420 for (i = 0; i < FactBinaryData(theEnv)->NumberOfPatterns; i++)
00421 {
00422 if ((FactBinaryData(theEnv)->FactPatternArray[i].lastLevel != NULL) &&
00423 (FactBinaryData(theEnv)->FactPatternArray[i].lastLevel->header.selector))
00424 {
00425 RemoveHashedPatternNode(theEnv,FactBinaryData(theEnv)->FactPatternArray[i].lastLevel,
00426 &FactBinaryData(theEnv)->FactPatternArray[i],
00427 FactBinaryData(theEnv)->FactPatternArray[i].networkTest->type,
00428 FactBinaryData(theEnv)->FactPatternArray[i].networkTest->value);
00429 }
00430 }
00431
00432
00433 space = FactBinaryData(theEnv)->NumberOfPatterns * sizeof(struct factPatternNode);
00434 if (space != 0) genfree(theEnv,(void *) FactBinaryData(theEnv)->FactPatternArray,space);
00435 FactBinaryData(theEnv)->NumberOfPatterns = 0;
00436 }
00437
00438 #endif
00439
00440