2017-05-29 90 views
0

我有像(Student (Name x) (Age y))這樣的模板。我可以檢查一個使用以下命名Name插槽的所有事實得到Name值:CLIPS - 從事實列表中獲取特定模板的事實

EnvGetFactList(theEnv, &factlist, NULL); 
if (GetType(factlist) == MULTIFIELD) 
{ 
    end = GetDOEnd(factlist); 
    multifieldPtr = GetValue(factlist); 
    for (i = GetDOBegin(factlist); i <= end; i++) 
    { 
     EnvGetFactSlot(theEnv,GetMFValue(multifieldPtr, i),"Name",&theValue); 
     buf = DOToString(theValue); 
     printf("%s\n", buf); 
    } 
} 

我想檢查,如果這一事實是類型Student與否。如果是,則獲得Name插槽的值。 我認爲我應該使用EnvFactDeftemplate但我無法使它工作。這裏是我的代碼

templatePtr = EnvFindDeftemplate(theEnv, "Student"); 
templatePtr = EnvFactDeftemplate(theEnv,templatePtr); 
EnvGetFactSlot(theEnv,&templatePtr,"Name",&theValue); 

但我得到以下運行時錯誤:Segmentation fault (core dumped)。哪裏有問題?

回答

0

EnvGetFactSlot期待指向事實的指針,而不是指向deftemplate的指針。使用EnvGetNextFact函數之一而不是EnvGetFactList來遍歷事實也更容易。下面是一個工作示例:

int main() 
    { 
    void *theEnv; 
    void *theFact; 
    void *templatePtr; 
    DATA_OBJECT theValue; 

    theEnv = CreateEnvironment(); 

    EnvBuild(theEnv,"(deftemplate Student (slot Name))"); 
    EnvBuild(theEnv,"(deftemplate Teacher (slot Name))"); 

    EnvAssertString(theEnv,"(Student (Name \"John Brown\"))"); 
    EnvAssertString(theEnv,"(Teacher (Name \"Susan Smith\"))"); 
    EnvAssertString(theEnv,"(Student (Name \"Sally Green\"))"); 
    EnvAssertString(theEnv,"(Teacher (Name \"Jack Jones\"))"); 

    templatePtr = EnvFindDeftemplate(theEnv,"Student"); 

    for (theFact = EnvGetNextFact(theEnv,NULL); 
     theFact != NULL; 
     theFact = EnvGetNextFact(theEnv,theFact)) 
    { 
     if (EnvFactDeftemplate(theEnv,theFact) != templatePtr) continue; 

     EnvGetFactSlot(theEnv,theFact,"Name",&theValue); 
     EnvPrintRouter(theEnv,STDOUT,DOToString(theValue)); 
     EnvPrintRouter(theEnv,STDOUT,"\n"); 
    } 

    EnvPrintRouter(theEnv,STDOUT,"-------------\n"); 

    for (theFact = EnvGetNextFactInTemplate(theEnv,templatePtr,NULL); 
     theFact != NULL; 
     theFact = EnvGetNextFactInTemplate(theEnv,templatePtr,theFact)) 
    { 
     EnvGetFactSlot(theEnv,theFact,"Name",&theValue); 
     EnvPrintRouter(theEnv,STDOUT,DOToString(theValue)); 
     EnvPrintRouter(theEnv,STDOUT,"\n"); 
    } 
    } 
+0

非常感謝Gary對您的一貫支持。你真好。 –