00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #define _FACTCMP_SOURCE_
00024
00025 #include "setup.h"
00026
00027 #if DEFRULE_CONSTRUCT && (! RUN_TIME) && DEFTEMPLATE_CONSTRUCT && CONSTRUCT_COMPILER
00028
00029 #define FactPrefix() ArbitraryPrefix(FactData(theEnv)->FactCodeItem,0)
00030
00031 #include <stdio.h>
00032 #define _STDIO_INCLUDED_
00033
00034 #include "factbld.h"
00035 #include "conscomp.h"
00036 #include "factcmp.h"
00037 #include "tmpltdef.h"
00038 #include "envrnmnt.h"
00039
00040
00041
00042
00043
00044 static int PatternNetworkToCode(void *,char *,char *,char *,int,FILE *,int,int);
00045 static void BeforePatternNetworkToCode(void *);
00046 static struct factPatternNode *GetNextPatternNode(struct factPatternNode *);
00047 static void CloseNetworkFiles(void *,FILE *,int);
00048 static void PatternNodeToCode(void *,FILE *,struct factPatternNode *,int,int);
00049
00050
00051
00052
00053
00054 globle void FactPatternsCompilerSetup(
00055 void *theEnv)
00056 {
00057 FactData(theEnv)->FactCodeItem = AddCodeGeneratorItem(theEnv,"facts",0,BeforePatternNetworkToCode,
00058 NULL,PatternNetworkToCode,1);
00059 }
00060
00061
00062
00063
00064
00065
00066 static void BeforePatternNetworkToCode(
00067 void *theEnv)
00068 {
00069 int whichPattern = 0;
00070 int whichDeftemplate = 0;
00071 struct defmodule *theModule;
00072 struct deftemplate *theDeftemplate;
00073 struct factPatternNode *thePattern;
00074
00075
00076
00077
00078
00079 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00080 theModule != NULL;
00081 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00082 {
00083
00084
00085
00086
00087 EnvSetCurrentModule(theEnv,(void *) theModule);
00088
00089
00090
00091
00092
00093 for (theDeftemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,NULL);
00094 theDeftemplate != NULL;
00095 theDeftemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,theDeftemplate))
00096 {
00097
00098
00099
00100
00101
00102 theDeftemplate->header.bsaveID = whichDeftemplate++;
00103 for (thePattern = theDeftemplate->patternNetwork;
00104 thePattern != NULL;
00105 thePattern = GetNextPatternNode(thePattern))
00106 { thePattern->bsaveID = whichPattern++; }
00107 }
00108 }
00109 }
00110
00111
00112
00113
00114
00115 static struct factPatternNode *GetNextPatternNode(
00116 struct factPatternNode *thePattern)
00117 {
00118
00119
00120
00121
00122
00123 if (thePattern->nextLevel != NULL) return(thePattern->nextLevel);
00124
00125
00126
00127
00128
00129
00130 while (thePattern->rightNode == NULL)
00131 {
00132
00133
00134
00135
00136 thePattern = thePattern->lastLevel;
00137
00138
00139
00140
00141
00142
00143 if (thePattern == NULL) return(NULL);
00144 }
00145
00146
00147
00148
00149
00150 return(thePattern->rightNode);
00151 }
00152
00153
00154
00155
00156
00157 static int PatternNetworkToCode(
00158 void *theEnv,
00159 char *fileName,
00160 char *pathName,
00161 char *fileNameBuffer,
00162 int fileID,
00163 FILE *headerFP,
00164 int imageID,
00165 int maxIndices)
00166 {
00167 int fileCount = 1;
00168 struct defmodule *theModule;
00169 struct deftemplate *theTemplate;
00170 struct factPatternNode *thePatternNode;
00171 int networkArrayCount = 0, networkArrayVersion = 1;
00172 FILE *networkFile = NULL;
00173
00174
00175
00176
00177
00178 fprintf(headerFP,"#include \"factbld.h\"\n");
00179
00180
00181
00182
00183
00184 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00185 theModule != NULL;
00186 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00187 {
00188
00189
00190
00191
00192 EnvSetCurrentModule(theEnv,(void *) theModule);
00193
00194
00195
00196
00197
00198
00199 for (theTemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,NULL);
00200 theTemplate != NULL;
00201 theTemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,theTemplate))
00202 {
00203
00204
00205
00206
00207
00208
00209 for (thePatternNode = theTemplate->patternNetwork;
00210 thePatternNode != NULL;
00211 thePatternNode = GetNextPatternNode(thePatternNode))
00212 {
00213 networkFile = OpenFileIfNeeded(theEnv,networkFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
00214 networkArrayVersion,headerFP,
00215 "struct factPatternNode",FactPrefix(),FALSE,NULL);
00216 if (networkFile == NULL)
00217 {
00218 CloseNetworkFiles(theEnv,networkFile,maxIndices);
00219 return(0);
00220 }
00221
00222 PatternNodeToCode(theEnv,networkFile,thePatternNode,imageID,maxIndices);
00223 networkArrayCount++;
00224 networkFile = CloseFileIfNeeded(theEnv,networkFile,&networkArrayCount,
00225 &networkArrayVersion,maxIndices,NULL,NULL);
00226 }
00227 }
00228 }
00229
00230
00231
00232
00233
00234 CloseNetworkFiles(theEnv,networkFile,maxIndices);
00235
00236
00237
00238
00239
00240
00241 return(TRUE);
00242 }
00243
00244
00245
00246
00247
00248
00249
00250 static void CloseNetworkFiles(
00251 void *theEnv,
00252 FILE *networkFile,
00253 int maxIndices)
00254 {
00255 int count = maxIndices;
00256 int arrayVersion = 0;
00257
00258 if (networkFile != NULL)
00259 {
00260 CloseFileIfNeeded(theEnv,networkFile,&count,&arrayVersion,maxIndices,NULL,NULL);
00261 }
00262 }
00263
00264
00265
00266
00267
00268 static void PatternNodeToCode(
00269 void *theEnv,
00270 FILE *theFile,
00271 struct factPatternNode *thePatternNode,
00272 int imageID,
00273 int maxIndices)
00274 {
00275 fprintf(theFile,"{");
00276
00277
00278
00279
00280
00281 PatternNodeHeaderToCode(theEnv,theFile,&thePatternNode->header,imageID,maxIndices);
00282
00283
00284
00285
00286
00287 fprintf(theFile,",0,%d,%d,%d,",thePatternNode->whichField,
00288 thePatternNode->whichSlot,
00289 thePatternNode->leaveFields);
00290
00291
00292
00293
00294
00295 PrintHashedExpressionReference(theEnv,theFile,thePatternNode->networkTest,imageID,maxIndices);
00296
00297
00298
00299
00300
00301 if (thePatternNode->nextLevel == NULL)
00302 { fprintf(theFile,",NULL,"); }
00303 else
00304 {
00305 fprintf(theFile,",&%s%d_%ld[%ld],",FactPrefix(),
00306 imageID,(thePatternNode->nextLevel->bsaveID / maxIndices) + 1,
00307 thePatternNode->nextLevel->bsaveID % maxIndices);
00308 }
00309
00310
00311
00312
00313
00314 if (thePatternNode->lastLevel == NULL)
00315 { fprintf(theFile,"NULL,"); }
00316 else
00317 {
00318 fprintf(theFile,"&%s%d_%ld[%ld],",FactPrefix(),
00319 imageID,(thePatternNode->lastLevel->bsaveID / maxIndices) + 1,
00320 thePatternNode->lastLevel->bsaveID % maxIndices);
00321 }
00322
00323
00324
00325
00326
00327 if (thePatternNode->leftNode == NULL)
00328 { fprintf(theFile,"NULL,"); }
00329 else
00330 {
00331 fprintf(theFile,"&%s%d_%ld[%ld],",FactPrefix(),
00332 imageID,(thePatternNode->leftNode->bsaveID / maxIndices) + 1,
00333 thePatternNode->leftNode->bsaveID % maxIndices);
00334 }
00335
00336
00337
00338
00339
00340 if (thePatternNode->rightNode == NULL)
00341 { fprintf(theFile,"NULL}"); }
00342 else
00343 {
00344 fprintf(theFile,"&%s%d_%ld[%ld]}",FactPrefix(),
00345 imageID,(thePatternNode->rightNode->bsaveID / maxIndices) + 1,
00346 thePatternNode->rightNode->bsaveID % maxIndices);
00347 }
00348 }
00349
00350
00351
00352
00353
00354 globle void FactPatternNodeReference(
00355 void *theEnv,
00356 void *theVPattern,
00357 FILE *theFile,
00358 int imageID,
00359 int maxIndices)
00360 {
00361 struct factPatternNode *thePattern = (struct factPatternNode *) theVPattern;
00362
00363 if (thePattern == NULL) fprintf(theFile,"NULL");
00364 else
00365 {
00366 fprintf(theFile,"&%s%d_%ld[%ld]",FactPrefix(),
00367 imageID,(thePattern->bsaveID / maxIndices) + 1,
00368 thePattern->bsaveID % maxIndices);
00369 }
00370 }
00371
00372 #endif
00373
00374