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> </o:p></p><p clas=
s=3DMsoNormal>(defrule Test<o:p></o:p></p><p class=3DMsoNormal> =
&nb=
sp; ?r <- (logical (CurrentTime))<o:p></o:p></p><p cl=
ass=3DMsoNormal>  =
; (logical (Condition (tim=
e ?time&:(> (+ ?time 5) ?r.t))))<o:p></o:p></p><p class=3DMsoNormal>=
&nb=
sp; =3D><o:p></o:p></p><p class=3DMsoNormal> &nbs=
p; =
(assert (Condition-met))<o:p></o:p></p><p class=3DMsoNor=
mal>)<o:p></o:p></p><p class=3DMsoNormal><o:p> </o:p></p><p class=3DMs=
oNormal>Where “CurrentTime” is a fact that holds the current ti=
me, and I change the slot value “t” as time progresses. The rul=
e fires if the “time” 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 “?r.t”) 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 “*time*” and have that change value as time progre=
sses. But there is no way to make the global a “logical” depend=
ency of the asserted condition-met so it doesn’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> </o:p></p><p class=3DMsoNormal=
>I came up with a workaround where in addition to the defglobal “*tim=
e*” I assert a Fact called CurrentTime that holds the same value in t=
he “t” slot. Now my rule is:<o:p></o:p></p><p class=3DMsoNormal=
><o:p> </o:p></p><p class=3DMsoNormal>(defrule Test<o:p></o:p></p><p c=
lass=3DMsoNormal> &nbs=
p; ?r <- (logical (Curr=
entTime (t ?t&:(=3D ?t ?*time*)))) <o:p></o:p></p><p class=3DMsoNormal>=
&nb=
sp; (logical (Condition (time ?time&=
;:(> (+ ?time 5) ?*time*)))) ; 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>  =
; =3D><o:p></o:p></p><p class=3DMsoNormal> =
&nb=
sp; (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> </o:p></p><p class=3DMsoNormal>This says that if there is a =
CurrentTime fact whose slot “t” has the same value as the globa=
l “*time*” and there is a Condition whose “time” sl=
ot plus 5 is greater than that time, then assert the Condition-met. My java=
code sets the global to “4”, asserts the fact “(CurrentT=
ime (t 4))”, and then asserts the fact “(Condition (time 0))=
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>”. All is well=
though a bit clumsy. Now I want to move time forward where the “Cond=
ition-met” should no longer be true. I change the global to 10 and mo=
dify the CurrentTime fact and set the “t” slot to 10 too. <o:p>=
</o:p></p><p class=3DMsoNormal><o:p> </o:p></p><p class=3DMsoNormal>Th=
en I run the rules engine and it retracts the “<span style=3D'font-si=
ze:10.0pt;font-family:"Courier New";color:black'>(MAIN::Condition-met (glob=
time 4) (facttime 4))</span>” but then mysteriously asserts “<s=
pan style=3D'font-size:10.0pt;font-family:"Courier New";color:black'>(MAIN:=
:Condition-met (globtime 10) (facttime 10))</span>”. 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> </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> </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> </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> </o:p></p><p class=3DMsoNormal>Dwight<o:p></o:p></p=
></div></body></html>=
--_000_B96DD689550AE14286D5886F15063C5001E0F7037F99MAILSERVER2_--