I think Ben Allen wrote:
> I was wondering whether it is possible to create rules from within rules?

Yes, absolutely.  Jess is designed as a dynamic system where it's easy
to add and remove rules as the engine runs.

The added rules have to be built up as data (i.e., as a string, as
source code) and handed to the engine to compile and integrate into
the Rete network using the "build" function. It could look like this
interactive session, where a new rule is defined to match a "foo" fact
containing a single constant value. The value is pulled out of a "data"
fact, and used to build a new rule. Note that the ?X is the *only* part
of the new rule that's not in double quotes -- we're taking the value
of the variable ?X at one point in time, and hard-coding it into a new
rule. Remember that Jess strings can contain newlines.

----------------------------------------------------------------------
Jess> (defrule make-new-rule
  (data ?X)
  =>
  (build
    (str-cat "(defrule new-rule
                (foo " ?X ")
                =>
                (printout t \"Found matching foo fact.\" crlf))")))
TRUE
Jess>
(reset)
TRUE
Jess> (watch all)
TRUE
Jess> (assert (data 1))
 ==> f-1 (MAIN::data 1)
==> Activation: MAIN::make-new-rule :  f-1
<Fact-1>
Jess> (run)
FIRE 1 MAIN::make-new-rule f-1
MAIN::new-rule: +1+1+1+1+t
 <== Focus MAIN
1
Jess> (ppdefrule new-rule)
"(defrule MAIN::new-rule
   (MAIN::foo 1)
   =>
   (printout t \"Found matching foo fact.\" crlf))"
Jess>

So you see, Alan was right. There is a super-simple way to do it. :)





---------------------------------------------------------
Ernest Friedman-Hill  
Science and Engineering PSEs        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]
--------------------------------------------------------------------

Reply via email to