I was recently evaluating jess for one of our projects and did exactly this to see what the performance hit was for dynamicaly creating rules and facts. Here is what I used
 

(deffacts globals (f 1))

(defrule generate-rule

(f ?x&:(< ?x 10))

=>

 (build (str-cat "(defrule " (gensym*) "(f " ?x ") => (assert (f " (+ ?x 1) ")))"))

)

(reset)

(run)

 

The generate-rule will fire first for the fact defiend in globals, this will create a rule that will fire for the same fact and also create another fact causing generate-rule to fire once more. The happens repeatedly until the LHS in the generate-rule is false resulting in 10 facts and 10 generated rules. Hope this helps.

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]On Behalf Of Mitch Christensen
Sent: Friday, February 13, 2004 1:06 PM
To: [EMAIL PROTECTED]
Subject: RE: JESS: Creating rules within rules

Probably what you want can be accomplished by defining your child rule in a different module, then activate that module in your RHS.
 
-Mitch
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]On Behalf Of Ben Allen
Sent: Friday, February 13, 2004 5:42 AM
To: Jess Users
Subject: JESS: Creating rules within rules

I was wondering whether it is possible to create rules from within rules? For example, the program below tries to create a rule from within its RHS.

(defrule create-rule-on-rhs
        "This is a test to see if you can create a rule"
        ;LHS
        (some-fact)
        =>
        ;RHS
        ;call to the function defrule
        (defrule child-rule
                "This is the rule that will be created"
                (another-fact)
                =>
                (printout t "Hello world - i'm a new rule and i am alive!" crlf)
        )
)

(deffacts ben-facts
        "Some test data"
        (some-fact)
)

(reset)
(rules)
(run)
(rules)

However, this code generates the exception below:

MAIN::create-rule-on-rhs
For a total of 1 rules.
Jess reported an error in routine Funcall.execute
        while executing (defrule child-rule "This is the rule that will be creat
ed" (another-fact) "=" > (printout t "Hello world - i'm a new rule and i am aliv
e!" crlf))
        while executing defrule MAIN::create-rule-on-rhs
        while executing (run).
  Message: Unimplemented function defrule.
  Program text: ( run )  at line 35.
        at jess.Funcall.execute(Funcall.java:258)
        at jess.Defrule.fire(Defrule.java:215)
        at jess.Activation.fire(Activation.java:84)
        at jess.Agenda.run(Agenda.java:203)
        at jess.Agenda.run(Agenda.java:183)
        at jess.Rete.run(Rete.java:1098)
        at jess.HaltEtc.call(Funcall.java:1487)
        at jess.FunctionHolder.call(FunctionHolder.java:30)
        at jess.Funcall.execute(Funcall.java:266)
        at jess.Jesp.parseAndExecuteFuncall(Jesp.java:1590)
        at jess.Jesp.parseSexp(Jesp.java:173)
        at jess.Jesp.parse(Jesp.java:69)
        at jess.Jesp.parse(Jesp.java:54)
        at jess.Main.execute(Main.java:126)
        at jess.Main.main(Main.java:24)

Can anyone help? This may turn out to be a crazy thing to do but that is an equally good response to the problem.

Ben Allen -------------------------------------------------------------------- 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