Hi Sarah, Well, if anybody knows, it's going to be me! Jess is very flexible, so you can embed it in a Java program at a number of different levels. The very easiest way looks like this:
There you go. Now, anytime you want to execute Jess code (say "(assert (foo))", for example) you just say
You can execute functions and get their return values back using the Value class. For example, this prints "4":
If you want to exchange Java objects between Jess and Java code, one easy is to use the "fetch and store" method. This prints "xxxyyy":
This, on the other hand, prints "yyyxxx":
All of this (and more) is in chapter 4 of the manual. There's even more information, with many more full-size examples, in my book (see the link in my signature.) More Jess questions welcome here anytime.
Thank you so very much for your reply! Its helped me a great deal!
posted 16 years ago
Dear Mr Ernest Friedman-Hill or anyone else who can help!! Hi, I am still experiencing some problems while trying to embed Jess into Java...and was wondering if you could please please help me! I am creating a system that will diagnose Tuberculosis and am using backwards chaining. I have created a Java Swing GUI page and have (after looking at many examples and books) tried to embed my Jess code...however it is not working! I have only started using Jess about 2-3 weeks ago and so am very inexperienced with it...any advice or help will be greatly appreciated! I have attached my Java Class below and my Jess rules beneath that... Thanks in advance!
posted 16 years ago
so sorry...i forgot to mention the nature of my problem when i compile everything is ok, but when i run it nothing happens at all. I think either I am on totally the wrong track in terms of embedding the jess code, or I have missed something very important out... any help will be welcome! thanks...sarah
Well, if Jess's "batch" command encounters an error in the .clp file, it will report the problem using a JessException. You're catching JessException and calling "getMessage()", which returns a useful description of the problem -- but then you're neglecting to do anything with the String (like, for instance, printing it someplace where the user could see it!) Try replacing your "getMessage" calls with something like rete.getOutputRouter("t").println(je.getMessage()); and your exceptions would go to the TextArea where they can help you diagnose your problems. [ April 17, 2004: Message edited by: Ernest Friedman-Hill ]
Dear Mr. Ernest Friedman-Hill, Firstly many thanks for your reply..I can now see that it is vital that I add something that would display the results/questions in the TextArea. I tried replacing my getMessage calls but it keeps saying that it cannot resolve the symbol... cannot resolve symbol symbol : method println (java.lang.String) location: class java.io.Writer rete.getOutputRouter("t").println(je.getMessage()); ^ Am Sorry to hassle you once again. Sarah.
Ah, I'm sorry. The general getOutputRouter() routine returns Writer, not PrintWriter, so println() is not available; my mistake. Well, you could just use write(String), which the Writer class does have.
Hi, Thanks for your reply and help - It is much appreciated. However I have now given up on trying to embed this TB solution in Java code - as I still cant get it to work!! I think I will just go for a GUI written in Jess. Thanks once again! Sarah
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop