Skip to content

Commit

Permalink
Adds minimal rule tracing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurii Rashkovskii committed Feb 16, 2012
1 parent dac7228 commit 3a7d5c2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions apps/htoad/src/htoad.erl
Expand Up @@ -29,6 +29,8 @@ assert(Fact) when is_list(Fact); is_tuple(Fact) ->
htoad_engine:assert(?ENGINE, Fact).

assert(Engine, Fact) when is_list(Fact); is_tuple(Fact) ->
{Fun, Args} = seresye_engine:get_fired_rule(Engine),
htoad_trace:assert(Engine, Fun, Args, Fact),
seresye_engine:assert(Engine, Fact).

retract(Fact) when is_list(Fact); is_tuple(Fact) ->
Expand Down
1 change: 1 addition & 0 deletions apps/htoad/src/htoad_app.erl
Expand Up @@ -20,6 +20,7 @@ start(_StartType, _StartArgs) ->
init:stop(),
{ok, self()};
_ ->
ets:new(htoad_trace, [named_table, public, bag]),
process_args(Args),
Result = htoad_sup:start_link(Args),
[ htoad:assert({load, File}) || File <- Files ],
Expand Down
19 changes: 19 additions & 0 deletions apps/htoad/src/htoad_engine.erl
Expand Up @@ -17,6 +17,7 @@
-export([start/0, start/1, start/2, stop/1, get_engine/1,
add_rules/2, add_rule/2, add_rule/3, assert/2, get_kb/1,
get_rules_fired/1, get_client_state/1,
set_hooks/2, get_fired_rule/1,
set_client_state/2, query_kb/2, remove_rule/2, retract/2]).

%% gen_server callbacks
Expand All @@ -35,6 +36,9 @@ start(Name) ->
start(Name, ClientState) ->
seresye_sup:start_engine(Name, ClientState).

set_hooks(Name, Hooks) when is_list(Hooks) ->
gen_server:cast(Name, {set_hooks, Hooks}).

set_client_state(Name, NewState) ->
gen_server:cast(Name, {set_client_state, NewState}).

Expand Down Expand Up @@ -75,6 +79,9 @@ remove_rule(Name, Rule) ->
get_rules_fired(Name) ->
gen_server:call(Name, get_rules_fired).

get_fired_rule(Name) ->
gen_server:call(Name, get_fired_rule).

get_kb(Name) ->
gen_server:call(Name, get_kb).

Expand Down Expand Up @@ -175,6 +182,15 @@ handle_call(get_rules_fired, _From, State0) ->
{error, {Type, Reason}}
end,
{reply, Reply, State0};
handle_call(get_fired_rule, _From, State0) ->
Reply =
try
seresye_engine:get_fired_rule(State0)
catch
Type:Reason ->
{error, {Type, Reason}}
end,
{reply, Reply, State0};
handle_call(get_engine, _From, State0) ->
{reply, State0, State0};
handle_call(get_kb, _From, State0) ->
Expand All @@ -196,6 +212,9 @@ handle_call({query_kb, Pattern}, _From, State0) ->
end,
{reply, Reply, State0}.

handle_cast({set_hooks, Hooks}, State) ->
{noreply, seresye_engine:set_hooks(State, Hooks)};

handle_cast({set_client_state, CS}, State) ->
{noreply, seresye_engine:set_client_state(State, CS)}.

Expand Down
5 changes: 4 additions & 1 deletion apps/htoad/src/htoad_sup.erl
Expand Up @@ -18,7 +18,10 @@ start_link(Args) ->
esupervisor:start_link({local, ?MODULE}, ?MODULE, [Args]).

start_seresye() ->
htoad_engine:start(?ENGINE).
{ok, Pid} = htoad_engine:start(?ENGINE),
htoad_engine:set_hooks(?ENGINE,[{before_rule, fun htoad_trace:rule/3}]),
{ok, Pid}.


%% ===================================================================
%% Supervisor callbacks
Expand Down
23 changes: 23 additions & 0 deletions apps/htoad/src/htoad_trace.erl
@@ -0,0 +1,23 @@
-module(htoad_trace).
-export([rule/3, assert/4]).

-include_lib("htoad/include/htoad.hrl").

rule(_Engine, Fun, Args) ->
Info = erlang:fun_info(Fun),
Key = {rule,
proplists:get_value(module, Info),
proplists:get_value(name, Info),
proplists:get_value(arity, Info)},
ets:insert(htoad_trace, [{{facts, Args}, Key}|[ {{fact, Arg}, Key} || Arg <- Args ]]).

assert(Engine, Fun, Args, Facts) when is_list(Facts) ->
[ assert(Engine, Fun, Args, Fact) || Fact <- Facts ];
assert(_Engine, Fun, _Args, Fact) ->
Info = erlang:fun_info(Fun),
Key = {rule,
proplists:get_value(module, Info),
proplists:get_value(name, Info),
proplists:get_value(arity, Info)},
ets:insert(htoad_trace, {Key, {fact, Fact}}).

0 comments on commit 3a7d5c2

Please sign in to comment.