2017-05-29 3 views
0

私は(Student (Name x) (Age y))のようなテンプレートを持っています。この事実は、タイプStudentかないのであれば、私はチェックしたい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); 
    } 
} 

:私は、次を使用してNameという名前スロットのすべての事実を確認することでNameの値を取得することができます。はいの場合は、Nameスロットの値を取得します。 私はEnvFactDeftemplateを使うべきだと思いますが、それを動作させることはできません。ここに私のコードは

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

私は次の実行時のエラー:Segmentation fault (core dumped)を取得します。問題はどこだ?

答えて

0

EnvGetFactSlotは、deftemplateへのポインタではなく、ファクトへのポインタを期待しています。 EnvGetFactListではなくEnvGetNextFact関数のいずれかを使用して、ファクトを反復処理する方が簡単です。ここに実例があります:

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

ご協力ありがとうございました。これはあなたにとても親切です。 –

関連する問題