📄 objrtmch.c
字号:
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.24 05/17/06 */
/* */
/* OBJECT PATTERN MATCHER MODULE */
/*******************************************************/
/**************************************************************/
/* Purpose: RETE Network Interface for Objects */
/* */
/* Principal Programmer(s): */
/* Brian L. Donnell */
/* */
/* Contributing Programmer(s): */
/* */
/* Revision History: */
/* 6.23: Correction for FalseSymbol/TrueSymbol. DR0859 */
/* */
/* 6.24: Removed INCREMENTAL_RESET and */
/* LOGICAL_DEPENDENCIES compilation flags. */
/* */
/* Converted INSTANCE_PATTERN_MATCHING to */
/* DEFRULE_CONSTRUCT. */
/* */
/* Renamed BOOLEAN macro type to intBool. */
/* */
/**************************************************************/
/* =========================================
*****************************************
EXTERNAL DEFINITIONS
=========================================
***************************************** */
#include "setup.h"
#if DEFRULE_CONSTRUCT && OBJECT_SYSTEM
#include "classfun.h"
#include "memalloc.h"
#include "drive.h"
#include "engine.h"
#include "envrnmnt.h"
#include "lgcldpnd.h"
#include "multifld.h"
#if (! RUN_TIME) && (! BLOAD_ONLY)
#include "incrrset.h"
#endif
#include "reteutil.h"
#include "ruledlt.h"
#include "reorder.h"
#include "retract.h"
#include "router.h"
#include "objrtfnx.h"
#define _OBJRTMCH_SOURCE_
#include "objrtmch.h"
#include "insmngr.h"
/* =========================================
*****************************************
INTERNALLY VISIBLE FUNCTION HEADERS
=========================================
***************************************** */
static void QueueObjectMatchAction(void *,int,INSTANCE_TYPE *,int);
static SLOT_BITMAP *QueueModifySlotMap(void *,SLOT_BITMAP *,int);
static void ReturnObjectMatchAction(void *,OBJECT_MATCH_ACTION *);
static void ProcessObjectMatchQueue(void *);
static void MarkObjectPatternNetwork(void *,SLOT_BITMAP *);
static intBool CompareSlotBitMaps(SLOT_BITMAP *,SLOT_BITMAP *);
static void ObjectPatternMatch(void *,int,OBJECT_PATTERN_NODE *,struct multifieldMarker *);
static void ProcessPatternNode(void *,int,OBJECT_PATTERN_NODE *,struct multifieldMarker *);
static void CreateObjectAlphaMatch(void *,OBJECT_ALPHA_NODE *);
static intBool EvaluateObjectPatternTest(void *,int,struct multifieldMarker *,EXPRESSION *,
OBJECT_PATTERN_NODE *);
static void ObjectAssertAction(void *,INSTANCE_TYPE *);
static void ObjectModifyAction(void *,INSTANCE_TYPE *,SLOT_BITMAP *);
static void ObjectRetractAction(void *,INSTANCE_TYPE *,SLOT_BITMAP *);
static void ObjectPatternNetErrorMessage(void *,OBJECT_PATTERN_NODE *);
static void TraceErrorToObjectPattern(void *,int,OBJECT_PATTERN_NODE *);
/* =========================================
*****************************************
EXTERNALLY VISIBLE FUNCTIONS
=========================================
***************************************** */
/***************************************************************************
NAME : ObjectMatchDelay
DESCRIPTION : H/L interface for SetDelayObjectPatternMatching
INPUTS : None
RETURNS : Nothing useful
SIDE EFFECTS : DelayObjectPatternMatching set and Rete network updates
delayed until pattern-matching is completed
NOTES : H/L Syntax: (object-pattern-match-delay <action>*)
***************************************************************************/
globle void ObjectMatchDelay(
void *theEnv,
DATA_OBJECT *result)
{
register int ov;
ov = SetDelayObjectPatternMatching(theEnv,TRUE);
EvaluateExpression(theEnv,GetFirstArgument(),result);
if (EvaluationData(theEnv)->EvaluationError)
{
SetHaltExecution(theEnv,FALSE);
SetEvaluationError(theEnv,FALSE);
SetDelayObjectPatternMatching(theEnv,ov);
SetEvaluationError(theEnv,TRUE);
}
else
SetDelayObjectPatternMatching(theEnv,ov);
}
/***************************************************
NAME : SetDelayObjectPatternMatching
DESCRIPTION : Sets the flag determining if Rete
network activity is to be delayed
for objects or not
INPUTS : The value of the flag
RETURNS : The old value of the flag
SIDE EFFECTS : DelayObjectPatternMatching set
NOTES : When the delay is set to FALSE,
all pending Rete network updates
are performed
***************************************************/
globle intBool SetDelayObjectPatternMatching(
void *theEnv,
int value)
{
intBool oldval;
oldval = ObjectReteData(theEnv)->DelayObjectPatternMatching;
if (value)
ObjectReteData(theEnv)->DelayObjectPatternMatching = TRUE;
else
{
ObjectReteData(theEnv)->DelayObjectPatternMatching = FALSE;
ObjectNetworkAction(theEnv,0,NULL,-1);
}
return(oldval);
}
/***************************************************
NAME : GetDelayObjectPatternMatching
DESCRIPTION : Gets the flag determining if Rete
network activity is to be delayed
for objects or not
INPUTS : None
RETURNS : The flag
SIDE EFFECTS : None
NOTES : None
***************************************************/
globle intBool GetDelayObjectPatternMatching(
void *theEnv)
{
return(ObjectReteData(theEnv)->DelayObjectPatternMatching);
}
/********************************************************
NAME : ObjectNetworkPointer
DESCRIPTION : Returns the first object network
pattern node
INPUTS : None
RETURNS : The top of the object pattern network
SIDE EFFECTS : None
NOTES : None
********************************************************/
globle OBJECT_PATTERN_NODE *ObjectNetworkPointer(
void *theEnv)
{
return(ObjectReteData(theEnv)->ObjectPatternNetworkPointer);
}
/********************************************************
NAME : ObjectNetworkTerminalPointer
DESCRIPTION : Returns the first terminal pattern node
INPUTS : None
RETURNS : The last node of a pattern
SIDE EFFECTS : None
NOTES : None
********************************************************/
globle OBJECT_ALPHA_NODE *ObjectNetworkTerminalPointer(
void *theEnv)
{
return(ObjectReteData(theEnv)->ObjectPatternNetworkTerminalPointer);
}
/***************************************************
NAME : SetObjectNetworkPointer
DESCRIPTION : Sets the object pattern network to
the given network
INPUTS : Top of the new pattern network
RETURNS : Nothing useful
SIDE EFFECTS : ObjectPatternNetworkPointer set
NOTES : None
***************************************************/
globle void SetObjectNetworkPointer(
void *theEnv,
OBJECT_PATTERN_NODE *value)
{
ObjectReteData(theEnv)->ObjectPatternNetworkPointer = value;
}
/*******************************************************
NAME : SetObjectNetworkTerminalPointer
DESCRIPTION : Sets the global list of terminal
pattern nodes (the ones containing
the bitmaps) to the given node
INPUTS : The last node of a pattern
RETURNS : Nothing useful
SIDE EFFECTS : ObjectPatternNetworkTerminalPointer set
NOTES : None
*******************************************************/
globle void SetObjectNetworkTerminalPointer(
void *theEnv,
OBJECT_ALPHA_NODE *value)
{
ObjectReteData(theEnv)->ObjectPatternNetworkTerminalPointer = value;
}
/************************************************************************
NAME : ObjectNetworkAction
DESCRIPTION : Main driver for pattern-matching on objects
If the pattern-matching is current delayed or another
object is currently being pattern-matched, the requested
match action is queued for later processing.
Otherwise, the match action is performed and the
Rete network is updated.
INPUTS : 1) The match action type
OBJECT_ASSERT (1)
OBJECT_RETRACT (2)
OBJECT_MODIFY (3)
2) The instance to be matched (can be NULL if only
want pending actions to be performed)
3) The name id of the slot being updated (can be -1)
If this argument is -1, it is assumed that any
pattern which could match this instance must be
checked. Otherwise, only the patterns which
explicitly match on the named slot will be checked.
RETURNS : Nothing useful
SIDE EFFECTS : Action queued or Rete network updated
NOTES : None
************************************************************************/
globle void ObjectNetworkAction(
void *theEnv,
int type,
INSTANCE_TYPE *ins,
int slotNameID)
{
SLOT_BITMAP *tmpMap;
if (EngineData(theEnv)->JoinOperationInProgress)
return;
EngineData(theEnv)->JoinOperationInProgress = TRUE;
/* ================================================
For purposes of conflict resolution, all objects
which have had pattern-matching delayed will
have the same relative timestamp, i.e., the
inference engine thinks they all just appeared
simultaneously
When delay is off, however, each object gets the
new and current timestamp as expected.
================================================ */
ObjectReteData(theEnv)->UseEntityTimeTag = DefruleData(theEnv)->CurrentEntityTimeTag++;
/* ==================================================
If pattern-matching is delayed (by use of the
set-object-pattern-match-delay function), then
the instance should be marked for later processing
(when the delay is turned off).
================================================== */
if (ins != NULL)
{
/* 6.05 Bug Fix */
ins->reteSynchronized = FALSE;
if (ObjectReteData(theEnv)->DelayObjectPatternMatching == FALSE)
switch (type)
{
case OBJECT_ASSERT :
ObjectAssertAction(theEnv,ins);
break;
case OBJECT_RETRACT :
ObjectRetractAction(theEnv,ins,NULL);
break;
default :
tmpMap = QueueModifySlotMap(theEnv,NULL,slotNameID);
ObjectModifyAction(theEnv,ins,tmpMap);
rm(theEnv,(void *) tmpMap,SlotBitMapSize(tmpMap));
}
else
QueueObjectMatchAction(theEnv,type,ins,slotNameID);
}
/* ========================================
Process all pending actions in the queue
All updates will use the same timestamp
======================================== */
ProcessObjectMatchQueue(theEnv);
EngineData(theEnv)->JoinOperationInProgress = FALSE;
ForceLogicalRetractions(theEnv);
/*=========================================*/
/* Free partial matches that were released */
/* by the assertion of the fact. */
/*=========================================*/
if (EngineData(theEnv)->ExecutingRule == NULL) FlushGarbagePartialMatches(theEnv);
}
/* =========================================
*****************************************
INTERNALLY VISIBLE FUNCTIONS
=========================================
***************************************** */
/***************************************************
NAME : ResetObjectMatchTimeTags
DESCRIPTION : If CurrentObjectMatchTimeTag + 1
would cause an overflow,
CurrentObjectMatchTimeTag
is reset to 0L and all time tags
in object pattern nodes are reset.
INPUTS : None
RETURNS : Nothing useful
SIDE EFFECTS : CurrentObjectMatchTimeTag reset to
0, and all match time tags reset
These tags are used to recognize
valid pattern nodes on a match
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -