Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Event loop with rules handling

by Brovnik (Hermit)
on Nov 03, 2004 at 22:07 UTC ( #405015=perlquestion: print w/ replies, xml ) Need Help??
Brovnik has asked for the wisdom of the Perl Monks concerning the following question:

I am about to start on a largish project and one of the modules is what I have called the "watcher".

This will manage a set of rules, and watch for external events. If any external event could affect the rules, then the (many) rules in the active rules list will get reevaluated and any that fire will be processed.

I've looked around but haven't seen anything that might help with the rules / event loop. Has anyone seen something like this ?

More info :

The rules will be something like (rules syntax not yet defined):

if ($last_price > $trigger_level) and ($timenow > $start) and ($timenow < $end) and ($auth == 2) then execute()
The intention is that the rules can be written by a non-programmer (but someone with deep knowledge of the domain).

The variables (e.g. $last_price) will get changed by external (to the rules) events, so on one evaluation pass, a rule could evaluate false, but shortly after, could evaluate true and need to fire.

This is a bit similar to an Expert System that I worked on once, but, in this case, there is no searching, one rule does not lead to another, and all rules are in the form if (conditions) then execute(actions).

Some of the actions could lead to other rules being added or deleted though.

Any pointers appreciated before I start building something from scratch.

Comment on Event loop with rules handling
Download Code
Re: Event loop with rules handling
by kvale (Monsignor) on Nov 03, 2004 at 23:31 UTC
    As a start, I think that you could use the Event module as a generic event handler. It even has a 'watcher' as per your description and allows you to register and unregister handlers as appropriate. Handlers are in the form of callback subs. There are no specialized rule methods per se, but if simple if-then statments suffice, then there don't need to be.

    -Mark

Re: Event loop with rules handling
by NetWallah (Abbot) on Nov 03, 2004 at 23:36 UTC
    Your requirements seem to fit the pattern of "Discrete Finite Automata" - please look at the DFA::Simple module.

        Earth first! (We'll rob the other planets later)

Re: Event loop with rules handling
by BrowserUk (Pope) on Nov 03, 2004 at 23:45 UTC

    A few questions:

    • How will your program be notified (or detect) the external events?
    • When an external event is notified, do you antisipate iterating through all your rules in order to decide what to execute?
    • Alternatively, do you want the external event to cause only those rules that might cause some actions to be executed, to be evaluated?
    • Where will your program pick up the value of the variables used within the conditions, from?
    • How (and in what form) would your program pick up the rules?
      • Read them in from a file at startup?
      • Update them dynamically?
      • Import them as Perl code?
      • Parse as non-Perl expressions and interpret them at runtime?

    There really isn't enough information in your post to base suggestions on. Things that come to mind are:

    • triggers set on columns in a DB, that invoke stored procedures when values are added or updated.
    • Something like a tied hash.

      When the main loop (or thread) monitoring the external events updates one of the keys in the hash, the tie implementation processes a set of rules and associated actions for that key.

    Several other ideas are alluded to in my questions above, but there's little point in expanding them on the basis of what you've told us so far.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      All good questions. I'm early enough in the cycle that I haven't made the decision on a lot of this stuff yet, but, here goes :
      1. External events will prob. be either via signals for occasional triggers, or using sockets for regular changes.
      2. Whether to iterate over all rules is really a performance issue. One option is a register of variable that would change per rule, so on a change, only those affected would need to be evaluated. I expect the eval loop to be so quick though that this won't be needed initially
      3. Picking up rules - There would be a set of startup rules. Some would need to be created dynamically. My ideal would be to parse non-Perl expressions and interpret them.
      4. Triggers on DB columns - I considered this, but don't want the business logic encoded in the DB. What would be ideal is for e.g. a new/changed row in a DB to trigger an external event to prompt the Watcher to reevaluate. The DB will be MySQL, not sure if this would be possible with callbacks. May need some homework.
Re: Event loop with rules handling
by osunderdog (Deacon) on Nov 04, 2004 at 14:39 UTC

    Brovnik, if you can track down the following article it covers a lot of what you are describing. It's also pretty easy to implement in Perl.

    Decision-making with production systems
    Source C/C++ Users Journal archive
    Volume 16 , Issue 2 (February 1998) table of contents
    Pages: 57 - 66
    Year of Publication: 1998
    ISSN:1075-2838
    Author
    Dwayne Phillips
    Publisher
    CMP Media, Inc. USA

    Unfortunately I couldn't find this article on line anywhere. I know it's available on the C/C++ Journal cdrom....

    I have a package based on this from a few years ago. However I just checked and it doesn't test out. :(


    "Look, Shiny Things!" is not a better business strategy than compatibility and reuse.


    OSUnderdog

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://405015]
Approved by cyocum
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2014-09-16 11:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (13 votes), past polls