CLIPS(专家系统)与C - CLIPS和MATLAB之间的通信

时间:2013-05-30 07:36:09

标签: c matlab expert-system clips

我有一个名为C的{​​{1}}函数,我想将其连接到"amortiss.c"。事实上,我想将函数CLIPS (Expert System Tool)返回的变量“result”传递给"amortiss.c",以便将此“CLIPS”与1进行比较,然后根据比较

result

根据if (result <1) then (do...); else if (result ==1) then do (...); 用户指南,我应该定义一个称为用户定义函数的外部函数。问题是这个函数是用Clips编写的CLIPS函数。所以我看不出它如何帮助我将“C”连接到amortiss.c

是否也可以将剪辑连接到CLIPS? (.clp文件和.m文件之间的通信)?

我感谢您的所有建议和意见。

1 个答案:

答案 0 :(得分:0)

您无需定义外部功能。那就是你想要CLIPS来调用C函数。

查看本文档中的“4.4.4 CreateFact”部分:

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

它展示了如何在CLIPS环境中声明新事实。上一节4.4.3给出了如何将新字符串声明为CLIPS的示例。我没有测试字符串断言,但我可以确认4.4.4示例适用于deftemplate。

例如,创建一个文本文件“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)
)

创建一个C程序“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;

}

使用以下命令运行:

foo foo.clp

这可能有点矫枉过正,但我​​认为它完成了工作!

相关问题