Search code examples
c++clipsexpert-system

Can't assert facts in clips embedded application


I'm trying to assert a new fact in CLIPS in embedded application. I tried two ways: - The first using assert as in the example in page 74 in the advanced programming guide. - The second way is using assert-string. I tried the each way alone and also the two ways together.

I'm using RUN_TIME module. My code outputs the right constructs (defrules and deftemplates) but the new fact is not asserted. Only initial-fact is there. I don't know why!

Here is my code:

#include "clips.h"

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

    extern void *InitCImage_1();
    theEnv = InitCImage_1();

    EnvReset(theEnv);

    // One way
    templatePtr = EnvFindDeftemplate(theEnv, "Navigation");
    newFact = EnvCreateFact(theEnv, templatePtr);
    if (newFact == NULL) return -1;

    theValue.type = SYMBOL;
    theValue.value = EnvAddSymbol(theEnv, "Auto");
    EnvPutFactSlot(theEnv, newFact, "FlightStatus", &theValue);

    EnvAssert(theEnv, newFact);

    // The other way
    EnvAssertString(theEnv, "(Navigation (FlightStatus Auto))");

    EnvRun(theEnv,-1);

    EnvListDeftemplates(theEnv, "stdout", NULL);
    EnvListDefrules(theEnv, "stdout", NULL);
    EnvListDeffacts(theEnv, "stdout", NULL);
}

What is wrong in my code?


Solution

  • Use:

    EnvFacts(theEnv,"stdout",NULL,-1,-1,-1);
    

    Rather than:

    EnvListDeffacts(theEnv, "stdout", NULL);
    

    Deffacts are constructs that define a list of facts to be asserted when a (reset) command is performed. There is a pre-defined initial-facts deffacts that asserts the (initial-fact) when a reset is performed. That's what you're seeing listed when you call EnvListDeffacts. You want to call EnvFacts instead to see the facts that have actually been asserted (whether created by a deffacts after a reset or directly using assert).