-module(erl_parse_sequence). -include("bt_nodes.hrl"). -export([ parse/3 ]). get_child_and_blackboard({Child, Idx}, Namespace, BB) -> IdxString = integer_to_list(Idx), ChildNamespace = bt_parser_utils:get_namespace(Namespace, IdxString), bt_erl_parser:parse(Child, ChildNamespace, BB). -spec parse(Node :: bt_node(), Namespace :: namespace(), BB :: blackboard()) -> {Node :: bt_node(), NewBB :: blackboard()}. parse(Sequence = #sequence{children = Children}, Namespace, BB) when is_record(Sequence, sequence) -> %% parse the children with their own namespaces. ChildrenBBPairs = [ get_child_and_blackboard(X, Namespace, BB) || X <- lists:zip(Children, lists:seq(1, length(Children)))], %% merge all the blackboards NewBB = lists:foldl( fun({_ChildTree, ChildBB}, Acc) -> maps:merge(ChildBB, Acc) end, BB, ChildrenBBPairs ), ChildrenTrees = [X || {X, _} <- ChildrenBBPairs], {Sequence#sequence{children = ChildrenTrees, namespace = Namespace}, NewBB}.