Search code examples
javastringclipsexpert-system

[CLIPS][JAVA]How to acquire string from console and insert inputs


I'm developing a little expert-system with Clips and Java using Clipsjni. 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.

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:

(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

So I want " How old are you? " to be saved in a String type and put the "12" from a string. How can I solve this? Hoping for your help!


Solution

  • 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. For example, here's a snippet from the WineDemo example included with CLIPSJNI:

    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:

     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. You retrieve the facts from the multifield and then use the getFactSlot function to retrieve specific slot values.