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
00027
00028
00029
00030
00031
00032
00033 #define _RULEBSC_SOURCE_
00034
00035 #include "setup.h"
00036
00037 #if DEFRULE_CONSTRUCT
00038
00039 #include <stdio.h>
00040 #define _STDIO_INCLUDED_
00041
00042 #include "argacces.h"
00043 #include "constrct.h"
00044 #include "envrnmnt.h"
00045 #include "router.h"
00046 #include "watch.h"
00047 #include "extnfunc.h"
00048 #include "ruledef.h"
00049 #include "engine.h"
00050 #include "drive.h"
00051 #include "reteutil.h"
00052 #if BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE
00053 #include "rulebin.h"
00054 #endif
00055 #if CONSTRUCT_COMPILER && (! RUN_TIME)
00056 #include "rulecmp.h"
00057 #endif
00058
00059 #include "rulebsc.h"
00060
00061
00062
00063
00064
00065 static void ResetDefrules(void *);
00066 static void ResetDefrulesPrime(void *);
00067 static void SaveDefrules(void *,void *,char *);
00068 #if (! RUN_TIME)
00069 static int ClearDefrulesReady(void *);
00070 static void ClearDefrules(void *);
00071 #endif
00072
00073
00074
00075
00076 globle void DefruleBasicCommands(
00077 void *theEnv)
00078 {
00079 EnvAddResetFunction(theEnv,"defrule",ResetDefrules,70);
00080 EnvAddResetFunction(theEnv,"defrule",ResetDefrulesPrime,10);
00081 AddSaveFunction(theEnv,"defrule",SaveDefrules,0);
00082 #if (! RUN_TIME)
00083 AddClearReadyFunction(theEnv,"defrule",ClearDefrulesReady,0);
00084 EnvAddClearFunction(theEnv,"defrule",ClearDefrules,0);
00085 #endif
00086
00087 #if DEBUGGING_FUNCTIONS
00088 AddWatchItem(theEnv,"rules",0,&DefruleData(theEnv)->WatchRules,70,DefruleWatchAccess,DefruleWatchPrint);
00089 #endif
00090
00091 #if ! RUN_TIME
00092 EnvDefineFunction2(theEnv,"get-defrule-list",'m',PTIEF GetDefruleListFunction,"GetDefruleListFunction","01w");
00093 EnvDefineFunction2(theEnv,"undefrule",'v',PTIEF UndefruleCommand,"UndefruleCommand","11w");
00094 EnvDefineFunction2(theEnv,"defrule-module",'w',PTIEF DefruleModuleFunction,"DefruleModuleFunction","11w");
00095
00096 #if DEBUGGING_FUNCTIONS
00097 EnvDefineFunction2(theEnv,"rules",'v', PTIEF ListDefrulesCommand,"ListDefrulesCommand","01w");
00098 EnvDefineFunction2(theEnv,"list-defrules",'v', PTIEF ListDefrulesCommand,"ListDefrulesCommand","01w");
00099 EnvDefineFunction2(theEnv,"ppdefrule",'v',PTIEF PPDefruleCommand,"PPDefruleCommand","11w");
00100 #endif
00101
00102 #if (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE)
00103 DefruleBinarySetup(theEnv);
00104 #endif
00105
00106 #if CONSTRUCT_COMPILER && (! RUN_TIME)
00107 DefruleCompilerSetup(theEnv);
00108 #endif
00109
00110 #endif
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120 static void ResetDefrules(
00121 void *theEnv)
00122 {
00123 struct defmodule *theModule;
00124 struct joinLink *theLink;
00125 struct partialMatch *notParent;
00126
00127 DefruleData(theEnv)->CurrentEntityTimeTag = 1L;
00128 EnvClearFocusStack(theEnv);
00129 theModule = (struct defmodule *) EnvFindDefmodule(theEnv,"MAIN");
00130 EnvFocus(theEnv,(void *) theModule);
00131
00132 for (theLink = DefruleData(theEnv)->RightPrimeJoins;
00133 theLink != NULL;
00134 theLink = theLink->next)
00135 { PosEntryRetractAlpha(theEnv,theLink->join->rightMemory->beta[0]); }
00136
00137 for (theLink = DefruleData(theEnv)->LeftPrimeJoins;
00138 theLink != NULL;
00139 theLink = theLink->next)
00140 {
00141 if ((theLink->join->patternIsNegated || theLink->join->joinFromTheRight) &&
00142 (! theLink->join->patternIsExists))
00143 {
00144 notParent = theLink->join->leftMemory->beta[0];
00145
00146 if (notParent->marker)
00147 { RemoveBlockedLink(notParent); }
00148
00149
00150
00151
00152
00153 notParent->marker = notParent;
00154
00155 if (notParent->children != NULL)
00156 { PosEntryRetractBeta(theEnv,notParent,notParent->children); }
00157
00158
00159
00160 }
00161 }
00162 }
00163
00164
00165
00166
00167 static void ResetDefrulesPrime(
00168 void *theEnv)
00169 {
00170 struct joinLink *theLink;
00171 struct partialMatch *notParent;
00172
00173 for (theLink = DefruleData(theEnv)->RightPrimeJoins;
00174 theLink != NULL;
00175 theLink = theLink->next)
00176 { NetworkAssert(theEnv,theLink->join->rightMemory->beta[0],theLink->join); }
00177
00178 for (theLink = DefruleData(theEnv)->LeftPrimeJoins;
00179 theLink != NULL;
00180 theLink = theLink->next)
00181 {
00182 if ((theLink->join->patternIsNegated || theLink->join->joinFromTheRight) &&
00183 (! theLink->join->patternIsExists))
00184 {
00185 notParent = theLink->join->leftMemory->beta[0];
00186
00187 if (theLink->join->secondaryNetworkTest != NULL)
00188 {
00189 if (EvaluateSecondaryNetworkTest(theEnv,notParent,theLink->join) == FALSE)
00190 { continue; }
00191 }
00192
00193 notParent->marker = NULL;
00194
00195 EPMDrive(theEnv,notParent,theLink->join);
00196 }
00197 }
00198
00199 }
00200 #if (! RUN_TIME)
00201
00202
00203
00204
00205 static int ClearDefrulesReady(
00206 void *theEnv)
00207 {
00208 if (EngineData(theEnv)->ExecutingRule != NULL) return(FALSE);
00209
00210 EnvClearFocusStack(theEnv);
00211 if (EnvGetCurrentModule(theEnv) == NULL) return(FALSE);
00212
00213 DefruleData(theEnv)->CurrentEntityTimeTag = 1L;
00214
00215 return(TRUE);
00216 }
00217
00218
00219
00220
00221 static void ClearDefrules(
00222 void *theEnv)
00223 {
00224 struct defmodule *theModule;
00225
00226 theModule = (struct defmodule *) EnvFindDefmodule(theEnv,"MAIN");
00227 EnvFocus(theEnv,(void *) theModule);
00228 }
00229 #endif
00230
00231
00232
00233
00234
00235 static void SaveDefrules(
00236 void *theEnv,
00237 void *theModule,
00238 char *logicalName)
00239 {
00240 SaveConstruct(theEnv,theModule,logicalName,DefruleData(theEnv)->DefruleConstruct);
00241 }
00242
00243
00244
00245
00246
00247 globle void UndefruleCommand(
00248 void *theEnv)
00249 {
00250 UndefconstructCommand(theEnv,"undefrule",DefruleData(theEnv)->DefruleConstruct);
00251 }
00252
00253
00254
00255
00256
00257 globle intBool EnvUndefrule(
00258 void *theEnv,
00259 void *theDefrule)
00260 {
00261 return(Undefconstruct(theEnv,theDefrule,DefruleData(theEnv)->DefruleConstruct));
00262 }
00263
00264
00265
00266
00267
00268 globle void GetDefruleListFunction(
00269 void *theEnv,
00270 DATA_OBJECT_PTR returnValue)
00271 {
00272 GetConstructListFunction(theEnv,"get-defrule-list",returnValue,DefruleData(theEnv)->DefruleConstruct);
00273 }
00274
00275
00276
00277
00278
00279 globle void EnvGetDefruleList(
00280 void *theEnv,
00281 DATA_OBJECT_PTR returnValue,
00282 void *theModule)
00283 {
00284 GetConstructList(theEnv,returnValue,DefruleData(theEnv)->DefruleConstruct,(struct defmodule *) theModule);
00285 }
00286
00287
00288
00289
00290
00291 globle void *DefruleModuleFunction(
00292 void *theEnv)
00293 {
00294 return(GetConstructModuleCommand(theEnv,"defrule-module",DefruleData(theEnv)->DefruleConstruct));
00295 }
00296
00297 #if DEBUGGING_FUNCTIONS
00298
00299
00300
00301
00302
00303 globle void PPDefruleCommand(
00304 void *theEnv)
00305 {
00306 PPConstructCommand(theEnv,"ppdefrule",DefruleData(theEnv)->DefruleConstruct);
00307 }
00308
00309
00310
00311
00312
00313 globle int PPDefrule(
00314 void *theEnv,
00315 char *defruleName,
00316 char *logicalName)
00317 {
00318 return(PPConstruct(theEnv,defruleName,logicalName,DefruleData(theEnv)->DefruleConstruct));
00319 }
00320
00321
00322
00323
00324
00325 globle void ListDefrulesCommand(
00326 void *theEnv)
00327 {
00328 ListConstructCommand(theEnv,"list-defrules",DefruleData(theEnv)->DefruleConstruct);
00329 }
00330
00331
00332
00333
00334
00335 globle void EnvListDefrules(
00336 void *theEnv,
00337 char *logicalName,
00338 void *theModule)
00339 {
00340 ListConstruct(theEnv,DefruleData(theEnv)->DefruleConstruct,logicalName,(struct defmodule *) theModule);
00341 }
00342
00343
00344
00345
00346
00347
00348 #if WIN_BTC
00349 #pragma argsused
00350 #endif
00351 globle unsigned EnvGetDefruleWatchActivations(
00352 void *theEnv,
00353 void *rulePtr)
00354 {
00355 struct defrule *thePtr;
00356 #if MAC_MCW || WIN_MCW || MAC_XCD
00357 #pragma unused(theEnv)
00358 #endif
00359
00360 for (thePtr = (struct defrule *) rulePtr;
00361 thePtr != NULL;
00362 thePtr = thePtr->disjunct)
00363 { if (thePtr->watchActivation) return(TRUE); }
00364
00365 return(FALSE);
00366 }
00367
00368
00369
00370
00371
00372
00373 #if WIN_BTC
00374 #pragma argsused
00375 #endif
00376 globle unsigned EnvGetDefruleWatchFirings(
00377 void *theEnv,
00378 void *rulePtr)
00379 {
00380 struct defrule *thePtr;
00381 #if MAC_MCW || WIN_MCW || MAC_XCD
00382 #pragma unused(theEnv)
00383 #endif
00384
00385 for (thePtr = (struct defrule *) rulePtr;
00386 thePtr != NULL;
00387 thePtr = thePtr->disjunct)
00388 { if (thePtr->watchFiring) return(TRUE); }
00389
00390 return(FALSE);
00391 }
00392
00393
00394
00395
00396
00397
00398 #if WIN_BTC
00399 #pragma argsused
00400 #endif
00401 globle void EnvSetDefruleWatchActivations(
00402 void *theEnv,
00403 unsigned newState,
00404 void *rulePtr)
00405 {
00406 struct defrule *thePtr;
00407 #if MAC_MCW || WIN_MCW || MAC_XCD
00408 #pragma unused(theEnv)
00409 #endif
00410
00411 for (thePtr = (struct defrule *) rulePtr;
00412 thePtr != NULL;
00413 thePtr = thePtr->disjunct)
00414 { thePtr->watchActivation = newState; }
00415 }
00416
00417
00418
00419
00420
00421
00422 #if WIN_BTC
00423 #pragma argsused
00424 #endif
00425 globle void EnvSetDefruleWatchFirings(
00426 void *theEnv,
00427 unsigned newState,
00428 void *rulePtr)
00429 {
00430 struct defrule *thePtr;
00431 #if MAC_MCW || WIN_MCW || MAC_XCD
00432 #pragma unused(theEnv)
00433 #endif
00434
00435 for (thePtr = (struct defrule *) rulePtr;
00436 thePtr != NULL;
00437 thePtr = thePtr->disjunct)
00438 { thePtr->watchFiring = newState; }
00439 }
00440
00441
00442
00443
00444
00445 globle unsigned DefruleWatchAccess(
00446 void *theEnv,
00447 int code,
00448 unsigned newState,
00449 struct expr *argExprs)
00450 {
00451 if (code)
00452 return(ConstructSetWatchAccess(theEnv,DefruleData(theEnv)->DefruleConstruct,newState,argExprs,
00453 EnvGetDefruleWatchActivations,EnvSetDefruleWatchActivations));
00454 else
00455 return(ConstructSetWatchAccess(theEnv,DefruleData(theEnv)->DefruleConstruct,newState,argExprs,
00456 EnvGetDefruleWatchFirings,EnvSetDefruleWatchFirings));
00457 }
00458
00459
00460
00461
00462
00463 globle unsigned DefruleWatchPrint(
00464 void *theEnv,
00465 char *logName,
00466 int code,
00467 struct expr *argExprs)
00468 {
00469 if (code)
00470 return(ConstructPrintWatchAccess(theEnv,DefruleData(theEnv)->DefruleConstruct,logName,argExprs,
00471 EnvGetDefruleWatchActivations,EnvSetDefruleWatchActivations));
00472 else
00473 return(ConstructPrintWatchAccess(theEnv,DefruleData(theEnv)->DefruleConstruct,logName,argExprs,
00474 EnvGetDefruleWatchActivations,EnvSetDefruleWatchActivations));
00475 }
00476
00477 #endif
00478
00479 #endif
00480
00481