Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Loading rules from external file
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jake Dempsey  
View profile  
 More options Oct 26, 11:56 am
From: Jake Dempsey <angelo0...@gmail.com>
Date: Mon, 26 Oct 2009 08:56:13 -0700 (PDT)
Local: Mon, Oct 26 2009 11:56 am
Subject: Loading rules from external file
Is there a way to load the rules from an external source? I tried
making my SampleRulebook take in block where the block was a simple
load('filename'), but that doesnt work. I know that there is an effort
underway to create an external dsl using treetop but my question is a
bit different.  I would like to be able to define the rule blocks in
an external file..like a text file.  Then in the code create the
engine..create the rulebook..and somehow say load these rules.
Something like:

engine :engine do |e|
  rulebook = SampleRulebook.new(e)
  rulebook.load('sample_rulebook.ruleby')
  e.assert Message.new(:HELLO, 'Hello World')
  e.match
end

//contents of rules.ruleby
rule :hello, [Message, :m, method.status == :HELLO] do |context|
  puts context[:m].message
end


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jake Dempsey  
View profile  
 More options Oct 26, 12:52 pm
From: Jake Dempsey <angelo0...@gmail.com>
Date: Mon, 26 Oct 2009 09:52:31 -0700 (PDT)
Local: Mon, Oct 26 2009 12:52 pm
Subject: Re: Loading rules from external file
I got this to work..not sure if its the best approach..eval always
makes me worried.  Can you think of another way of doing it in a safer
way?

engine :engine do |e|
  e.assert Message.new(:HELLO, 'Hello World')

  mrb = SampleRulebook.new(e)

  mrb.class.class_eval do
    def rules
      eval(IO.readlines(File.join(File.dirname(__FILE__),
'rules.ruleby'), '').to_s)
    end
  end

  mrb.rules
  e.match
end

//contents of rules.ruleby
rule :hello, [Message, :m, method.status == :HELLO] do |context|
  puts context[:m].message
end

On Oct 26, 10:56 am, Jake Dempsey <angelo0...@gmail.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jake Dempsey  
View profile  
 More options Oct 26, 1:23 pm
From: Jake Dempsey <angelo0...@gmail.com>
Date: Mon, 26 Oct 2009 10:23:52 -0700 (PDT)
Local: Mon, Oct 26 2009 1:23 pm
Subject: Re: Loading rules from external file
Ok i put the eval in the actual SampleRulebook class...i think i like
this better because the client accessing the rules doesnt know where
the rules are defined...which i like.  So now I have:

engine :engine do |e|
  e.assert Message.new(:HELLO, 'Hello World')
  mrb = SampleRulebook.new(e)
  mrb.rules
  e.match
end

class SimpleRulebook < Rulebook
  attr :messages

  def initialize(e, &block)
    @messages = []
    super(e)
    yield self if block_given?
  end

  def rules
    path =File.join(File.dirname(__FILE__), 'rules/*.ruleby')
    Dir[path].each do |f|
      eval((IO.readlines(f, '').to_s))
    end
  end

end

On Oct 26, 11:52 am, Jake Dempsey <angelo0...@gmail.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Pruss  
View profile  
 More options Oct 26, 3:17 pm
From: Richard Pruss <boa...@gmail.com>
Date: Tue, 27 Oct 2009 05:17:54 +1000
Local: Mon, Oct 26 2009 3:17 pm
Subject: Re: [ruleby] Re: Loading rules from external file

I am doing something similar, I have the rules written in a code field  
in an events table in rails.

@event = Event.find_by_title(f)
   engine :engine do |e|
     TestRulebook.class_eval "def rules;" + @event.code + ";end;"
     TestRulebook.new(e).rules
...etc

While I was reading the doc recently I did notice a method which may  
be a better way to go for this,
but I have not got around to figuring out how to do it.:
assert_rule(rule)
This method is invoked when a new rule is added to the system. The  
rule is processed and the appropriate nodes are added to the network.

- Ric

On 27/10/2009, at 3:23 AM, Jake Dempsey wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joe Kutner  
View profile  
 More options Oct 27, 9:40 pm
From: Joe Kutner <jpkut...@gmail.com>
Date: Tue, 27 Oct 2009 20:40:06 -0500
Local: Tues, Oct 27 2009 9:40 pm
Subject: Re: [ruleby] Re: Loading rules from external file
These are both very interesting ways of building your rule base.
While they seem to work for the two of you, they make me feel like the
Rulebook design is lacking something.  I would greatly welcome your
suggestions on how to improve it for these kinds of cases.

The assert_rule method take a Rule object as a parameter.  The Rule
class is not complicated, and the DSL simply builds one of these up
behind the scenes.  You can see an example of this around line 130 in
ferrari.rb

Joe


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jake Dempsey  
View profile  
 More options Oct 28, 8:45 am
