⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 insfile.c

📁 NASA 开发使用的一个专家系统
💻 C
📖 第 1 页 / 共 4 页
字号:
         return(instanceCount);        }      ExpressionInstall(top);      EvaluateExpression(top,&temp);      ExpressionDeinstall(top);      if (! EvaluationError)        instanceCount++;      ReturnExpression(top->argList);      GetToken(ilog,&ObjectParseToken);     }   rtn_struct(expr,top);   if (isFileName) {     fclose(sfile);     SetFastLoad(svload);   }   MkInsMsgPass = svoverride;   return(instanceCount);  }  /***************************************************  NAME         : ProcessFileErrorMessage  DESCRIPTION  : Prints an error message when a                 file containing text or binary                 instances cannot be processed.  INPUTS       : The name of the input file and the                 function which opened it.  RETURNS      : No value  SIDE EFFECTS : None  NOTES        : None ***************************************************/static VOID ProcessFileErrorMessage(functionName,fileName)  char *functionName, *fileName;  {   PrintErrorID("INSFILE",1,CLIPS_FALSE);   PrintCLIPS(WERROR,"Function ");   PrintCLIPS(WERROR,functionName);   PrintCLIPS(WERROR," could not completely process file ");   PrintCLIPS(WERROR,fileName);   PrintCLIPS(WERROR,".\n");  }  #if BLOAD_INSTANCES/*******************************************************  NAME         : VerifyBinaryHeader  DESCRIPTION  : Reads the prefix and version headers                 from a file to verify that the                 input is a valid binary instances file  INPUTS       : The name of the file  RETURNS      : CLIPS_TRUE if OK, CLIPS_FALSE otherwise  SIDE EFFECTS : Input prefix and version read  NOTES        : Assumes file already open with GenOpen *******************************************************/static BOOLEAN VerifyBinaryHeader(theFile)  char *theFile;  {   char buf[20];      GenRead((VOID *) buf,(unsigned long) (strlen(InstanceBinaryPrefixID) + 1));   if (strcmp(buf,InstanceBinaryPrefixID) != 0)     {      PrintErrorID("INSFILE",2,CLIPS_FALSE);      PrintCLIPS(WERROR,theFile);      PrintCLIPS(WERROR," file is not a binary instances file.\n");      return(CLIPS_FALSE);     }   GenRead((VOID *) buf,(unsigned long) (strlen(InstanceBinaryVersionID) + 1));   if (strcmp(buf,InstanceBinaryVersionID) != 0)     {      PrintErrorID("INSFILE",3,CLIPS_FALSE);      PrintCLIPS(WERROR,theFile);      PrintCLIPS(WERROR," file is not a compatible binary instances file.\n");      return(CLIPS_FALSE);     }   return(CLIPS_TRUE);  }/***************************************************  NAME         : LoadSingleBinaryInstance  DESCRIPTION  : Reads the binary data for a new                 instance and its slot values and                 creates/initializes the instance  INPUTS       : None  RETURNS      : CLIPS_TRUE if all OK,                 CLIPS_FALSE otherwise  SIDE EFFECTS : Binary data read and instance                 created  NOTES        : Uses global GenRead() ***************************************************/static BOOLEAN LoadSingleBinaryInstance()  {   SYMBOL_HN *instanceName,             *className;   unsigned slotCount;   DEFCLASS *theDefclass;   INSTANCE_TYPE *newInstance;   struct bsaveSlotValue *bsArray;   struct bsaveSlotValueAtom HUGE_ADDR *bsaArray;   long nameIndex;   unsigned long totalValueCount;   register unsigned i;   unsigned long j;   INSTANCE_SLOT *sp;   DATA_OBJECT slotValue,junkValue;   /* =====================      Get the instance name      ===================== */   BufferedRead((VOID *) &nameIndex,(unsigned long) sizeof(long));   instanceName = SymbolPointer(nameIndex);      /* ==================      Get the class name      ================== */   BufferedRead((VOID *) &nameIndex,(unsigned long) sizeof(long));   className = SymbolPointer(nameIndex);      /* ==================      Get the slot count      ================== */   BufferedRead((VOID *) &slotCount,(unsigned long) sizeof(unsigned));   /* =============================      Make sure the defclass exists      and check the slot count      ============================= */   theDefclass = LookupDefclassInScope(ValueToString(className));   if (theDefclass == NULL)     {      ClassExistError("bload-instances",ValueToString(className));      return(CLIPS_FALSE);     }   if (theDefclass->instanceSlotCount != slotCount)     {      BinaryLoadInstanceError(instanceName,theDefclass);      return(CLIPS_FALSE);     }      /* ===================================      Create the new unitialized instance      =================================== */   newInstance = BuildInstance(instanceName,theDefclass,CLIPS_FALSE);   if (newInstance == NULL)     {      BinaryLoadInstanceError(instanceName,theDefclass);      return(CLIPS_FALSE);     }   if (slotCount == 0)     return(CLIPS_TRUE);        /* ====================================      Read all slot override info and slot      value atoms into big arrays      ==================================== */   bsArray = (struct bsaveSlotValue *) gm2((int) (sizeof(struct bsaveSlotValue) * slotCount));   BufferedRead((VOID *) bsArray,(unsigned long) (sizeof(struct bsaveSlotValue) * slotCount));      BufferedRead((VOID *) &totalValueCount,(unsigned long) sizeof(unsigned long));      if (totalValueCount != 0L)     {      bsaArray = (struct bsaveSlotValueAtom HUGE_ADDR *)                   gm3((long) (totalValueCount * sizeof(struct bsaveSlotValueAtom)));      BufferedRead((VOID *) bsaArray,                   (unsigned long) (totalValueCount * sizeof(struct bsaveSlotValueAtom)));     }        /* =========================      Insert the values for the      slot overrides      ========================= */               for (i = 0 , j = 0L ; i < slotCount ; i++)     {      /* ===========================================================         Here is another check for the validity of the binary file -         the order of the slots in the file should match the         order in the class definition         =========================================================== */      sp = newInstance->slotAddresses[i];      if (sp->desc->slotName->name != SymbolPointer(bsArray[i].slotName))        goto LoadError;      CreateSlotValue(&slotValue,(struct bsaveSlotValueAtom *) &bsaArray[j],                      bsArray[i].valueCount);      if (PutSlotValue(newInstance,sp,&slotValue,&junkValue,"bload-instances") == CLIPS_FALSE)        goto LoadError;      j += (unsigned long) bsArray[i].valueCount;     }        rm((VOID *) bsArray,(int) (sizeof(struct bsaveSlotValue) * slotCount));      if (totalValueCount != 0L)     rm3((VOID *) bsaArray,         (long) (totalValueCount * sizeof(struct bsaveSlotValueAtom)));      return(CLIPS_TRUE);   LoadError:   BinaryLoadInstanceError(instanceName,theDefclass);   QuashInstance(newInstance);   rm((VOID *) bsArray,(int) (sizeof(struct bsaveSlotValue) * slotCount));   rm3((VOID *) bsaArray,       (long) (totalValueCount * sizeof(struct bsaveSlotValueAtom)));   return(CLIPS_FALSE);  }/***************************************************  NAME         : BinaryLoadInstanceError  DESCRIPTION  : Prints out an error message when                 an instance could not be                 successfully loaded from a                 binary file  INPUTS       : 1) The instance name                 2) The defclass  RETURNS      : Nothing useful  SIDE EFFECTS : Error message printed  NOTES        : None ***************************************************/static VOID BinaryLoadInstanceError(instanceName,theDefclass)  SYMBOL_HN *instanceName;  DEFCLASS *theDefclass;  {   PrintErrorID("INSFILE",4,CLIPS_FALSE);   PrintCLIPS(WERROR,"Function bload-instances unable to load instance [");   PrintCLIPS(WERROR,ValueToString(instanceName));   PrintCLIPS(WERROR,"] of class ");   PrintClassName(WERROR,theDefclass,CLIPS_TRUE);  }/***************************************************  NAME         : CreateSlotValue  DESCRIPTION  : Creates a data object value from                 the binary slot value atom data  INPUTS       : 1) A data object buffer                 2) The slot value atoms array                 3) The number of values to put                    in the data object  RETURNS      : Nothing useful  SIDE EFFECTS : Data object initialized                 (if more than one value, a                 multifield is created)  NOTES        : None ***************************************************/static VOID CreateSlotValue(result,bsaValues,valueCount)  DATA_OBJECT *result;  struct bsaveSlotValueAtom *bsaValues;  long valueCount; /* 6.04 Bug Fix */  {   register unsigned i;      if (valueCount == 0)     {      result->type = MULTIFIELD;      result->value = CreateMultifield(0L);      result->begin = 0;      result->end = -1;     }   else if (valueCount == 1)     {      result->type = bsaValues[0].type;      result->value = GetBinaryAtomValue(&bsaValues[0]);     }   else     {      result->type = MULTIFIELD;      result->value = CreateMultifield(valueCount);      result->begin = 0;      result->end = valueCount - 1;      for (i = 1 ; i <= valueCount ; i++)        {         SetMFType(result->value,i,(short) bsaValues[i-1].type);         SetMFValue(result->value,i,GetBinaryAtomValue(&bsaValues[i-1]));        }     }  }/***************************************************  NAME         : GetBinaryAtomValue  DESCRIPTION  : Uses the binary index of an atom                 to find the ephemeris value  INPUTS       : The binary type and index  RETURNS      : The symbol/etc. pointer  SIDE EFFECTS : None  NOTES        : None ***************************************************/static VOID *GetBinaryAtomValue(ba)  struct bsaveSlotValueAtom *ba;  {   switch (ba->type)     {      case SYMBOL:      case STRING:      case INSTANCE_NAME:         return((VOID *) SymbolPointer(ba->value));      case FLOAT:         return((VOID *) FloatPointer(ba->value));      case INTEGER:         return((VOID *) IntegerPointer(ba->value));      case FACT_ADDRESS:#if DEFTEMPLATE_CONSTRUCT && DEFRULE_CONSTRUCT         return((VOID *) &DummyFact);#else         return(NULL);#endif      case EXTERNAL_ADDRESS:        return(NULL);      default:        {         CLIPSSystemError("INSFILE",1);         ExitCLIPS(2);        }     }   return(NULL);  }/***************************************************  NAME         : BufferedRead  DESCRIPTION  : Reads data from binary file                 (Larger blocks than requested size                  may be read and buffered)  INPUTS       : 1) The buffer                 2) The buffer size  RETURNS      : Nothing useful  SIDE EFFECTS : Data stored in buffer  NOTES        : None ***************************************************/static VOID BufferedRead(buf,bufsz)  VOID *buf;  unsigned long bufsz;  {   static unsigned long CurrentReadBufferOffset = 0L;   unsigned long i,amountLeftToRead;   if (CurrentReadBuffer != NULL)     {      amountLeftToRead = CurrentReadBufferSize - CurrentReadBufferOffset;      if (bufsz <= amountLeftToRead)        {         for (i = 0L ; i < bufsz ; i++)           ((char HUGE_ADDR *) buf)[i] = CurrentReadBuffer[i + CurrentReadBufferOffset];         CurrentReadBufferOffset += bufsz;         if (CurrentReadBufferOffset == CurrentReadBufferSize)           FreeReadBuffer();        }      else        {         if (CurrentReadBufferOffset < CurrentReadBufferSize)           {            for (i = 0L ; i < amountLeftToRead ; i++)              ((char HUGE_ADDR *) buf)[i] = CurrentReadBuffer[i + CurrentReadBufferOffset];            bufsz -= amountLeftToRead;            buf = (VOID *) (((char HUGE_ADDR *) buf) + amountLeftToRead);           }         FreeReadBuffer();         BufferedRead(buf,bufsz);        }     }   else     {      if (bufsz > MAX_BLOCK_SIZE)        {         CurrentReadBufferSize = bufsz;         if (bufsz > (BinaryInstanceFileSize - BinaryInstanceFileOffset))           {            CLIPSSystemError("INSFILE",2);            ExitCLIPS(2);           }        }      else if (MAX_BLOCK_SIZE >               (BinaryInstanceFileSize - BinaryInstanceFileOffset))        CurrentReadBufferSize = BinaryInstanceFileSize - BinaryInstanceFileOffset;      else        CurrentReadBufferSize = (unsigned long) MAX_BLOCK_SIZE;      CurrentReadBuffer = (char HUGE_ADDR *) genlongalloc(CurrentReadBufferSize);      GenRead((VOID *) CurrentReadBuffer,CurrentReadBufferSize);      for (i = 0L ; i < bufsz ; i++)        ((char HUGE_ADDR *) buf)[i] = CurrentReadBuffer[i];      CurrentReadBufferOffset = bufsz;      BinaryInstanceFileOffset += CurrentReadBufferSize;     }  }  /*****************************************************  NAME         : FreeReadBuffer  DESCRIPTION  : Deallocates buffer for binary reads  INPUTS       : None  RETURNS      : Nothing usefu  SIDE EFFECTS : Binary global read buffer deallocated  NOTES        : None *****************************************************/static VOID FreeReadBuffer()  {   if (CurrentReadBufferSize != 0L)     {      genlongfree((VOID *) CurrentReadBuffer,CurrentReadBufferSize);      CurrentReadBuffer = NULL;      CurrentReadBufferSize = 0L;     }  }  #endif    #endif /***************************************************  NAME         :   DESCRIPTION  :   INPUTS       :   RETURNS      :   SIDE EFFECTS :   NOTES        :  ***************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -