This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am developping a prototype to help evaluate social projects within specific criteria. I would like to have here a specific simple example on how to take a value from a text field in a Jframe from java and to pass it to Jess and the opposite way. The examples i found are not complete and did not get the whole story about I/O routers .... I need help plz. Felicity
I/O routers are really not the best way to connect a GUI to Jess. They're good for sending printed output to a JTextArea, and connecting a text component to a read-eval loop like the Jess> prompt. But for general programming, it's better to use another mechanism like fetch() and store(), as documented here.
The important thing to remember when you're integrating Jess and Java is that they're not two independent things, running in parallel, waiting for messages from eachother. If main() is written in Java, then no Jess code runs unless you invoke Jess somehow; so sending data to Jess means calling store() to put the information into Jess, and then invoking some Jess code to fetch the value and do something with it. Likewise, from Jess, you use (store) to save some data, and then either invoke a Java method to deal with it, or perhaps simply return from Jess to give control back to Java.
The book "Jess in Action" (see my signature below) spends three chapters on a Swing example, so that's a good resource too.
Okay! thx a lot! But then again, if I want to put what i retrieved from Jess into a jFrame in a java based GUI, how to do it? Is it possible to have more than only one TextField to read from and more than a TextArea to write in? What i would like to do is to have severals frames with in each one several textfields who contavins future facts for my jess programme... How to design all that?
author and iconoclast
The same way as if this were any other Java Swing program: the event handlers for the various components would manipulate Jess in some way.
For example, the actionPerformed() method of a JButton's ActionListener might read the values of several JTextFields, construct an object based on those values, call Rete.definstance() to add that object to Jess's working memory, then call Rete.run() to let Jess fire some rules (although this last would best be done in a separately-spawned Thread.) The rules, in turn, could compute some value and use (store) to store the value for later retrieval. Your Java code could then use Rete.fetch() to get the stored value and store that result in a JTextArea.
This is just one scenario out of an infinite range of possibilities. If you want a detailed example with lots of annotated code and explanation, then you'll find one in my book.