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
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 08/06/16 */
/* */
/* DEFFACTS DEFINITION MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Defines basic deffacts primitive functions such */
/* as allocating and deallocating, traversing, and finding */
/* deffacts data structures. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* Brian L. Dantes */
/* */
/* Revision History: */
/* */
/* 6.24: Renamed BOOLEAN macro type to intBool. */
/* */
/* Corrected code to remove run-time program */
/* compiler warning. */
/* */
/* 6.30: Removed conditional code for unsupported */
/* compilers/operating systems (IBM_MCW, */
/* MAC_MCW, and IBM_TBC). */
/* */
/* Added const qualifiers to remove C++ */
/* deprecation warnings. */
/* */
/* Converted API macros to function calls. */
/* */
/* Changed find construct functionality so that */
/* imported modules are search when locating a */
/* named construct. */
/* */
/* 6.40: Pragma once and other inclusion changes. */
/* */
/* Added support for booleans with <stdbool.h>. */
/* */
/* Removed use of void pointers for specific */
/* data structures. */
/* */
/* ALLOW_ENVIRONMENT_GLOBALS no longer supported. */
/* */
/*************************************************************/
#include "setup.h"
#if DEFFACTS_CONSTRUCT
#include <stdio.h>
#include "dffctbsc.h"
#include "dffctpsr.h"
#include "envrnmnt.h"
#include "memalloc.h"
#if BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE
#include "bload.h"
#include "dffctbin.h"
#endif
#if CONSTRUCT_COMPILER && (! RUN_TIME)
#include "dffctcmp.h"
#endif
#include "dffctdef.h"
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
static void *AllocateModule(Environment *);
static void ReturnModule(Environment *,void *);
static void ReturnDeffacts(Environment *,Deffacts *);
static void InitializeDeffactsModules(Environment *);
static void DeallocateDeffactsData(Environment *);
#if ! RUN_TIME
static void DestroyDeffactsAction(Environment *,ConstructHeader *,void *);
#else
static void RuntimeDeffactsAction(Environment *,ConstructHeader *,void *);
#endif
/***********************************************************/
/* InitializeDeffacts: Initializes the deffacts construct. */
/***********************************************************/
void InitializeDeffacts(
Environment *theEnv)
{
AllocateEnvironmentData(theEnv,DEFFACTS_DATA,sizeof(struct deffactsData),DeallocateDeffactsData);
InitializeDeffactsModules(theEnv);
DeffactsBasicCommands(theEnv);
DeffactsData(theEnv)->DeffactsConstruct =
AddConstruct(theEnv,"deffacts","deffacts",ParseDeffacts,
(FindConstructFunction *) FindDeffacts,
GetConstructNamePointer,GetConstructPPForm,
GetConstructModuleItem,
(GetNextConstructFunction *) GetNextDeffacts,
SetNextConstruct,
(IsConstructDeletableFunction *) DeffactsIsDeletable,
(DeleteConstructFunction *) Undeffacts,
(FreeConstructFunction *) ReturnDeffacts);
}
/***************************************************/
/* DeallocateDeffactsData: Deallocates environment */
/* data for the deffacts construct. */
/***************************************************/
static void DeallocateDeffactsData(
Environment *theEnv)
{
#if ! RUN_TIME
struct deffactsModule *theModuleItem;
Defmodule *theModule;
#if BLOAD || BLOAD_AND_BSAVE
if (Bloaded(theEnv)) return;
#endif
DoForAllConstructs(theEnv,
DestroyDeffactsAction,
DeffactsData(theEnv)->DeffactsModuleIndex,
false,NULL);
for (theModule = GetNextDefmodule(theEnv,NULL);
theModule != NULL;
theModule = GetNextDefmodule(theEnv,theModule))
{
theModuleItem = (struct deffactsModule *)
GetModuleItem(theEnv,theModule,
DeffactsData(theEnv)->DeffactsModuleIndex);
rtn_struct(theEnv,deffactsModule,theModuleItem);
}
#else
#if MAC_XCD
#pragma unused(theEnv)
#endif
#endif
}
#if ! RUN_TIME
/*********************************************************/
/* DestroyDeffactsAction: Action used to remove deffacts */
/* as a result of DestroyEnvironment. */
/*********************************************************/
static void DestroyDeffactsAction(
Environment *theEnv,
ConstructHeader *theConstruct,
void *buffer)
{
#if MAC_XCD
#pragma unused(buffer)
#endif
#if (! BLOAD_ONLY) && (! RUN_TIME)
Deffacts *theDeffacts = (Deffacts *) theConstruct;
if (theDeffacts == NULL) return;
ReturnPackedExpression(theEnv,theDeffacts->assertList);
DestroyConstructHeader(theEnv,&theDeffacts->header);
rtn_struct(theEnv,deffacts,theDeffacts);
#else
#if MAC_XCD
#pragma unused(theEnv,theConstruct)
#endif
#endif
}
#endif
#if RUN_TIME
/***********************************************/
/* RuntimeDeffactsAction: Action to be applied */
/* to each deffacts construct when a runtime */
/* initialization occurs. */
/***********************************************/
static void RuntimeDeffactsAction(
Environment *theEnv,
ConstructHeader *theConstruct,
void *buffer)
{
#if MAC_XCD
#pragma unused(buffer)
#endif
Deffacts *theDeffacts = (Deffacts *) theConstruct;
theDeffacts->header.env = theEnv;
}
/******************************/
/* DeffactsRunTimeInitialize: */
/******************************/
void DeffactsRunTimeInitialize(
Environment *theEnv)
{
DoForAllConstructs(theEnv,RuntimeDeffactsAction,DeffactsData(theEnv)->DeffactsModuleIndex,true,NULL);
}
#endif
/*******************************************************/
/* InitializeDeffactsModules: Initializes the deffacts */
/* construct for use with the defmodule construct. */
/*******************************************************/
static void InitializeDeffactsModules(
Environment *theEnv)
{
DeffactsData(theEnv)->DeffactsModuleIndex =
RegisterModuleItem(theEnv,"deffacts",
AllocateModule,
ReturnModule,
#if BLOAD_AND_BSAVE || BLOAD || BLOAD_ONLY
BloadDeffactsModuleReference,
#else
NULL,
#endif
#if CONSTRUCT_COMPILER && (! RUN_TIME)
DeffactsCModuleReference,
#else
NULL,
#endif
(FindConstructFunction *) FindDeffactsInModule);
}
/************************************************/
/* AllocateModule: Allocates a deffacts module. */
/************************************************/
static void *AllocateModule(
Environment *theEnv)
{
return((void *) get_struct(theEnv,deffactsModule));
}
/************************************************/
/* ReturnModule: Deallocates a deffacts module. */
/************************************************/
static void ReturnModule(
Environment *theEnv,
void *theItem)
{
FreeConstructHeaderModule(theEnv,(struct defmoduleItemHeader *) theItem,DeffactsData(theEnv)->DeffactsConstruct);
rtn_struct(theEnv,deffactsModule,theItem);
}
/*************************************************************/
/* GetDeffactsModuleItem: Returns a pointer to the defmodule */
/* item for the specified deffacts or defmodule. */
/*************************************************************/
struct deffactsModule *GetDeffactsModuleItem(
Environment *theEnv,
Defmodule *theModule)
{
return((struct deffactsModule *) GetConstructModuleItemByIndex(theEnv,theModule,DeffactsData(theEnv)->DeffactsModuleIndex));
}
/************************************************/
/* FindDeffacts: Searches for a deffact in the */
/* list of deffacts. Returns a pointer to the */
/* deffact if found, otherwise NULL. */
/************************************************/
Deffacts *FindDeffacts(
Environment *theEnv,
const char *deffactsName)
{
return (Deffacts *) FindNamedConstructInModuleOrImports(theEnv,deffactsName,DeffactsData(theEnv)->DeffactsConstruct);
}
/************************************************/
/* FindDeffactsInModule: Searches for a deffact */
/* in the list of deffacts. Returns a pointer */
/* to the deffact if found, otherwise NULL. */
/************************************************/
Deffacts *FindDeffactsInModule(
Environment *theEnv,
const char *deffactsName)
{
return (Deffacts *) FindNamedConstructInModule(theEnv,deffactsName,DeffactsData(theEnv)->DeffactsConstruct);
}
/*********************************************************/
/* GetNextDeffacts: If passed a NULL pointer, returns */
/* the first deffacts in the ListOfDeffacts. Otherwise */
/* returns the next deffacts following the deffacts */
/* passed as an argument. */
/*********************************************************/
Deffacts *GetNextDeffacts(
Environment *theEnv,
Deffacts *deffactsPtr)
{
return (Deffacts *) GetNextConstructItem(theEnv,&deffactsPtr->header,DeffactsData(theEnv)->DeffactsModuleIndex);
}
/*******************************************************/
/* DeffactsIsDeletable: Returns true if a particular */
/* deffacts can be deleted, otherwise returns false. */
/*******************************************************/
bool DeffactsIsDeletable(
Deffacts *theDeffacts)
{
Environment *theEnv = theDeffacts->header.env;
if (! ConstructsDeletable(theEnv))
{ return false; }
if (ConstructData(theEnv)->ResetInProgress) return false;
return true;
}
/***********************************************************/
/* ReturnDeffacts: Returns the data structures associated */
/* with a deffacts construct to the pool of free memory. */
/***********************************************************/
static void ReturnDeffacts(
Environment *theEnv,
Deffacts *theDeffacts)
{
#if (! BLOAD_ONLY) && (! RUN_TIME)
if (theDeffacts == NULL) return;
ExpressionDeinstall(theEnv,theDeffacts->assertList);
ReturnPackedExpression(theEnv,theDeffacts->assertList);
DeinstallConstructHeader(theEnv,&theDeffacts->header);
rtn_struct(theEnv,deffacts,theDeffacts);
#endif
}
/*##################################*/
/* Additional Environment Functions */
/*##################################*/
const char *DeffactsModule(
Deffacts *theDeffacts)
{
return GetConstructModuleName(&theDeffacts->header);
}
const char *DeffactsName(
Deffacts *theDeffacts)
{
return GetConstructNameString(&theDeffacts->header);
}
const char *DeffactsPPForm(
Deffacts *theDeffacts)
{
return GetConstructPPForm(&theDeffacts->header);
}
#endif /* DEFFACTS_CONSTRUCT */