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
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #include <stdio.h>
00053 #define _STDIO_INCLUDED_
00054 #include <string.h>
00055
00056 #include "setup.h"
00057
00058 #if DEFTEMPLATE_CONSTRUCT
00059
00060 #define _FACTFUN_SOURCE_
00061
00062 #include "extnfunc.h"
00063 #include "envrnmnt.h"
00064 #include "argacces.h"
00065 #include "prntutil.h"
00066 #include "tmpltutl.h"
00067 #include "router.h"
00068 #include "sysdep.h"
00069
00070 #include "factfun.h"
00071
00072
00073
00074
00075 globle void FactFunctionDefinitions(
00076 void *theEnv)
00077 {
00078 #if ! RUN_TIME
00079 EnvDefineFunction2(theEnv,"fact-existp", 'b', PTIEF FactExistpFunction, "FactExistpFunction", "11z");
00080 EnvDefineFunction2(theEnv,"fact-relation",'w', PTIEF FactRelationFunction,"FactRelationFunction", "11z");
00081 EnvDefineFunction2(theEnv,"fact-slot-value",'u', PTIEF FactSlotValueFunction,"FactSlotValueFunction", "22*zw");
00082 EnvDefineFunction2(theEnv,"fact-slot-names",'u', PTIEF FactSlotNamesFunction,"FactSlotNamesFunction", "11z");
00083 EnvDefineFunction2(theEnv,"get-fact-list",'m',PTIEF GetFactListFunction,"GetFactListFunction","01w");
00084 EnvDefineFunction2(theEnv,"ppfact",'v',PTIEF PPFactFunction,"PPFactFunction","13*z");
00085 #else
00086 #if MAC_MCW || WIN_MCW || MAC_XCD
00087 #pragma unused(theEnv)
00088 #endif
00089 #endif
00090 }
00091
00092
00093
00094
00095
00096 globle void *FactRelationFunction(
00097 void *theEnv)
00098 {
00099 struct fact *theFact;
00100
00101 if (EnvArgCountCheck(theEnv,"fact-relation",EXACTLY,1) == -1) return(EnvFalseSymbol(theEnv));
00102
00103 theFact = GetFactAddressOrIndexArgument(theEnv,"fact-relation",1,FALSE);
00104
00105 if (theFact == NULL) return(EnvFalseSymbol(theEnv));
00106
00107 return(FactRelation(theFact));
00108 }
00109
00110
00111
00112
00113
00114 globle void *FactRelation(
00115 void *vTheFact)
00116 {
00117 struct fact *theFact = (struct fact *) vTheFact;
00118
00119 return((void *) theFact->whichDeftemplate->header.name);
00120 }
00121
00122
00123
00124
00125
00126 #if WIN_BTC
00127 #pragma argsused
00128 #endif
00129 globle void *EnvFactDeftemplate(
00130 void *theEnv,
00131 void *vTheFact)
00132 {
00133 #if MAC_MCW || WIN_MCW || MAC_XCD
00134 #pragma unused(theEnv)
00135 #endif
00136
00137 struct fact *theFact = (struct fact *) vTheFact;
00138
00139 return((void *) theFact->whichDeftemplate);
00140 }
00141
00142
00143
00144
00145
00146 globle int FactExistpFunction(
00147 void *theEnv)
00148 {
00149 struct fact *theFact;
00150
00151 if (EnvArgCountCheck(theEnv,"fact-existp",EXACTLY,1) == -1) return(-1L);
00152
00153 theFact = GetFactAddressOrIndexArgument(theEnv,"fact-existp",1,FALSE);
00154
00155 return(EnvFactExistp(theEnv,theFact));
00156 }
00157
00158
00159
00160
00161
00162 #if WIN_BTC
00163 #pragma argsused
00164 #endif
00165 globle int EnvFactExistp(
00166 void *theEnv,
00167 void *vTheFact)
00168 {
00169 #if MAC_MCW || WIN_MCW || MAC_XCD
00170 #pragma unused(theEnv)
00171 #endif
00172 struct fact *theFact = (struct fact *) vTheFact;
00173
00174 if (theFact == NULL) return(FALSE);
00175
00176 if (theFact->garbage) return(FALSE);
00177
00178 return(TRUE);
00179 }
00180
00181
00182
00183
00184
00185 globle void FactSlotValueFunction(
00186 void *theEnv,
00187 DATA_OBJECT *returnValue)
00188 {
00189 struct fact *theFact;
00190 DATA_OBJECT theValue;
00191
00192
00193
00194
00195
00196 returnValue->type = SYMBOL;
00197 returnValue->value = EnvFalseSymbol(theEnv);
00198
00199
00200
00201
00202
00203 if (EnvArgCountCheck(theEnv,"fact-slot-value",EXACTLY,2) == -1) return;
00204
00205
00206
00207
00208
00209 theFact = GetFactAddressOrIndexArgument(theEnv,"fact-slot-value",1,TRUE);
00210 if (theFact == NULL) return;
00211
00212
00213
00214
00215
00216 if (EnvArgTypeCheck(theEnv,"fact-slot-value",2,SYMBOL,&theValue) == FALSE)
00217 { return; }
00218
00219
00220
00221
00222
00223 FactSlotValue(theEnv,theFact,DOToString(theValue),returnValue);
00224 }
00225
00226
00227
00228
00229
00230 globle void FactSlotValue(
00231 void *theEnv,
00232 void *vTheFact,
00233 char *theSlotName,
00234 DATA_OBJECT *returnValue)
00235 {
00236 struct fact *theFact = (struct fact *) vTheFact;
00237 short position;
00238
00239
00240
00241
00242
00243
00244 if (theFact->whichDeftemplate->implied)
00245 {
00246 if (strcmp(theSlotName,"implied") != 0)
00247 {
00248 SetEvaluationError(theEnv,TRUE);
00249 InvalidDeftemplateSlotMessage(theEnv,theSlotName,
00250 ValueToString(theFact->whichDeftemplate->header.name),FALSE);
00251 return;
00252 }
00253 }
00254
00255 else if (FindSlot(theFact->whichDeftemplate,(SYMBOL_HN *) EnvAddSymbol(theEnv,theSlotName),&position) == NULL)
00256 {
00257 SetEvaluationError(theEnv,TRUE);
00258 InvalidDeftemplateSlotMessage(theEnv,theSlotName,
00259 ValueToString(theFact->whichDeftemplate->header.name),FALSE);
00260 return;
00261 }
00262
00263
00264
00265
00266
00267 if (theFact->whichDeftemplate->implied)
00268 { EnvGetFactSlot(theEnv,theFact,NULL,returnValue); }
00269 else
00270 { EnvGetFactSlot(theEnv,theFact,theSlotName,returnValue); }
00271 }
00272
00273
00274
00275
00276
00277 globle void FactSlotNamesFunction(
00278 void *theEnv,
00279 DATA_OBJECT *returnValue)
00280 {
00281 struct fact *theFact;
00282
00283
00284
00285
00286
00287 returnValue->type = SYMBOL;
00288 returnValue->value = EnvFalseSymbol(theEnv);
00289
00290
00291
00292
00293
00294 if (EnvArgCountCheck(theEnv,"fact-slot-names",EXACTLY,1) == -1) return;
00295
00296
00297
00298
00299
00300 theFact = GetFactAddressOrIndexArgument(theEnv,"fact-slot-names",1,TRUE);
00301 if (theFact == NULL) return;
00302
00303
00304
00305
00306
00307 EnvFactSlotNames(theEnv,theFact,returnValue);
00308 }
00309
00310
00311
00312
00313
00314 globle void EnvFactSlotNames(
00315 void *theEnv,
00316 void *vTheFact,
00317 DATA_OBJECT *returnValue)
00318 {
00319 struct fact *theFact = (struct fact *) vTheFact;
00320 struct multifield *theList;
00321 struct templateSlot *theSlot;
00322 unsigned long count;
00323
00324
00325
00326
00327
00328
00329 if (theFact->whichDeftemplate->implied)
00330 {
00331 SetpType(returnValue,MULTIFIELD);
00332 SetpDOBegin(returnValue,1);
00333 SetpDOEnd(returnValue,1);
00334 theList = (struct multifield *) EnvCreateMultifield(theEnv,(int) 1);
00335 SetMFType(theList,1,SYMBOL);
00336 SetMFValue(theList,1,EnvAddSymbol(theEnv,"implied"));
00337 SetpValue(returnValue,(void *) theList);
00338 return;
00339 }
00340
00341
00342
00343
00344
00345 for (count = 0, theSlot = theFact->whichDeftemplate->slotList;
00346 theSlot != NULL;
00347 count++, theSlot = theSlot->next)
00348 { }
00349
00350
00351
00352
00353
00354 SetpType(returnValue,MULTIFIELD);
00355 SetpDOBegin(returnValue,1);
00356 SetpDOEnd(returnValue,(long) count);
00357 theList = (struct multifield *) EnvCreateMultifield(theEnv,count);
00358 SetpValue(returnValue,(void *) theList);
00359
00360
00361
00362
00363
00364 for (count = 1, theSlot = theFact->whichDeftemplate->slotList;
00365 theSlot != NULL;
00366 count++, theSlot = theSlot->next)
00367 {
00368 SetMFType(theList,count,SYMBOL);
00369 SetMFValue(theList,count,theSlot->slotName);
00370 }
00371 }
00372
00373
00374
00375
00376
00377 globle void GetFactListFunction(
00378 void *theEnv,
00379 DATA_OBJECT_PTR returnValue)
00380 {
00381 struct defmodule *theModule;
00382 DATA_OBJECT result;
00383 int numArgs;
00384
00385
00386
00387
00388
00389 if ((numArgs = EnvArgCountCheck(theEnv,"get-fact-list",NO_MORE_THAN,1)) == -1)
00390 {
00391 EnvSetMultifieldErrorValue(theEnv,returnValue);
00392 return;
00393 }
00394
00395 if (numArgs == 1)
00396 {
00397 EnvRtnUnknown(theEnv,1,&result);
00398
00399 if (GetType(result) != SYMBOL)
00400 {
00401 EnvSetMultifieldErrorValue(theEnv,returnValue);
00402 ExpectedTypeError1(theEnv,"get-fact-list",1,"defmodule name");
00403 return;
00404 }
00405
00406 if ((theModule = (struct defmodule *) EnvFindDefmodule(theEnv,DOToString(result))) == NULL)
00407 {
00408 if (strcmp("*",DOToString(result)) != 0)
00409 {
00410 EnvSetMultifieldErrorValue(theEnv,returnValue);
00411 ExpectedTypeError1(theEnv,"get-fact-list",1,"defmodule name");
00412 return;
00413 }
00414
00415 theModule = NULL;
00416 }
00417 }
00418 else
00419 { theModule = ((struct defmodule *) EnvGetCurrentModule(theEnv)); }
00420
00421
00422
00423
00424
00425 EnvGetFactList(theEnv,returnValue,theModule);
00426 }
00427
00428
00429
00430
00431
00432 globle void EnvGetFactList(
00433 void *theEnv,
00434 DATA_OBJECT_PTR returnValue,
00435 void *vTheModule)
00436 {
00437 struct fact *theFact;
00438 unsigned long count;
00439 struct multifield *theList;
00440 struct defmodule *theModule = (struct defmodule *) vTheModule;
00441
00442
00443
00444
00445
00446 SaveCurrentModule(theEnv);
00447
00448
00449
00450
00451
00452 if (theModule == NULL)
00453 {
00454 for (theFact = (struct fact *) EnvGetNextFact(theEnv,NULL), count = 0;
00455 theFact != NULL;
00456 theFact = (struct fact *) EnvGetNextFact(theEnv,theFact), count++)
00457 { }
00458 }
00459 else
00460 {
00461 EnvSetCurrentModule(theEnv,(void *) theModule);
00462 UpdateDeftemplateScope(theEnv);
00463 for (theFact = (struct fact *) GetNextFactInScope(theEnv,NULL), count = 0;
00464 theFact != NULL;
00465 theFact = (struct fact *) GetNextFactInScope(theEnv,theFact), count++)
00466 { }
00467 }
00468
00469
00470
00471
00472
00473 SetpType(returnValue,MULTIFIELD);
00474 SetpDOBegin(returnValue,1);
00475 SetpDOEnd(returnValue,(long) count);
00476 theList = (struct multifield *) EnvCreateMultifield(theEnv,count);
00477 SetpValue(returnValue,(void *) theList);
00478
00479
00480
00481
00482
00483 if (theModule == NULL)
00484 {
00485 for (theFact = (struct fact *) EnvGetNextFact(theEnv,NULL), count = 1;
00486 theFact != NULL;
00487 theFact = (struct fact *) EnvGetNextFact(theEnv,theFact), count++)
00488 {
00489 SetMFType(theList,count,FACT_ADDRESS);
00490 SetMFValue(theList,count,(void *) theFact);
00491 }
00492 }
00493 else
00494 {
00495 for (theFact = (struct fact *) GetNextFactInScope(theEnv,NULL), count = 1;
00496 theFact != NULL;
00497 theFact = (struct fact *) GetNextFactInScope(theEnv,theFact), count++)
00498 {
00499 SetMFType(theList,count,FACT_ADDRESS);
00500 SetMFValue(theList,count,(void *) theFact);
00501 }
00502 }
00503
00504
00505
00506
00507
00508 RestoreCurrentModule(theEnv);
00509 UpdateDeftemplateScope(theEnv);
00510 }
00511
00512
00513
00514
00515
00516 globle void PPFactFunction(
00517 void *theEnv)
00518 {
00519 struct fact *theFact;
00520 int numberOfArguments;
00521 #if WIN_BTC
00522 char *logicalName;
00523 #else
00524 char *logicalName = NULL;
00525 #endif
00526 int ignoreDefaults = FALSE;
00527 DATA_OBJECT theArg;
00528
00529 if ((numberOfArguments = EnvArgRangeCheck(theEnv,"ppfact",1,3)) == -1) return;
00530
00531 theFact = GetFactAddressOrIndexArgument(theEnv,"ppfact",1,TRUE);
00532 if (theFact == NULL) return;
00533
00534
00535
00536
00537
00538 if (numberOfArguments == 1)
00539 { logicalName = "stdout"; }
00540 else
00541 {
00542 logicalName = GetLogicalName(theEnv,2,"stdout");
00543 if (logicalName == NULL)
00544 {
00545 IllegalLogicalNameMessage(theEnv,"ppfact");
00546 SetHaltExecution(theEnv,TRUE);
00547 SetEvaluationError(theEnv,TRUE);
00548 return;
00549 }
00550 }
00551
00552
00553
00554
00555
00556
00557 if (numberOfArguments == 3)
00558 {
00559 EnvRtnUnknown(theEnv,3,&theArg);
00560
00561 if ((theArg.value == EnvFalseSymbol(theEnv)) && (theArg.type == SYMBOL))
00562 { ignoreDefaults = FALSE; }
00563 else
00564 { ignoreDefaults = TRUE; }
00565 }
00566
00567
00568
00569
00570
00571 if (strcmp(logicalName,"nil") == 0)
00572 { return; }
00573 else if (QueryRouters(theEnv,logicalName) == FALSE)
00574 {
00575 UnrecognizedRouterMessage(theEnv,logicalName);
00576 return;
00577 }
00578
00579 EnvPPFact(theEnv,theFact,logicalName,ignoreDefaults);
00580 }
00581
00582
00583
00584
00585
00586 #if WIN_BTC
00587 #pragma argsused
00588 #endif
00589 globle void EnvPPFact(
00590 void *theEnv,
00591 void *vTheFact,
00592 char *logicalName,
00593 int ignoreDefaults)
00594 {
00595 #if MAC_MCW || WIN_MCW || MAC_XCD
00596 #pragma unused(theEnv)
00597 #endif
00598 struct fact *theFact = (struct fact *) vTheFact;
00599
00600 if (theFact == NULL) return;
00601
00602 if (theFact->garbage) return;
00603
00604 PrintFact(theEnv,logicalName,theFact,TRUE,ignoreDefaults);
00605
00606 EnvPrintRouter(theEnv,logicalName,"\n");
00607 }
00608
00609
00610
00611
00612
00613 globle struct fact *GetFactAddressOrIndexArgument(
00614 void *theEnv,
00615 char *theFunction,
00616 int position,
00617 int noFactError)
00618 {
00619 DATA_OBJECT item;
00620 long long factIndex;
00621 struct fact *theFact;
00622 char tempBuffer[20];
00623
00624 EnvRtnUnknown(theEnv,position,&item);
00625
00626 if (GetType(item) == FACT_ADDRESS)
00627 {
00628 if (((struct fact *) GetValue(item))->garbage) return(NULL);
00629 else return (((struct fact *) GetValue(item)));
00630 }
00631 else if (GetType(item) == INTEGER)
00632 {
00633 factIndex = ValueToLong(item.value);
00634 if (factIndex < 0)
00635 {
00636 ExpectedTypeError1(theEnv,theFunction,position,"fact-address or fact-index");
00637 return(NULL);
00638 }
00639
00640 theFact = FindIndexedFact(theEnv,factIndex);
00641 if ((theFact == NULL) && noFactError)
00642 {
00643 gensprintf(tempBuffer,"f-%lld",factIndex);
00644 CantFindItemErrorMessage(theEnv,"fact",tempBuffer);
00645 return(NULL);
00646 }
00647
00648 return(theFact);
00649 }
00650
00651 ExpectedTypeError1(theEnv,theFunction,position,"fact-address or fact-index");
00652 return(NULL);
00653 }
00654
00655 #endif
00656
00657