JESS: [EXTERNAL] No cast needed when inspecting shadow facts' data members?
"Henrique Lopes Cardoso" <[email protected]> Fri, 10 May 2013 14:15:36 +0100
| Newsgroups | gmane.comp.java.jess |
|---|---|
| Message-ID | <CALLS5+DB=nfvjCtrNRRF-MUt5TEL9T5t3hYg0=O50tQq92akvA@mail.gmail.com> |
--089e0111dd5a6d9ce404dc5cf6b1
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=iso-8859-1
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
--089e0111dd5a6d9ce404dc5cf6b1
Content-Type: text/html;
charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
<div>Hi,</div><div><br></div><div>I've just noticed an interesting beha=
vior of Jess.</div><div>I was working with a couple of classes like this:</=
div><div><br></div><div>public class X {</div><div>=A0 =A0 Object obj;</div=
>
<div>=A0 =A0 // getter and setter for obj</div><div>=A0 =A0 ...</div><div>}=
</div><br>
<div>public class Y {</div><div>=A0 =A0 int i;</div><div><div>=A0 =A0 // ge=
tter and setter for i</div><div>=A0 =A0 ...</div></div><div>}</div><div><br=
></div><div>Then in Jess I wrote:</div><div><br></div><div>(deftemplate X (=
declare (from-class X)))</div>
<div><div>(deftemplate a (slot s))</div></div><div><br></div><div><div>(def=
rule r</div><div>=A0 =A0 (X (obj=A0?o))</div><div>=A0 =A0 (test (eq ((?o ge=
tClass) getSimpleName) "Y"))</div><div>=A0 =A0 (a (s ?o.i))</div>=
<div>=A0 =A0 =3D></div>
<div>=A0 =A0 (printout t ?o.i crlf))</div></div><div><br></div><div>(bind ?=
x (new X))</div><div>(bind ?y (new Y))</div><div>(?y setI 123)</div><div>(?=
x setObj ?y)</div><div>(add ?x)</div><div>(run)</div><div><br></div><div>Th=
is actually works! My surprise is related with the fact that the obj data m=
ember is declared as an Object, and the _i_ data member only exists for ins=
tances 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 a=
dd to working memory an instance of X for which the obj is not an Y, I get =
a runtime exception.</div>
<div><br></div><div>Is there anything I should know about the appropriatene=
ss of implementations such as this? Best practices?</div><div><br></div><di=
v>Thank you in advance.</div><div><br></div><div>Henrique</div><div><br>
</div>
--089e0111dd5a6d9ce404dc5cf6b1--