[erlang-questions] config based rules grammar

Robert Raschke rtrlists@REDACTED
Wed Feb 16 16:49:47 CET 2011


Hi Nav,

On Wed, Feb 16, 2011 at 12:35 PM, Nav <orionqwest@REDACTED> wrote:

> Hi,
>
> I have been defining quite many rules in config file, so that my
> erlang program can read and interpret. These rules apply for every
> incoming message to gen_server, and looks somewhat like
>
> - if message "contains", expression "XYZ200", do this / that.
> - if message was sent more than 10 mins earlier, do this / that ...
> ... and so on, the implementation gets complex, with several rules
>
> Does Erlang have any feature, that can help me define extensive
> "grammar" in configuration file, with generic OTP/implementation?
>
> Nav.
>
>
You could use file:script (http://www.erlang.org/doc/man/file.html#script-1)
to load a list of functions.

For example, if myconfig.conf looks like this:
[

fun ({_Time, Data} = Msg) ->
    case string:str(Data, "XYZ200") of
        X when X > 0 ->
            found_XYZ200;
        _ ->
            not_found
    end
end,

fun ({{Date, Time} = T, _Data} = Msg) ->
    Now = calendar:datetime_to_gregorian_seconds(calendar:local_time()),
    Msg_Time = calendar:datetime_to_gregorian_seconds(T),
    if
        Now - Msg_Time > 10 * 60 ->
            older_than_10_mins;
        true ->
            recent
    end
end

].

Then you can:

Erlang (BEAM) emulator version 5.6.5 [smp:2] [async-threads:0]

Eshell V5.6.5  (abort with ^G)

1> {ok, Funs} = file:script("myconfig.conf").
{ok,[#Fun<erl_eval.6.13229925>,#Fun<erl_eval.6.13229925>]}

2> Msg1 = {calendar:local_time(), "abc"}.
{{{2011,2,16},{15,42,59}},"abc"}

3> Msg2 = {calendar:local_time(), "abc  XYZ200 def"}.
{{{2011,2,16},{15,43,21}},"abc  XYZ200 def"}

4> Msg3 = {{{2011,2,16},{12,0,0}}, "abc  XYZ200 def"}.
{{{2011,2,16},{12,0,0}},"abc  XYZ200 def"}

5> Do_Rules = fun (Msg) -> lists:map( fun(F) -> F(Msg) end, Funs ) end.
#Fun<erl_eval.6.13229925>

6> Do_Rules(Msg1).
[not_found,recent]

7> Do_Rules(Msg2).
[found_XYZ200,recent]

8> Do_Rules(Msg3).
[found_XYZ200,older_than_10_mins]

9>

This the kind of thing you're after?

Robby


More information about the erlang-questions mailing list