JESS: [EXTERNAL] Jess asserts fact that is not true

"Dwight Hare" <[email protected]> Tue, 26 Jun 2012 14:48:42 -0700
Newsgroups gmane.comp.java.jess
Message-ID <B96DD689550AE14286D5886F15063C5001E0F7037F99@MAILSERVER-2.office.paritycomputing.com>
--_000_B96DD689550AE14286D5886F15063C5001E0F7037F99MAILSERVER2_
Content-Type: text/plain;
 charset=us-ascii
Content-Transfer-Encoding: quoted-printable

I am trying to reason about the passage of time in my rules. I would like t=
o write the following rule:

(defrule Test
                    ?r <- (logical (CurrentTime))
                    (logical (Condition (time ?time&:(> (+ ?time 5) ?r.t)))=
)
                =3D>
                    (assert (Condition-met))
)

Where "CurrentTime" is a fact that holds the current time, and I change the=
 slot value "t" as time progresses. The rule fires if the "time" slot in th=
e Condition plus 5 is greater than the current time (that is, is true withi=
n the last 5 time increments). But this syntax is not allowed because you c=
annot access a dotted variable (the "?r.t") here.

My first alternative was to declare a defglobal called "*time*" and have th=
at change value as time progresses. But there is no way to make the global =
a "logical" dependency of the asserted condition-met so it doesn't get retr=
acted when the global changes in value sufficient to make the rule no longe=
r be true.

I came up with a workaround where in addition to the defglobal "*time*" I a=
ssert a Fact called CurrentTime that holds the same value in the "t" slot. =
Now my rule is:

(defrule Test
                    ?r <- (logical (CurrentTime (t ?t&:(=3D ?t ?*time*))))
                    (logical (Condition (time ?time&:(> (+ ?time 5) ?*time*=
))))  ; Note I use the global here instead of the dotted var but the value =
is the same
                =3D>
                    (assert (Condition-met (globtime ?*time*)(facttime ?r.t=
)))
)

This says that if there is a CurrentTime fact whose slot "t" has the same v=
alue as the global "*time*" and there is a Condition whose "time" slot plus=
 5 is greater than that time, then assert the Condition-met. My java code s=
ets the global to "4", asserts the fact "(CurrentTime (t 4))", and then ass=
erts the fact "(Condition (time 0))". Since all the conditions are met the =
rules engine asserts the fact "(MAIN::Condition-met (globtime 4) (facttime =
4))". All is well though a bit clumsy. Now I want to move time forward wher=
e the "Condition-met" should no longer be true. I change the global to 10 a=
nd modify the CurrentTime fact and set the "t" slot to 10 too.

Then I run the rules engine and it retracts the "(MAIN::Condition-met (glob=
time 4) (facttime 4))" but then mysteriously asserts "(MAIN::Condition-met =
(globtime 10) (facttime 10))". Since the Condition time of 0 plus 5 is not =
greater than 10, it should not have asserted this. Here is a dump of the fa=
cts in the rules engine at the end:

0: (MAIN::CurrentTime (t 10))
1: (MAIN::Condition (time 0))
3: (MAIN::Condition-met (globtime 10) (facttime 10))

I see no justification for the 3rd fact given the only rule I have.

Is there a better way of accomplishing this?

Dwight

--_000_B96DD689550AE14286D5886F15063C5001E0F7037F99MAILSERVER2_
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=3DContent-Type content=
=3D"text/html; charset=3Dus-ascii"><meta name=3DGenerator content=3D"Micros=
oft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	font-size:11.0pt;
	font-family:"Calibri","sans-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-compose;
	font-family:"Calibri","sans-serif";
	color:windowtext;}
.MsoChpDefault
	{mso-style-type:export-only;}
