问题
I try to connect CLIPS to my C program. Set of rules will be loaded from external .clp file into CLIPS. My new C program will in fixed time intervals set new facts (example (temperature 35C)) which will represent current measurements from some temperature sensors. Then expert system will be started and make some conclusions and give necessary actions based on provided measurements. Conclusions will be in the form of facts (Answer-is x).
How I can read desired fact field from CLIPS in the form of C variable ? For example, if fact of interest is (Answer-is, x), and if I know that first field is Answer-is, how can I make my C program to find that fact and read x ?
Example code which can help you to understand what I would like to do is the following:
int main(int argc,char *argv[]) {
void *theEnv;
theEnv = CreateEnvironment();
InitializeEnvironment();
Clear();
Reset();
// Load rules to CLIPS
Load("example.clp");
// Set facts and start CLIPS
AssertString("(TemperatureSensor1 35)");
AssertString("(TemperatureSensor2 32)");
AssertString("(TemperatureSensor2 41)");
Run(-1L);
// Extract expert system answer to C
// Read second field of generated fact from the example.clp
// which is in the form like (Answer-is xxx)
// ????????????????????????????
// This part I do not know ....
// ????????????????????????????
return(-1);
}
回答1:
You can use the Eval API function to invoke the find-all-facts fact CLIPS query function. The return value from Eval will have the list of facts satisfying your query. You can then iterate over each fact (if there is more than one) and use the GetFactSlot function to retrieve the slot values of the fact.
来源:https://stackoverflow.com/questions/16265951/clips-c-code-that-read-a-value-from-the-fact-answer-is-value-if-it-is-known-fi