Mayar Rihab
Mayar Rihab

Reputation: 43

Communication between CLIPS (expert system) and C - CLIPS and MATLAB

I have a C function called "amortiss.c" and I want to connect it to CLIPS (Expert System Tool). Infact, I want to pass the variable "result" returned by the function "amortiss.c" to CLIPS so that it compares this "result" to 1 and then displays messages depending on the comparaison

if (result <1) then (do...); 
else if (result ==1) then do (...);

According to the Clips user guide I should define an external function called user-defined function. The problem is that this function is a CLIPS function written in C ..so I don't see how it helps me connect my "amortiss.c" to CLIPS.

Is it also possible to connect Clips to Matlab? (communication between .clp file and .m file)?

I appreciate all your suggestions and advice.

Upvotes: 1

Views: 1486

Answers (1)

bonafidegeek
bonafidegeek

Reputation: 106

You don't need to define an external function. That would be if you wanted CLIPS to call a C function.

Check out section "4.4.4 CreateFact" in this document:

http://clipsrules.sourceforge.net/documentation/v624/apg.htm

It shows how to assert new facts into the CLIPS environment. The previous section 4.4.3 gives an example how to assert a new string into CLIPS. I have not tested the string assert, but I can confirm the 4.4.4 example works with a deftemplate.

For example, create a text file, "foo.clp":

(deftemplate foo
 (slot x (type INTEGER) )
 (slot y (type INTEGER) )
)

(defrule IsOne
?f<-(foo (x ?xval))
(test (= ?xval 1))
=>
(printout t ?xval " is equal to 1" crlf)
)

(defrule NotOne
?f<-(foo (x ?xval))
(test (!= ?xval 1))
=>
(printout t ?xval " is not equal to 1" crlf)
)

And create a C program "foo.c"

#include <stdio.h>
#include "clips.h"

int addFact(int result)
{

  VOID *newFact;
  VOID *templatePtr;
  DATA_OBJECT theValue;

  //==================
  // Create the fact. 
  //==================
  templatePtr = FindDeftemplate("foo");
  newFact = CreateFact(templatePtr);

  if (newFact == NULL) return -1;

  //======================================
  // Set the value of the x
  //======================================
  theValue.type = INTEGER;
  theValue.value = AddLong(result);
  PutFactSlot(newFact,"x",&theValue);

  int rval;

  if (Assert(newFact) != NULL){
    Run(-1);
    rval = 0;
  }
  else{
    rval = -2;
  }

  return rval;
}


int main(int argc, char *argv[]){

  if (argc < 2) {
    printf("Usage: ");
    printf(argv[0]);
    printf(" <Clips File>\n");
    return 0;
  }
  else {

    InitializeEnvironment();
    Reset();

    char *waveRules = argv[1];     
    int wv = Load(waveRules);

    if(wv != 1){
      printf("Error opening wave rules!\n");
    }

    int result = 1;

    addFact(result);
    result = 3;
    addFact(result);
  }
  return 0;

}

Run with:

foo foo.clp

It might be overkill, but I think it gets the job done!

Upvotes: 0

Related Questions