@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=3DEN-US link=3Dblue vli=
nk=3Dpurple><div class=3DWordSection1><p class=3DMsoNormal>I am trying to r=
eason about the passage of time in my rules. I would like to write the foll=
owing rule:<o:p></o:p></p><p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p clas=
s=3DMsoNormal>(defrule Test<o:p></o:p></p><p class=3DMsoNormal>&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; &nbsp;&nbsp;&nbsp; ?r &lt;- (logical (CurrentTime))<o:p></o:p></p><p cl=
ass=3DMsoNormal>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (logical (Condition (tim=
e ?time&amp;:(&gt; (+ ?time 5) ?r.t))))<o:p></o:p></p><p class=3DMsoNormal>=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp; =3D&gt;<o:p></o:p></p><p class=3DMsoNormal>&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp; (assert (Condition-met))<o:p></o:p></p><p class=3DMsoNor=
mal>)<o:p></o:p></p><p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMs=
oNormal>Where &#8220;CurrentTime&#8221; is a fact that holds the current ti=
me, and I change the slot value &#8220;t&#8221; as time progresses. The rul=
e fires if the &#8220;time&#8221; slot in the Condition plus 5 is greater t=
han the current time (that is, is true within the last 5 time increments). =
But this syntax is not allowed because you cannot access a dotted variable =
(the &#8220;?r.t&#8221;) here.<o:p></o:p></p><p class=3DMsoNormal><o:p>&nbs=
p;</o:p></p><p class=3DMsoNormal>My first alternative was to declare a defg=
lobal called &#8220;*time*&#8221; and have that change value as time progre=
sses. But there is no way to make the global a &#8220;logical&#8221; depend=
ency of the asserted condition-met so it doesn&#8217;t get retracted when t=
he global changes in value sufficient to make the rule no longer be true.<o=
:p></o:p></p><p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal=
>I came up with a workaround where in addition to the defglobal &#8220;*tim=
e*&#8221; I assert a Fact called CurrentTime that holds the same value in t=
he &#8220;t&#8221; slot. Now my rule is:<o:p></o:p></p><p class=3DMsoNormal=
><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>(defrule Test<o:p></o:p></p><p c=
lass=3DMsoNormal>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ?r &lt;- (logical (Curr=
entTime (t ?t&amp;:(=3D ?t ?*time*)))) <o:p></o:p></p><p class=3DMsoNormal>=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;(logical (Condition (time ?time&amp=
;:(&gt; (+ ?time 5) ?*time*))))&nbsp; ; Note I use the global here instead =
of the dotted var but the value is the same<o:p></o:p></p><p class=3DMsoNor=
mal>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp; =3D&gt;<o:p></o:p></p><p class=3DMsoNormal>&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; &nbsp;&nbsp;&nbsp; (assert (Condition-met (globtime ?*time*)(facttime ?=
r.t)))<o:p></o:p></p><p class=3DMsoNormal>)<o:p></o:p></p><p class=3DMsoNor=
mal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>This says that if there is a =
CurrentTime fact whose slot &#8220;t&#8221; has the same value as the globa=
l &#8220;*time*&#8221; and there is a Condition whose &#8220;time&#8221; sl=
ot plus 5 is greater than that time, then assert the Condition-met. My java=
 code sets the global to &#8220;4&#8221;, asserts the fact &#8220;(CurrentT=
ime (t 4))&#8221;, and then asserts the fact &#8220;(Condition (time 0))&#8=
221;. Since all the conditions are met the rules engine asserts the fact &#=
8220;<span style=3D'font-size:10.0pt;font-family:"Courier New";color:black'=
>(MAIN::Condition-met (globtime 4) (facttime 4))</span>&#8221;. All is well=
 though a bit clumsy. Now I want to move time forward where the &#8220;Cond=
ition-met&#8221; should no longer be true. I change the global to 10 and mo=
dify the CurrentTime fact and set the &#8220;t&#8221; slot to 10 too. <o:p>=
</o:p></p><p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>Th=
en I run the rules engine and it retracts the &#8220;<span style=3D'font-si=
ze:10.0pt;font-family:"Courier New";color:black'>(MAIN::Condition-met (glob=
time 4) (facttime 4))</span>&#8221; but then mysteriously asserts &#8220;<s=
pan style=3D'font-size:10.0pt;font-family:"Courier New";color:black'>(MAIN:=
:Condition-met (globtime 10) (facttime 10))</span>&#8221;. Since the Condit=
ion time of 0 plus 5 is not greater than 10, it should not have asserted th=
is. Here is a dump of the facts in the rules engine at the end:<o:p></o:p><=
/p><p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal style=3D'=
text-autospace:none'><span style=3D'font-size:10.0pt;font-family:"Courier N=
ew";color:black'>0: (MAIN::CurrentTime (t 10))</span><span style=3D'font-si=
ze:10.0pt;font-family:"Courier New"'><o:p></o:p></span></p><p class=3DMsoNo=
rmal style=3D'text-autospace:none'><span style=3D'font-size:10.0pt;font-fam=
ily:"Courier New";color:black'>1: (MAIN::Condition (time 0))</span><span st=
yle=3D'font-size:10.0pt;font-family:"Courier New"'><o:p></o:p></span></p><p=
 class=3DMsoNormal><span style=3D'font-size:10.0pt;font-family:"Courier New=
";color:black'>3: (MAIN::Condition-met (globtime 10) (facttime 10))<o:p></o=
:p></span></p><p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNorma=
l>I see no justification for the 3<sup>rd</sup> fact given the only rule I =
have.<o:p></o:p></p><p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMs=
oNormal>Is there a better way of accomplishing this?<o:p></o:p></p><p class=
=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>Dwight<o:p></o:p></p=
></div></body></html>=

--_000_B96DD689550AE14286D5886F15063C5001E0F7037F99MAILSERVER2_--