Current section
Files
Jump to
Current section
Files
src/bt_engine/util.erl
-module(util).
-export([to_file/1, to_tree/1]).
%% Usage:
%% ---
%% util:to_tree(parser:parse("src/samples/trees/rackloop.yml", "single_rackloop", [{"BId", 3}])).
%% io:format("~s ~n", [jsx:encode(util:to_tree(parser:parse("src/samples/trees/rackloop.yml", "single_rackloop", [{"BId", 3}])))]).
to_tree({RootNode, _}) ->
node_to_tree({<<"null">>, 0}, RootNode).
to_file(ParseResult) ->
Data = jsx:encode(to_tree(ParseResult)),
{ok, F} = file:open("treedata.js", [write]),
file:write(F, "var treeData = ["),
file:write(F, Data),
file:write(F, "];"),
file:close(F).
node_to_tree({ParentName, Depth}, {sequence, Details, Children}) ->
%io:format("adding sequence with children ~p ~n", [Children]),
Name = list_to_binary(lists:flatten(io_lib:format("(~p) ->", [Depth]))),
Map = #{name => Name, parent => ParentName},
case length(Children) of
0 -> Map;
_ -> maps:put(children, lists:map(fun(X) -> node_to_tree({Name, Depth + 1}, X) end, Children), Map)
end;
node_to_tree({ParentName, Depth}, {decorator, DecoratorName, _, _, Node}) ->
%io:format("adding decorator with children ~p ~n", [Children]),
Name = list_to_binary(lists:flatten(io_lib:format("(~p) ~p", [Depth, DecoratorName]))),
Map = #{name => Name, parent => ParentName},
maps:put(children, [node_to_tree({Name, Depth + 1}, Node)], Map);
%case length(Children) of
% 0 -> Map;
% _ -> maps:put(children, lists:map(fun(X) -> node_to_tree({Name, Depth + 1}, X) end, Children), Map)
%end;
node_to_tree({ParentName, Depth}, {task, TaskName, _, _}) ->
%io:format("adding task with name ~p ~n", [TaskName]),
Name = list_to_binary(lists:flatten(io_lib:format("(~p) ~p", [Depth, TaskName]))),
#{name => Name, parent => ParentName}.