Permalink
Browse files

get rid of all object.erl and agent.erl leftovers

  • Loading branch information...
1 parent 77e665d commit de888bfed86bf816b1da5b08ac24c3d192349db5 @gleber committed Apr 19, 2012
@@ -1,39 +1,49 @@
-module(exat_agent).
--export([extends/0]).
+-export([start/0]).
--export([action/2, do_request/4, event/2, on_starting/1,
- pattern/2, start/0]).
+-behaviour(simple_agent).
+
+-export([code_change/3, handle_acl/2, handle_call/3,
+ handle_cast/2, handle_info/2, init/2,
+ terminate/2]).
-include_lib("exat/include/acl.hrl").
-include_lib("exat/include/fipa_ontology.hrl").
-extends() -> nil.
-
-pattern(Self, request) ->
- [#aclmessage{speechact = 'REQUEST'}].
+-record(state, {name}).
-event(Self, evt_request) -> {acl, request}.
-
-action(Self, start) -> {evt_request, do_request}.
+start() ->
+ simple_agent:new(the_exat_agent, the_exat_agent, [{behaviour, exat_agent}]).
-fellow_agent() ->
- #'agent-identifier'{name = "jadeagent@jadeplatform",
- addresses = ["http://localhost:7778/acc"]}.
+handle_acl(#aclmessage{speechact = 'REQUEST'} = Message, #state{self = Self} = State) ->
+ io:format("[Agent:~w] Request received from agent ~p\n",
+ [Self, Message#aclmessage.sender]),
+
+ {noreply, State}.
-on_starting(Self) ->
- io:format("[Agent:~w] Starting\n",
- [object:agentof(Self)]),
+init(the_exat_agent, Params) ->
acl:sendacl(#aclmessage{speechact = 'REQUEST',
content = "ping", sender = Self,
- receiver = fellow_agent()}).
+ receiver = fellow_agent()}),
+ {ok, #state{self = the_exat_agent}}.
-do_request(Self, EventName, Message, ActionName) ->
- io:format("[Agent:~w] Request received from agent "
- "~p\n",
- [object:agentof(Self), Message#aclmessage.sender]),
- object:do(Self, start).
+handle_call(Call, _From, State) ->
+ {reply, {error, unknown_call}, State}.
-start() ->
- agent:new(the_exat_agent, [{behaviour, exat_agent}]).
+handle_cast(_Call, State) ->
+ {reply, {error, unknown_cast}, State}.
+
+handle_info(Msg, State) ->
+ {noreply, State}.
+
+code_change(OldVsn, State, Extra) -> {ok, State}.
+
+terminate(_Reason, _State) ->
+ ok.
+
+
+fellow_agent() ->
+ #'agent-identifier'{name = "jadeagent@jadeplatform",
+ addresses = ["http://localhost:7778/acc"]}.
@@ -1,69 +0,0 @@
-%%
-%%pingagent.erl
-%%
-%%----------------------------------------------------------------------
-%%
-%% eXAT, an erlang eXperimental Agent Tool
-%% Copyright (C) 2005-07 Corrado Santoro (csanto@diit.unict.it)
-%%
-%% This program is free software: you can redistribute it and/or modify
-%% it under the terms of the GNU General Public License as published by
-%% the Free Software Foundation, either version 3 of the License, or
-%% (at your option) any later version.
-%%
-%% This program is distributed in the hope that it will be useful,
-%% but WITHOUT ANY WARRANTY; without even the implied warranty of
-%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-%% GNU General Public License for more details.
-%%
-%% You should have received a copy of the GNU General Public License
-%% along with this program. If not, see <http://www.gnu.org/licenses/>
-%%
-%%
-%%
-%%-------------------------------------------
-%%THIS IS THE AGENTCITIES PING AGENT
-%%-------------------------------------------
-%%
--module(pingagent).
-
--export([action/2, event/2, extends/0, get_ping_proc/4,
- on_starting/1, pattern/2, start/0]).
-
--include("acl.hrl").
-
-extends() -> nil.
-
-action(Self, start) -> [{acl_event, get_ping_proc}].
-
-event(Self, acl_event) -> {acl, all_pattern}.
-
-pattern(Self, all_pattern) -> [#aclmessage{}].
-
-%%
-%%
-%%
-on_starting(Self) -> logger:start('PING-AGENT').
-
-%%
-%% get the ping message
-%%
-get_ping_proc(Self, Event,
- Msg = #aclmessage{speechact = 'QUERY-REF',
- content = "ping"},
- Action) ->
- logger:log('PING-AGENT', "Received PING MESSAGE"),
- acl:reply(Msg, 'INFORM', "alive");
-%%
-%%
-%%
-get_ping_proc(Self, Event, Msg, Action) ->
- logger:log('PING-AGENT',
- {"Received ~p, NOT UNDERSTOOD", [Msg]}),
- acl:reply(Msg, 'NOT-UNDERSTOOD',
- Msg#aclmessage.content).%%
-%%
-
-%%
-start() ->
- agent:new(pingagent, [{behaviour, pingagent}]).
@@ -1,114 +0,0 @@
-%%
-%%pingeragent.erl
-%%
-%%----------------------------------------------------------------------
-%%
-%% eXAT, an erlang eXperimental Agent Tool
-%% Copyright (C) 2005-07 Corrado Santoro (csanto@diit.unict.it)
-%%
-%% This program is free software: you can redistribute it and/or modify
-%% it under the terms of the GNU General Public License as published by
-%% the Free Software Foundation, either version 3 of the License, or
-%% (at your option) any later version.
-%%
-%% This program is distributed in the hope that it will be useful,
-%% but WITHOUT ANY WARRANTY; without even the implied warranty of
-%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-%% GNU General Public License for more details.
-%%
-%% You should have received a copy of the GNU General Public License
-%% along with this program. If not, see <http://www.gnu.org/licenses/>
-%%
-%%
-%%
-%%-------------------------------------------
-%%THIS IS THE AGENTCITIES PINGER AGENT
-%%-------------------------------------------
-%%
--module(pingeragent).
-
--export([action/2, event/2, extends/0, on_starting/1,
- pattern/2, send_ping_proc/4, start/0,
- wait_pong_proc/4]).
-
--include("acl.hrl").
-
--include("fipa_ontology.hrl").
-
-extends() -> nil.
-
-action(Self, start) ->
- [{timeout_event, send_ping_proc}];
-action(Self, wait_pong) ->
- [{acl_event, wait_pong_proc}].
-
-event(Self, timeout_event) -> {timeout, to_value};
-event(Self, acl_event) -> {acl, all_pattern}.
-
-pattern(Self, to_value) -> 1000;
-pattern(Self, all_pattern) -> [#aclmessage{}].
-
-%%
-%%
-%%
-on_starting(Self) ->
- logger:start('PINGER'),
- case exat:get_argument(destagent) of
- {ok, [PingedAgent]} ->
- {ID, ADR} = lists:splitwith(fun (X) -> X =/= $, end,
- PingedAgent),
- AgentID = case ADR of
- [] -> PingedAgent;
- [$, | ADDR] ->
- #'agent-identifier'{name = ID, addresses = [ADDR]}
- end,
- object:set(Self, destagent, AgentID);
- _ ->
- logger:log('PINGER',
- {"Invalid destination agent. Use '-destagent "
- "name,mtp_address",
- []}),
- object:set(Self, destagent, none)
- end.
-
-%%
-%% get the ping message
-%%
-send_ping_proc(Self, Event, _, Action) ->
- Dest = object:get(Self, destagent),
- if Dest == none -> object:stop(Self);
- true ->
- object:set(Self, timestamp, erlang:now()),
- acl:query_ref(#aclmessage{sender = Self,
- receiver = Dest, content = "ping"}),
- object:do(Self, wait_pong)
- end.
-
-%%
-%%
-%%
-
-%%
-%% wait the 'alive' message
-%%
-wait_pong_proc(Self, Event,
- Msg = #aclmessage{speechact = 'INFORM'}, Action) ->
- T1 = erlang:now(),
- T0 = object:get(Self, timestamp),
- Diff = timer:now_diff(T1, T0) / 1.0e+3,
- logger:log('PINGER',
- {"Received MESSAGE ~p, Elapsed ~p ms",
- [Msg#aclmessage.content, Diff]}),
- object:do(Self, start);
-%%
-%%
-%%
-wait_pong_proc(Self, Event, Msg, Action) ->
- logger:log('PINGER',
- {"BAD MESSAGE ~p", [Msg#aclmessage.content]}),
- object:do(Self, start).%%
-%%
-
-%%
-start() ->
- agent:new(pingeragent, [{behaviour, pingeragent}]).
@@ -42,6 +42,6 @@ handle_cast(_Call, State) -> {noreply, State}.
handle_info(Msg, State) -> {noreply, State}.
-code_change(_, _, _) -> ok.
+code_change(_, State, _) -> {ok, State}.
terminate(_, _) -> ok.
@@ -52,6 +52,6 @@ handle_info(ping, {SelfName, DestAgent} = State) ->
{noreply, State};
handle_info(Msg, State) -> {noreply, State}.
-code_change(_, _, _) -> ok.
+code_change(_, State, _) -> {ok, State}.
terminate(_, _) -> ok.
View
@@ -1,24 +0,0 @@
-%
-% object.hrl
-%
-% ----------------------------------------------------------------------
-%
-% eXAT, an erlang eXperimental Agent Tool
-% Copyright (C) 2005-07 Corrado Santoro (csanto@diit.unict.it)
-%
-% This program is free software: you can redistribute it and/or modify
-% it under the terms of the GNU General Public License as published by
-% the Free Software Foundation, either version 3 of the License, or
-% (at your option) any later version.
-%
-% This program is distributed in the hope that it will be useful,
-% but WITHOUT ANY WARRANTY; without even the implied warranty of
-% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-% GNU General Public License for more details.
-%
-% You should have received a copy of the GNU General Public License
-% along with this program. If not, see <http://www.gnu.org/licenses/>
-%
-%
-
--record (object, {class, context, property_server, executor}).
View
@@ -25,15 +25,12 @@
-export([accept_proposal/1, ask_if/1,
call_for_proposal/1, ensure_list/1, erlang_to_sl0/1,
hexlify/1, inform/1, parse_message/1, propose/1,
- query_if/1, query_ref/1, refinepattern/2,
- refinepattern/4, refuse/1, reject_proposal/1, reply/3,
+ query_if/1, query_ref/1, refuse/1, reject_proposal/1, reply/3,
request/1, sendacl/1, sl0_getcontent/1, sl0_getslot/2,
sl0_parse/1, sl0_parsecontent/1, unhexlify/1]).
-author('csanto@diit.unict.it').
--include("object.hrl").
-
-include("acl.hrl").
-include("fipa_ontology.hrl").
@@ -146,10 +143,6 @@ ensure_list(X) -> to_list(X).
%% if the ID is an object, get the associated agent
encode_agent_identifier(Identifier)
- when is_record(Identifier, object) ->
- encode_agent_identifier(object:agentof(Identifier));
-%% if the ID is an atom (i.e. agent/process name), transform it into a string
-encode_agent_identifier(Identifier)
when is_atom(Identifier) ->
encode_agent_identifier(atom_to_list(Identifier));
%% if the ID is a string, transform it into a #agent-identifier
@@ -240,38 +233,6 @@ reply(Request, NewSpeechAct, NewContent) ->
'in-reply-to' = Request#aclmessage.'reply-with',
content = NewContent}).
-%%
-%% pattern library
-%%
-unify_atoms(?ACL_ANY, ?ACL_ANY) -> ?ACL_ANY;
-unify_atoms(A1, ?ACL_ANY) -> A1;
-unify_atoms(?ACL_ANY, A2) -> A2;
-unify_atoms(A1, A2) -> A2.
-
-unify_patterns([], [], Acc) -> lists:reverse(Acc);
-unify_patterns([H1 | T1], [H2 | T2], Acc) ->
- unify_patterns(T1, T2, [unify_atoms(H1, H2) | Acc]).
-
-refinepattern(BasePattern, RefiningPattern) ->
- LBasePattern = tuple_to_list(BasePattern),
- LRefiningPattern = tuple_to_list(RefiningPattern),
- L1 = length(LBasePattern),
- L2 = length(LRefiningPattern),
- if L1 =/= L2 ->
- exit({badarg, 'pattern lengths differ'});
- true ->
- list_to_tuple(unify_patterns(LBasePattern,
- LRefiningPattern, []))
- end.
-
-refinepattern(Object, PatternName, Index,
- RefiningPattern) ->
- PatternList = object:super(Object, pattern,
- [PatternName]),
- lists:sublist(PatternList, Index - 1) ++
- [refinepattern(lists:nth(Index, PatternList),
- RefiningPattern)]
- ++ lists:nthtail(Index, PatternList).
%%
%% acl utils
View
@@ -105,7 +105,7 @@ handle_cast(_Call, State) ->
handle_info(Msg, State) ->
{noreply, State}.
-code_change(_, _, _) -> erlang:error(notimp).
+code_change(OldVsn, State, Extra) -> {ok, State}.
terminate(_Reason, _State) ->
ok.
Oops, something went wrong.

2 comments on commit de888bf

@jnorthrup

why was this done and to what degree does this change the OO semantics in the doc/ExatManual ? was there runtime dispatch overhead to the approach?

@gleber
Owner
gleber commented on de888bf Sep 13, 2013

OO approach in Erlang is very un-ideomatic and it complicates the code too much. New approach is very-very similar to approach of gen_server. It simplifies the code a lot and fits Erlang idiomatic code standards much better.

Please sign in to comment.