从CLIPS获取事实列表时获取FACT_ADDRESS数据类型

时间:2014-04-03 11:03:25

标签: function embed clips

我将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获取表示事实值的String值并将事实打印到屏幕

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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);
  }