forked from almostearthling/pyclips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclips_or.c
421 lines (341 loc) · 12.5 KB
/
clips_or.c
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* clips_or.c
* Source of functions from CLIPS explicitly reimplemented for PyCLIPS:
* depending on how CLIPS changes in time, some functions may appear or
* disappear (or be conditionally compiled) from here. Functions defined
* here have the same name as in the CLIPS API followed by _PY, and their
* arguments may be different from the official CLIPS ones. However these
* functions were actually copied from the original CLIPS sources, and
* only slightly changed: therefore I list myself only as a contributing
* programmer. Moreover, the license for this file is the one that covers
* the CLIPS source (which is BSD-like).
* $Id: clips_or.c 323 2007-04-02 22:24:35Z Franz $
*/
/* NOTE: This file used to be split in three parts, one per function */
/*************************************************************/
/* Principal Programmer(s): Gary D. Riley */
/* Brian L. Donnell */
/* */
/* Contributing Programmer(s): Francesco Garosi */
/* */
/*************************************************************/
#define _RULECOM_SOURCE_
#define _CONSTRCT_SOURCE_
#define _INSCOM_SOURCE_
#include <stdio.h>
#define _STDIO_INCLUDED_
#include <string.h>
#include "setup.h"
/* many of these might not be needed but they were in the original sources */
#include "argacces.h"
#include "commline.h"
#include "classcom.h"
#include "classfun.h"
#include "classinf.h"
#include "constant.h"
#include "constrct.h"
#include "crstrtgy.h"
#include "engine.h"
#include "envrnmnt.h"
#include "evaluatn.h"
#include "extnfunc.h"
#include "exprnpsr.h"
#include "insfile.h"
#include "insfun.h"
#include "insmngr.h"
#include "insmoddp.h"
#include "insmult.h"
#include "inspsr.h"
#include "lgcldpnd.h"
#include "memalloc.h"
#include "moduldef.h"
#include "msgcom.h"
#include "msgfun.h"
#include "multifld.h"
#include "pattern.h"
#include "prcdrfun.h"
#include "prcdrpsr.h"
#include "reteutil.h"
#include "router.h"
#include "ruledlt.h"
#include "scanner.h"
#include "sysdep.h"
#include "strngrtr.h"
#include "utility.h"
#include "watch.h"
#if BLOAD || BLOAD_AND_BSAVE || BLOAD_ONLY
#include "rulebin.h"
#endif
#if LOGICAL_DEPENDENCIES
#include "lgcldpnd.h"
#endif
#if INCREMENTAL_RESET
#include "incrrset.h"
#endif
#include "rulecom.h"
#include "constrct.h"
#include "inscom.h"
#include "clips_or.h"
/* EnvMatches_PY
* provides a customized version of the EnvMatches API, that can redirect
* its output to a specified output stream. This will be used instead of the
* Matches() standard API.
*/
#if DEFRULE_CONSTRUCT
/********************************/
/* EnvMatches: C access routine */
/* for the matches command. */
/********************************/
globle BOOLEAN EnvMatches_PY(
void *theEnv,
char *logicalName,
void *theRule)
{
struct defrule *rulePtr, *tmpPtr;
struct partialMatch *listOfMatches, **theStorage;
struct joinNode *theJoin, *lastJoin;
int i, depth;
ACTIVATION *agendaPtr;
int flag;
int matchesDisplayed;
/*=================================================*/
/* Loop through each of the disjuncts for the rule */
/*=================================================*/
for (rulePtr = (struct defrule *) theRule, tmpPtr = rulePtr;
rulePtr != NULL;
rulePtr = rulePtr->disjunct)
{
/*======================================*/
/* Determine the last join in the rule. */
/*======================================*/
lastJoin = rulePtr->lastJoin;
/*===================================*/
/* Determine the number of patterns. */
/*===================================*/
depth = GetPatternNumberFromJoin(lastJoin);
/*=========================================*/
/* Store the alpha memory partial matches. */
/*=========================================*/
theStorage = (struct partialMatch **)
genalloc(theEnv,(unsigned) (depth * sizeof(struct partialMatch)));
theJoin = lastJoin;
i = depth - 1;
while (theJoin != NULL)
{
if (theJoin->joinFromTheRight)
{ theJoin = (struct joinNode *) theJoin->rightSideEntryStructure; }
else
{
theStorage[i] = ((struct patternNodeHeader *) theJoin->rightSideEntryStructure)->alphaMemory;
i--;
theJoin = theJoin->lastLevel;
}
}
/*========================================*/
/* List the alpha memory partial matches. */
/*========================================*/
for (i = 0; i < depth; i++)
{
if (GetHaltExecution(theEnv) == TRUE)
{
genfree(theEnv,theStorage,(unsigned) (depth * sizeof(struct partialMatch)));
return(TRUE);
}
EnvPrintRouter(theEnv,logicalName,"Matches for Pattern ");
PrintLongInteger(theEnv,logicalName,(long int) i + 1);
EnvPrintRouter(theEnv,logicalName,"\n");
listOfMatches = theStorage[i];
if (listOfMatches == NULL) EnvPrintRouter(theEnv,logicalName," None\n");
while (listOfMatches != NULL)
{
if (GetHaltExecution(theEnv) == TRUE)
{
genfree(theEnv,theStorage,(unsigned) (depth * sizeof(struct partialMatch)));
return(TRUE);
}
PrintPartialMatch(theEnv,logicalName,listOfMatches);
EnvPrintRouter(theEnv,logicalName,"\n");
listOfMatches = listOfMatches->next;
}
}
genfree(theEnv,theStorage,(unsigned) (depth * sizeof(struct partialMatch)));
/*========================================*/
/* Store the beta memory partial matches. */
/*========================================*/
depth = lastJoin->depth;
theStorage = (struct partialMatch **) genalloc(theEnv,(unsigned) (depth * sizeof(struct partialMatch)));
theJoin = lastJoin;
for (i = depth - 1; i >= 0; i--)
{
theStorage[i] = theJoin->beta;
theJoin = theJoin->lastLevel;
}
/*=======================================*/
/* List the beta memory partial matches. */
/*=======================================*/
for (i = 1; i < depth; i++)
{
if (GetHaltExecution(theEnv) == TRUE)
{
genfree(theEnv,theStorage,(unsigned) (depth * sizeof(struct partialMatch)));
return(TRUE);
}
matchesDisplayed = 0;
EnvPrintRouter(theEnv,logicalName,"Partial matches for CEs 1 - ");
PrintLongInteger(theEnv,logicalName,(long int) i + 1);
EnvPrintRouter(theEnv,logicalName,"\n");
listOfMatches = theStorage[i];
while (listOfMatches != NULL)
{
if (GetHaltExecution(theEnv) == TRUE)
{
genfree(theEnv,theStorage,(unsigned) (depth * sizeof(struct partialMatch)));
return(TRUE);
}
if (listOfMatches->counterf == FALSE)
{
matchesDisplayed++;
PrintPartialMatch(theEnv,logicalName,listOfMatches);
EnvPrintRouter(theEnv,logicalName,"\n");
}
listOfMatches = listOfMatches->next;
}
if (matchesDisplayed == 0) { EnvPrintRouter(theEnv,logicalName," None\n"); }
}
genfree(theEnv,theStorage,(unsigned) (depth * sizeof(struct partialMatch)));
}
/*===================*/
/* List activations. */
/*===================*/
rulePtr = tmpPtr;
EnvPrintRouter(theEnv,logicalName,"Activations\n");
flag = 1;
for (agendaPtr = (struct activation *) EnvGetNextActivation(theEnv,NULL);
agendaPtr != NULL;
agendaPtr = (struct activation *) EnvGetNextActivation(theEnv,agendaPtr))
{
if (GetHaltExecution(theEnv) == TRUE) return(TRUE);
if (((struct activation *) agendaPtr)->theRule->header.name == rulePtr->header.name)
{
flag = 0;
PrintPartialMatch(theEnv,logicalName,GetActivationBasis(agendaPtr));
EnvPrintRouter(theEnv,logicalName,"\n");
}
}
if (flag) EnvPrintRouter(theEnv,logicalName," None\n");
return(TRUE);
}
#endif /* DEFRULE_CONSTRUCT */
/* EnvClear_PY
* Overrides the EnvClear function that would normally conflict with PyCLIPS.
*/
/*****************************************************/
/* EnvClear: C access routine for the clear command. */
/*****************************************************/
globle BOOLEAN EnvClear_PY(
void *theEnv)
{
struct callFunctionItem *theFunction;
/*==========================================*/
/* Activate the watch router which captures */
/* trace output so that it is not displayed */
/* during a clear. */
/*==========================================*/
#if DEBUGGING_FUNCTIONS
EnvActivateRouter(theEnv,WTRACE);
#endif
/*===================================*/
/* Determine if a clear is possible. */
/*===================================*/
ConstructData(theEnv)->ClearReadyInProgress = TRUE;
if (ClearReady(theEnv) == FALSE)
{
PrintErrorID(theEnv,"CONSTRCT",1,FALSE);
EnvPrintRouter(theEnv,WERROR,"Some constructs are still in use. Clear cannot continue.\n");
#if DEBUGGING_FUNCTIONS
EnvDeactivateRouter(theEnv,WTRACE);
#endif
ConstructData(theEnv)->ClearReadyInProgress = FALSE;
return FALSE;
}
ConstructData(theEnv)->ClearReadyInProgress = FALSE;
/*===========================*/
/* Call all clear functions. */
/*===========================*/
ConstructData(theEnv)->ClearInProgress = TRUE;
for (theFunction = ConstructData(theEnv)->ListOfClearFunctions;
theFunction != NULL;
theFunction = theFunction->next)
{
if (theFunction->environmentAware)
{ (*theFunction->func)(theEnv); }
else
{ (* (void (*)(void)) theFunction->func)(); }
}
/*=============================*/
/* Deactivate the watch router */
/* for capturing output. */
/*=============================*/
#if DEBUGGING_FUNCTIONS
EnvDeactivateRouter(theEnv,WTRACE);
#endif
/*===========================================*/
/* Perform periodic cleanup if the clear was */
/* issued from an embedded controller. */
/*===========================================*/
if ((EvaluationData(theEnv)->CurrentEvaluationDepth == 0) && (! CommandLineData(theEnv)->EvaluatingTopLevelCommand) &&
(EvaluationData(theEnv)->CurrentExpression == NULL))
{ PeriodicCleanup(theEnv,TRUE,FALSE); }
/*===========================*/
/* Clear has been completed. */
/*===========================*/
ConstructData(theEnv)->ClearInProgress = FALSE;
return TRUE;
}
/* EnvGetNextInstanceInClassAndSubclasses_PY
* provides a customized version of the EnvGetNextInstanceInClassAndSubclasses
* API, that does not "return" a pointer to the subclass. This will be used
* instead of the EnvGetNextInstanceInClassAndSubclasses() standard API.
*/
#if OBJECT_SYSTEM
/***************************************************
NAME : EnvGetNextInstanceInClassAndSubclasses
DESCRIPTION : Finds next instance of class
(or first instance of class) and
all of its subclasses
INPUTS : 1) Class address (DIRECT POINTER!)
2) Instance address
(NULL to get first instance)
RETURNS : The next or first class instance
SIDE EFFECTS : None
NOTES : None
***************************************************/
globle void *EnvGetNextInstanceInClassAndSubclasses_PY(
void *theEnv,
void *cptr, /* this has changed */
void *iptr,
DATA_OBJECT *iterationInfo)
{
INSTANCE_TYPE *nextInstance;
DEFCLASS *theClass;
theClass = (DEFCLASS *)cptr;
if (iptr == NULL)
{
ClassSubclassAddresses(theEnv,theClass,iterationInfo,TRUE);
nextInstance = theClass->instanceList;
}
else if (((INSTANCE_TYPE *) iptr)->garbage == 1)
{ nextInstance = NULL; }
else
{ nextInstance = ((INSTANCE_TYPE *) iptr)->nxtClass; }
while ((nextInstance == NULL) &&
(GetpDOBegin(iterationInfo) <= GetpDOEnd(iterationInfo)))
{
theClass = (struct defclass *) GetMFValue(DOPToPointer(iterationInfo),
GetpDOBegin(iterationInfo));
SetpDOBegin(iterationInfo,GetpDOBegin(iterationInfo) + 1);
nextInstance = theClass->instanceList;
}
return(nextInstance);
}
#endif /* OBJECT_SYSTEM */
/* end. */