JESS: global variable comparisons with = and <>

View: New views
5 Messages — Rating Filter:   Alert me  

Parent Message unknown JESS: global variable comparisons with = and <>

by Chirag Vesuvala :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all,

I am a beginner at JESS and have a question about the comparing a global variable I have, called ?*currentOpenToolbar*, via = and <>.  I can't figure out why the global variable does not reflect any updates I make to it.    Please have a look at the code below.  It is a simple example where I have 2 rules; 1stTime and SubsequentTime.  1stTime is activated when the global variable is set to its default value.  SubsequentTime SHOULD be activated at all other times.  However, 1stTime is always being triggered, even though a printout shows that my global variable is not set to the default value any longer.

I'm sure it is something small I am missing.  Any idea what it is?! 

Thanks.
---

(reset)
(defglobal ?*currentOpenToolbar* = MasterToolbar)

(deftemplate toDisplayToolbar
    (slot toolbar)
    (slot visible))

(defrule 1stTime
    (test (= ?*currentOpenToolbar* MasterToolbar))   ; <------------- ALWAYS TRUE.  WHY??
    (toDisplayToolbar (toolbar ?toolbar) (visible ?v&:(eq ?v TRUE)))
    =>
    (printout t "1st Time" crlf)
    (bind ?*currentOpenToolbar* ?toolbar)
    (printout t "?*currentOpenToolbar is now " ?*currentOpenToolbar* crlf))

(defrule SubsequentTime
    (test (<> ?*currentOpenToolbar* MasterToolbar)) ;<--------- NEVER TRUE.  WHY??
    (toDisplayToolbar (toolbar ?toolbar) (visible ?v&:(eq ?v TRUE)))
    =>
    (printout t "Not1stTime" crlf)
    (bind ?*currentOpenToolbar* ?toolbar)
    (printout t "?*currentOpenToolbar is now " ?*currentOpenToolbar* crlf))

(watch facts)

(assert (toDisplayToolbar
        (toolbar PenToolbar)
        (visible FALSE)))

(assert (toDisplayToolbar
        (toolbar ShapeToolbar)
        (visible FALSE)))

(assert (toDisplayToolbar
        (toolbar TextToolbar)
        (visible FALSE)))

(run)

(assert (toDisplayToolbar
        (toolbar PenToolbar)
        (visible TRUE)))
(run)

(assert (toDisplayToolbar
        (toolbar ShapeToolbar)
        (visible TRUE)))
(run)

(assert (toDisplayToolbar
        (toolbar TextToolbar)
        (visible TRUE)))
(run)

Re: JESS: global variable comparisons with = and <>

by Ana Tanasescu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hye,
 
I have read your message and I looked a little in my documentation about Jess.
In the book Jess in Action the author has written about Matching global variables the followings:
 
"If you match to a defglobal with a pattern like (score ?*x*), the match only considers the value of the defglobal when the fact is first asserted. Subsequent changes to the defglobal’s value will not invalidate the match—if the rule was activated based on the value of the defglobal, it stays activated even if the defglobal’s value changes. The match does not reflect the current value of the defglobal, but only the value at the time the matching fact was asserted."

I hope you will find a solution for your problem.
 
Ana Tanasescu


Never miss a thing. Make Yahoo your homepage.

Re: JESS: global variable comparisons with = and <>

by Ernest Friedman-Hill :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That's right. More generally, the point is that pattern matching is  
driven by assertions, retractions, and modifications to working  
memory -- changes to facts. Changing a defglobal will not cause a  
match to be reconsidered. But if there were a fact being matched  
*before* the "test" pattern, then when that fact changed, the global  
would be re-checked.


On Mar 29, 2008, at 9:10 AM, ANA TANASESCU wrote:

> Hye,
>
> I have read your message and I looked a little in my documentation  
> about Jess.
> In the book Jess in Action the author has written about Matching  
> global variables the followings:
>
> "If you match to a defglobal with a pattern like (score ?*x*), the  
> match only considers the value of the defglobal when the fact is  
> first asserted. Subsequent changes to the defglobal’s value will  
> not invalidate the match—if the rule was activated based on the  
> value of the defglobal, it stays activated even if the defglobal’s  
> value changes. The match does not reflect the current value of the  
> defglobal, but only the value at the time the matching fact was  
> asserted."
>
> I hope you will find a solution for your problem.
>
> Ana Tanasescu
>
> Never miss a thing. Make Yahoo your homepage.

