Skip to content

Commit

Permalink
Style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rishiso committed Nov 6, 2023
1 parent 2f800ee commit b48684d
Showing 1 changed file with 122 additions and 39 deletions.
161 changes: 122 additions & 39 deletions soccer/src/soccer/strategy/agent/communication/communication_gen.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os
import re


def convert_msg_to_hpp_include(msg):
words = re.findall("[A-Z][^A-Z]*", msg[:-4])
words = list(map(lambda x: x.lower(), words))
return "#include \"rj_msgs/msg/" + "_".join(words) + ".hpp\""
return '#include "rj_msgs/msg/' + "_".join(words) + '.hpp"'


def map_message_type_to_cpp_type(msg_type):
msg_type = msg_type.strip()
Expand All @@ -15,11 +17,12 @@ def map_message_type_to_cpp_type(msg_type):
return "std::" + msg_type + ";"
elif "float64" in msg_type:
space_loc = msg_type.find(" ")
return "double " + msg_type[space_loc + 1:] + ";"
return "double " + msg_type[space_loc + 1 :] + ";"
return msg_type + ";"


def convert_individual_hpp(msg, path, msg_type):
msgName = msg[:-4] #Stripping .msg from the end
msgName = msg[:-4] # Stripping .msg from the end
hpp = "#pragma once \n\n#include <mutex>\n#include <string>\n#include <variant>\n#include <vector>\n\n#include <rj_common/time.hpp>\n#include <rj_convert/ros_convert.hpp>\n"
hpp += convert_msg_to_hpp_include(msg)
hpp += "\n\n"
Expand All @@ -37,8 +40,20 @@ def convert_individual_hpp(msg, path, msg_type):
hpp += "}\n\n"
hpp += "namespace rj_convert {\n\n"
hpp += "template <>\n"
hpp += "struct RosConverter<strategy::communication::" + msgName + ", rj_msgs::msg::" + msgName + "> {\n"
hpp += "\tstatic rj_msgs::msg::" + msgName + " to_ros(const strategy::communication::" + msgName + "& from) {\n"
hpp += (
"struct RosConverter<strategy::communication::"
+ msgName
+ ", rj_msgs::msg::"
+ msgName
+ "> {\n"
)
hpp += (
"\tstatic rj_msgs::msg::"
+ msgName
+ " to_ros(const strategy::communication::"
+ msgName
+ "& from) {\n"
)
hpp += "\t\trj_msgs::msg::" + msgName + " result;\n"
with open(path + "/" + msg) as f:
for line in f:
Expand All @@ -47,7 +62,13 @@ def convert_individual_hpp(msg, path, msg_type):
hpp += "\t\tresult." + varName + " = from." + varName + ";\n"
hpp += "\t\treturn result;\n"
hpp += "\t}\n\n"
hpp += "\tstatic strategy::communication::" + msgName + " from_ros(const rj_msgs::msg::" + msgName + "& from) {\n"
hpp += (
"\tstatic strategy::communication::"
+ msgName
+ " from_ros(const rj_msgs::msg::"
+ msgName
+ "& from) {\n"
)
hpp += "\t\treturn strategy::communication::" + msgName + "{\n"
with open(path + "/" + msg) as f:
for line in f:
Expand All @@ -57,26 +78,33 @@ def convert_individual_hpp(msg, path, msg_type):
hpp += "\t\t};\n"
hpp += "\t}\n\n"
hpp += "};\n\n"
hpp += "ASSOCIATE_CPP_ROS(strategy::communication::" + msgName + ", rj_msgs::msg::" + msgName + ");\n\n"
hpp += (
"ASSOCIATE_CPP_ROS(strategy::communication::"
+ msgName
+ ", rj_msgs::msg::"
+ msgName
+ ");\n\n"
)
hpp += "}"
return hpp


def convert_cpp(requests, responses, hpp_names):
cpp = "#include \"communication.hpp\"\n"
cpp = '#include "communication.hpp"\n'
cpp += "\nnamespace strategy::communication {\n\n"
cpp += "std::mutex request_uid_mutex;\n"
cpp += "u_int32_t request_uid = 0;\n\n"
cpp += "std::mutex response_uid_mutex;\n"
cpp += "u_int32_t response_uid = 0;\n\n"

