-module(dsl). -export([broker/0, load_biz_rules/2, apply_biz_rules/2]). broker() -> receive {buy, Quantity, Ticker} -> % place order to an external system here Msg = "Order placed: buying ~p shares of ~p", io:format(Msg, [Quantity, Ticker]), broker(); {sell, Quantity, Ticker} -> % place order to an external system here Msg = "Order placed: selling ~p shares of ~p", io:format(Msg, [Quantity, Ticker]), broker() end. load_biz_rules(Pid, File) -> {ok, Bin} = file:read_file(File), Rules = string:tokens(erlang:binary_to_list(Bin), "\n"), [rule_to_function(Pid, Rule) || Rule <- Rules]. rule_to_function(Pid, Rule) -> {ok, Scanned, _} = erl_scan:string(Rule), [{_,_,Action},{_,_,Quantity},_,_|Tail] = Scanned, [{_,_,Ticker},_,_,_,{_,_,Operator},_,{_,_,Limit}] = Tail, to_function(Pid, Action, Quantity, Ticker, Operator, Limit). to_function(Pid, Action, Quantity, Ticker, Operator, Limit) -> fun(Ticker_, Price) -> if Ticker =:= Ticker_ andalso ( ( Price < Limit andalso Operator =:= less ) orelse ( Price > Limit andalso Operator =:= greater ) ) -> Pid ! {Action, Quantity, Ticker}; true -> erlang:display("no rule applied") end end. apply_biz_rules(Functions, MarketData) -> lists:map(fun({Ticker,Price}) -> lists:map(fun(Function) -> Function(Ticker, Price) end, Functions) end, MarketData), ok.