📄 insfun.c
字号:
/*******************************************************/ /* "C" Language Integrated Production System */ /* */ /* CLIPS Version 6.05 04/09/97 */ /* */ /* INSTANCE FUNCTIONS MODULE */ /*******************************************************//*************************************************************//* Purpose: Internal instance manipulation routines *//* *//* Principal Programmer(s): *//* Brian L. Donnell *//* *//* Contributing Programmer(s): *//* *//* *//* Revision History: *//* *//*************************************************************/ /* ========================================= ***************************************** EXTERNAL DEFINITIONS ========================================= ***************************************** */#include "setup.h"#if OBJECT_SYSTEM#include "argacces.h"#include "classcom.h"#include "classfun.h"#include "clipsmem.h"#include "cstrnchk.h"#include "inscom.h"#include "insmngr.h"#include "modulutl.h"#include "msgfun.h"#include "prccode.h"#include "router.h"#include "utility.h"#if INSTANCE_PATTERN_MATCHING#include "drive.h"#include "objrtmch.h"#endif#define _INSFUN_SOURCE_#include "insfun.h"/* ========================================= ***************************************** CONSTANTS ========================================= ***************************************** */#define BIG_PRIME 11329/* ========================================= ***************************************** MACROS AND TYPES ========================================= ***************************************** */ /* ========================================= ***************************************** INTERNALLY VISIBLE FUNCTION HEADERS ========================================= ***************************************** */#if ANSI_COMPILERstatic INSTANCE_TYPE *FindImportedInstance(struct defmodule *,struct defmodule *,INSTANCE_TYPE *);static VOID PrintInstanceName(char *,VOID *);static VOID PrintInstanceLongForm(char *,VOID *);#if INSTANCE_PATTERN_MATCHINGstatic VOID NetworkModifyForSharedSlot(int,DEFCLASS *,SLOT_DESC *);static VOID DecrementObjectBasisCount(VOID *);static VOID IncrementObjectBasisCount(VOID *);static VOID MatchObjectFunction(VOID *);static BOOLEAN NetworkSynchronized(VOID *);#endif#elsestatic INSTANCE_TYPE *FindImportedInstance();static VOID PrintInstanceName();static VOID PrintInstanceLongForm();#if INSTANCE_PATTERN_MATCHINGstatic VOID NetworkModifyForSharedSlot();static VOID DecrementObjectBasisCount();static VOID IncrementObjectBasisCount();static VOID MatchObjectFunction();static BOOLEAN NetworkSynchronized();#endif#endif/* ========================================= ***************************************** EXTERNALLY VISIBLE GLOBAL VARIABLES ========================================= ***************************************** */globle INSTANCE_TYPE **InstanceTable = NULL;globle int WithinInit = CLIPS_FALSE;globle int MaintainGarbageInstances = CLIPS_FALSE;globle int MkInsMsgPass = CLIPS_TRUE;globle int ChangesToInstances = CLIPS_FALSE;globle IGARBAGE *InstanceGarbageList = NULL;globle struct patternEntityRecord InstanceInfo = { { INSTANCE_ADDRESS,0,0,0, PrintInstanceName, PrintInstanceLongForm, UnmakeInstance, NULL, GetNextInstance, DecrementInstanceCount, IncrementInstanceCount, NULL,NULL,NULL,NULL },#if INSTANCE_PATTERN_MATCHING DecrementObjectBasisCount, IncrementObjectBasisCount, MatchObjectFunction, NetworkSynchronized#else NULL,NULL,NULL,NULL#endif };/* ========================================= ***************************************** INTERNALLY VISIBLE GLOBAL VARIABLES ========================================= ***************************************** *//* ========================================= ***************************************** EXTERNALLY VISIBLE FUNCTIONS ========================================= ***************************************** *//*************************************************** NAME : IncrementInstanceCount DESCRIPTION : Increments instance busy count - prevents it from being deleted INPUTS : The address of the instance RETURNS : Nothing useful SIDE EFFECTS : Count set NOTES : None ***************************************************/globle VOID IncrementInstanceCount(vptr) VOID *vptr; { ((INSTANCE_TYPE *) vptr)->busy++; } /*************************************************** NAME : DecrementInstanceCount DESCRIPTION : Decrements instance busy count - might allow it to be deleted INPUTS : The address of the instance RETURNS : Nothing useful SIDE EFFECTS : Count set NOTES : None ***************************************************/globle VOID DecrementInstanceCount(vptr) VOID *vptr; { ((INSTANCE_TYPE *) vptr)->busy--; } /*************************************************** NAME : InitializeInstanceTable DESCRIPTION : Initializes instance hash table to all NULL addresses INPUTS : None RETURNS : Nothing useful SIDE EFFECTS : Hash table initialized NOTES : None ***************************************************/globle VOID InitializeInstanceTable() { register int i; InstanceTable = (INSTANCE_TYPE **) gm2((int) (sizeof(INSTANCE_TYPE *) * INSTANCE_TABLE_HASH_SIZE)); for (i = 0 ; i < INSTANCE_TABLE_HASH_SIZE ; i++) InstanceTable[i] = NULL; }/******************************************************* NAME : CleanupInstances DESCRIPTION : Iterates through instance garbage list looking for nodes that have become unused - and purges them INPUTS : None RETURNS : Nothing useful SIDE EFFECTS : Non-busy instance garbage nodes deleted NOTES : None *******************************************************/globle VOID CleanupInstances() { IGARBAGE *gprv,*gtmp,*dump; if (MaintainGarbageInstances) return; gprv = NULL; gtmp = InstanceGarbageList; while (gtmp != NULL) { if ((gtmp->ins->busy == 0) && (gtmp->ins->depth > CurrentEvaluationDepth)#if INSTANCE_PATTERN_MATCHING && (gtmp->ins->header.busyCount == 0)#endif ) { EphemeralItemCount -= 2; EphemeralItemSize -= InstanceSizeHeuristic(gtmp->ins) + sizeof(IGARBAGE); DecrementSymbolCount(gtmp->ins->name); rtn_struct(instance,gtmp->ins); if (gprv == NULL) InstanceGarbageList = gtmp->nxt; else gprv->nxt = gtmp->nxt; dump = gtmp; gtmp = gtmp->nxt; rtn_struct(igarbage,dump); } else { gprv = gtmp; gtmp = gtmp->nxt; } } } /******************************************************* NAME : HashInstance DESCRIPTION : Generates a hash index for a given instance name INPUTS : The address of the instance name SYMBOL_HN RETURNS : The hash index value SIDE EFFECTS : None NOTES : Counts on the fact that the symbol has already been hashed into the CLIPS symbol table - uses that hash value multiplied by a prime for a new hash *******************************************************/globle unsigned HashInstance(cname) SYMBOL_HN *cname; { unsigned long tally; tally = ((unsigned long) cname->bucket) * BIG_PRIME; return((unsigned) (tally % INSTANCE_TABLE_HASH_SIZE)); }/*************************************************** NAME : DestroyAllInstances DESCRIPTION : Deallocates all instances, reinitializes hash table and resets class instance pointers INPUTS : None RETURNS : Nothing useful SIDE EFFECTS : All instances deallocated NOTES : None ***************************************************/globle VOID DestroyAllInstances() { INSTANCE_TYPE *iptr; int svmaintain; SaveCurrentModule(); svmaintain = MaintainGarbageInstances; MaintainGarbageInstances = CLIPS_TRUE; iptr = InstanceList; while (iptr != NULL) { SetCurrentModule((VOID *) iptr->cls->header.whichModule->theModule); DirectMessage(DELETE_SYMBOL,iptr,NULL,NULL); iptr = iptr->nxtList; while ((iptr != NULL) ? iptr->garbage : CLIPS_FALSE) iptr = iptr->nxtList; } MaintainGarbageInstances = svmaintain; RestoreCurrentModule(); }/****************************************************** NAME : RemoveInstanceData DESCRIPTION : Deallocates all the data objects in instance slots and then dealloactes the slots themeselves INPUTS : The instance RETURNS : Nothing useful SIDE EFFECTS : Instance slots removed NOTES : An instance made with CopyInstanceData will have shared values removed in all cases because they are not "real" instances. Instance class busy count decremented ******************************************************/globle VOID RemoveInstanceData(ins) INSTANCE_TYPE *ins; { register unsigned i; INSTANCE_SLOT *sp; DecrementDefclassBusyCount((VOID *) ins->cls); for (i = 0 ; i < ins->cls->instanceSlotCount ; i++) { sp = ins->slotAddresses[i]; if ((sp == &sp->desc->sharedValue) ? (--sp->desc->sharedCount == 0) : CLIPS_TRUE) { if (sp->desc->multiple) { MultifieldDeinstall((MULTIFIELD_PTR) sp->value); AddToMultifieldList((MULTIFIELD_PTR) sp->value); } else AtomDeinstall((int) sp->type,sp->value); sp->value = NULL; } } if (ins->cls->instanceSlotCount != 0) { rm((VOID *) ins->slotAddresses, (int) (ins->cls->instanceSlotCount * sizeof(INSTANCE_SLOT *))); if (ins->cls->localInstanceSlotCount != 0) rm((VOID *) ins->slots, (int) (ins->cls->localInstanceSlotCount * sizeof(INSTANCE_SLOT))); } ins->slots = NULL; ins->slotAddresses = NULL; } /*************************************************************************** NAME : FindInstanceBySymbol
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -