Wow, good explanation. :-) I never realized that the ?matches would have been gone by then, but it makes sense, thanks.
As far as defglobals go, I am a bit concerned when it comes to several of my agents all using the same Rete instance at the same time. If more than one agent called the user function at the same time, which would also mean creating/using the same defglobal, wouldn't bad things happen? how do I make it safe to make multiple calls to the same user function at the same time, in the same Rete? (I am familiar with Java synchronization, if that's relevant). Can I write user functions similar to the previous one, without using defglobals? Thanks! Matt On 4/14/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > I think Matthew Hutchinson wrote: > > > > 1. Why can't defrule be used directly within a user function? > > Basically because it's executed by the parser, not by the runtime > engine. It's executed immediately when it's encountered in the > source. If you look in the back of the (Jess 7) manual, you'll find an > appendix listing all the "constructs", separate from the list of > "functions"; all the constructs are the same way. In standard > Lisp-ese, these are known as "special forms". > > > 2. What is the scope of a variabled bound (binded?) within a user > function? > > Limited to that user function. > > > 3. If "build" comes across a variable ( ?someVar ) in a string it is > > evaluating, does it treat it like a variable? > > > > That depends on what the variable means in the code being parsed by > "build." If you're "build"-ing a function call, then that function > call will be evalulated right away, using the deffunction's execution > context to find the values for variables. So, for example, > > (bind ?x 1) > (build "(printout t ?x crlf)") > > prints "1". But if you're build-ing a defrule, then the parser knows > that variables are supposed to be inserted literally into the defrule > itself. It's exactly as if the rule were included at the top level of > your Jess program. Your variable "?matches" is eventually interpreted > in the context of the firing defrule; the deffunction context is long > gone by then. > > Since you basically want ?matches to be a global variable shared by > multiple rule firings, you need to make it a global variable. Use a > defglobal for this. > > > > > > (clear) > > (reset) > > > > (import java.util.ArrayList) > > > > (deftemplate street_name_alias (slot actual_name) (slot actual_type) > (slot > > dli_id) (slot soundex) (multislot alias) (slot lga) (slot locality) ) > > (load-facts "road_facts.fct") > > > > (deffunction street-names-soundex (?input) > > (bind ?matches (new ArrayList 0) ) > > (bind ?code (str-cat "\""?input"\"") ) > > (printout t ?code crlf) > > > > (bind ?rule (str-cat "(defrule name-from-soundex (street_name_alias > > (soundex " ?code ") (actual_name ?name)) => (call ?matches add > ?name) )") ) > > (build ?rule) > > > > (return true) > > ) > > > > (street-names-soundex "M640") > > (run) > > > > ------------------------------------------------------- > > > > When I refer to ?matches in my RHS, shouldn't it see the variable I have > > already established, which points to the ArrayList? > > > > > > As always thanks. I posted this I have been stuck for ages. > > > > Cheers, > > Matt > > > > > > > > -- > > Matthew Hutchinson > > Ph.D. Candidate > > Department of Spatial Sciences > > Curtin University of Technology > > GPO Box U1987 > > Perth, Western Australia 6845 > > > > Visiting Scholar > > Department of Geography and Planning > > University of Akron > > Akron, Ohio USA > > > > --------------------------------------------------------- > 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://herzberg.ca.sandia.gov > > -------------------------------------------------------------------- > To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]' > in the BODY of a message to [EMAIL PROTECTED], NOT to the list > (use your own address!) List problems? Notify [EMAIL PROTECTED]
