[英][CLIPS][JAVA]How to acquire string from console and insert inputs
I'm developing a little expert-system with Clips and Java using Clipsjni.我正在使用 Clipsjni 使用 Clips 和 Java 开发一个小专家系统。 I've encountered a problem and I can't find a solution online, so I'm asking to You.我遇到了一个问题,我在网上找不到解决方案,所以我问你。 I want to put the output of the function clips.run() in a JLable becouse I need to use java swing and I want to put the input from a TextBox and not from the console.我想将函数 clips.run() 的输出放在 JLable 中,因为我需要使用 java swing 并且我想从 TextBox 而不是来自控制台的输入。
Here's an example of a program that runs normally with the console:下面是一个与控制台一起正常运行的程序示例:
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();
}
}
And this is my Hello.clp:这是我的 Hello.clp:
(defrule question
=>
(printout t "How old are you?" crlf)
(assert (age (read)))
)
this is what i get from the system console:这是我从系统控制台得到的:
How old are you?你几岁? 12 12
So I want " How old are you? " to be saved in a String type and put the "12" from a string.所以我希望“你多大了?”以字符串类型保存并从字符串中输入“12”。 How can I solve this?我该如何解决这个问题? Hoping for your help!希望得到您的帮助!
Rather than placing strings that will be seen by the user directly within the rules, use facts instead.不要将用户直接看到的字符串放置在规则中,而是使用事实。
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>
In CLIPSJNI, you can use the assertString function to assert facts into CLIPS from your Swing application.在 CLIPSJNI 中,您可以使用 assertString 函数从 Swing 应用程序将事实断言到 CLIPS。 For example, here's a snippet from the WineDemo example included with CLIPSJNI:例如,这里是 CLIPSJNI 中包含的 WineDemo 示例中的一个片段:
clips.assertString("(attribute (name sauce) (value unknown))");
Use the fact query functions to extract information from facts.使用事实查询函数从事实中提取信息。 For example, here's a snippet from the SudokoDemo example included with CLIPSJNI:例如,这里是 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);
Basically you use the eval function to execute the query and return a list of facts in a CLIPS multifield value.基本上,您使用 eval 函数来执行查询并返回 CLIPS 多字段值中的事实列表。 You retrieve the facts from the multifield and then use the getFactSlot function to retrieve specific slot values.您从多字段中检索事实,然后使用 getFactSlot 函数检索特定的槽值。
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) ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.