Since control structures are functions in Jess, the ability for a function to peek from one stack frame to another is important. I could have implemented this using some kind of special form or keyword -- like the Tcl "uplevel" function -- but in the first go-round, I think I just took the easy way out, and this behavior has stuck ever since. Note that it's read-only by default: binding a value to a variable in a deffunction creates a new variable in the local scope. That's why recursive functions are possible.
________________________________ From: owner-jess-us...@sandia.gov [mailto:owner-jess-us...@sandia.gov] On Behalf Of David Scuse Sent: Wednesday, November 09, 2011 11:43 AM To: jess-users Subject: JESS: Scoping Question Hi Ernest: I came across a strange "feature" of Jess as a result of a typing error. It appears as though Jess uses dynamic scoping -- any variables defined in a rule are visible in any functions called by the RHS of the rule. I looked for references to this in the book and the manual but couldn't find anything. Is this the expected behaviour? (This is just a curiosity question.) Thanks David (deftemplate person "" (slot name) (slot age)) (deffacts initialFacts (person (name bill) (age 20)) (person (name will) (age 25)) (person (name dill) (age 25)) (person (name jill) (age 22)) ) (defrule findOldest ?f <- (person (age ?max)) (not (person (age ?less&:(> ?less ?max)))) => (my-function) ) (deffunction my-function () (printout t crlf "Processing fact " (?f toString) crlf) (printout t crlf "Max age is " ?max crlf) (printout t crlf "Max age is " ?f.age crlf crlf) ) (reset) (run) (facts)