Hi,

I've just noticed an interesting behavior of Jess.
I was working with a couple of classes like this:

public class X {
    Object obj;
    // getter and setter for obj
    ...
}

public class Y {
    int i;
    // getter and setter for i
    ...
}

Then in Jess I wrote:

(deftemplate X (declare (from-class X)))
(deftemplate a (slot s))

(defrule r
    (X (obj ?o))
    (test (eq ((?o getClass) getSimpleName) "Y"))
    (a (s ?o.i))
    =>
    (printout t ?o.i crlf))

(bind ?x (new X))
(bind ?y (new Y))
(?y setI 123)
(?x setObj ?y)
(add ?x)
(run)

This actually works! My surprise is related with the fact that the obj data
member is declared as an Object, and the _i_ data member only exists for
instances of Y. Despite this, the rule is able to get ?o.i in both the LHS
adn the RHS (no cast needed). Of course if I remove the test in the rule
and add to working memory an instance of X for which the obj is not an Y, I
get a runtime exception.

Is there anything I should know about the appropriateness of
implementations such as this? Best practices?

Thank you in advance.

Henrique

Reply via email to