1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 11/01/16 */
/* */
/* FACT PATTERN NETWORK CONSTRUCTS-TO-C MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Implements the constructs-to-c feature for the */
/* fact pattern network. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* */
/* Revision History: */
/* */
/* 6.30: Added support for path name argument to */
/* constructs-to-c. */
/* */
/* Added const qualifiers to remove C++ */
/* deprecation warnings. */
/* */
/* 6.40: Pragma once and other inclusion changes. */
/* */
/* Added support for booleans with <stdbool.h>. */
/* */
/* Removed use of void pointers for specific */
/* data structures. */
/* */
/*************************************************************/
#include "setup.h"
#if DEFRULE_CONSTRUCT && (! RUN_TIME) && DEFTEMPLATE_CONSTRUCT && CONSTRUCT_COMPILER
#define FactPrefix() ArbitraryPrefix(FactData(theEnv)->FactCodeItem,0)
#include <stdio.h>
#include "factbld.h"
#include "factmngr.h"
#include "conscomp.h"
#include "factcmp.h"
#include "tmpltdef.h"
#include "envrnmnt.h"
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
static bool PatternNetworkToCode(Environment *,const char *,const char *,char *,
unsigned int,FILE *,unsigned int,unsigned int);
static void BeforePatternNetworkToCode(Environment *);
static struct factPatternNode *GetNextPatternNode(struct factPatternNode *);
static void CloseNetworkFiles(Environment *,FILE *,unsigned int);
static void PatternNodeToCode(Environment *,FILE *,struct factPatternNode *,unsigned int,unsigned int);
/**************************************************************/
/* FactPatternsCompilerSetup: Initializes the constructs-to-c */
/* command for use with the fact pattern network. */
/**************************************************************/
void FactPatternsCompilerSetup(
Environment *theEnv)
{
FactData(theEnv)->FactCodeItem = AddCodeGeneratorItem(theEnv,"facts",0,BeforePatternNetworkToCode,
NULL,PatternNetworkToCode,1);
}
/****************************************************************/
/* BeforePatternNetworkToCode: Assigns each pattern node with a */
/* unique ID which will be used for pointer references when */
/* the data structures are written to a file as C code */
/****************************************************************/
static void BeforePatternNetworkToCode(
Environment *theEnv)
{
unsigned int whichPattern = 0;
unsigned int whichDeftemplate = 0;
Defmodule *theModule;
Deftemplate *theDeftemplate;
struct factPatternNode *thePattern;
/*===========================*/
/* Loop through each module. */
/*===========================*/
for (theModule = GetNextDefmodule(theEnv,NULL);
theModule != NULL;
theModule = GetNextDefmodule(theEnv,theModule))
{
/*=========================*/
/* Set the current module. */
/*=========================*/
SetCurrentModule(theEnv,theModule);
/*======================================================*/
/* Loop through each deftemplate in the current module. */
/*======================================================*/
for (theDeftemplate = GetNextDeftemplate(theEnv,NULL);
theDeftemplate != NULL;
theDeftemplate = GetNextDeftemplate(theEnv,theDeftemplate))
{
/*=================================================*/
/* Assign each pattern node in the pattern network */
/* for the deftemplate a unique integer ID. */
/*=================================================*/
theDeftemplate->header.bsaveID = whichDeftemplate++;
for (thePattern = theDeftemplate->patternNetwork;
thePattern != NULL;
thePattern = GetNextPatternNode(thePattern))
{ thePattern->bsaveID = whichPattern++; }
}
}
}
/********************************************************************/
/* GetNextPatternNode: Returns the next node in a pattern network */
/* tree. The next node is computed using a depth first traversal. */
/********************************************************************/
static struct factPatternNode *GetNextPatternNode(
struct factPatternNode *thePattern)
{
/*=========================================*/
/* If it's possible to go deeper into the */
/* tree, then move down to the next level. */
/*=========================================*/
if (thePattern->nextLevel != NULL) return(thePattern->nextLevel);
/*========================================*/
/* Keep backing up toward the root of the */
/* tree until a side branch can be taken. */
/*========================================*/
while (thePattern->rightNode == NULL)
{
/*========================================*/
/* Back up to check the next side branch. */
/*========================================*/
thePattern = thePattern->lastLevel;
/*======================================*/
/* If we branched up from the root, the */
/* entire tree has been traversed. */
/*======================================*/
if (thePattern == NULL) return NULL;
}
/*==================================*/
/* Move on to the next side branch. */
/*==================================*/
return(thePattern->rightNode);
}
/********************************************************************/
/* PatternNetworkToCode: Produces the fact pattern network code for */
/* a run-time module created using the constructs-to-c function. */
/********************************************************************/
static bool PatternNetworkToCode(
Environment *theEnv,
const char *fileName,
const char *pathName,
char *fileNameBuffer,
unsigned int fileID,
FILE *headerFP,
unsigned int imageID,
unsigned int maxIndices)
{
unsigned int fileCount = 1;
Defmodule *theModule;
Deftemplate *theTemplate;
struct factPatternNode *thePatternNode;
unsigned int networkArrayCount = 0, networkArrayVersion = 1;
FILE *networkFile = NULL;
/*===========================================================*/
/* Include the appropriate fact pattern network header file. */
/*===========================================================*/
fprintf(headerFP,"#include \"factbld.h\"\n");
/*===============================*/
/* Loop through all the modules. */
/*===============================*/
for (theModule = GetNextDefmodule(theEnv,NULL);
theModule != NULL;
theModule = GetNextDefmodule(theEnv,theModule))
{
/*=========================*/
/* Set the current module. */
/*=========================*/
SetCurrentModule(theEnv,theModule);
/*======================================*/
/* Loop through all of the deftemplates */
/* in the current module. */
/*======================================*/
for (theTemplate = GetNextDeftemplate(theEnv,NULL);
theTemplate != NULL;
theTemplate = GetNextDeftemplate(theEnv,theTemplate))
{
/*======================================================*/
/* Loop through each pattern node in the deftemplate's */
/* pattern network writing its C code representation to */
/* the file as it is traversed. */
/*======================================================*/
for (thePatternNode = theTemplate->patternNetwork;
thePatternNode != NULL;
thePatternNode = GetNextPatternNode(thePatternNode))
{
networkFile = OpenFileIfNeeded(theEnv,networkFile,fileName,pathName,fileNameBuffer,fileID,imageID,&fileCount,
networkArrayVersion,headerFP,
"struct factPatternNode",FactPrefix(),false,NULL);
if (networkFile == NULL)
{
CloseNetworkFiles(theEnv,networkFile,maxIndices);
return false;
}
PatternNodeToCode(theEnv,networkFile,thePatternNode,imageID,maxIndices);
networkArrayCount++;
networkFile = CloseFileIfNeeded(theEnv,networkFile,&networkArrayCount,
&networkArrayVersion,maxIndices,NULL,NULL);
}
}
}
/*==============================*/
/* Close any C files left open. */
/*==============================*/
CloseNetworkFiles(theEnv,networkFile,maxIndices);
/*===============================*/
/* Return true to indicate the C */
/* code was successfully saved. */
/*===============================*/
return true;
}
/****************************************************************/
/* CloseNetworkFiles: Closes all of the C files created for the */
/* fact pattern network. Called when an error occurs or when */
/* the fact pattern network data structures have all been */
/* written to the files. */
/****************************************************************/
static void CloseNetworkFiles(
Environment *theEnv,
FILE *networkFile,
unsigned int maxIndices)
{
unsigned int count = maxIndices;
unsigned int arrayVersion = 0;
if (networkFile != NULL)
{
CloseFileIfNeeded(theEnv,networkFile,&count,&arrayVersion,maxIndices,NULL,NULL);
}
}
/************************************************************/
/* PatternNodeToCode: Writes the C code representation of a */
/* single fact pattern node slot to the specified file. */
/************************************************************/
static void PatternNodeToCode(
Environment *theEnv,
FILE *theFile,
struct factPatternNode *thePatternNode,
unsigned int imageID,
unsigned int maxIndices)
{
fprintf(theFile,"{");
/*=====================*/
/* Pattern Node Header */
/*=====================*/
PatternNodeHeaderToCode(theEnv,theFile,&thePatternNode->header,imageID,maxIndices);
/*========================*/
/* Field and Slot Indices */
/*========================*/
fprintf(theFile,",0,%d,%d,%d,",thePatternNode->whichField,
thePatternNode->whichSlot,
thePatternNode->leaveFields);
/*===============*/
/* Network Tests */
/*===============*/
PrintHashedExpressionReference(theEnv,theFile,thePatternNode->networkTest,imageID,maxIndices);
/*============*/
/* Next Level */
/*============*/
if (thePatternNode->nextLevel == NULL)
{ fprintf(theFile,",NULL,"); }
else
{
fprintf(theFile,",&%s%d_%ld[%ld],",FactPrefix(),
imageID,(thePatternNode->nextLevel->bsaveID / maxIndices) + 1,
thePatternNode->nextLevel->bsaveID % maxIndices);
}
/*============*/
/* Last Level */
/*============*/
if (thePatternNode->lastLevel == NULL)
{ fprintf(theFile,"NULL,"); }
else
{
fprintf(theFile,"&%s%d_%ld[%ld],",FactPrefix(),
imageID,(thePatternNode->lastLevel->bsaveID / maxIndices) + 1,
thePatternNode->lastLevel->bsaveID % maxIndices);
}
/*===========*/
/* Left Node */
/*===========*/
if (thePatternNode->leftNode == NULL)
{ fprintf(theFile,"NULL,"); }
else
{
fprintf(theFile,"&%s%d_%ld[%ld],",FactPrefix(),
imageID,(thePatternNode->leftNode->bsaveID / maxIndices) + 1,
thePatternNode->leftNode->bsaveID % maxIndices);
}
/*============*/
/* Right Node */
/*============*/
if (thePatternNode->rightNode == NULL)
{ fprintf(theFile,"NULL}"); }
else
{
fprintf(theFile,"&%s%d_%ld[%ld]}",FactPrefix(),
imageID,(thePatternNode->rightNode->bsaveID / maxIndices) + 1,
thePatternNode->rightNode->bsaveID % maxIndices);
}
}
/**********************************************************/
/* FactPatternNodeReference: Prints C code representation */
/* of a fact pattern node data structure reference. */
/**********************************************************/
void FactPatternNodeReference(
Environment *theEnv,
void *theVPattern,
FILE *theFile,
unsigned int imageID,
unsigned int maxIndices)
{
struct factPatternNode *thePattern = (struct factPatternNode *) theVPattern;
if (thePattern == NULL) fprintf(theFile,"NULL");
else
{
fprintf(theFile,"&%s%u_%lu[%lu]",FactPrefix(),
imageID,(thePattern->bsaveID / maxIndices) + 1,
thePattern->bsaveID % maxIndices);
}
}
#endif /* DEFRULE_CONSTRUCT && (! RUN_TIME) && DEFTEMPLATE_CONSTRUCT && CONSTRUCT_COMPILER */