📄 cstrnchk.c
字号:
/*******************************************************/ /* "C" Language Integrated Production System */ /* */ /* CLIPS Version 6.05 04/09/97 */ /* */ /* CONSTRAINT CHECKING MODULE */ /*******************************************************//*************************************************************//* Purpose: Provides functions for constraint checking of *//* data types. *//* *//* Principal Programmer(s): *//* Gary D. Riley *//* *//* Contributing Programmer(s): *//* Brian Donnell *//* *//* Revision History: *//* *//*************************************************************/#define _CSTRNCHK_SOURCE_#include <stdio.h>#define _CLIPS_STDIO_#if ANSI_COMPILER#include <stdlib.h>#endif#include "setup.h"#include "router.h"#include "multifld.h"#include "extnfunc.h"#include "cstrnutl.h"#include "cstrnchk.h"/***************************************//* LOCAL INTERNAL FUNCTION DEFINITIONS *//***************************************/#if ANSI_COMPILER static BOOLEAN CheckRangeAgainstCardinalityConstraint(int,int,CONSTRAINT_RECORD *); static int CheckFunctionReturnType(int,CONSTRAINT_RECORD *); static BOOLEAN CheckTypeConstraint(int,CONSTRAINT_RECORD *); static BOOLEAN CheckRangeConstraint(int,VOID *,CONSTRAINT_RECORD *); static VOID PrintRange(char *,CONSTRAINT_RECORD *);#else static BOOLEAN CheckRangeAgainstCardinalityConstraint(); static int CheckFunctionReturnType(); static BOOLEAN CheckTypeConstraint(); static BOOLEAN CheckRangeConstraint(); static VOID PrintRange();#endif/******************************************************//* CheckFunctionReturnType: Checks a functions return *//* type against a set of permissable return values. *//* Returns TRUE if the return type is included *//* among the permissible values, otherwise FALSE. *//******************************************************/static int CheckFunctionReturnType(functionReturnType,constraints) int functionReturnType; CONSTRAINT_RECORD *constraints; { if (constraints == NULL) return(CLIPS_TRUE); if (constraints->anyAllowed) return(CLIPS_TRUE); switch(functionReturnType) { case 'c': case 'w': case 'b': if (constraints->symbolsAllowed) return(CLIPS_TRUE); else return(CLIPS_FALSE); case 's': if (constraints->stringsAllowed) return(CLIPS_TRUE); else return(CLIPS_FALSE); case 'j': if ((constraints->symbolsAllowed) || (constraints->stringsAllowed) || (constraints->instanceNamesAllowed)) return(CLIPS_TRUE); else return(CLIPS_FALSE); case 'k': if ((constraints->symbolsAllowed) || (constraints->stringsAllowed)) return(CLIPS_TRUE); else return(CLIPS_FALSE); case 'd': case 'f': if (constraints->floatsAllowed) return(CLIPS_TRUE); else return(CLIPS_FALSE); case 'i': case 'l': if (constraints->integersAllowed) return(CLIPS_TRUE); else return(CLIPS_FALSE); case 'n': if ((constraints->integersAllowed) || (constraints->floatsAllowed)) return(CLIPS_TRUE); else return(CLIPS_FALSE); case 'm': if (constraints->multifieldsAllowed) return(CLIPS_TRUE); else return(CLIPS_FALSE); case 'a': if (constraints->externalAddressesAllowed) return(CLIPS_TRUE); else return(CLIPS_FALSE); case 'x': if (constraints->instanceAddressesAllowed) return(CLIPS_TRUE); else return(CLIPS_FALSE); case 'o': if (constraints->instanceNamesAllowed) return(CLIPS_TRUE); else return(CLIPS_FALSE); case 'u': return(CLIPS_TRUE); case 'v': return(CLIPS_FALSE); } return(CLIPS_TRUE); } /****************************************************//* CheckTypeConstraint: Determines if a primitive *//* data type satisfies the type constraint fields *//* of aconstraint record. *//****************************************************/static BOOLEAN CheckTypeConstraint(type,constraints) int type; CONSTRAINT_RECORD *constraints; { if (type == RVOID) return(CLIPS_FALSE); if (constraints == NULL) return(CLIPS_TRUE); if (constraints->anyAllowed == CLIPS_TRUE) return(CLIPS_TRUE); if ((type == SYMBOL) && (constraints->symbolsAllowed != CLIPS_TRUE)) { return(CLIPS_FALSE); } if ((type == STRING) && (constraints->stringsAllowed != CLIPS_TRUE)) { return(CLIPS_FALSE); } if ((type == FLOAT) && (constraints->floatsAllowed != CLIPS_TRUE)) { return(CLIPS_FALSE); } if ((type == INTEGER) && (constraints->integersAllowed != CLIPS_TRUE)) { return(CLIPS_FALSE); } #if OBJECT_SYSTEM if ((type == INSTANCE_NAME) && (constraints->instanceNamesAllowed != CLIPS_TRUE)) { return(CLIPS_FALSE); } if ((type == INSTANCE_ADDRESS) && (constraints->instanceAddressesAllowed != CLIPS_TRUE)) { return(CLIPS_FALSE); }#endif if ((type == EXTERNAL_ADDRESS) && (constraints->externalAddressesAllowed != CLIPS_TRUE)) { return(CLIPS_FALSE); } if ((type == FACT_ADDRESS) && (constraints->factAddressesAllowed != CLIPS_TRUE)) { return(CLIPS_FALSE); } return(CLIPS_TRUE); } /********************************************************//* CheckCardinalityConstraint: Determines if an integer *//* falls within the range of allowed cardinalities *//* for a constraint record. *//********************************************************/globle BOOLEAN CheckCardinalityConstraint(number,constraints) long number; CONSTRAINT_RECORD *constraints; { /*=========================================*/ /* If the constraint record is NULL, there */ /* are no cardinality restrictions. */ /*=========================================*/ if (constraints == NULL) return(CLIPS_TRUE); /*==================================*/ /* Determine if the integer is less */ /* than the minimum cardinality. */ /*==================================*/ if (constraints->minFields != NULL) { if (constraints->minFields->value != NegativeInfinity) { if (number < ValueToLong(constraints->minFields->value)) { return(CLIPS_FALSE); } } } /*=====================================*/ /* Determine if the integer is greater */ /* than the maximum cardinality. */ /*=====================================*/ if (constraints->maxFields != NULL) { if (constraints->maxFields->value != PositiveInfinity) { if (number > ValueToLong(constraints->maxFields->value)) { return(CLIPS_FALSE); } } } /*=========================================================*/ /* The integer falls within the allowed cardinality range. */ /*=========================================================*/ return(CLIPS_TRUE); } /*****************************************************************//* CheckRangeAgainstCardinalityConstraint: Determines if a range *//* of numbers could possibly fall within the range of allowed *//* cardinalities for a constraint record. Returns TRUE if at *//* least one of the numbers in the range is within the allowed *//* cardinality, otherwise FALSE is returned. *//*****************************************************************/static BOOLEAN CheckRangeAgainstCardinalityConstraint(min,max,constraints) int min,max; CONSTRAINT_RECORD *constraints; { /*=========================================*/ /* If the constraint record is NULL, there */ /* are no cardinality restrictions. */ /*=========================================*/ if (constraints == NULL) return(CLIPS_TRUE); /*===============================================================*/ /* If the minimum value of the range is greater than the maximum */ /* value of the cardinality, then there are no numbers in the */ /* range which could fall within the cardinality range, and so */ /* FALSE is returned. */ /*===============================================================*/ if (constraints->maxFields != NULL) { if (constraints->maxFields->value != PositiveInfinity) { if (min > ValueToLong(constraints->maxFields->value)) { return(CLIPS_FALSE); } } } /*===============================================================*/ /* If the maximum value of the range is less than the minimum */ /* value of the cardinality, then there are no numbers in the */ /* range which could fall within the cardinality range, and so */ /* FALSE is returned. A maximum range value of -1 indicates that */ /* the maximum possible value of the range is positive infinity. */ /*===============================================================*/ if ((constraints->minFields != NULL) && (max != -1)) { if (constraints->minFields->value != NegativeInfinity) { if (max < ValueToLong(constraints->minFields->value)) { return(CLIPS_FALSE); } } } /*=============================================*/ /* At least one number in the specified range */ /* falls within the allowed cardinality range. */ /*=============================================*/ return(CLIPS_TRUE); }/**********************************************************************//* CheckAllowedValuesConstraint: Determines if a primitive data type *//* satisfies the allowed-... constraint fields of a constraint *//* record. Returns TRUE if the constraints are satisfied, otherwise *//* FALSE is returned. *//**********************************************************************/globle BOOLEAN CheckAllowedValuesConstraint(type,vPtr,constraints) int type; VOID *vPtr; CONSTRAINT_RECORD *constraints; { struct expr *tmpPtr; /*=========================================*/ /* If the constraint record is NULL, there */ /* are no allowed-... restrictions. */ /*=========================================*/ if (constraints == NULL) return(CLIPS_TRUE); /*=====================================================*/ /* Determine if there are any allowed-... restrictions */ /* for the type of the value being checked. */ /*=====================================================*/ switch (type) { case SYMBOL: if ((constraints->symbolRestriction == CLIPS_FALSE) && (constraints->anyRestriction == CLIPS_FALSE)) { return(CLIPS_TRUE); } break; #if OBJECT_SYSTEM case INSTANCE_NAME: if ((constraints->instanceNameRestriction == CLIPS_FALSE) && (constraints->anyRestriction == CLIPS_FALSE)) { return(CLIPS_TRUE); } break;#endif case STRING: if ((constraints->stringRestriction == CLIPS_FALSE) && (constraints->anyRestriction == CLIPS_FALSE)) { return(CLIPS_TRUE); } break; case INTEGER: if ((constraints->integerRestriction == CLIPS_FALSE) && (constraints->anyRestriction == CLIPS_FALSE)) { return(CLIPS_TRUE); } break; case FLOAT: if ((constraints->floatRestriction == CLIPS_FALSE) && (constraints->anyRestriction == CLIPS_FALSE)) { return(CLIPS_TRUE); } break; default: return(CLIPS_TRUE); } /*=========================================================*/ /* Search through the restriction list to see if the value */ /* matches one of the allowed values in the list. */ /*=========================================================*/ for (tmpPtr = constraints->restrictionList; tmpPtr != NULL; tmpPtr = tmpPtr->nextArg) { if ((tmpPtr->type == type) && (tmpPtr->value == vPtr)) return(CLIPS_TRUE); } /*====================================================*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -