0

我将 CLIPS 嵌入到 C 语言中。并有代码:

main{
    DATA_OBJECT factlist;
    int end,i;
    void *multifieldPtr;
    char *tempPtr;
    InitializeEnvironment();
    Load("constructs.clp");
    Reset();
    GetFactList(&factlist,NULL);//send facts from clips to c application.   
    end=GetDOEnd(factlist);
    printf("end=%d\n",end);
    printf("Lengthis%d\n",GetDOLength(factlist));
    multifieldPtr=GetValue(factlist);
    printf("%d\n",end);

    for (i = GetDOBegin(factlist); i <= end; i++){
        printf("i=%d\n",i);
        if (GetMFType(multifieldPtr,i) == FACT_ADDRESS){
        tempPtr = ValueToString(GetMFValue(multifieldPtr,i));//have problems here 
        printf("%d\n",*tempPtr);//have problems here
        }
        else{printf("%d\n",GetMFType(multifieldPtr,i));
        }
    }

}

我知道 GetMFType(muntifieldPtr,i) 返回 FACT_ADDRESS,我想知道如何从 FACT_ADDRESS 中获取代表事实值的字符串值并将事实打印到屏幕上

4

2 回答 2

2

更新到最新的剪辑版本(6.30)。

#include "clips.h"  
#define BUFFER_SIZE 1024

int main( int argc, char *argv[]){

DATA_OBJECT factlist;
char factBuffer[BUFFER_SIZE+1];
int end,i;
void *multifieldPtr;

void *theEnv;
theEnv = CreateEnvironment();

EnvBuild(theEnv, "(deffacts initial (colors red green blue) (animals cat dog chicken))");

EnvReset(theEnv);

EnvGetFactList(theEnv,&factlist,NULL);

if (GetType(factlist) == MULTIFIELD)
  {
   end = GetDOEnd(factlist);

   multifieldPtr = GetValue(factlist);

   for (i = GetDOBegin(factlist); i <= end; i++)
     {
      EnvGetFactPPForm(theEnv, factBuffer,BUFFER_SIZE,GetMFValue(multifieldPtr,i));
      printf("%s\n",factBuffer);
     }
  }

return(-1);
}

你可以编译:

make -f makefile.lib
gcc <source file name> -L./ -lclips -lm -o <executable name>
于 2015-04-28T18:16:42.490 回答
0

使用 GetFactPPForm 函数。ValueToString 只能用于 DATA_OBJECT 的类型为 SYMBOL、STRING 或 INSTANCE_NAME。

#define BUFFER_SIZE 1024

int main(
  int argc,
  char *argv[])
  {
    DATA_OBJECT factlist;
    char factBuffer[BUFFER_SIZE+1];
    int end,i;
    void *multifieldPtr;

    InitializeEnvironment();

    Build("(deffacts initial (colors red green blue) (animals cat dog chicken))");

    Reset();

    GetFactList(&factlist,NULL);

    if (GetType(factlist) == MULTIFIELD)
      {
       end = GetDOEnd(factlist);

       multifieldPtr = GetValue(factlist);

       for (i = GetDOBegin(factlist); i <= end; i++)
         {
          GetFactPPForm(factBuffer,BUFFER_SIZE,GetMFValue(multifieldPtr,i));
          printf("%s\n",factBuffer);
         }
      }

   return(-1);
  }
于 2014-04-06T21:38:09.533 回答