Current section

Files

Jump to
erlang_behaviour_trees src bt_engine erl_parser bt_erl_parser.erl
Raw

src/bt_engine/erl_parser/bt_erl_parser.erl

-module(bt_erl_parser).
-include("../bt_node_behaviours/bt_nodes.hrl").
-export([
parse/2,
parse/3
]).
%% root
-spec parse(TreeModule :: atom(), ArgsAsProplist :: list()) -> {Node :: bt_node(), NewBB :: blackboard()}.
parse(TreeModule, ArgsAsProplist) when is_list(ArgsAsProplist) ->
parse({TreeModule, maps:from_list(ArgsAsProplist)}, bt_parser_utils:get_namespace(), bt_blackboard:get_empty()).
%% sequence
-spec parse(Node :: bt_node() | {TreeModule :: atom(), Args :: map()}, Namespace :: namespace(), BB :: blackboard()) -> {Node :: bt_node(), NewBB :: blackboard()}.
parse(Sequence, Namespace, BB) when is_record(Sequence, sequence) ->
erl_parse_sequence:parse(Sequence, Namespace, BB);
%% selector (TODO)
parse(Selector, Namespace, BB) when is_record(Selector, selector) ->
erl_parse_selector:parse(Selector, Namespace, BB);
%% decorator
parse(Decorator = #decorator{child = Child}, Namespace, BB) when is_record(Decorator, decorator) ->
{NewChild, NewBB} = parse(Child, Namespace, BB),
{Decorator#decorator{child = NewChild}, NewBB};
%% task
parse(Task, Namespace, BB) when is_record(Task, task) ->
erl_parse_task:parse(Task, Namespace, BB);
%% if none of the above, then its a subtree
parse({TreeModule, Args}, Namespace, BB) when is_map(Args) ->
UnresolvedKeys = TreeModule:get_bb_keys(),
Params = TreeModule:get_params(),
NewBB = bt_parser_utils:get_blackboard(UnresolvedKeys, Namespace, BB),
bt_parser_utils:throw_error_if_args_and_params_dont_match(atom_to_list(TreeModule), Args, Params),
BBKeyMap = bt_parser_utils:get_unresolved_key_bb_map(UnresolvedKeys, Namespace),
parse(TreeModule:get_tree(Args, BBKeyMap), Namespace, NewBB).