The rule works, which shows that Jess can assign the ?val variable, but the query does not. In the second branch of the "OR", the ?foreign variable is tested for no match, not the ?val variable. My intent with the second branch was to assign a default value to ?val in the second conditional element of the "AND" if nothing matches the ?foreign value in the pair template. This looks like a defquery defect rather than an issue with data-flow analysis.
Bob Kirby At 09:43 AM 5/24/2007, Ernest Friedman-Hill wrote: >Hi Robert, > >Sorry I didn't respond to this sooner -- your message was lost in a >flurry of other email and I'm just now finding it. > >What's happening here is that your 'or' has two branches, one of >which defines a variable named '?val' (the first branch) and the >other of which doesn't (the second.) In the second branch, you're >matching the absence of facts, so obviously we can't later refer to >the contents of a slot in a fact which doesn't exist. Jess could do a >better job of data-flow analysis so that this is pointed out when the >rule is compiled, but as it stands, the error doesn't get noticed >until runtime. If it's that second branch that matches, then there's >no "?val" and you get an error when you try to examine the query >results. > >So you need to rethink the rule and what you want to do with "?val". > >On May 21, 2007, at 12:25 PM, Robert Kirby wrote: >>While running Jess Version 7.0p1 12/21/2006, accessing defquery >>results fails with a compound pattern that works with a defrule. >> >>Bob Kirby >> >>---------------- >>(deftemplate row >> "A row in a test table." >> (declare (ordered FALSE)) >> (slot primary (type SYMBOL)) >> (slot foreign (type SYMBOL))) >> >>(deffacts rows >> "Rows of a test table of primary and foreign keys." >> (row (primary first) (foreign key1)) >> (row (primary second) (foreign unmatched))) >> >>(deftemplate pair >> "A pair of a key and a value." >> (declare (ordered FALSE)) >> (slot key (type SYMBOL)) >> (slot val (type STRING))) >> >>(deffacts pairs >> "Pairs of keys and values including a default value." >> (pair (key key1) (val val1)) >> (pair (key default) (val default-val))) >> >>(defrule print-test-rule >> "Test to print results from a rule" >> (row (primary ?primary) (foreign ?foreign)) >> (or (pair (key ?foreign) (val ?val)) >> (and (not (pair (key ?foreign))) >> (pair (key default) (val ?val)))) >> => >> (printout t "print-test-rule " ?primary " " ?foreign " " ?val >>crlf)) >> >>(reset) >>(run) >>;;;print-test-rule second unmatched default-val >>;;;print-test-rule first key1 val1 >> >>(defquery test-query >> "A test query to assign defaults for foreign keys." >> (row (primary ?primary) (foreign ?foreign)) >> (or (pair (key ?foreign) (val ?val)) >> (and (not (pair (key ?foreign))) >> (pair (key default) (val ?val))))) >> >>(deffunction print-test-query () >> "Run test-query" >> (bind ?results (run-query* test-query)) >> (while (?results next) >> (printout t "print-test-query " >> (?results getString primary) " " >> (?results getString foreign) " " >> (?results getString val) crlf)) >> (?results close)) >> >>(print-test-query) >>Jess reported an error in routine Context.getReturn >> while executing (call ?results getString val) >> while executing (printout t "print-test-query " (call ?results >>getString primary) " " (call ?results getString foreign) " " (call ? results >>getString val) crlf) >> while executing (while (call ?results next) (printout t "print- >> test-query " (call ?results getString primary) " " (call ?results >>getString foreign) " " (call ?results getString val) crlf)) >> while executing deffunction print-test-query >> while executing (print-test-query) >> while executing (batch q.clp). >> Message: No such variable val. >> Program text: ( batch q.clp ) at line 1. > >--------------------------------------------------------- >Ernest Friedman-Hill >Advanced Software Research Phone: (925) 294-2154 >Sandia National Labs FAX: (925) 294-2234 >PO Box 969, MS 9012 [EMAIL PROTECTED] >Livermore, CA 94550 http://www.jessrules.com
