Remove rule at runtime

11 views
Skip to first unread message

Jake Dempsey

unread,
Oct 27, 2009, 4:21:36 PM10/27/09
to Ruleby
Is there a way to remove a rule from the rulebase at runtime?

I want to create a rules engine that is exposed as a webservice.
Clients will post to /facts/assert to put a new fact into the working
memory and will post to /facts/retract to remove a fact from the
working memory. I currently start my environment by creating a single
engine and single rulebook:

//happens on start:
RULER_ENGINE = Ruleby::Core::Engine.new
RULE_BOOK = SimpleRulebook.new(RULER_ENGINE)
RULE_BOOK.rules //loads rules from external source

In my facts controller I have something like this:

def assert
RULER_ENGINE.assert CoolObj.new(params[:obj])
RULER_ENGINE.match
render :nothing => true
end

def retract
GarbageManRules.new(RULER_ENGINE) do |rb|
rb.rule [CoolObj, :c, m.pk == params[:pk], m.type == params
[:type]] do |v|
rb.retract(v[:c])
end
end
RULER_ENGINE.match
render :nothing => true
end


My understanding is that when I add the rule to the rulebase..its
there forever. In my case its really a transient rule just used to
remove something from the working memory and should be removed after
that action. Am I maybe approaching this wrong? Is there a way to
remove this rule from the rulebase once I am done with it?

Joe Kutner

unread,
Oct 27, 2009, 9:32:49 PM10/27/09
to rul...@googlegroups.com
Ruleby does not support retracting rules at this time.

In your case, I think you would be better served by creating a new
class that you can write rules for retracting facts around. Something
like this:

class Retractor
attr :class_to_retract
end

rule [CoolObj, :c, m.pk == params[:pk], m.type == params[:type]],
[Retractor, :r, m.class_to_retract = CoolObj] do |v|
retract(v[:c])
retract(v[:r])
end

The Retractor class could even contain the patterns you use to select
the CoolObjs. I hope this helps, please let me know if its not clear.

Joe

Jake Dempsey

unread,
Oct 29, 2009, 9:57:50 AM10/29/09
to Ruleby
Yeah I have something like this now...It works well if its a one to
one of retractor obj to cool obj. If you have 3 CoolObjs in working
memory and then send one retractor in..you will only remove one
CoolObj.

On Oct 27, 8:32 pm, Joe Kutner <jpkut...@gmail.com> wrote:
> Ruleby does not support retracting rules at this time.
>
> In your case, I think you would be better served by creating a new
> class that you can write rules for retracting facts around.  Something
> like this:
>
> class Retractor
>   attr :class_to_retract
> end
>
> rule [CoolObj, :c, m.pk == params[:pk], m.type == params[:type]],
>            [Retractor, :r, m.class_to_retract = CoolObj] do |v|
>        retract(v[:c])
>        retract(v[:r])
> end
>
> The Retractor class could even contain the patterns you use to select
> the CoolObjs.  I hope this helps, please let me know if its not clear.
>
> Joe
>
Reply all
Reply to author
Forward
0 new messages