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 #define _TMPLTUTL_SOURCE_
00033
00034 #include "setup.h"
00035
00036 #if DEFTEMPLATE_CONSTRUCT
00037
00038 #include <stdio.h>
00039
00040 #define _STDIO_INCLUDED_
00041
00042 #include <string.h>
00043
00044 #include "extnfunc.h"
00045 #include "memalloc.h"
00046 #include "constrct.h"
00047 #include "router.h"
00048 #include "argacces.h"
00049 #include "cstrnchk.h"
00050 #include "envrnmnt.h"
00051 #include "tmpltfun.h"
00052 #include "tmpltpsr.h"
00053 #include "modulutl.h"
00054 #include "watch.h"
00055 #include "sysdep.h"
00056 #include "tmpltbsc.h"
00057 #include "tmpltdef.h"
00058
00059 #include "tmpltutl.h"
00060
00061
00062
00063
00064
00065
00066 globle void InvalidDeftemplateSlotMessage(
00067 void *theEnv,
00068 char *slotName,
00069 char *deftemplateName,
00070 int printCR)
00071 {
00072 PrintErrorID(theEnv,"TMPLTDEF",1,printCR);
00073 EnvPrintRouter(theEnv,WERROR,"Invalid slot ");
00074 EnvPrintRouter(theEnv,WERROR,slotName);
00075 EnvPrintRouter(theEnv,WERROR," not defined in corresponding deftemplate ");
00076 EnvPrintRouter(theEnv,WERROR,deftemplateName);
00077 EnvPrintRouter(theEnv,WERROR,".\n");
00078 }
00079
00080
00081
00082
00083
00084
00085 globle void SingleFieldSlotCardinalityError(
00086 void *theEnv,
00087 char *slotName)
00088 {
00089 PrintErrorID(theEnv,"TMPLTDEF",2,TRUE);
00090 EnvPrintRouter(theEnv,WERROR,"The single field slot ");
00091 EnvPrintRouter(theEnv,WERROR,slotName);
00092 EnvPrintRouter(theEnv,WERROR," can only contain a single field value.\n");
00093 }
00094
00095
00096
00097
00098
00099 globle void MultiIntoSingleFieldSlotError(
00100 void *theEnv,
00101 struct templateSlot *theSlot,
00102 struct deftemplate *theDeftemplate)
00103 {
00104 PrintErrorID(theEnv,"TMPLTFUN",2,TRUE);
00105 EnvPrintRouter(theEnv,WERROR,"Attempted to assert a multifield value \n");
00106 EnvPrintRouter(theEnv,WERROR,"into the single field slot ");
00107 if (theSlot != NULL) EnvPrintRouter(theEnv,WERROR,theSlot->slotName->contents);
00108 else EnvPrintRouter(theEnv,WERROR,"<<unknown>>");
00109 EnvPrintRouter(theEnv,WERROR," of deftemplate ");
00110 if (theDeftemplate != NULL) EnvPrintRouter(theEnv,WERROR,theDeftemplate->header.name->contents);
00111 else EnvPrintRouter(theEnv,WERROR,"<<unknown>>");
00112 EnvPrintRouter(theEnv,WERROR,".\n");
00113
00114 SetEvaluationError(theEnv,TRUE);
00115 }
00116
00117
00118
00119
00120
00121 globle void CheckTemplateFact(
00122 void *theEnv,
00123 struct fact *theFact)
00124 {
00125 struct field *sublist;
00126 int i;
00127 struct deftemplate *theDeftemplate;
00128 struct templateSlot *slotPtr;
00129 DATA_OBJECT theData;
00130 char thePlace[20];
00131 int rv;
00132
00133 if (! EnvGetDynamicConstraintChecking(theEnv)) return;
00134
00135 sublist = theFact->theProposition.theFields;
00136
00137
00138
00139
00140
00141
00142
00143 theDeftemplate = theFact->whichDeftemplate;
00144 if (theDeftemplate == NULL) return;
00145 if (theDeftemplate->implied) return;
00146
00147
00148
00149
00150
00151 i = 0;
00152 for (slotPtr = theDeftemplate->slotList;
00153 slotPtr != NULL;
00154 slotPtr = slotPtr->next)
00155 {
00156
00157
00158
00159
00160
00161 if (slotPtr->multislot == FALSE)
00162 {
00163 theData.type = sublist[i].type;
00164 theData.value = sublist[i].value;
00165 i++;
00166 }
00167 else
00168 {
00169 theData.type = MULTIFIELD;
00170 theData.value = (void *) sublist[i].value;
00171 SetDOBegin(theData,1);
00172 SetDOEnd(theData,((struct multifield *) sublist[i].value)->multifieldLength);
00173 i++;
00174 }
00175
00176
00177
00178
00179
00180
00181 rv = ConstraintCheckDataObject(theEnv,&theData,slotPtr->constraints);
00182 if (rv != NO_VIOLATION)
00183 {
00184 gensprintf(thePlace,"fact f-%-5lld ",theFact->factIndex);
00185
00186 PrintErrorID(theEnv,"CSTRNCHK",1,TRUE);
00187 EnvPrintRouter(theEnv,WERROR,"Slot value ");
00188 PrintDataObject(theEnv,WERROR,&theData);
00189 EnvPrintRouter(theEnv,WERROR," ");
00190 ConstraintViolationErrorMessage(theEnv,NULL,thePlace,FALSE,0,slotPtr->slotName,
00191 0,rv,slotPtr->constraints,TRUE);
00192 SetHaltExecution(theEnv,TRUE);
00193 return;
00194 }
00195 }
00196
00197 return;
00198 }
00199
00200
00201
00202
00203
00204
00205 globle intBool CheckRHSSlotTypes(
00206 void *theEnv,
00207 struct expr *rhsSlots,
00208 struct templateSlot *slotPtr,
00209 char *thePlace)
00210 {
00211 int rv;
00212 char *theName;
00213
00214 if (EnvGetStaticConstraintChecking(theEnv) == FALSE) return(TRUE);
00215 rv = ConstraintCheckExpressionChain(theEnv,rhsSlots,slotPtr->constraints);
00216 if (rv != NO_VIOLATION)
00217 {
00218 if (rv != CARDINALITY_VIOLATION) theName = "A literal slot value";
00219 else theName = "Literal slot values";
00220 ConstraintViolationErrorMessage(theEnv,theName,thePlace,TRUE,0,
00221 slotPtr->slotName,0,rv,slotPtr->constraints,TRUE);
00222 return(0);
00223 }
00224
00225 return(1);
00226 }
00227
00228
00229
00230
00231
00232 globle struct templateSlot *GetNthSlot(
00233 struct deftemplate *theDeftemplate,
00234 int position)
00235 {
00236 struct templateSlot *slotPtr;
00237 int i = 0;
00238
00239 slotPtr = theDeftemplate->slotList;
00240 while (slotPtr != NULL)
00241 {
00242 if (i == position) return(slotPtr);
00243 slotPtr = slotPtr->next;
00244 i++;
00245 }
00246
00247 return(NULL);
00248 }
00249
00250
00251
00252
00253
00254 globle int FindSlotPosition(
00255 struct deftemplate *theDeftemplate,
00256 SYMBOL_HN *name)
00257 {
00258 struct templateSlot *slotPtr;
00259 int position;
00260
00261 for (slotPtr = theDeftemplate->slotList, position = 1;
00262 slotPtr != NULL;
00263 slotPtr = slotPtr->next, position++)
00264 {
00265 if (slotPtr->slotName == name)
00266 { return(position); }
00267 }
00268
00269 return(0);
00270 }
00271
00272
00273
00274
00275
00276
00277 globle void PrintTemplateFact(
00278 void *theEnv,
00279 char *logicalName,
00280 struct fact *theFact,
00281 int seperateLines,
00282 int ignoreDefaults)
00283 {
00284 struct field *sublist;
00285 int i;
00286 struct deftemplate *theDeftemplate;
00287 struct templateSlot *slotPtr;
00288 DATA_OBJECT tempDO;
00289 int slotPrinted = FALSE;
00290
00291
00292
00293
00294
00295 theDeftemplate = theFact->whichDeftemplate;
00296 sublist = theFact->theProposition.theFields;
00297
00298
00299
00300
00301
00302 EnvPrintRouter(theEnv,logicalName,"(");
00303 EnvPrintRouter(theEnv,logicalName,theDeftemplate->header.name->contents);
00304
00305
00306
00307
00308
00309 slotPtr = theDeftemplate->slotList;
00310
00311 i = 0;
00312 while (slotPtr != NULL)
00313 {
00314
00315
00316
00317
00318
00319
00320 if (ignoreDefaults && (slotPtr->defaultDynamic == FALSE))
00321 {
00322 DeftemplateSlotDefault(theEnv,theDeftemplate,slotPtr,&tempDO,TRUE);
00323
00324 if (slotPtr->multislot == FALSE)
00325 {
00326 if ((GetType(tempDO) == sublist[i].type) &&
00327 (GetValue(tempDO) == sublist[i].value))
00328 {
00329 i++;
00330 slotPtr = slotPtr->next;
00331 continue;
00332 }
00333 }
00334 else if (MultifieldsEqual((struct multifield*) GetValue(tempDO),
00335 (struct multifield *) sublist[i].value))
00336 {
00337 i++;
00338 slotPtr = slotPtr->next;
00339 continue;
00340 }
00341 }
00342
00343
00344
00345
00346
00347
00348 if (! slotPrinted)
00349 {
00350 slotPrinted = TRUE;
00351 EnvPrintRouter(theEnv,logicalName," ");
00352 }
00353
00354 if (seperateLines)
00355 { EnvPrintRouter(theEnv,logicalName,"\n "); }
00356
00357 EnvPrintRouter(theEnv,logicalName,"(");
00358 EnvPrintRouter(theEnv,logicalName,slotPtr->slotName->contents);
00359
00360
00361
00362
00363
00364 if (slotPtr->multislot == FALSE)
00365 {
00366 EnvPrintRouter(theEnv,logicalName," ");
00367 PrintAtom(theEnv,logicalName,sublist[i].type,sublist[i].value);
00368 }
00369
00370
00371
00372
00373
00374 else
00375 {
00376 struct multifield *theSegment;
00377
00378 theSegment = (struct multifield *) sublist[i].value;
00379 if (theSegment->multifieldLength > 0)
00380 {
00381 EnvPrintRouter(theEnv,logicalName," ");
00382 PrintMultifield(theEnv,logicalName,(struct multifield *) sublist[i].value,
00383 0,(long) theSegment->multifieldLength-1,FALSE);
00384 }
00385 }
00386
00387
00388
00389
00390
00391 i++;
00392 EnvPrintRouter(theEnv,logicalName,")");
00393 slotPtr = slotPtr->next;
00394 if (slotPtr != NULL) EnvPrintRouter(theEnv,logicalName," ");
00395 }
00396
00397 EnvPrintRouter(theEnv,logicalName,")");
00398 }
00399
00400
00401
00402
00403 globle void UpdateDeftemplateScope(
00404 void *theEnv)
00405 {
00406 struct deftemplate *theDeftemplate;
00407 int moduleCount;
00408 struct defmodule *theModule;
00409 struct defmoduleItemHeader *theItem;
00410
00411
00412
00413
00414
00415 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00416 theModule != NULL;
00417 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00418 {
00419
00420
00421
00422
00423 theItem = (struct defmoduleItemHeader *)
00424 GetModuleItem(theEnv,theModule,DeftemplateData(theEnv)->DeftemplateModuleIndex);
00425
00426 for (theDeftemplate = (struct deftemplate *) theItem->firstItem;
00427 theDeftemplate != NULL ;
00428 theDeftemplate = (struct deftemplate *) EnvGetNextDeftemplate(theEnv,theDeftemplate))
00429 {
00430
00431
00432
00433
00434
00435 if (FindImportedConstruct(theEnv,"deftemplate",theModule,
00436 ValueToString(theDeftemplate->header.name),
00437 &moduleCount,TRUE,NULL) != NULL)
00438 { theDeftemplate->inScope = TRUE; }
00439 else
00440 { theDeftemplate->inScope = FALSE; }
00441 }
00442 }
00443 }
00444
00445
00446
00447
00448 globle struct templateSlot *FindSlot(
00449 struct deftemplate *theDeftemplate,
00450 SYMBOL_HN *name,
00451 short *whichOne)
00452 {
00453 struct templateSlot *slotPtr;
00454
00455 *whichOne = 1;
00456 slotPtr = theDeftemplate->slotList;
00457 while (slotPtr != NULL)
00458 {
00459 if (slotPtr->slotName == name)
00460 { return(slotPtr); }
00461 (*whichOne)++;
00462 slotPtr = slotPtr->next;
00463 }
00464
00465 *whichOne = -1;
00466 return(NULL);
00467 }
00468
00469 #if (! RUN_TIME) && (! BLOAD_ONLY)
00470
00471
00472
00473
00474
00475 globle struct deftemplate *CreateImpliedDeftemplate(
00476 void *theEnv,
00477 SYMBOL_HN *deftemplateName,
00478 int setFlag)
00479 {
00480 struct deftemplate *newDeftemplate;
00481
00482 newDeftemplate = get_struct(theEnv,deftemplate);
00483 newDeftemplate->header.name = deftemplateName;
00484 newDeftemplate->header.ppForm = NULL;
00485 newDeftemplate->header.usrData = NULL;
00486 newDeftemplate->slotList = NULL;
00487 newDeftemplate->implied = setFlag;
00488 newDeftemplate->numberOfSlots = 0;
00489 newDeftemplate->inScope = 1;
00490 newDeftemplate->patternNetwork = NULL;
00491 newDeftemplate->factList = NULL;
00492 newDeftemplate->lastFact = NULL;
00493 newDeftemplate->busyCount = 0;
00494 newDeftemplate->watch = FALSE;
00495 newDeftemplate->header.next = NULL;
00496
00497 #if DEBUGGING_FUNCTIONS
00498 if (EnvGetWatchItem(theEnv,"facts"))
00499 { EnvSetDeftemplateWatch(theEnv,ON,(void *) newDeftemplate); }
00500 #endif
00501
00502 newDeftemplate->header.whichModule = (struct defmoduleItemHeader *)
00503 GetModuleItem(theEnv,NULL,DeftemplateData(theEnv)->DeftemplateModuleIndex);
00504
00505 AddConstructToModule(&newDeftemplate->header);
00506 InstallDeftemplate(theEnv,newDeftemplate);
00507
00508 return(newDeftemplate);
00509 }
00510
00511 #endif
00512
00513 #endif
00514