-module(bt_parser_utils). -include("bt_nodes.hrl"). -export([ get_namespace/0, get_namespace/2, get_blackboard/3, get_unresolved_key_bb_map/2, throw_error_if_args_and_params_dont_match/3 ]). %% root namespace get_namespace() -> []. get_namespace(ParentNamespace, ChildName) -> ParentNamespace ++ [ChildName]. get_blackboard([], _Namespace, Blackboard) -> Blackboard; %% if reference is to an arg, then replace it with that. otherwise just whatever constant is their %% TODO: should probably do a better check for whether its an arg or some constant get_blackboard([BBKey | RestBBKeyList], Namespace, Blackboard) -> ResolvedKey = bt_blackboard:get_bb_key(Namespace, BBKey), NewBlackboard = maps:put(ResolvedKey, null, Blackboard), get_blackboard(RestBBKeyList, Namespace, NewBlackboard). %% get map from string => its bb key with namespace. used for passing BB to tree generators get_unresolved_key_bb_map(Keys, Namespace) -> get_unresolved_key_bb_map(Keys, Namespace, #{}). get_unresolved_key_bb_map([], _Namespace, Map) -> Map; get_unresolved_key_bb_map([Key | RestOfKeys], Namespace, Map) -> ResolvedKey = bt_blackboard:get_bb_key(Namespace, Key), get_unresolved_key_bb_map(RestOfKeys, Namespace, maps:put(Key, ResolvedKey, Map)). %% two types: sometimes args is map, sometimes proplist... throw_error_if_args_and_params_dont_match(NodeName, ArgsAsMap, Params) when is_map(ArgsAsMap) -> throw_error_if_args_and_params_dont_match(NodeName, maps:to_list(ArgsAsMap), Params); throw_error_if_args_and_params_dont_match(NodeName, Args, Params) -> %% args is proplist, params is list ArgKeys = proplists:get_keys(Args), Set1 = ordsets:from_list(ArgKeys), Set2 = ordsets:from_list(Params), case Set1 == Set2 of true -> ok; false -> ErrorReason = lists:flatten(io_lib:format("Args ~p do not match params ~p for node '~s'", [ordsets:to_list(Set1), ordsets:to_list(Set2), NodeName])), erlang:error(ErrorReason) end.