Re: Add/remove rules dynamically in CLIPS?

1,288 views
Skip to first unread message

CLIPS Support

unread,
Jun 5, 2013, 3:26:12 PM6/5/13
to clip...@googlegroups.com
You can dynamically create new rules and these rules will match any preexisting facts. You can either load them from a file, enter them at the command prompt, or use the build command:

CLIPS> (defrule rule-1 (a) =>)
CLIPS> (assert (a) (b))
<Fact-2>
CLIPS> (agenda)
0      rule-1: f-1
For a total of 1 activation.
CLIPS> (defrule rule-2 (b) =>)
CLIPS> (build "(defrule rule-3 (a) (b) =>)")
TRUE
CLIPS> (agenda)
0      rule-3: f-1,f-2
0      rule-2: f-2
0      rule-1: f-1
For a total of 3 activations.
CLIPS> 

On Wednesday, June 5, 2013 6:56:24 AM UTC-5, anders....@gmail.com wrote:
Hi,

I'm implementing a Publish/Subscribe service where CLIPS rules defines the subscription. At anytime a device can create/remove a subscription so creating CLIPS rules must be performed dynamically. Is it possible to add and remove rules from CLIPS on the fly? For adding rules I guess I could write the new rule to a new clp file and Load it into CLIPS.

Thanks,
Anders Berg

Nick Bassiliades

unread,
Jun 6, 2013, 2:19:07 AM6/6/13
to clip...@googlegroups.com
You could also use the build function.
See CLIPS Basic Programming Guide page 165-166

Cheers,
Nick

Στις 5/6/2013 2:56 μμ, ο/η anders....@gmail.com έγραψε:
> Hi,
>
> I'm implementing a Publish/Subscribe service where CLIPS rules defines
> the subscription. At anytime a device can create/remove a subscription
> so creating CLIPS rules must be performed dynamically. Is it possible
> to add and remove rules from CLIPS on the fly? For adding rules I
> guess I could write the new rule to a new clp file and Load it into CLIPS.
>
> Thanks,
> Anders Berg
> --
> --
> You received this message because you are subscribed to the Google
> Groups "CLIPSESG" group.
> To post to this group, send email to CLIP...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/CLIPSESG?hl=en
>
> --> IF YOU NO LONGER WANT TO RECEIVE EMAIL <--
> Visit this group at http://groups.google.com/group/CLIPSESG?hl=en
> Click on "Edit my membership" link.
> Select the "No Email" radio button.
> Click the "Save these settings" button.
>
> --> IF YOU WANT TO UNSUBSCRIBE <--
> Visit this group at http://groups.google.com/group/CLIPSESG?hl=en
> Sign in
> Click on "Edit my membership" link.
> Click the "Unsubscribe" button.
> Note: This appears to be the most reliable way to unsubscribe
>
> Alternately, send email to CLIPSESG-u...@googlegroups.com. You
> will receive an email which you must respond to as well to
> unsubscribe. Clicking the link mentioned in the unsubscribe reply does
> not appear to work reliably.
> ---
> You received this message because you are subscribed to the Google
> Groups "CLIPSESG" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to clipsesg+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

--
**************************************************************
* Dr. Nick Bassiliades, Associate Professor *
* Dept. of Informatics, Aristotle University of Thessaloniki *
* 54124 Thessaloniki, Greece *
* *
* Tel: +302310997913 E-mail: nbas...@csd.auth.gr *
* Fax: +302310997913 URL: http://tinyurl.com/nbassili *
* ========================================================== *
* LPIS (Logic Programming & Intelligent Systems) Group *
* URL: http://lpis.csd.auth.gr/ *
**************************************************************

anders....@gmail.com

unread,
Jun 6, 2013, 8:21:30 AM6/6/13
to clip...@googlegroups.com
Thank you guys, the Build(constructString) works great for adding rules!
Now I can't get the Undefrule function to work, 4.6.21 in Advance Programming Guide, for removing defined rules.
Here is some of the code i use:

int main()
{
...
    char *newRule;
    int ok;

    /* Create a new rule */
    newRule = "(defrule turn_off_all_lights \n\
        ?fact <- (turn_off_all_lights_action)\n\
        => \n\
        (retract ?fact) \n\
        (printout t \"Turning of all the lights\" crlf))";
    ok = Build(newRule);
    if (ok == 1) printf("Rule: %s\n\nwas created successfully!\n", newRule);
    else printf("Error creating rule: %s\n", newRule);

...

    /* Remove a defrule */
    char * removeRule = "turn_off_all_lights";
    void * rulePtr = FindDefrule(removeRule);
    ok = Undefrule(rulePtr);
    if (ok == 1) printf("Successfully deleted rule: turn_off_all_lights");
    else printf("Error: could not delete rule!!");

...

}

When i run my program it gets stuck at the "ok = Undefrule(rulePtr);" and the program never finishes.
The rulePtr is not NULL, so it finds the rule.
Any suggestions?

Thanks,
Anders

CLIPS Support

unread,
Jun 6, 2013, 1:07:24 PM6/6/13
to clip...@googlegroups.com
It worked when I tried in with version 6.3 of CLIPS. I would suggest removing the ... code in your example and see if it works properly in that case.


On Wednesday, June 5, 2013 6:56:24 AM UTC-5, anders....@gmail.com wrote:

anders....@gmail.com

unread,
Jun 7, 2013, 7:24:02 AM6/7/13
to clip...@googlegroups.com
I think I found the problem. I'm adding facts in between the defrule and undefrule, which activates the rule. So I'm trying to delete a rule that is in the agenda and haven't been fired yet.
I moved the Run(-1); function call before the undefrule function call and then it worked. Now I'll just have to figure out if I can remove an activated rule from the agenda and delete it before I call the Run() function =)
Reply all
Reply to author
Forward
0 new messages