Ejemplo n.º 1
0
static int EvaluateSalience(
    void *theEnv,
    void *vPtr)
{
    struct defrule *rPtr = (struct defrule *) vPtr;
    DATA_OBJECT salienceValue;
    int salience;

    /*==================================================*/
    /* If saliences are only being evaluated when rules */
    /* are defined, then just return the last salience  */
    /* value evaluated for the rule.                    */
    /*==================================================*/

    if (EnvGetSalienceEvaluation(theEnv) == WHEN_DEFINED)
    {
        return(rPtr->salience);
    }

    /*=================================================================*/
    /* If the rule's salience value was defined as an integer constant */
    /* (i.e., not an expression or global variable which could change  */
    /* on reevaluation), then just return the salience value computed  */
    /* for the rule when it was defined.                               */
    /*=================================================================*/

    if (rPtr->dynamicSalience == NULL) return(rPtr->salience);

    /*====================================================*/
    /* Reevaluate the rule's salience. If an error occurs */
    /* during evaluation, print an error message.         */
    /*====================================================*/

    SetEvaluationError(theEnv,FALSE);
    if (EvaluateExpression(theEnv,rPtr->dynamicSalience,&salienceValue))
    {
        SalienceInformationError(theEnv,(char*)"defrule",ValueToString(rPtr->header.name));
        return(rPtr->salience);
    }

    /*========================================*/
    /* The salience value must be an integer. */
    /*========================================*/

    if (salienceValue.type != INTEGER)
    {
        SalienceNonIntegerError(theEnv);
        SalienceInformationError(theEnv,(char*)"defrule",ValueToString(rPtr->header.name));
        SetEvaluationError(theEnv,TRUE);
        return(rPtr->salience);
    }

    /*==========================================*/
    /* The salience value must fall between the */
    /* minimum and maximum allowed values.      */
    /*==========================================*/

    salience = (int) ValueToLong(salienceValue.value);

    if ((salience > MAX_DEFRULE_SALIENCE) || (salience < MIN_DEFRULE_SALIENCE))
    {
        SalienceRangeError(theEnv,MIN_DEFRULE_SALIENCE,MAX_DEFRULE_SALIENCE);
        SetEvaluationError(theEnv,TRUE);
        SalienceInformationError(theEnv,(char*)"defrule",ValueToString(((struct defrule *) rPtr)->header.name));
        return(rPtr->salience);
    }

    /*===================================*/
    /* Store the new salience value with */
    /* the rule and return this value.   */
    /*===================================*/

    rPtr->salience = salience;
    return(rPtr->salience);
}
Ejemplo n.º 2
0
static void ParseSalience(
  void *theEnv,
  char *readSource,
  char *ruleName,
  int *error)
  {
   int salience;
   DATA_OBJECT salienceValue;

   /*==============================*/
   /* Get the salience expression. */
   /*==============================*/

   SavePPBuffer(theEnv," ");

   PatternData(theEnv)->SalienceExpression = ParseAtomOrExpression(theEnv,readSource,NULL);
   if (PatternData(theEnv)->SalienceExpression == NULL)
     {
      *error = TRUE;
      return;
     }

   /*============================================================*/
   /* Evaluate the expression and determine if it is an integer. */
   /*============================================================*/

   SetEvaluationError(theEnv,FALSE);
   if (EvaluateExpression(theEnv,PatternData(theEnv)->SalienceExpression,&salienceValue))
     {
      SalienceInformationError(theEnv,"defrule",ruleName);
      *error = TRUE;
      return;
     }

   if (salienceValue.type != INTEGER)
     {
      SalienceNonIntegerError(theEnv);
      *error = TRUE;
      return;
     }

   /*=======================================================*/
   /* Salience number must be in the range -10000 to 10000. */
   /*=======================================================*/

   salience = (int) ValueToLong(salienceValue.value);

   if ((salience > MAX_DEFRULE_SALIENCE) || (salience < MIN_DEFRULE_SALIENCE))
     {
      SalienceRangeError(theEnv,MIN_DEFRULE_SALIENCE,MAX_DEFRULE_SALIENCE);
      *error = TRUE;
      return;
     }

   /*==========================================*/
   /* If the expression is a constant integer, */
   /* don't bother storing the expression.     */
   /*==========================================*/

   if (PatternData(theEnv)->SalienceExpression->type == INTEGER)
     {
      ReturnExpression(theEnv,PatternData(theEnv)->SalienceExpression);
      PatternData(theEnv)->SalienceExpression = NULL;
     }

   PatternData(theEnv)->GlobalSalience = salience;
  }