Skip to content

Commit

Permalink
add support for binaries in sl and reformat sl.erl
Browse files Browse the repository at this point in the history
  • Loading branch information
gleber committed Jan 5, 2012
1 parent 6f7f5a4 commit 1a9ead5
Showing 1 changed file with 47 additions and 45 deletions.
92 changes: 47 additions & 45 deletions src/sl.erl
@@ -1,25 +1,25 @@
%
% sl.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/>
%
%
%%
%% sl.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/>
%%
%%
-module (sl).
%%====================================================================
%% Include files
Expand All @@ -44,21 +44,21 @@
%% Func: decode/1
%%====================================================================
decode (Message) ->
decode (Message, ascii_sl, ontology).
decode (Message, ascii_sl, ontology).

%%====================================================================
%% Func: decode/3
%%====================================================================
decode (AsciiMessage, ascii_sl, erlang_sl) ->
%%io:format ("ASCII ~s~n", [AsciiMessage]),
T = tokenize (AsciiMessage),
sl_parser:parse (T);
%%io:format ("ASCII ~s~n", [AsciiMessage]),
T = tokenize (AsciiMessage),
sl_parser:parse (T);
decode (SLMessage, erlang_sl, ontology) ->
fipa_ontology_sl_codec:decode (SLMessage);
fipa_ontology_sl_codec:decode (SLMessage);
decode (AsciiMessage, ascii_sl, ontology) ->
{ok, ErlangSL} = decode (AsciiMessage, ascii_sl, erlang_sl),
%%io:format ("~w~n", [ErlangSL]),
decode (ErlangSL, erlang_sl, ontology).
{ok, ErlangSL} = decode (AsciiMessage, ascii_sl, erlang_sl),
%%io:format ("~w~n", [ErlangSL]),
decode (ErlangSL, erlang_sl, ontology).



Expand All @@ -67,25 +67,25 @@ decode (AsciiMessage, ascii_sl, ontology) ->
%% Description: ErlangSLForm --> ASCII SL Form
%% Returns: ASCIIForm
%%====================================================================
encode (Message) ->
lists:flatten (encode (Message, isList (Message))).
encode(Message) ->
binary_to_list(iolist_to_binary(encode (Message, isList (Message)))).


encode (X, true) -> % isList
[ "(", encode_list_terms ([], X), ")" ];
[ "(", encode_list_terms ([], X), ")" ];
encode ({_, ?ACL_ANY}, false) -> % is not List
[ ];
[ ];
encode ({SlotName, SlotValue}, false) -> % is not List
[ ":", atom_to_list (SlotName), " ",
encode (SlotValue, isList (SlotValue)) , " " ];
[ ":", atom_to_list (SlotName), " ",
encode (SlotValue, isList (SlotValue)) , " " ];
encode (TermValue, false) -> % is String
[TermValue, " "].
[TermValue, " "].


encode_list_terms (Acc, []) -> lists:reverse (Acc);
encode_list_terms (Acc, [H|T]) ->
Encoding = encode (H, isList (H)),
encode_list_terms ([Encoding | Acc], T).
Encoding = encode (H, isList (H)),
encode_list_terms ([Encoding | Acc], T).


%%====================================================================
Expand All @@ -94,18 +94,18 @@ encode_list_terms (Acc, [H|T]) ->
%% Returns: Value | ?ACL_ANY
%%====================================================================
get_slot (Key, List) ->
case lists:keysearch (Key, 1, List) of
{value, {_, Value}} -> Value;
_ -> ?ACL_ANY
end.
case lists:keysearch (Key, 1, List) of
{value, {_, Value}} -> Value;
_ -> ?ACL_ANY
end.

%%====================================================================
%% Func: replace_slot/3
%% Description: changes the value of a given slot name
%% Returns: None
%%====================================================================
replace_slot (Key, List, NewValue) ->
lists:keyreplace (Key, 1, List, {Key, NewValue}).
lists:keyreplace (Key, 1, List, {Key, NewValue}).

%%====================================================================
%% Func: isList/1
Expand All @@ -114,7 +114,9 @@ replace_slot (Key, List, NewValue) ->
%%====================================================================
isList ([]) -> false;
isList ([H | _]) when is_number (H) -> false;
isList ([H | _]) when is_binary (H) -> true;
isList ([H | _]) when is_list (H) -> true;
isList (X) when is_binary (X) -> false;
isList (X) when is_tuple (X) -> false;
isList (_) -> true.

Expand All @@ -126,7 +128,7 @@ isList (_) -> true.
%%====================================================================
isString ([]) -> true;
isString ([H | T]) when is_number (H) -> isString (T);
isString (X) when is_tuple (X) -> false;
isString ([H | T]) when is_binary (H) -> isString (T);
isString (_) -> false.


Expand Down

0 comments on commit 1a9ead5

Please sign in to comment.