📄 globldef.c
字号:
/*******************************************************/ /* "C" Language Integrated Production System */ /* */ /* CLIPS Version 6.05 04/09/97 */ /* */ /* DEFGLOBAL MODULE */ /*******************************************************//*************************************************************//* Purpose: Provides core routines for the creation and *//* maintenance of the defglobal construct. *//* *//* Principal Programmer(s): *//* Gary D. Riley *//* *//* Contributing Programmer(s): *//* Brian L. Donnell *//* *//* Revision History: *//* *//*************************************************************/#define _GLOBLDEF_SOURCE_#include "setup.h"#if DEFGLOBAL_CONSTRUCT#include <stdio.h>#define _CLIPS_STDIO_#include "clipsmem.h"#include "modulpsr.h"#include "multifld.h"#include "router.h"#include "strngrtr.h"#include "modulutl.h"#include "globlbsc.h"#include "globlpsr.h"#include "globlcom.h"#include "utility.h"#include "commline.h"#if BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE#include "bload.h"#include "globlbin.h"#endif#if CONSTRUCT_COMPILER && (! RUN_TIME)#include "globlcmp.h"#endif#include "globldef.h"/***************************************//* LOCAL INTERNAL FUNCTION DEFINITIONS *//***************************************/#if ANSI_COMPILER static VOID *AllocateModule(void); static VOID FreeModule(VOID *); static VOID ReturnDefglobal(VOID *); static VOID InitializeDefglobalModules(void); static BOOLEAN GetDefglobalValue2(VOID *,DATA_OBJECT_PTR); static int QGetDefglobalValue(VOID *,DATA_OBJECT_PTR); static VOID IncrementDefglobalBusyCount(VOID *); static VOID DecrementDefglobalBusyCount(VOID *);#else static VOID *AllocateModule(); static VOID FreeModule(); static VOID ReturnDefglobal(); static VOID InitializeDefglobalModules(); static BOOLEAN GetDefglobalValue2(); static int QGetDefglobalValue(); static VOID IncrementDefglobalBusyCount(); static VOID DecrementDefglobalBusyCount();#endif/****************************************//* GLOBAL INTERNAL VARIABLE DEFINITIONS *//****************************************/ globle struct construct *DefglobalConstruct; globle int DefglobalModuleIndex; globle int ChangeToGlobals = CLIPS_FALSE; globle struct entityRecord GlobalInfo = { GBL_VARIABLE,0,0,0, NULL, NULL, NULL, GetDefglobalValue2, NULL,NULL, NULL,NULL,NULL }; globle struct entityRecord DefglobalPtrRecord = { DEFGLOBAL_PTR,0,0,0, NULL,NULL,NULL, QGetDefglobalValue, NULL, DecrementDefglobalBusyCount, IncrementDefglobalBusyCount, NULL,NULL,NULL,NULL };/**************************************************************//* InitializeDefglobals: Initializes the defglobal construct. *//**************************************************************/globle VOID InitializeDefglobals() { InstallPrimitive(&GlobalInfo,GBL_VARIABLE); InstallPrimitive((ENTITY_RECORD_PTR) &DefglobalPtrRecord,DEFGLOBAL_PTR); InitializeDefglobalModules(); DefglobalBasicCommands(); DefglobalCommandDefinitions(); DefglobalConstruct = AddConstruct("defglobal","defglobals",ParseDefglobal,FindDefglobal, GetConstructNamePointer,GetConstructPPForm, GetConstructModuleItem,GetNextDefglobal,SetNextConstruct, IsDefglobalDeletable,Undefglobal,ReturnDefglobal); }/*********************************************************//* InitializeDefglobalModules: Initializes the defglobal *//* construct for use with the defmodule construct. *//*********************************************************/static VOID InitializeDefglobalModules() { DefglobalModuleIndex = RegisterModuleItem("defglobal", AllocateModule, FreeModule,#if BLOAD_AND_BSAVE || BLOAD || BLOAD_ONLY BloadDefglobalModuleReference,#else NULL,#endif#if CONSTRUCT_COMPILER && (! RUN_TIME) DefglobalCModuleReference,#else NULL,#endif FindDefglobal);#if (! BLOAD_ONLY) && (! RUN_TIME) && DEFMODULE_CONSTRUCT AddPortConstructItem("defglobal",SYMBOL);#endif }/*************************************************//* AllocateModule: Allocates a defglobal module. *//*************************************************/static VOID *AllocateModule() { return((VOID *) get_struct(defglobalModule)); } /***********************************************//* FreeModule: Deallocates a defglobal module. */ /***********************************************/static VOID FreeModule(theItem) VOID *theItem; { FreeConstructHeaderModule(theItem,DefglobalConstruct); rtn_struct(defglobalModule,theItem); } /**************************************************************//* GetDefglobalModuleItem: Returns a pointer to the defmodule *//* item for the specified defglobal or defmodule. *//**************************************************************/globle struct defglobalModule *GetDefglobalModuleItem(theModule) struct defmodule *theModule; { return((struct defglobalModule *) GetConstructModuleItemByIndex(theModule,DefglobalModuleIndex)); } /**********************************************************************//* FindDefglobal: Searches for a defglobal in the list of defglobals. *//* Returns a pointer to the defglobal if found, otherwise NULL. *//**********************************************************************/globle VOID *FindDefglobal(defglobalName) char *defglobalName; { return(FindNamedConstruct(defglobalName,DefglobalConstruct)); }/*****************************************************************//* GetNextDefglobal: If passed a NULL pointer, returns the first *//* defglobal in the defglobal list. Otherwise returns the next *//* defglobal following the defglobal passed as an argument. *//*****************************************************************/globle VOID *GetNextDefglobal(defglobalPtr) VOID *defglobalPtr; { return((VOID *) GetNextConstructItem(defglobalPtr,DefglobalModuleIndex)); } /********************************************************//* IsDefglobalDeletable: Returns TRUE if a particular *//* defglobal can be deleted, otherwise returns FALSE. *//********************************************************/#if IBM_TBC#pragma argsused#endifgloble BOOLEAN IsDefglobalDeletable(ptr) VOID *ptr; {#if MAC_MPW || MAC_MCW#pragma unused(ptr)#endif#if BLOAD_ONLY || RUN_TIME return(FALSE);#else#if BLOAD || BLOAD_AND_BSAVE if (Bloaded()) return(FALSE);#endif if (((struct defglobal *) ptr)->busyCount) return(CLIPS_FALSE); return(CLIPS_TRUE);#endif } /************************************************************//* ReturnDefglobal: Returns the data structures associated *//* with a defglobal construct to the pool of free memory. *//************************************************************/static VOID ReturnDefglobal(vTheDefglobal) VOID *vTheDefglobal; {#if (MAC_MPW || MAC_MCW) && (RUN_TIME || BLOAD_ONLY)#pragma unused(vTheDefglobal)#endif#if (! BLOAD_ONLY) && (! RUN_TIME) struct defglobal *theDefglobal = (struct defglobal *) vTheDefglobal; if (theDefglobal == NULL) return; /*====================================*/ /* Return the global's current value. */ /*====================================*/ ValueDeinstall(&theDefglobal->current); if (theDefglobal->current.type == MULTIFIELD) { ReturnMultifield(theDefglobal->current.value); } /*================================================*/ /* Return the expression representing the initial */ /* value of the defglobal when it was defined. */ /*================================================*/ RemoveHashedExpression(theDefglobal->initial); /*===============================*/ /* Release items stored in the */ /* defglobal's construct header. */ /*===============================*/ DeinstallConstructHeader(&theDefglobal->header); /*======================================*/ /* Return the defglobal data structure. */ /*======================================*/ rtn_struct(defglobal,theDefglobal); /*===========================================*/ /* Set the variable indicating that a change */ /* has been made to a global variable. */ /*===========================================*/ ChangeToGlobals = CLIPS_TRUE;#endif } /************************************************//* QSetDefglobalValue: Lowest level routine for *//* setting a defglobal's value. *//************************************************/globle VOID QSetDefglobalValue(theGlobal,vPtr,resetVar) struct defglobal *theGlobal; DATA_OBJECT_PTR vPtr; int resetVar; { /*====================================================*/ /* If the new value passed for the defglobal is NULL, */ /* then reset the defglobal to the initial value it */ /* had when it was defined. */ /*====================================================*/ if (resetVar) { EvaluateExpression(theGlobal->initial,vPtr); if (EvaluationError) { vPtr->type = SYMBOL; vPtr->value = CLIPSFalseSymbol; } } /*==========================================*/ /* If globals are being watch, then display */ /* the change to the global variable. */ /*==========================================*/ #if DEBUGGING_FUNCTIONS if (theGlobal->watch) { PrintCLIPS(WTRACE,":== ?*"); PrintCLIPS(WTRACE,ValueToString(theGlobal->header.name)); PrintCLIPS(WTRACE,"* ==> "); PrintDataObject(WTRACE,vPtr); PrintCLIPS(WTRACE," <== "); PrintDataObject(WTRACE,&theGlobal->current); PrintCLIPS(WTRACE,"\n"); }#endif /*==============================================*/ /* Remove the old value of the global variable. */ /*==============================================*/ ValueDeinstall(&theGlobal->current); if (theGlobal->current.type == MULTIFIELD) { ReturnMultifield(theGlobal->current.value); } /*===========================================*/ /* Set the new value of the global variable. */ /*===========================================*/ theGlobal->current.type = vPtr->type; if (vPtr->type != MULTIFIELD) theGlobal->current.value = vPtr->value; else DuplicateMultifield(&theGlobal->current,vPtr); ValueInstall(&theGlobal->current); /*===========================================*/ /* Set the variable indicating that a change */ /* has been made to a global variable. */ /*===========================================*/ ChangeToGlobals = CLIPS_TRUE; if ((CurrentEvaluationDepth == 0) && (! EvaluatingTopLevelCommand) && (CurrentExpression == NULL)) { PeriodicCleanup(CLIPS_TRUE,CLIPS_FALSE); } } /**************************************************************//* QFindDefglobal: Searches for a defglobal in the list of */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -