get FACT_ADDRESS data type when get fact list from CLIPS

自闭症网瘾萝莉.ら 提交于 2020-01-06 08:18:13

问题


I embed CLIPS into C Language. and have code:

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));
        }
    }

}

I know that GetMFType(muntifieldPtr,i)returns FACT_ADDRESS, I want to how to from the FACT_ADDRESS get the String value that represents the fact value and print the fact to the screen


回答1:


Updating to the newest clips version(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);
}

You can compile with:

make -f makefile.lib
gcc <source file name> -L./ -lclips -lm -o <executable name>



回答2:


Use the GetFactPPForm function. ValueToString should only be used in the type of the DATA_OBJECT is SYMBOL, STRING, or 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);
  }


来源:https://stackoverflow.com/questions/22835564/get-fact-address-data-type-when-get-fact-list-from-clips

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!