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
00034
00035
00036
00037 #define _AGENDA_SOURCE_
00038
00039 #include <stdio.h>
00040 #define _STDIO_INCLUDED_
00041 #include <string.h>
00042
00043 #include "setup.h"
00044
00045 #if DEFRULE_CONSTRUCT
00046
00047 #include "argacces.h"
00048 #include "constant.h"
00049 #include "crstrtgy.h"
00050 #include "engine.h"
00051 #include "envrnmnt.h"
00052 #include "extnfunc.h"
00053 #include "memalloc.h"
00054 #include "moduldef.h"
00055 #include "modulutl.h"
00056 #include "multifld.h"
00057 #include "reteutil.h"
00058 #include "retract.h"
00059 #include "router.h"
00060 #include "rulebsc.h"
00061 #include "ruledef.h"
00062 #include "strngrtr.h"
00063 #include "sysdep.h"
00064 #include "watch.h"
00065
00066 #include "agenda.h"
00067
00068
00069
00070
00071
00072 static void PrintActivation(void *,char *,void *);
00073 static void AgendaClearFunction(void *);
00074 static char *SalienceEvaluationName(int);
00075 static int EvaluateSalience(void *,void *);
00076 static struct salienceGroup *ReuseOrCreateSalienceGroup(void *,struct defruleModule *,int);
00077 static struct salienceGroup *FindSalienceGroup(struct defruleModule *,int);
00078 static void RemoveActivationFromGroup(void *,struct activation *,struct defruleModule *);
00079
00080
00081
00082
00083
00084
00085 globle void InitializeAgenda(
00086 void *theEnv)
00087 {
00088 AllocateEnvironmentData(theEnv,AGENDA_DATA,sizeof(struct agendaData),NULL);
00089
00090 AgendaData(theEnv)->SalienceEvaluation = WHEN_DEFINED;
00091
00092 AgendaData(theEnv)->Strategy = DEFAULT_STRATEGY;
00093
00094 EnvAddClearFunction(theEnv,"agenda",AgendaClearFunction,0);
00095 #if DEBUGGING_FUNCTIONS
00096 AddWatchItem(theEnv,"activations",1,&AgendaData(theEnv)->WatchActivations,40,DefruleWatchAccess,DefruleWatchPrint);
00097 #endif
00098 #if ! RUN_TIME
00099 EnvDefineFunction2(theEnv,"refresh", 'v', PTIEF RefreshCommand, "RefreshCommand", "11w");
00100
00101 EnvDefineFunction2(theEnv,"refresh-agenda",'v',
00102 PTIEF RefreshAgendaCommand,"RefreshAgendaCommand", "01w");
00103 EnvDefineFunction2(theEnv,"get-salience-evaluation",'w',
00104 PTIEF GetSalienceEvaluationCommand,
00105 "GetSalienceEvaluationCommand", "00");
00106 EnvDefineFunction2(theEnv,"set-salience-evaluation",'w',
00107 PTIEF SetSalienceEvaluationCommand,
00108 "SetSalienceEvaluationCommand",
00109 "11w");
00110
00111 #if DEBUGGING_FUNCTIONS
00112 EnvDefineFunction2(theEnv,"agenda", 'v', PTIEF AgendaCommand, "AgendaCommand", "01w");
00113 #endif
00114 #endif
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124 globle void AddActivation(
00125 void *theEnv,
00126 void *vTheRule,
00127 void *vBinds)
00128 {
00129 struct activation *newActivation;
00130 struct defrule *theRule = (struct defrule *) vTheRule;
00131 struct partialMatch *binds = (struct partialMatch *) vBinds;
00132 struct defruleModule *theModuleItem;
00133 struct salienceGroup *theGroup;
00134
00135
00136
00137
00138
00139
00140 if (theRule->autoFocus)
00141 { EnvFocus(theEnv,(void *) theRule->header.whichModule->theModule); }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 newActivation = get_struct(theEnv,activation);
00152 newActivation->theRule = theRule;
00153 newActivation->basis = binds;
00154 newActivation->timetag = AgendaData(theEnv)->CurrentTimetag++;
00155 newActivation->salience = EvaluateSalience(theEnv,theRule);
00156
00157 newActivation->randomID = genrand();
00158 newActivation->prev = NULL;
00159 newActivation->next = NULL;
00160
00161 AgendaData(theEnv)->NumberOfActivations++;
00162
00163
00164
00165
00166
00167
00168 binds->marker = (void *) newActivation;
00169
00170
00171
00172
00173
00174 #if DEBUGGING_FUNCTIONS
00175 if (newActivation->theRule->watchActivation)
00176 {
00177 EnvPrintRouter(theEnv,WTRACE,"==> Activation ");
00178 PrintActivation(theEnv,WTRACE,(void *) newActivation);
00179 EnvPrintRouter(theEnv,WTRACE,"\n");
00180 }
00181 #endif
00182
00183
00184
00185
00186
00187 theModuleItem = (struct defruleModule *) theRule->header.whichModule;
00188
00189 theGroup = ReuseOrCreateSalienceGroup(theEnv,theModuleItem,newActivation->salience);
00190
00191 PlaceActivation(theEnv,&(theModuleItem->agenda),newActivation,theGroup);
00192 }
00193
00194
00195
00196
00197 static struct salienceGroup *ReuseOrCreateSalienceGroup(
00198 void *theEnv,
00199 struct defruleModule *theRuleModule,
00200 int salience)
00201 {
00202 struct salienceGroup *theGroup, *lastGroup, *newGroup;
00203
00204 for (lastGroup = NULL, theGroup = theRuleModule->groupings;
00205 theGroup != NULL;
00206 lastGroup = theGroup, theGroup = theGroup->next)
00207 {
00208 if (theGroup->salience == salience)
00209 { return(theGroup); }
00210
00211 if (theGroup->salience < salience)
00212 { break; }
00213 }
00214
00215 newGroup = get_struct(theEnv,salienceGroup);
00216 newGroup->salience = salience;
00217 newGroup->first = NULL;
00218 newGroup->last = NULL;
00219 newGroup->next = theGroup;
00220 newGroup->prev = lastGroup;
00221
00222 if (newGroup->next != NULL)
00223 { newGroup->next->prev = newGroup; }
00224
00225 if (newGroup->prev != NULL)
00226 { newGroup->prev->next = newGroup; }
00227
00228 if (lastGroup == NULL)
00229 { theRuleModule->groupings = newGroup; }
00230
00231 return newGroup;
00232 }
00233
00234
00235
00236
00237 static struct salienceGroup *FindSalienceGroup(
00238 struct defruleModule *theRuleModule,
00239 int salience)
00240 {
00241 struct salienceGroup *theGroup;
00242
00243 for (theGroup = theRuleModule->groupings;
00244 theGroup != NULL;
00245 theGroup = theGroup->next)
00246 {
00247 if (theGroup->salience == salience)
00248 { return(theGroup); }
00249
00250 if (theGroup->salience < salience)
00251 { break; }
00252 }
00253
00254 return NULL;
00255 }
00256
00257
00258
00259
00260 globle void ClearRuleFromAgenda(
00261 void *theEnv,
00262 void *vTheRule)
00263 {
00264 struct defrule *theRule = (struct defrule *) vTheRule;
00265 struct defrule *tempRule;
00266 struct activation *agendaPtr, *agendaNext;
00267
00268
00269
00270
00271
00272
00273 agendaPtr = ((struct defruleModule *) theRule->header.whichModule)->agenda;
00274
00275
00276
00277
00278
00279 while (agendaPtr != NULL)
00280 {
00281 agendaNext = agendaPtr->next;
00282
00283
00284
00285
00286
00287
00288
00289 for (tempRule = theRule;
00290 tempRule != NULL;
00291 tempRule = tempRule->disjunct)
00292 {
00293 if (agendaPtr->theRule == tempRule)
00294 {
00295 RemoveActivation(theEnv,agendaPtr,TRUE,TRUE);
00296 break;
00297 }
00298 }
00299
00300 agendaPtr = agendaNext;
00301 }
00302 }
00303
00304
00305
00306
00307
00308
00309
00310 globle void *EnvGetNextActivation(
00311 void *theEnv,
00312 void *actPtr)
00313 {
00314 struct defruleModule *theModuleItem;
00315
00316 if (actPtr == NULL)
00317 {
00318 theModuleItem = (struct defruleModule *) GetModuleItem(theEnv,NULL,DefruleData(theEnv)->DefruleModuleIndex);
00319 if (theModuleItem == NULL) return(NULL);
00320 return((void *) theModuleItem->agenda);
00321 }
00322 else
00323 { return((void *) (((struct activation *) actPtr)->next)); }
00324 }
00325
00326
00327
00328
00329
00330 #if WIN_BTC
00331 #pragma argsused
00332 #endif
00333 globle char *EnvGetActivationName(
00334 void *theEnv,
00335 void *actPtr)
00336 {
00337 #if MAC_MCW || WIN_MCW || MAC_XCD
00338 #pragma unused(theEnv)
00339 #endif
00340
00341 return(ValueToString(((struct activation *) actPtr)->theRule->header.name));
00342 }
00343
00344
00345
00346
00347
00348 #if WIN_BTC
00349 #pragma argsused
00350 #endif
00351 globle int EnvSetActivationSalience(
00352 void *theEnv,
00353 void *actPtr,
00354 int value)
00355 {
00356 int temp;
00357 #if MAC_MCW || WIN_MCW || MAC_XCD
00358 #pragma unused(theEnv)
00359 #endif
00360
00361 temp = ((struct activation *) actPtr)->salience;
00362 ((struct activation *) actPtr)->salience = value;
00363 return(temp);
00364 }
00365
00366
00367
00368
00369
00370 globle void EnvGetActivationPPForm(
00371 void *theEnv,
00372 char *buffer,
00373 unsigned bufferLength,
00374 void *theActivation)
00375 {
00376 OpenStringDestination(theEnv,"ActPPForm",buffer,bufferLength);
00377 PrintActivation(theEnv,"ActPPForm",(void *) theActivation);
00378 CloseStringDestination(theEnv,"ActPPForm");
00379 }
00380
00381
00382
00383
00384
00385 globle void EnvGetActivationBasisPPForm(
00386 void *theEnv,
00387 char *buffer,
00388 unsigned bufferLength,
00389 void *vTheActivation)
00390 {
00391 struct activation *theActivation = (struct activation *) vTheActivation;
00392
00393 OpenStringDestination(theEnv,"ActPPForm",buffer,bufferLength);
00394 PrintPartialMatch(theEnv,"ActPPForm",theActivation->basis);
00395 CloseStringDestination(theEnv,"ActPPForm");
00396 }
00397
00398
00399
00400
00401
00402 globle intBool MoveActivationToTop(
00403 void *theEnv,
00404 void *vtheActivation)
00405 {
00406 struct activation *prevPtr;
00407 struct activation *theActivation = (struct activation *) vtheActivation;
00408 struct defruleModule *theModuleItem;
00409
00410
00411
00412
00413
00414
00415 theModuleItem = (struct defruleModule *) theActivation->theRule->header.whichModule;
00416
00417
00418
00419
00420
00421
00422 if (theActivation == theModuleItem->agenda) return(FALSE);
00423
00424
00425
00426
00427
00428
00429 prevPtr = theActivation->prev;
00430 prevPtr->next = theActivation->next;
00431 if (theActivation->next != NULL) theActivation->next->prev = prevPtr;
00432
00433
00434
00435
00436
00437
00438
00439 theActivation->next = theModuleItem->agenda;
00440 theModuleItem->agenda->prev = theActivation;
00441 theActivation->prev = NULL;
00442 theModuleItem->agenda = theActivation;
00443
00444
00445
00446
00447
00448 AgendaData(theEnv)->AgendaChanged = TRUE;
00449
00450 return(TRUE);
00451 }
00452
00453
00454
00455
00456
00457 globle intBool EnvDeleteActivation(
00458 void *theEnv,
00459 void *theActivation)
00460 {
00461 if (theActivation == NULL) RemoveAllActivations(theEnv);
00462 else RemoveActivation(theEnv,(struct activation *) theActivation,TRUE,TRUE);
00463
00464 return(TRUE);
00465 }
00466
00467
00468
00469
00470
00471 globle intBool DetachActivation(
00472 void *theEnv,
00473 void *vTheActivation)
00474 {
00475 struct defruleModule *theModuleItem;
00476 struct activation *theActivation = (struct activation *) vTheActivation;
00477
00478
00479
00480
00481
00482 if (theActivation == NULL) SystemError(theEnv,"AGENDA",1);
00483
00484
00485
00486
00487
00488
00489 theModuleItem = (struct defruleModule *) theActivation->theRule->header.whichModule;
00490
00491 RemoveActivationFromGroup(theEnv,theActivation,theModuleItem);
00492
00493
00494
00495
00496
00497
00498 if (theActivation == theModuleItem->agenda)
00499 { theModuleItem->agenda = theActivation->next; }
00500
00501
00502
00503
00504
00505 if (theActivation->prev != NULL)
00506 { theActivation->prev->next = theActivation->next; }
00507
00508
00509
00510
00511
00512 if (theActivation->next != NULL)
00513 { theActivation->next->prev = theActivation->prev; }
00514
00515
00516
00517
00518
00519 theActivation->prev = NULL;
00520 theActivation->next = NULL;
00521
00522
00523
00524
00525
00526 AgendaData(theEnv)->AgendaChanged = TRUE;
00527
00528 return(TRUE);
00529 }
00530
00531
00532
00533
00534
00535 static void PrintActivation(
00536 void *theEnv,
00537 char *logicalName,
00538 void *vTheActivation)
00539 {
00540 struct activation *theActivation = (struct activation *) vTheActivation;
00541 char printSpace[20];
00542
00543 gensprintf(printSpace,"%-6d ",theActivation->salience);
00544 EnvPrintRouter(theEnv,logicalName,printSpace);
00545 EnvPrintRouter(theEnv,logicalName,ValueToString(theActivation->theRule->header.name));
00546 EnvPrintRouter(theEnv,logicalName,": ");
00547 PrintPartialMatch(theEnv,logicalName,theActivation->basis);
00548 }
00549
00550
00551
00552
00553
00554 globle void EnvAgenda(
00555 void *theEnv,
00556 char *logicalName,
00557 void *vTheModule)
00558 {
00559 struct defmodule *theModule = (struct defmodule *) vTheModule;
00560
00561 ListItemsDriver(theEnv,logicalName,theModule,"activation","activations",
00562 EnvGetNextActivation,NULL,PrintActivation,NULL);
00563 }
00564
00565
00566
00567
00568
00569
00570 globle void RemoveActivation(
00571 void *theEnv,
00572 void *vTheActivation,
00573 int updateAgenda,
00574 int updateLinks)
00575 {
00576 struct defruleModule *theModuleItem;
00577 struct activation *theActivation = (struct activation *) vTheActivation;
00578
00579
00580
00581
00582
00583
00584 theModuleItem = (struct defruleModule *) theActivation->theRule->header.whichModule;
00585
00586
00587
00588
00589
00590 if (updateAgenda == TRUE)
00591 {
00592 RemoveActivationFromGroup(theEnv,theActivation,theModuleItem);
00593
00594
00595
00596
00597
00598 if (theActivation->prev == NULL)
00599 {
00600 theModuleItem->agenda = theModuleItem->agenda->next;
00601 if (theModuleItem->agenda != NULL) theModuleItem->agenda->prev = NULL;
00602 }
00603 else
00604 {
00605 theActivation->prev->next = theActivation->next;
00606 if (theActivation->next != NULL)
00607 { theActivation->next->prev = theActivation->prev; }
00608 }
00609
00610
00611
00612
00613
00614
00615 #if DEBUGGING_FUNCTIONS
00616 if (theActivation->theRule->watchActivation)
00617 {
00618 EnvPrintRouter(theEnv,WTRACE,"<== Activation ");
00619 PrintActivation(theEnv,WTRACE,(void *) theActivation);
00620 EnvPrintRouter(theEnv,WTRACE,"\n");
00621 }
00622 #endif
00623
00624
00625
00626
00627
00628 AgendaData(theEnv)->AgendaChanged = TRUE;
00629 }
00630
00631
00632
00633
00634
00635 if ((updateLinks == TRUE) && (theActivation->basis != NULL))
00636 { theActivation->basis->marker = NULL; }
00637
00638
00639
00640
00641
00642 AgendaData(theEnv)->NumberOfActivations--;
00643
00644 rtn_struct(theEnv,activation,theActivation);
00645 }
00646
00647
00648
00649
00650 static void RemoveActivationFromGroup(
00651 void *theEnv,
00652 struct activation *theActivation,
00653 struct defruleModule *theRuleModule)
00654 {
00655 struct salienceGroup *theGroup;
00656
00657 theGroup = FindSalienceGroup(theRuleModule,theActivation->salience);
00658 if (theGroup == NULL) return;
00659
00660 if (theActivation == theGroup->first)
00661 {
00662
00663
00664
00665
00666
00667 if (theActivation == theGroup->last)
00668 {
00669 if (theGroup->prev == NULL)
00670 { theRuleModule->groupings = theGroup->next; }
00671 else
00672 { theGroup->prev->next = theGroup->next; }
00673
00674 if (theGroup->next != NULL)
00675 { theGroup->next->prev = theGroup->prev; }
00676
00677 rtn_struct(theEnv,salienceGroup,theGroup);
00678 }
00679
00680
00681
00682
00683
00684
00685 else
00686 { theGroup->first = theActivation->next; }
00687 }
00688 else
00689 {
00690
00691
00692
00693
00694
00695 if (theActivation == theGroup->last)
00696 { theGroup->last = theActivation->prev; }
00697
00698
00699
00700
00701
00702
00703 else
00704 { return; }
00705 }
00706 }
00707
00708
00709
00710
00711
00712 static void AgendaClearFunction(
00713 void *theEnv)
00714 {
00715 AgendaData(theEnv)->CurrentTimetag = 0;
00716 }
00717
00718
00719
00720
00721
00722 globle void RemoveAllActivations(
00723 void *theEnv)
00724 {
00725 struct activation *tempPtr, *theActivation;
00726 struct salienceGroup *theGroup, *tempGroup;
00727
00728 theActivation = GetDefruleModuleItem(theEnv,NULL)->agenda;
00729 while (theActivation != NULL)
00730 {
00731 tempPtr = theActivation->next;
00732 RemoveActivation(theEnv,theActivation,TRUE,TRUE);
00733 theActivation = tempPtr;
00734 }
00735
00736 theGroup = GetDefruleModuleItem(theEnv,NULL)->groupings;
00737 while (theGroup != NULL)
00738 {
00739 tempGroup = theGroup->next;
00740 rtn_struct(theEnv,salienceGroup,theGroup);
00741 theGroup = tempGroup;
00742 }
00743 }
00744
00745
00746
00747
00748
00749
00750 globle int EnvGetAgendaChanged(
00751 void *theEnv)
00752 {
00753 return(AgendaData(theEnv)->AgendaChanged);
00754 }
00755
00756
00757
00758
00759
00760 globle void EnvSetAgendaChanged(
00761 void *theEnv,
00762 int value)
00763 {
00764 AgendaData(theEnv)->AgendaChanged = value;
00765 }
00766
00767
00768
00769
00770
00771 globle void EnvReorderAgenda(
00772 void *theEnv,
00773 void *vTheModule)
00774 {
00775 struct activation *theActivation, *tempPtr;
00776 struct defmodule *theModule = (struct defmodule *) vTheModule;
00777 int allModules = FALSE;
00778 struct defruleModule *theModuleItem;
00779 struct salienceGroup *theGroup, *tempGroup;
00780
00781
00782
00783
00784
00785
00786 if (theModule == NULL)
00787 {
00788 allModules = TRUE;
00789 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00790 }
00791
00792
00793
00794
00795
00796 for (;
00797 theModule != NULL;
00798 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00799 {
00800
00801
00802
00803
00804
00805 theModuleItem = GetDefruleModuleItem(theEnv,theModule);
00806 theActivation = theModuleItem->agenda;
00807 theModuleItem->agenda = NULL;
00808
00809 theGroup = theModuleItem->groupings;
00810 while (theGroup != NULL)
00811 {
00812 tempGroup = theGroup->next;
00813 rtn_struct(theEnv,salienceGroup,theGroup);
00814 theGroup = tempGroup;
00815 }
00816
00817 theModuleItem->groupings = NULL;
00818
00819
00820
00821
00822
00823
00824 while (theActivation != NULL)
00825 {
00826 tempPtr = theActivation->next;
00827 theActivation->next = NULL;
00828 theActivation->prev = NULL;
00829 theGroup = ReuseOrCreateSalienceGroup(theEnv,theModuleItem,theActivation->salience);
00830 PlaceActivation(theEnv,&(theModuleItem->agenda),theActivation,theGroup);
00831 theActivation = tempPtr;
00832 }
00833
00834
00835
00836
00837
00838 if (! allModules) return;
00839 }
00840 }
00841
00842
00843
00844
00845
00846 globle unsigned long GetNumberOfActivations(
00847 void *theEnv)
00848 {
00849 return(AgendaData(theEnv)->NumberOfActivations);
00850 }
00851
00852
00853
00854
00855
00856 globle void RefreshCommand(
00857 void *theEnv)
00858 {
00859 char *ruleName;
00860 void *rulePtr;
00861
00862
00863
00864
00865
00866 ruleName = GetConstructName(theEnv,"refresh","rule name");
00867 if (ruleName == NULL) return;
00868
00869
00870
00871
00872
00873 rulePtr = EnvFindDefrule(theEnv,ruleName);
00874 if (rulePtr == NULL)
00875 {
00876 CantFindItemErrorMessage(theEnv,"defrule",ruleName);
00877 return;
00878 }
00879
00880
00881
00882
00883
00884 EnvRefresh(theEnv,rulePtr);
00885 }
00886
00887
00888
00889
00890
00891 globle intBool EnvRefresh(
00892 void *theEnv,
00893 void *theRule)
00894 {
00895 struct defrule *rulePtr;
00896 struct partialMatch *listOfMatches;
00897 unsigned long b;
00898
00899
00900
00901
00902
00903 for (rulePtr = (struct defrule *) theRule;
00904 rulePtr != NULL;
00905 rulePtr = rulePtr->disjunct)
00906 {
00907
00908
00909
00910
00911
00912 for (b = 0; b < rulePtr->lastJoin->leftMemory->size; b++)
00913 {
00914 for (listOfMatches = rulePtr->lastJoin->leftMemory->beta[b];
00915 listOfMatches != NULL;
00916 listOfMatches = listOfMatches->nextInMemory)
00917 {
00918
00919
00920
00921
00922
00923
00924
00925 if (((struct joinNode *) listOfMatches->owner)->ruleToActivate != NULL)
00926 {
00927 if (listOfMatches->marker == NULL)
00928 { AddActivation(theEnv,rulePtr,listOfMatches); }
00929 }
00930 }
00931 }
00932 }
00933
00934 return(TRUE);
00935 }
00936
00937
00938
00939
00940
00941 globle void RefreshAgendaCommand(
00942 void *theEnv)
00943 {
00944 int numArgs, error;
00945 struct defmodule *theModule;
00946
00947
00948
00949
00950
00951 if ((numArgs = EnvArgCountCheck(theEnv,"refresh-agenda",NO_MORE_THAN,1)) == -1) return;
00952
00953
00954
00955
00956
00957
00958
00959 if (numArgs == 1)
00960 {
00961 theModule = GetModuleName(theEnv,"refresh-agenda",1,&error);
00962 if (error) return;
00963 }
00964 else
00965 { theModule = ((struct defmodule *) EnvGetCurrentModule(theEnv)); }
00966
00967
00968
00969
00970
00971 EnvRefreshAgenda(theEnv,theModule);
00972 }
00973
00974
00975
00976
00977
00978 globle void EnvRefreshAgenda(
00979 void *theEnv,
00980 void *vTheModule)
00981 {
00982 struct activation *theActivation;
00983 struct defmodule *theModule = (struct defmodule *) vTheModule;
00984 intBool oldValue;
00985 int allModules = FALSE;
00986
00987
00988
00989
00990
00991 SaveCurrentModule(theEnv);
00992
00993
00994
00995
00996
00997
00998 if (theModule == NULL)
00999 {
01000 allModules = TRUE;
01001 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
01002 }
01003
01004
01005
01006
01007
01008
01009
01010 oldValue = EnvGetSalienceEvaluation(theEnv);
01011 EnvSetSalienceEvaluation(theEnv,WHEN_ACTIVATED);
01012
01013
01014
01015
01016
01017 for (;
01018 theModule != NULL;
01019 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
01020 {
01021
01022
01023
01024
01025
01026 EnvSetCurrentModule(theEnv,(void *) theModule);
01027
01028
01029
01030
01031
01032 for (theActivation = (struct activation *) EnvGetNextActivation(theEnv,NULL);
01033 theActivation != NULL;
01034 theActivation = (struct activation *) EnvGetNextActivation(theEnv,theActivation))
01035 { theActivation->salience = EvaluateSalience(theEnv,theActivation->theRule); }
01036
01037
01038
01039
01040
01041 EnvReorderAgenda(theEnv,theModule);
01042
01043
01044
01045
01046
01047 if (! allModules)
01048 {
01049 EnvSetSalienceEvaluation(theEnv,oldValue);
01050 RestoreCurrentModule(theEnv);
01051 return;
01052 }
01053 }
01054
01055
01056
01057
01058
01059 EnvSetSalienceEvaluation(theEnv,oldValue);
01060
01061
01062
01063
01064
01065 RestoreCurrentModule(theEnv);
01066 }
01067
01068
01069
01070
01071
01072
01073 globle void *SetSalienceEvaluationCommand(
01074 void *theEnv)
01075 {
01076 DATA_OBJECT argPtr;
01077 char *argument, *oldValue;
01078
01079
01080
01081
01082
01083 oldValue = SalienceEvaluationName(EnvGetSalienceEvaluation(theEnv));
01084
01085
01086
01087
01088
01089
01090 if (EnvArgCountCheck(theEnv,"set-salience-evaluation",EXACTLY,1) == -1)
01091 { return((SYMBOL_HN *) EnvAddSymbol(theEnv,oldValue)); }
01092
01093 if (EnvArgTypeCheck(theEnv,"set-salience-evaluation",1,SYMBOL,&argPtr) == FALSE)
01094 { return((SYMBOL_HN *) EnvAddSymbol(theEnv,oldValue)); }
01095
01096
01097
01098
01099
01100
01101 argument = DOToString(argPtr);
01102
01103 if (strcmp(argument,"when-defined") == 0)
01104 { EnvSetSalienceEvaluation(theEnv,WHEN_DEFINED); }
01105 else if (strcmp(argument,"when-activated") == 0)
01106 { EnvSetSalienceEvaluation(theEnv,WHEN_ACTIVATED); }
01107 else if (strcmp(argument,"every-cycle") == 0)
01108 { EnvSetSalienceEvaluation(theEnv,EVERY_CYCLE); }
01109 else
01110 {
01111 ExpectedTypeError1(theEnv,"set-salience-evaluation",1,
01112 "symbol with value when-defined, when-activated, or every-cycle");
01113 return((SYMBOL_HN *) EnvAddSymbol(theEnv,oldValue));
01114 }
01115
01116
01117
01118
01119
01120 return((SYMBOL_HN *) EnvAddSymbol(theEnv,oldValue));
01121 }
01122
01123
01124
01125
01126
01127
01128 globle void *GetSalienceEvaluationCommand(
01129 void *theEnv)
01130 {
01131 EnvArgCountCheck(theEnv,"get-salience-evaluation",EXACTLY,0);
01132
01133 return((SYMBOL_HN *) EnvAddSymbol(theEnv,SalienceEvaluationName(EnvGetSalienceEvaluation(theEnv))));
01134 }
01135
01136
01137
01138
01139
01140
01141 static char *SalienceEvaluationName(
01142 int strategy)
01143 {
01144 char *sname;
01145
01146 switch (strategy)
01147 {
01148 case WHEN_DEFINED:
01149 sname = "when-defined";
01150 break;
01151 case WHEN_ACTIVATED:
01152 sname = "when-activated";
01153 break;
01154 case EVERY_CYCLE:
01155 sname = "every-cycle";
01156 break;
01157 default:
01158 sname = "unknown";
01159 break;
01160 }
01161
01162 return(sname);
01163 }
01164
01165
01166
01167
01168
01169
01170 globle intBool EnvGetSalienceEvaluation(
01171 void *theEnv)
01172 {
01173 return(AgendaData(theEnv)->SalienceEvaluation);
01174 }
01175
01176
01177
01178
01179
01180 globle intBool EnvSetSalienceEvaluation(
01181 void *theEnv,
01182 int value)
01183 {
01184 int ov;
01185
01186 ov = AgendaData(theEnv)->SalienceEvaluation;
01187 AgendaData(theEnv)->SalienceEvaluation = value;
01188 return(ov);
01189 }
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199 static int EvaluateSalience(
01200 void *theEnv,
01201 void *vPtr)
01202 {
01203 struct defrule *rPtr = (struct defrule *) vPtr;
01204 DATA_OBJECT salienceValue;
01205 int salience;
01206
01207
01208
01209
01210
01211
01212
01213 if (EnvGetSalienceEvaluation(theEnv) == WHEN_DEFINED)
01214 { return(rPtr->salience); }
01215
01216
01217
01218
01219
01220
01221
01222
01223 if (rPtr->dynamicSalience == NULL) return(rPtr->salience);
01224
01225
01226
01227
01228
01229
01230 SetEvaluationError(theEnv,FALSE);
01231 if (EvaluateExpression(theEnv,rPtr->dynamicSalience,&salienceValue))
01232 {
01233 SalienceInformationError(theEnv,"defrule",ValueToString(rPtr->header.name));
01234 return(rPtr->salience);
01235 }
01236
01237
01238
01239
01240
01241 if (salienceValue.type != INTEGER)
01242 {
01243 SalienceNonIntegerError(theEnv);
01244 SalienceInformationError(theEnv,"defrule",ValueToString(rPtr->header.name));
01245 SetEvaluationError(theEnv,TRUE);
01246 return(rPtr->salience);
01247 }
01248
01249
01250
01251
01252
01253
01254 salience = (int) ValueToLong(salienceValue.value);
01255
01256 if ((salience > MAX_DEFRULE_SALIENCE) || (salience < MIN_DEFRULE_SALIENCE))
01257 {
01258 SalienceRangeError(theEnv,MIN_DEFRULE_SALIENCE,MAX_DEFRULE_SALIENCE);
01259 SetEvaluationError(theEnv,TRUE);
01260 SalienceInformationError(theEnv,"defrule",ValueToString(((struct defrule *) rPtr)->header.name));
01261 return(rPtr->salience);
01262 }
01263
01264
01265
01266
01267
01268
01269 rPtr->salience = salience;
01270 return(rPtr->salience);
01271 }
01272
01273 #if DEBUGGING_FUNCTIONS
01274
01275
01276
01277
01278
01279
01280 globle void AgendaCommand(
01281 void *theEnv)
01282 {
01283 int numArgs, error;
01284 struct defmodule *theModule;
01285
01286
01287
01288
01289
01290 if ((numArgs = EnvArgCountCheck(theEnv,"agenda",NO_MORE_THAN,1)) == -1) return;
01291
01292
01293
01294
01295
01296
01297
01298 if (numArgs == 1)
01299 {
01300 theModule = GetModuleName(theEnv,"agenda",1,&error);
01301 if (error) return;
01302 }
01303 else
01304 { theModule = ((struct defmodule *) EnvGetCurrentModule(theEnv)); }
01305
01306
01307
01308
01309
01310 EnvAgenda(theEnv,WDISPLAY,theModule);
01311 }
01312
01313 #endif
01314
01315 #endif
01316