我有一个名为"amortiss.c"的C函数,我想将它连接到CLIPS (Expert System Tool)。实际上,我希望将函数"amortiss.c"返回的变量"result“传递给CLIPS,以便它将此"result”与1进行比较,然后根据比较结果显示消息
if (result <1) then (do...);
else if (result ==1) then do (...);根据Clips用户指南,我应该定义一个称为用户定义函数的外部函数。问题是这个函数是用C ..so编写的剪辑函数,我不明白它如何帮助我将我的"amortiss.c“连接到CLIPS。
是否也可以将剪辑连接到Matlab?( .clp文件和.m文件之间的通信)?
我感谢你所有的建议和建议。
发布于 2013-06-08 05:52:05
您不需要定义外部函数。这是如果你想让剪辑调用C函数的话。
查看本文档中的"4.4.4 CreateFact“一节:
http://clipsrules.sourceforge.net/documentation/v624/apg.htm
它展示了如何在CLIPS环境中断言新的事实。上一节4.4.3给出了一个如何在剪辑中断言新字符串的示例。我还没有测试string assert,但我可以确认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这可能有点夸张,但我认为它能完成工作!
https://stackoverflow.com/questions/16830467
复制相似问题