Hi Mr Ernest,
Thank you very much for your help. I edited the codes as you instructed. I
bolded it in the code below.
To fetch a resulting fact from Jess, I store the fact in "FOOTWEAR" in the
RHS of the shoes.clp file. I refered to the "Jess in action" book at
241-243, section 15.2.2 on how to get it out.
Sorry to trouble you more, but I don't understand how to get the fact out.
Does the fact becomes a java string after I use
Value recommend_shoes = engine.fetch("FOOTWEAR");
String temp = recommend_shoes.stringValue();
Can jess separate a slot's ident from a slot's value into a array of string
or something similar?
updated
http://www.geocities.com/t_web_design/TestFrame.java
http://www.geocities.com/t_web_design/shoes.txt (yahoo won't let me upload
it as shoes.clp)
Thank you very very much for your help
rick
************************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.text.*;
import jess.*;
public class TestFrame extends JFrame implements ActionListener{
private JTextField inputField, outputField;
private JLabel inputLabel, outputLabel;;
private JButton exitButton, computeButton;
private Rete engine;
//frame method
public TestFrame(){
//frame's settings
setBounds(50,50,650,200); //set window size
//PANEL:input, to put the input & output fields
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
inputLabel = new JLabel("String to pass to Jess: ");
inputField = new JTextField(40);
outputLabel = new JLabel("Jess to return something: ");
outputField = new JTextField(40);
//add the Label & Field into the panel
inputPanel.add(inputLabel);
inputPanel.add(inputField);
inputPanel.add(outputLabel);
inputPanel.add(outputField);
inputField.setText("(assert (user (age 27) (weight 120) (height 509)
(ex-program soccer) )) ");
//PANEL:button, store 3 buttons, contentPane=SOUTH
JPanel buttonPanel = new JPanel();
//create Panel for 'button'
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
//set flow layout
computeButton = new JButton("Sent to Jess");
//create 'compute' button
exitButton = new JButton("Exit");
//create 'exit' button
buttonPanel.add(computeButton);
//add 'compute' button into Panel
buttonPanel.add(exitButton);
//add 'exit' button into Panel
computeButton.addActionListener(this);
//add listener to 'compute'
button
exitButton.addActionListener(this);
//add listener to 'exit' button
Container contentPane = getContentPane();
contentPane.add(buttonPanel, BorderLayout.SOUTH); //add
buttonPanel to
SOUTH
contentPane.add(inputPanel, BorderLayout.CENTER); //add
inputPanel to
CENTER
} //close GymTrainerFrame()
//detect mouse click
public void actionPerformed(ActionEvent e){
Object source = e.getSource(); //get source of click
if (source == exitButton) //check if exit button
is clicked
{ System.exit(0); } //exit prog
if (source == computeButton) //if compute button is clicked
{
String assert_fact = inputField.getText();
System.out.println("The string to send to jess is :" +
assert_fact);
try
{
engine = new Rete();
engine.reset();
engine.batch("shoes.clp");
engine.reset();
engine.assertString(assert_fact);
engine.run();
Value recommend_shoes = engine.fetch("FOOTWEAR");
String temp = recommend_shoes.stringValue();
System.out.println ( "Shoes recommended is: " + temp);
}
catch (JessException ex)
{
System.err.println("ERROR");
}
}
} //close actionPerformed()
//MAIN: make the frame show up
public static void main(String[] args)
{
JFrame frame = new TestFrame(); //create the frame
frame.show(); //show
the frame
} //close main
}
******************************************************************************
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;user object
(deftemplate user
(slot age)
(slot weight)
(slot height)
(slot ex-program) );;close
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;user footwear object
(deftemplate user-footwear
(slot ident) );;close
;;;;;;;;;;;;;;;;;;;;;;;;;;;; check if user is playing soccer, if so
recommend soccer boots
(defrule print-user (user (ex-program soccer))
=> (assert (user-footwear (ident soccer-boots))) );;close
(defrule user-shoes
(user-footwear (ident soccer-boots))
=>
(store FOOTWEAR (user-footwear (ident soccer-boots))) );;close
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;code to create user fact with user
playing soccer
;;;(assert (user (age 27) (weight 120) (height 509) (ex-program soccer) ))
***********************************************************************************
Ernest Friedman-Hill wrote:
>
> Jess is very flexible, perhaps too much so, because there are always
> lots of ways to do things.
>
> In the program you've written here, first you want to instantiate a
> jess.Rete object and store it in a member variable. When you
> instantiate it, use either Rete.batch() (if your version has this
> method) or just Rete.executeCommand("(batch filename.clp)") to read
> in your Jess code.
>
> Then in your event handler, assert the fact by calling the
> appropriate method on your Rete member object. There are many ways to
> do it: you could use executeCommand() again, or assertString(), or
> even build a jess.Fact object and call assertFact(). Then your
> handler can call Rete.run(). You might want it to call Rete.reset()
> before asserting the fact.
>
> Then when your rule fires, you want it to communicate back to your
> Java program. One easy way to do this would be with the store/fetch
> mechanism, which is described in the manual and also in the book on
> pages 308-309. Have the right-hand-side of the relevant rule store
> the result. The rule could also make changes directly to the GUI if
> you like, or do many other things: as I said, there are lots of
> different ways to do it.
>
>
> On Mar 17, 2007, at 11:02 PM, ricktee wrote:
>
>>
>> Hi,
>>
>> I am using a java frame as a menu to let user enter some inputs.
>> The java
>> program will then construct a assert string and send it to JESS to be
>> processed and get back a JESS object.
>>
>> I read the "JESS in action" book but I'm still confused, how do I
>> run the
>> JESS and pass the assert string to it. How can I get the output of
>> the JESS
>> program?
>>
>> I have attached a scaled down version of my program, at "send to jess"
>> button click it will send a string to JESS, and JESS is suppose to
>> return a
>> user-shoes fact to java, recommending soccer-boots.
>>
>> http://www.geocities.com/t_web_design/TestFrame.java
>> http://www.geocities.com/t_web_design/shoes.txt (yahoo won't let me
>> upload
>> it as shoes.clp)
>>
>> I'm justing java 1.509 (and IDE JCreator) and Jess61p4. Can you
>> please point
>> me to a similar example or please help me with the codes?
>>
>> thanks a bunch and rgds
>> rick
>>
>> Below is the code of the above 2 files
>>
>> **********************************************************************
>> ******
>>
>> import java.awt.*;
>> import java.awt.event.*;
>> import javax.swing.*;
>> import javax.swing.border.*;
>> import java.text.*;
>>
>> public class TestFrame extends JFrame implements ActionListener{
>>
>> //declare JTextFields for input
>> private JTextField inputField, outputField;
>>
>> //declare JLabel for labelling
>> private JLabel inputLabel, outputLabel;;
>>
>> //declare buttons
>> private JButton exitButton, computeButton;
>>
>> //frame method
>> public TestFrame(){
>>
>> //frame's settings
>> setTitle("version 1.0"); //set title of frame
>> setBounds(50,50,650,200); //set window
>>
>>
>> //PANEL:input, to put the input & output fields
>> JPanel inputPanel = new JPanel();
>> inputPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
>>
>> inputLabel = new JLabel("String to pass to Jess: ");
>> inputField = new JTextField(40);
>>
>> outputLabel = new JLabel("Jess to return something: ");
>> outputField = new JTextField(40);
>>
>> //add the Label & Field into the panel
>> inputPanel.add(inputLabel);
>> inputPanel.add(inputField);
>> inputPanel.add(outputLabel);
>> inputPanel.add(outputField);
>>
>> inputField.setText("(assert (user (age 27) (weight 120) (height 509)
>> (ex-program soccer) )) ");
>>
>> //PANEL:button, store 3 buttons, contentPane=SOUTH
>> JPanel buttonPanel = new JPanel();
>> //create Panel for 'button'
>> buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); //
>> set flow
>> layout
>>
>> computeButton = new JButton("Sent to Jess"); //create 'compute'
>> button
>> exitButton = new JButton("Exit"); //
>> create 'exit'
>> button
>>
>> buttonPanel.add(computeButton); //add 'compute' button
>> into
>> Panel
>> buttonPanel.add(exitButton); //add 'exit'
>> button into
>> Panel
>>
>> computeButton.addActionListener(this); //add listener to
>> 'compute'
>> button
>> exitButton.addActionListener(this); //add listener to
>> 'exit'
>> button
>>
>> Container contentPane = getContentPane();
>> contentPane.add(buttonPanel, BorderLayout.SOUTH); //add
>> buttonPanel to
>> SOUTH
>> contentPane.add(inputPanel, BorderLayout.CENTER); //add
>> inputPanel to
>> CENTER
>> } //close GymTrainerFrame()
>>
>> //detect mouse click
>> public void actionPerformed(ActionEvent e){
>> Object source = e.getSource();
>> //get source of click
>>
>> if (source == exitButton)
>> //check if exit button is clicked
>> { System.exit(0); }
>> //exit prog
>>
>> if (source == computeButton) //if clear button is clicked
>> { //send to JESS
>> }
>>
>> } //close actionPerformed()
>>
>> //MAIN: make the frame show up
>> public static void main(String[] args)
>> {
>> JFrame frame = new TestFrame(); //create the frame
>> frame.show(); //show the frame
>> } //close main
>>
>> }
>>
>>
>> **********************************************************************
>> ****
>>
>> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;user object
>> (deftemplate user
>> (slot age)
>> (slot weight)
>> (slot height)
>> (slot ex-program) );;close
>>
>> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;user footwear object
>> (deftemplate user-footwear
>> (slot ident) );;close
>>
>> ;;;;;;;;;;;;;;;;;;;;;;;;;;;; check if user is playing soccer, if so
>> recommend soccer boots
>> (defrule print-user (user (ex-program soccer))
>> => (assert (user-footwear (ident soccer-boots))) );;close
>>
>> (reset)
>> (focus)
>> (run)
>>
>> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;code to create user fact with user
>> playing soccer
>> ;;;(assert (user (age 27) (weight 120) (height 509) (ex-program
>> soccer) ))
>>
>>
>> --
>> View this message in context: http://www.nabble.com/How-to-use-java-
>> interface-to-pass--get-facts-from-JESS-tf3421248.html#a9535635
>> Sent from the Jess mailing list archive at Nabble.com.
>>
>> --------------------------------------------------------------------
>> To unsubscribe, send the words 'unsubscribe jess-users
>> [EMAIL PROTECTED]'
>> in the BODY of a message to [EMAIL PROTECTED], NOT to the list
>> (use your own address!) List problems? Notify owner-jess-
>> [EMAIL PROTECTED]
>> --------------------------------------------------------------------
>
> ---------------------------------------------------------
> Ernest Friedman-Hill
> Advanced Software Research Phone: (925) 294-2154
> Sandia National Labs FAX: (925) 294-2234
> PO Box 969, MS 9012 [EMAIL PROTECTED]
> Livermore, CA 94550 http://www.jessrules.com
>
> --------------------------------------------------------------------
> To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
> in the BODY of a message to [EMAIL PROTECTED], NOT to the list
> (use your own address!) List problems? Notify [EMAIL PROTECTED]
> --------------------------------------------------------------------
>
>
>
--
View this message in context:
http://www.nabble.com/How-to-use-java-interface-to-pass--get-facts-from-JESS-tf3421248.html#a9545750
Sent from the Jess mailing list archive at Nabble.com.
--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------