for request in requests:
msgName = request[:-4] #Stripping .msg from the end
msgName = request[:-4] # Stripping .msg from the end
cpp += "bool operator==(const " + msgName + "& a, const " + msgName + "& b) {\n"
cpp += "\treturn a.request_uid == b.request_uid;\n"
cpp += "}\n\n"

for response in responses:
msgName = response[:-4] #Stripping .msg from the end
msgName = response[:-4] # Stripping .msg from the end
cpp += "bool operator==(const " + msgName + "& a, const " + msgName + "& b) {\n"
cpp += "\treturn a.response_uid == b.response_uid;\n"
cpp += "}\n\n"
Expand All @@ -86,7 +114,7 @@ def convert_cpp(requests, responses, hpp_names):
cpp += "}\n\n"

for request in requests:
msgName = request[:-4] #Stripping .msg from the end
msgName = request[:-4] # Stripping .msg from the end
cpp += "void generate_uid(" + msgName + "& request) {\n"
cpp += "\trequest_uid_mutex.lock();\n"
cpp += "\trequest.request_uid = request_uid;\n"
Expand All @@ -95,7 +123,7 @@ def convert_cpp(requests, responses, hpp_names):
cpp += "}\n\n"

for response in responses:
msgName = response[:-4] #Stripping .msg from the end
msgName = response[:-4] # Stripping .msg from the end
cpp += "void generate_uid(" + msgName + "& response) {\n"
cpp += "\tresponse_uid_mutex.lock();\n"
cpp += "\tresponse.response_uid = response_uid;\n"
Expand All @@ -106,11 +134,13 @@ def convert_cpp(requests, responses, hpp_names):
cpp += "}"
return cpp


def create_cpp_file(requests, responses, hpp_names):
cpp = convert_cpp(requests, responses, hpp_names)
with open("communication.cpp", "w") as f:
f.write(cpp)


def create_hpp_files(requests, requestPath, responses, responsePath):
hpp_names = []
for request in requests:
Expand All @@ -126,17 +156,18 @@ def create_hpp_files(requests, requestPath, responses, responsePath):
hpp_names.append(fileName)
with open(fileName, "w") as f:
f.write(hpp)

return hpp_names


def convert_main_hpp_file(requests_msgs, response_msgs, hpp_names):
hpp = "#pragma once\n\n#include <mutex>\n#include <string>\n#include <variant>\n#include <vector>\n\n#include <rj_common/time.hpp>\n#include <rj_convert/ros_convert.hpp>\n\n"
hpp += "#include \"rj_msgs/msg/agent_request.hpp\"\n"
hpp += "#include \"rj_msgs/msg/agent_response.hpp\"\n"
hpp += "#include \"rj_msgs/msg/agent_response_variant.hpp\"\n"
hpp += '#include "rj_msgs/msg/agent_request.hpp"\n'
hpp += '#include "rj_msgs/msg/agent_response.hpp"\n'
hpp += '#include "rj_msgs/msg/agent_response_variant.hpp"\n'

for name in hpp_names:
hpp += "#include \"" + name + "\"\n"
hpp += '#include "' + name + '"\n'

hpp += "\nnamespace strategy::communication {\n\n"
hpp += "/**\n"
Expand All @@ -162,7 +193,9 @@ def convert_main_hpp_file(requests_msgs, response_msgs, hpp_names):
hpp += "* @brief response message that is sent from the receiver of the request to the\n"
hpp += "* sender of the request with an accompanying response.\n"
hpp += "*\n"
hpp += "* The agent response is the actual thing that gets sent from a receiver back\n"
hpp += (
"* The agent response is the actual thing that gets sent from a receiver back\n"
)
hpp += "* to the sender.\n"
hpp += "*\n"
hpp += "*/\n"
Expand Down Expand Up @@ -203,7 +236,9 @@ def convert_main_hpp_file(requests_msgs, response_msgs, hpp_names):
hpp += "};\n\n"