From: Jake Dempsey <angelo0...@gmail.com>
Date: Wed, 28 Oct 2009 05:45:22 -0700 (PDT)
Local: Wed, Oct 28 2009 8:45 am
Subject: Re: Loading rules from external file
I am looking at ruleby as a two part module. The first piece I want to
use it for is the execution of a ruleset.  The actual rules management
should be externalized in my case.  In an environment where customers
drive the rules you want them to be able to change those rules without
the need of a deployment or a svn commit.  I have been noodling how I
can use ruleby and remove the need for a larger implementation such as
drools.

The basic workflow I am after is:
Customer manages rules through a webui that persists their rules.
Ideally this would be done with a dsl specific to my application
domain and I have not yet figured out how best to approach this. I
really would like a grammar for my customer that generates ruleby
syntax. My app would start with a constant rule engine and empty
rulebook. On startup the persisted rules would be loaded.  At some
point, either event based or time based, I would want to clear the
current rules and reload them. (this is because the customer may have
modified, added, or removed a rule). I dont want to have to create a
new engine (which blows away my working memory).  Ideally I would have
a method .reset_rules on my rulebook that would remove all existing
rules and then calls .rules. This would allow me to keep a single
engine/rulebook and use my rule engine as a webservice where my users
can assert and retract facts at any point and each assert doesnt
create the need to create a new engine and reload all the rules.

Hope that explains what I'm after.

On Oct 27, 8:40 pm, Joe Kutner <jpkut...@gmail.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Smith  
View profile  
 More options Oct 28, 10:50 am
From: Matt Smith <codeaspe...@gmail.com>
Date: Wed, 28 Oct 2009 09:50:37 -0500
Local: Wed, Oct 28 2009 10:50 am
Subject: Re: [ruleby] Re: Loading rules from external file

Hi Jake,

I started working on this several times, but never ended up with a good
solution.

The ActiveRule code, http://github.com/mattup/activerule ,  was intending to
do something similar.  The idea was to store the rules in the db, including
any metadata for each rule.  I did manage to create a simple ActiveRecord
model as a base.  The model works in terms of loading the rule from the db.
But as you point out there is not a way to reload the rules without creating
a new engine.

Another approach I took was to create an external DSL, but sadly I never
finished it either.  Sort of a pattern of mine.....

Matt


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jake Dempsey  
View profile  
 More options Oct 28, 12:19 pm
From: Jake Dempsey <angelo0...@gmail.com>
Date: Wed, 28 Oct 2009 09:19:14 -0700 (PDT)
Local: Wed, Oct 28 2009 12:19 pm
Subject: Re: Loading rules from external file
Yeah I would think the external dsl would be more valuable.  I looked
at activerule but I dont think its needed.  If a user defines a rule
using a dsl you could "compile" them in to ruleby rule definitions in
a flat file and just load those at runtime.  At least that was my
thought :)

On Oct 28, 9:50 am, Matt Smith <codeaspe...@gmail.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jhc  
View profile  
 More options Nov 13, 10:34 am
From: jhc <placidpun...@gmail.com>
Date: Fri, 13 Nov 2009 07:34:19 -0800 (PST)
Local: Fri, Nov 13 2009 10:34 am
Subject: Re: Loading rules from external file
I'm just starting to play around with Ruleby, so I may be off here.
But wouldn't it be possible to do something like this?

class MyClass
  def initialize
    @wm = Ruleby::Core::WorkingMemory.new
    load_rules
  end

  def load_rules
    @engine = Ruleby::Core::Engine.new(@wm)
    # ...Read your (possibly updated) rules here...
    @rules = MyRulebook.new(@engine).rules
  end
end

On Oct 28, 7:45 am, Jake Dempsey <angelo0...@gmail.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joe Kutner  
View profile  
 More options Nov 13, 10:44 am
From: Joe Kutner <jpkut...@gmail.com>
Date: Fri, 13 Nov 2009 09:44:46 -0600
Local: Fri, Nov 13 2009 10:44 am
Subject: Re: [ruleby] Re: Loading rules from external file
That should be fine.  The only reason we have the Ruleby::engine
function is for convenience.  And its probably only convenient for the
tests :)

If you have any suggestions for other idioms that make instantiating
the engine easier, I'd be happy to work them into the sources.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jhc  
View profile  
 More options Nov 13, 11:10 am
From: jhc <placidpun...@gmail.com>
Date: Fri, 13 Nov 2009 08:10:50 -0800 (PST)
Local: Fri, Nov 13 2009 11:10 am
Subject: Re: Loading rules from external file
Ruleby::engine is OK with me. :-)

I just wondered if this approach might help Jake retain his working
memory between reloads, or if there's some pitfall there.  I saw
something in the source about needing to iterate over all facts when
new rules are added (the compare_to_wm method).  Would that come into
play with a large set of facts?

On Nov 13, 10:44 am, Joe Kutner <jpkut...@gmail.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google