On Feb 16, 2007, at 1:29 PM, B. Tenbergen wrote:


I have a simple Java class called "Course" with three private final int attributes and the corresponding get-methods. I instantiated three objects from this class and want to add these Objects as Facts to the working memory of Jess so that I can access the attributes (and mathods) of the objects as slots of the facts. Using r.defclass(Course, Course, null) -(r is my Rete Object)- resulted in an error - the class Course wasn't found.

Jess doesn't do anything especially magical here. The class has to be available on the class path when the defclass is executed, and the class has to be accessible (i.e., public).


Instead, I used something like this:
           Fact f1 = new Fact("course", r);
           f1.setSlotValue("crn", new Value(new Integer(1)));
           f1.setSlotValue("starttime", new Value(new Integer(1110)));
           f1.setSlotValue("endtime", new Value(new Integer(1230)));
           r.assertFact(f1);
(crn, starttime and endtime or the private final int attributes)

This is not at all the same thing, of course. It can be done in a more streamlined way by using "eval" or "assertString":

r.assertString("(course (crn 1) (starttime 1110) (endtime 1230))");

Also, do be careful using these Integer objects; if you mean to treat them as numbers, then use numbers (int values.)


Also, I wonder how I can define a rule from Java in Jess that allows me to check whether or not two facts have the same content in a slot and, iff true, do something. r.eval("(defrule r1 (forall (course (starttime ?time))) => (printout t \"Warining! Time collision!\" crlf))"); Did not work, as it terminated with an ArrayIndexOutOfBounceException at Index 4.


Don't know exactly what you're trying to accomplish using "forall" here. "forall" always includes at least two patterns, and it says "every time the first pattern matches, all the subsequent ones also match". If you just want to check if two facts match, then you just need a rule with two patterns, like

 (defrule r1
     ?c1 <- (course (starttime ?time))
     ?c2 <- (course (starttime ?time))
     (test (neq ?c1 ?c2))
     =>

Using the variable "time" twice "unifies" the starttime slots in the two facts, matching only if they're equal; and then we have to explicitly state that the patterns must match two separate facts.
---------------------------------------------------------
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]
--------------------------------------------------------------------

Reply via email to