hpp += "/**\n"
hpp += "* @brief Wraps a communication request to ensure symmetry for agent-to-agent\n"
hpp += (
"* @brief Wraps a communication request to ensure symmetry for agent-to-agent\n"
)
hpp += "* communication.\n"
hpp += "*\n"
hpp += "* Like the PosAgentResponseWrapper, this struct does nothing other than make the request\n"
Expand Down Expand Up @@ -244,11 +279,23 @@ def convert_main_hpp_file(requests_msgs, response_msgs, hpp_names):
hpp += "\t\t"
for request in requests_msgs:
msgName = convert_msg_to_hpp_include(request)[22:-5]
hpp += "if (const auto* " + msgName + " = std::get_if<strategy::communication::" + request[:-4] + ">(&from)) {\n"
hpp += "\t\t\tresult." + msgName + ".emplace_back(convert_to_ros(*" + msgName + "));\n"
hpp += (
"if (const auto* "
+ msgName
+ " = std::get_if<strategy::communication::"
+ request[:-4]
+ ">(&from)) {\n"
)
hpp += (
"\t\t\tresult."
+ msgName
+ ".emplace_back(convert_to_ros(*"
+ msgName
+ "));\n"
)
hpp += "\t\t} else "
hpp += "{\n"
hpp += "\t\t\tthrow std::runtime_error(\"Invalid variant of AgentRequest\");\n"
hpp += '\t\t\tthrow std::runtime_error("Invalid variant of AgentRequest");\n'
hpp += "\t\t}\n"
hpp += "\t\treturn result;\n"
hpp += "\t}\n\n"
Expand All @@ -261,7 +308,7 @@ def convert_main_hpp_file(requests_msgs, response_msgs, hpp_names):
hpp += "\t\t\tresult = convert_from_ros(from." + msgName + ".front());\n"
hpp += "\t\t} else "
hpp += "{\n"
hpp += "\t\t\tthrow std::runtime_error(\"Invalid variant of AgentRequest\");\n"
hpp += '\t\t\tthrow std::runtime_error("Invalid variant of AgentRequest");\n'
hpp += "\t\t}\n"
hpp += "\t\treturn result;\n"
hpp += "\t}\n\n"
Expand All @@ -272,14 +319,28 @@ def convert_main_hpp_file(requests_msgs, response_msgs, hpp_names):
hpp += "struct RosConverter<strategy::communication::AgentResponse, rj_msgs::msg::AgentResponse> {\n"
hpp += "\tstatic rj_msgs::msg::AgentResponse to_ros(const strategy::communication::AgentResponse& from) {\n"
hpp += "\t\trj_msgs::msg::AgentResponse result;\n"
hpp += "\t\tresult.associated_request = convert_to_ros(from.associated_request);\n\t\t"
hpp += (
"\t\tresult.associated_request = convert_to_ros(from.associated_request);\n\t\t"
)
for response in response_msgs:
msgName = convert_msg_to_hpp_include(response)[22:-5]
hpp += "if (const auto* " + msgName + " = std::get_if<strategy::communication::" + response[:-4] + ">(&(from.response))) {\n"
hpp += "\t\t\tresult.response." + msgName + ".emplace_back(convert_to_ros(*" + msgName + "));\n"
hpp += (
"if (const auto* "
+ msgName
+ " = std::get_if<strategy::communication::"
+ response[:-4]
+ ">(&(from.response))) {\n"
)
hpp += (
"\t\t\tresult.response."
+ msgName
+ ".emplace_back(convert_to_ros(*"
+ msgName
+ "));\n"
)
hpp += "\t\t} else "
hpp += "{\n"
hpp += "\t\t\tthrow std::runtime_error(\"Invalid variant of AgentResponse\");\n"
hpp += '\t\t\tthrow std::runtime_error("Invalid variant of AgentResponse");\n'
hpp += "\t\t}\n"
hpp += "\t\treturn result;\n"
hpp += "\t}\n\n"
Expand All @@ -290,10 +351,14 @@ def convert_main_hpp_file(requests_msgs, response_msgs, hpp_names):
for response in response_msgs:
msgName = convert_msg_to_hpp_include(response)[22:-5]
hpp += "if (!from.response." + msgName + ".empty()) {\n"
hpp += "\t\t\tresult.response = convert_from_ros(from.response." + msgName + ".front());\n"
hpp += (
"\t\t\tresult.response = convert_from_ros(from.response."
+ msgName
+ ".front());\n"
)
hpp += "\t\t} else "
hpp += "{\n"
hpp += "\t\t\tthrow std::runtime_error(\"Invalid variant of AgentResponse\");\n"
hpp += '\t\t\tthrow std::runtime_error("Invalid variant of AgentResponse");\n'
hpp += "\t\t}\n"
hpp += "\t\treturn result;\n"
hpp += "\t}\n\n"
Expand All @@ -303,11 +368,13 @@ def convert_main_hpp_file(requests_msgs, response_msgs, hpp_names):
hpp += "}"
return hpp