---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences          Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
PO Box 969, MS 9012                 ejfried@...
Livermore, CA 94550                 http://www.jessrules.com




--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users you@...'
in the BODY of a message to majordomo@..., NOT to the list
(use your own address!) List problems? Notify owner-jess-users@....
--------------------------------------------------------------------


Re: JESS: global variable comparisons with = and <>

by Ana Tanasescu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello again,
 
I followed Ernest's advice and I have modified your program. The only thing that I have done is that I put the test CE after the pattern matching.
 
Here it is the modified program:
 
(reset)
(defglobal ?*currentOpenToolbar* = MasterToolbar)
(deftemplate toDisplayToolbar
(slot toolbar)
(slot visible))
 
(defrule 1stTime
(toDisplayToolbar (toolbar ?toolbar) (visible ?v&:(eq ?v TRUE)))
(test (= ?*currentOpenToolbar* MasterToolbar))
=>
(printout t "1st Time" crlf)
(bind ?*currentOpenToolbar* ?toolbar)
(printout t "?*currentOpenToolbar is now " ?*currentOpenToolbar* crlf))
 
(defrule SubsequentTime
(toDisplayToolbar (toolbar ?toolbar) (visible ?v&:(eq ?v TRUE)))
(test (<> ?*currentOpenToolbar* MasterToolbar))
=>
(printout t "Not1stTime" crlf)
(bind ?*currentOpenToolbar* ?toolbar)
(printout t "?*currentOpenToolbar is now " ?*currentOpenToolbar* crlf))
 
(watch all)
(assert (toDisplayToolbar
(toolbar PenToolbar)
(visible FALSE)))
(run)
(assert (toDisplayToolbar
(toolbar PenToolbar)
(visible TRUE)))
(run)
(assert (toDisplayToolbar
(toolbar Anothertoolbar)
(visible TRUE)))
(run)


Best regards,
Ana Tanasescu


OMG, Sweet deal for Yahoo! users/friends: Get A Month of Blockbuster Total Access, No Cost. W00t

Re: JESS: global variable comparisons with = and <>

by Chirag Vesuvala :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Thanks Ana and Ernest,
It works now as it should!

/Chirag

On 29/03/2008, ANA TANASESCU <atanasescuro@...> wrote:
Hello again,
 
I followed Ernest's advice and I have modified your program. The only thing that I have done is that I put the test CE after the pattern matching.
 
Here it is the modified program:
 
(
reset)
(
defglobal ?*currentOpenToolbar* = MasterToolbar)
(
deftemplate toDisplayToolbar
(
slot toolbar)
(
slot visible))
 
(
defrule 1stTime
(
toDisplayToolbar (toolbar ?toolbar) (visible ?v&:(eq ?v TRUE)))
(
test (= ?*currentOpenToolbar* MasterToolbar))
=
>
(
printout t "1st Time" crlf)
(
bind ?*currentOpenToolbar* ?toolbar)
(
printout t "?*currentOpenToolbar is now " ?*currentOpenToolbar* crlf))
 
(
defrule SubsequentTime
(
toDisplayToolbar (toolbar ?toolbar) (visible ?v&:(eq ?v TRUE)))
(
test (<> ?*currentOpenToolbar* MasterToolbar))
=>
(
printout t "Not1stTime" crlf)
(
bind ?*currentOpenToolbar* ?toolbar)
(
printout t "?*currentOpenToolbar is now " ?*currentOpenToolbar* crlf))
 
(
watch all)
(
assert (toDisplayToolbar
(
toolbar PenToolbar)
(
visible FALSE)))
(
run)
(
assert (toDisplayToolbar
(
toolbar PenToolbar)
(
visible TRUE)))
(
run)
(
assert (toDisplayToolbar
(
toolbar Anothertoolbar)
(
visible TRUE)))
(
run)


Best regards,
Ana Tanasescu


OMG, Sweet deal for Yahoo! users/friends: Get A Month of Blockbuster Total Access, No Cost. W00t