0

I am trying to get the printout contents from a Jess RHS of a rule. A similar question is described here: Output of JESS in Java but there is not a concrete solution how to use a router for the printout command. Instead of printing the rule's printout contents in Java console I want to print them in a dedicated JTextArea. I declared a string e.g. String result; to hold the contents and then print out the string contents into JTextArea through outputTxt.setText(result);

Community
  • 1
  • 1
Edi
  • 109
  • 11
  • I think what you are after is a `ByteArrayOutputStream` to redirect the standard output (`System.setOut(new PrintWriter(baos))`). – Gábor Bakos Apr 25 '15 at 23:24
  • I am newbie in java. Can you explain it more precissly – Edi Apr 25 '15 at 23:33
  • The solution depends on the way you run Jess in combination with your Java application where you (probably) create the GUI with that JTextArea component. a) Pass this object to into Jess, perhaps as a variable in the global Context, b) in Jess, call the JTextArea method setText in the usual way. - Without any code posted by you it's too much work to create a demo. – laune Apr 26 '15 at 04:38

1 Answers1

1

The Jess manual discusses exactly this case, explicitly; see http://www.jessrules.com/jess/docs/71/library.html#routers and http://www.jessrules.com/jess/docs/71/library.html#reader . It really couldn't be easier:

 // Create a text area; you'll need to add it to your GUI, of course
 TextArea ta = new TextArea(20, 80);
 // This is a sort of adapter that lets Jess print into a textarea.
 // There's also a JTextAreaWriter for Swing GUIs
 TextAreaWriter taw = new TextAreaWriter(ta);
 // Create a rule engine instance
 Rete engine = new Rete();
 // Connect the "t" router to the TextArea. From this point on, 
 // Jess code that executes "(printout t ..." will send its output
 // to the TextArea
 engine.addOutputRouter("t", taw);
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186