def create_main_hpp_file(requests_msgs, response_msgs, hpp_names):
hpp = convert_main_hpp_file(requests_msgs, response_msgs, hpp_names)
with open("communication.hpp", "w") as f:
f.write(hpp)


def update_cmake(requests_msgs, response_msgs):
with open("../../../../../../rj_msgs/CMakeLists.txt", "r") as f:
contents = f.read().split("\n")
Expand All @@ -318,28 +385,43 @@ def update_cmake(requests_msgs, response_msgs):
requests = [" request/" + request for request in requests_msgs]
responses = [" response/" + response for response in response_msgs]

contents = contents[:start] + [" # Agent Request Messages"] + requests + ["\n # Agent Response Messages"] + responses + [""] + contents[end:]

contents = (
contents[:start]
+ [" # Agent Request Messages"]
+ requests
+ ["\n # Agent Response Messages"]
+ responses
+ [""]
+ contents[end:]
)

with open("../../../../../../rj_msgs/CMakeLists.txt", "w") as f:
f.write("\n".join(contents))



def update_agent_message(requests_msgs, response_msgs):
with open("../../../../../../rj_msgs/msg/AgentRequest.msg", "r") as f:
contents = f.read().split("\n")

end = contents.index("# ACKNOWLEDGE REQUEST DOES NOT (AND SHOULD NOT) EXIST")

requests = [request[:-4] + "[<=1] " + convert_msg_to_hpp_include(request)[22:-5] for request in requests_msgs]
responses = [response[:-4] + "[<=1] " + convert_msg_to_hpp_include(response)[22:-5] for response in response_msgs]
requests = [
request[:-4] + "[<=1] " + convert_msg_to_hpp_include(request)[22:-5]
for request in requests_msgs
]
responses = [
response[:-4] + "[<=1] " + convert_msg_to_hpp_include(response)[22:-5]
for response in response_msgs
]

contents = contents[: end + 1] + requests

contents = contents[:end + 1] + requests

with open("../../../../../../rj_msgs/msg/AgentRequest.msg", "w") as f:
f.write("\n".join(contents))

with open("../../../../../../rj_msgs/msg/AgentResponseVariant.msg", "w") as f:
f.write("\n".join(responses))


if __name__ == "__main__":
path_request = "../../../../../../rj_msgs/request"
Expand All @@ -348,9 +430,10 @@ def update_agent_message(requests_msgs, response_msgs):
requests_msgs = os.listdir(path_request)
response_msgs = os.listdir(path_response)

hpp_names = create_hpp_files(requests_msgs, path_request, response_msgs, path_response)
hpp_names = create_hpp_files(
requests_msgs, path_request, response_msgs, path_response
)
create_cpp_file(requests_msgs, response_msgs, hpp_names)
create_main_hpp_file(requests_msgs, response_msgs, hpp_names)
update_cmake(requests_msgs, response_msgs)
update_agent_message(requests_msgs, response_msgs)

0 comments on commit b48684d

Please sign in to comment.