我正在使用Clipsjni开发一个带有Clips和Java的小型专家系统。我遇到了一个问题,我在网上找不到解决办法,所以我问你。我想将函数clips.run()的输出放在JLable中,因为我需要使用java,我希望输入来自TextBox,而不是来自控制台。
下面是一个在控制台上正常运行的程序示例:
import net.sf.clipsrules.jni.Environment;
public class Example {
public static Environment clips = new Environment();
public static void main (String[] args)
{
clips.load("hello.clp");
clips.reset();
clips.run();
}
}这是我的Hello.clp:
(defrule question
=>
(printout t "How old are you?" crlf)
(assert (age (read)))
)这就是我从系统控制台得到的信息:
你多大了? 12岁
所以我想要“你多大了?”以字符串类型保存,并将字符串中的"12“放入其中。我怎么才能解决这个问题?希望你能帮忙!
发布于 2016-09-14 17:35:18
与其将用户直接看到的字符串放置在规则中,不如使用事实。
CLIPS>
(deftemplate question
(slot id)
(slot text))
CLIPS>
(deftemplate value
(slot id)
(slot value))
CLIPS>
(defrule ask-question
(question (id ?id)
(text ?text))
=>
(printout t ?text " ")
(assert (value (id ?id) (value (read)))))
CLIPS> (assert (question (id age) (text "How old are you?")))
<Fact-1>
CLIPS> (run)
How old are you? 44
CLIPS> (facts)
f-0 (initial-fact)
f-1 (question (id age) (text "How old are you?"))
f-2 (value (id age) (value 44))
For a total of 3 facts.
CLIPS> (reset)
CLIPS> (assert (question (id age) (text "Wie alt sind Sie?")))
<Fact-1>
CLIPS> (run)
Wie alt sind Sie? 44
CLIPS> (facts)
f-0 (initial-fact)
f-1 (question (id age) (text "Wie alt sind Sie?"))
f-2 (value (id age) (value 44))
For a total of 3 facts.
CLIPS> 在CLIPSJNI中,可以使用assertString函数将事实断言到Swing应用程序的剪辑中。例如,下面是CLIPSJNI附带的WineDemo示例的一个片段:
clips.assertString("(attribute (name sauce) (value unknown))");使用事实查询函数从事实中提取信息。例如,下面是CLIPSJNI附带的SudokoDemo示例的一个片段:
String evalStr;
String messageStr = "<html><p style=\"font-size:95%\">";
evalStr = "(find-all-facts ((?f technique)) TRUE)";
MultifieldValue mv = (MultifieldValue) clips.eval(evalStr);
int tNum = mv.size();
for (int i = 1; i <= tNum; i++)
{
evalStr = "(find-fact ((?f technique-employed)) " +
"(eq ?f:priority " + i + "))";
mv = (MultifieldValue) clips.eval(evalStr);
if (mv.size() == 0) continue;
FactAddressValue fv = (FactAddressValue) mv.get(0);
messageStr = messageStr + ((NumberValue) fv.getFactSlot("priority")).intValue() + ". " +
((LexemeValue) fv.getFactSlot("reason")).lexemeValue() + "<br>";
}
JOptionPane.showMessageDialog(jfrm,messageStr,sudokuResources.getString("SolutionTechniques"),JOptionPane.PLAIN_MESSAGE);基本上,您可以使用eval函数执行查询,并在CLIPS多字段值中返回事实列表。从多字段中检索事实,然后使用getFactSlot函数检索特定的槽值。
发布于 2018-12-29 11:59:45
FactAddressValue fv = (FactAddressValue) ((MultifieldValue) clips.eval("(find-fact ((?f flower_name)) TRUE)")).get(0); // "flower_name" is deftemplate name
String ou = fv.getFactSlot("name").toString() ; // "name" is the slot name
//String ou = value.toString() ;
System.out.println(ou) ; https://stackoverflow.com/questions/39474988
复制相似问题