RE: JESS: [EXTERNAL] No cast needed when inspecting shadow facts' data members?

"Friedman-Hill, Ernest" <[email protected]> Fri, 10 May 2013 14:32:34 +0000
Newsgroups gmane.comp.java.jess
Message-ID <[email protected]>
--_000_7F2CAF6713AFDD4392FC4DAA05671E072C79AD9AEXMB10srnsandia_
Content-Type: text/plain;
 charset=us-ascii
Content-Transfer-Encoding: quoted-printable

Jess doesn't actually try to look for the "I" member until the code actuall=
y runs, so it really has no choice but to accept the code as written. This =
really isn't any different from how other dynamically typed languages behav=
e; Java, being a strongly/statically typed language that would not allow th=
is kind of code is actually unusual these days. Ruby, Python, Scala, Groovy=
, etc would all allow this sort of thing, no casting needed.

From: [email protected] [mailto:[email protected]] On B=
ehalf Of Henrique Lopes Cardoso
Sent: Friday, May 10, 2013 9:16 AM
To: jess-users
Subject: JESS: [EXTERNAL] No cast needed when inspecting shadow facts' data=
 members?

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))
    =3D>
    (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 i=
nstances of Y. Despite this, the rule is able to get ?o.i in both the LHS a=
dn 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 ge=
t a runtime exception.

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

Thank you in advance.

Henrique


--_000_7F2CAF6713AFDD4392FC4DAA05671E072C79AD9AEXMB10srnsandia_
Content-Type: text/html;
 charset=us-ascii
Content-Transfer-Encoding: quoted-printable

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr=
osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" xmlns=3D"http:=
//www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii"=
>
<meta name=3D"Generator" content=3D"Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
	{font-family:Tahoma;
	panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{mso-style-priority:99;
	color:purple;
	text-decoration:underline;}
span.EmailStyle17
	{mso-style-type:personal-reply;
	font-family:"Calibri","sans-serif";
	color:#1F497D;}
.MsoChpDefault
	{mso-style-type:export-only;
	font-family:"Calibri","sans-serif";}
@page WordSection1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
	{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=3D"EN-US" link=3D"blue" vlink=3D"purple">
<div class=3D"WordSection1">
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:&quot;Ca=
libri&quot;,&quot;sans-serif&quot;;color:#1F497D">Jess doesn&#8217;t actual=
ly try to look for the &#8220;I&#8221; member until the code actually runs,=
 so it really has no choice but to accept the code as written. This really
 isn&#8217;t any different from how other dynamically typed languages behav=
e; Java, being a strongly/statically typed language that would not allow th=
is kind of code is actually unusual these days. Ruby, Python, Scala, Groovy=
, etc would all allow this sort of thing,
 no casting needed. <o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:&quot;Ca=
libri&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p>&nbsp;</o:p></span><=
/p>
<p class=3D"MsoNormal"><b><span style=3D"font-size:10.0pt;font-family:&quot=
;Tahoma&quot;,&quot;sans-serif&quot;">From:</span></b><span style=3D"font-s=
ize:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;"> owner-je=
[email protected] [mailto:[email protected]]
<b>On Behalf Of </b>Henrique Lopes Cardoso<br>
<b>Sent:</b> Friday, May 10, 2013 9:16 AM<br>
<b>To:</b> jess-users<br>
<b>Subject:</b> JESS: [EXTERNAL] No cast needed when inspecting shadow fact=
s' data members?<o:p></o:p></span></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<div>
<p class=3D"MsoNormal">Hi,<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class=3D"MsoNormal">I've just noticed an interesting behavior of Jess.<o=
:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal">I was working with a couple of classes like this:<o:=
p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class=3D"MsoNormal">public class X {<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal">&nbsp; &nbsp; Object obj;<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal">&nbsp; &nbsp; // getter and setter for obj<o:p></o:p=
></p>
</div>
<div>
<p class=3D"MsoNormal">&nbsp; &nbsp; ...<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal">}<o:p></o:p></p>
</div>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<div>
<p class=3D"MsoNormal">public class Y {<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal">&nbsp; &nbsp; int i;<o:p></o:p></p>
</div>
<div>
<div>
<p class=3D"MsoNormal">&nbsp; &nbsp; // getter and setter for i<o:p></o:p><=
/p>
</div>
<div>
<p class=3D"MsoNormal">&nbsp; &nbsp; ...<o:p></o:p></p>
</div>
</div>
<div>
<p class=3D"MsoNormal">}<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class=3D"MsoNormal">Then in Jess I wrote:<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class=3D"MsoNormal">(deftemplate X (declare (from-class X)))<o:p></o:p><=
/p>
</div>
<div>
<div>
<p class=3D"MsoNormal">(deftemplate a (slot s))<o:p></o:p></p>
</div>
</div>
<div>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<div>
<p class=3D"MsoNormal">(defrule r<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal">&nbsp; &nbsp; (X (obj&nbsp;?o))<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal">&nbsp; &nbsp; (test (eq ((?o getClass) getSimpleName=
) &quot;Y&quot;))<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal">&nbsp; &nbsp; (a (s ?o.i))<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal">&nbsp; &nbsp; =3D&gt;<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal">&nbsp; &nbsp; (printout t ?o.i crlf))<o:p></o:p></p>
</div>
</div>
<div>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class=3D"MsoNormal">(bind ?x (new X))<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal">(bind ?y (new Y))<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal">(?y setI 123)<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal">(?x setObj ?y)<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal">(add ?x)<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal">(run)<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class=3D"MsoNormal">This actually works! My surprise is related with the=
 fact that the obj data member is declared as an Object, and the _i_ data m=
ember 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 wo=
rking memory an instance of X for which the obj is not an Y, I get a runtim=
e exception.<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class=3D"MsoNormal">Is there anything I should know about the appropriat=
eness of implementations such as this? Best practices?<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class=3D"MsoNormal">Thank you in advance.<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class=3D"MsoNormal">Henrique<o:p></o:p></p>
</div>
<div>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
</div>
</body>
</html>

--_000_7F2CAF6713AFDD4392FC4DAA05671E072C79AD9AEXMB10srnsandia_--