【问题标题】:[CLIPS][JAVA]How to acquire string from console and insert inputs[CLIPS][JAVA]如何从控制台获取字符串并插入输入
【发布时间】:2017-01-21 08:20:47
【问题描述】:

我正在使用 Clipsjni 开发一个带有 Clips 和 Java 的小型专家系统。 我遇到了一个问题,我在网上找不到解决方案,所以我问你。 我想将函数 clips.run() 的输出放在 JLable 中,因为我需要使用 java swing,并且我想从 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”。我该如何解决这个问题? 希望得到您的帮助!

【问题讨论】:

    标签: java string clips expert-system


    【解决方案1】:

    不要将用户直接看到的字符串放在规则中,而是使用事实。

    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 应用程序的事实断言到 CLIPS。例如,这是 CLIPSJNI 中包含的 WineDemo 示例中的 sn-p:

    clips.assertString("(attribute (name sauce) (value unknown))");
    

    使用事实查询函数从事实中提取信息。例如,这是 CLIPSJNI 中包含的 SudokoDemo 示例中的 sn-p:

     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 函数检索特定的槽值。

    【讨论】:

      【解决方案2】:
         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) ; 
      

      【讨论】:

      • 欢迎来到堆栈溢出!您已经回答了一个老问题,您的答案与现有答案相同。为了收集更多的赞成票,您应该清楚您的答案与现有答案的不同之处,并写一些关于它是如何工作的。请看writing good answers
      猜你喜欢
      • 1970-01-01
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多