From 298cc9fc71ad8419b5fceeb7d34c849f29436492 Mon Sep 17 00:00:00 2001 From: ipa-nhg Date: Mon, 17 Feb 2020 17:13:47 +0100 Subject: [PATCH 01/17] add parameters support for the java snapshot model --- src/ros_graph_parser/core_class.py | 5 +++++ src/ros_graph_parser/snapshot.py | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/ros_graph_parser/core_class.py b/src/ros_graph_parser/core_class.py index bf227cc..0884979 100644 --- a/src/ros_graph_parser/core_class.py +++ b/src/ros_graph_parser/core_class.py @@ -112,6 +112,7 @@ def __init__(self, name=""): self.publishers = InterfaceSet() self.subscribers = InterfaceSet() self.services = InterfaceSet() + self.params = InterfaceSet() def get_namespace(self): return get_namespace(self.name) @@ -164,6 +165,7 @@ def dump_print(self): _str = _str +"\tServices:\n%s"%(self.services.str_format('\t\t')) _str = _str +"\tActionClients:\n%s"%(self.action_clients.str_format('\t\t')) _str = _str +"\tActionServers:\n%s"%(self.action_servers.str_format('\t\t')) + _str = _str +"\tParameters:\n%s"%(self.params.str_format('\t\t')) _str = _str + ("\n") print(_str) @@ -174,6 +176,7 @@ def dump_yaml(self): yaml_dict['Services'] = self.services.get_list() yaml_dict['ActionClients'] = self.action_clients.get_list() yaml_dict['ActionServers'] = self.action_servers.get_list() + yaml_dict['Parameters'] = self.params.get_list() return yaml_dict def dump_java_ros_model(self): @@ -184,6 +187,7 @@ def dump_java_ros_model(self): ros_model_str+=self.subscribers.java_format_ros_model(" ", "Subscriber", "message", "subscriber") ros_model_str+=self.action_servers.java_format_ros_model(" ", "ActionServer", "action","actionserver") ros_model_str+=self.action_clients.java_format_ros_model(" ", "ActionClient", "action","actionclient") + ros_model_str+=self.params.java_format_ros_model(" ", "Parameters", "type","param") ros_model_str+="}},\n" return ros_model_str @@ -194,6 +198,7 @@ def dump_java_system_model(self, package=""): system_model_str+=self.services.java_format_system_model(" ", "SrvServers", "Server", self.name, package, "ServiceServer") system_model_str+=self.action_servers.java_format_system_model(" ", "ActionServers", "Server", self.name, package) system_model_str+=self.action_clients.java_format_system_model(" ", "ActionClients", "Client", self.name, package) + system_model_str+=self.params.java_format_system_model(" ", "Parameters", "Param", self.name, package) system_model_str+="},\n" return system_model_str diff --git a/src/ros_graph_parser/snapshot.py b/src/ros_graph_parser/snapshot.py index 1b3b4b2..a28686b 100644 --- a/src/ros_graph_parser/snapshot.py +++ b/src/ros_graph_parser/snapshot.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import rosgraph +import rosparam import rosservice import ros_graph_parser.core_class as rg import yaml @@ -25,6 +26,7 @@ def create_ros_graph_snapshot(): params = list() services_dict = dict() topics_dict = dict() + parameter_dict = dict() if not(master.is_online()): print("Error: ROSMaster not found") @@ -45,6 +47,11 @@ def create_ros_graph_snapshot(): for service_name, _ in services: services_dict[service_name] = rosservice.get_service_type(service_name) + param_list = master.getParamNames() + for param_name in param_list: + parameter_dict[param_name]=master.getParam(param_name) + print(rosparam.get_param(param_name)) + # Get all nodes for s in state: for _, l in s: From a8980affaa5738a2a3c7ede41389a2026dfdae88 Mon Sep 17 00:00:00 2001 From: ipa-nhg Date: Tue, 18 Feb 2020 16:57:26 +0100 Subject: [PATCH 02/17] add interfaces for parameters and update java model output --- src/ros_graph_parser/core_class.py | 170 ++++++++++++++++++++++++++++- src/ros_graph_parser/snapshot.py | 9 +- 2 files changed, 172 insertions(+), 7 deletions(-) diff --git a/src/ros_graph_parser/core_class.py b/src/ros_graph_parser/core_class.py index 0884979..3739f72 100644 --- a/src/ros_graph_parser/core_class.py +++ b/src/ros_graph_parser/core_class.py @@ -103,6 +103,170 @@ def iteritems(self): def iterkeys(self): return [x.resolved for x in self] +class ParameterInterface(object): + + def __init__(self, name, value, itype): + self.resolved = name + self.namespace = get_namespace(name) + self.minimal = name[len(self.namespace)-1:] + self.value = value + self.itype = self.get_type(value) + + + def __eq__(self, other): + if self.value == other.value and self.resolved == other.resolved: + return True + else: + return False + + def get_type(self,value): + itype=type(value) + itype = (str(itype)).replace("","") + if itype == 'float': + return 'Double' + elif itype == 'bool': + return 'Boolean' + elif itype == 'int': + return 'Integer' + elif itype == 'str': + return 'String' + elif itype == 'list' or itype=='dict': + if ":" in str(value): + return 'Struc' + else: + return 'List' + else: + return itype + + def set_value(self,value,indent): + str_param_value="" + if self.itype =="String": + str_param_value+="'"+self.value+"'" + elif self.itype == "Boolean": + str_param_value+=str(self.value).lower() + elif self.itype =="List": + str_param_value+=str(self.value).replace("[","{").replace("]","}") + elif self.itype == 'Struc': + str_param_value+=self.value_struc(self.value[0],indent+" ") + else: + str_param_value+=str(value) + return str_param_value + + def get_dict(self): + return {"Value":self.value, "Name": self.resolved, + "Namespace":self.namespace, "Minimal":self.minimal} + + def str_format(self, indent=""): + return ("%sType: %s\n%sName: %s\n%sNamespace: %s\n%sMinimal: %s\n")%( + indent, self.value, indent, self.resolved, indent, + self.namespace, indent, self.minimal) + + def java_format(self, indent="", value=""): + str_param = "%sParameter { name '%s' type %s "%( + indent,self.resolved, self.itype) + if self.itype == 'Struc': + str_param += self.types_struc(self.value[0],indent) + #str_param = str_param[:-2] + if self.itype == 'List': + str_param += self.form_list(self.value) + str_param+="}" + return str_param + + def types_struc(self,struc_dict,indent): + str_param="{\n" + indent_new=indent+" " + for struc_element in struc_dict: + sub_name = struc_element + sub_value = struc_dict[struc_element] + sub_type=self.get_type(sub_value) + str_param += "%s'%s' %s"%(indent_new, sub_name, sub_type) + if sub_type == 'List': + str_param += self.form_list(sub_value) + if isinstance(sub_value,dict): + str_param += self.types_struc(struc_dict[struc_element],indent_new) + str_param += ",\n" + str_param = str_param[:-2] + str_param+="}" + indent_new="" + return str_param + + def value_struc(self,struc_dict,indent): + str_param="{\n" + indent_new=indent+" " + for struc_element in struc_dict: + sub_name = struc_element + sub_value = struc_dict[struc_element] + sub_type=self.get_type(sub_value) + str_param += "%s{ '%s' { value "%(indent_new, sub_name) + if sub_type =="String": + sub_value="'"+sub_value+"'" + if sub_type == 'List': + sub_value=str(sub_value).replace("[","{").replace("]","}").replace("{{","{").replace("}}","}") + if sub_type == "Boolean": + sub_value=str(sub_value).lower() + if isinstance(sub_value,dict): + str_param += self.value_struc(struc_dict[struc_element],indent_new) + else: + str_param += "%s}}"%(sub_value) + str_param += ",\n" + str_param = str_param[:-2] + str_param+="}" + indent_new="" + return str_param + + def form_list(self,value_in): + str_param = "{" + for i in value_in: + str_param+=self.get_type(i) + if self.get_type(i)=="List": + str_param+=self.form_list(i) + str_param+="," + str_param = str_param[:-1] + str_param+="}" + return str_param + +class ParameterSet(set): + + def str_format(self, indent=""): + str_ = "" + for elem in self: + str_ += elem.str_format(indent) + "\n" + return str_ + + def java_format_ros_model(self, indent="", value="", name_block=""): + if len(self) == 0: + return "" + str_ = ("\n%s%s {\n")%(indent, name_block) + for elem in self: + str_+= elem.java_format(indent+" ", value) +",\n" + str_ = str_[:-2] + str_+="}" + return str_ + + def java_format_system_model(self, indent="", name_type="", name_type2="", node_name="", pkg_name="", name_type3=""): + if len(self) == 0: + return "" + if not name_type3: + name_type3 = name_type2 + str_ = ("%sRos%s {\n")%(indent, name_type) + for elem in self: + str_ += ("%s Ros%s '%s' {Ref%s '%s.%s.%s.%s' value %s},\n")%( + indent, name_type3, elem.resolved, name_type2, pkg_name, node_name, node_name, elem.resolved,elem.set_value(elem.value,indent)) + str_ = str_[:-2] + str_+="}\n" + return str_ + + def get_list(self): + return [x.get_dict() for x in self] + + def __str__(self): + self.str_format() + + def iteritems(self): + return [(x.resolved, x.itype) for x in self] + + def iterkeys(self): + return [x.resolved for x in self] class Node(object): def __init__(self, name=""): @@ -112,7 +276,7 @@ def __init__(self, name=""): self.publishers = InterfaceSet() self.subscribers = InterfaceSet() self.services = InterfaceSet() - self.params = InterfaceSet() + self.params = ParameterSet() def get_namespace(self): return get_namespace(self.name) @@ -187,7 +351,7 @@ def dump_java_ros_model(self): ros_model_str+=self.subscribers.java_format_ros_model(" ", "Subscriber", "message", "subscriber") ros_model_str+=self.action_servers.java_format_ros_model(" ", "ActionServer", "action","actionserver") ros_model_str+=self.action_clients.java_format_ros_model(" ", "ActionClient", "action","actionclient") - ros_model_str+=self.params.java_format_ros_model(" ", "Parameters", "type","param") + ros_model_str+=self.params.java_format_ros_model(" ", "Parameters", "parameter") ros_model_str+="}},\n" return ros_model_str @@ -198,7 +362,7 @@ def dump_java_system_model(self, package=""): system_model_str+=self.services.java_format_system_model(" ", "SrvServers", "Server", self.name, package, "ServiceServer") system_model_str+=self.action_servers.java_format_system_model(" ", "ActionServers", "Server", self.name, package) system_model_str+=self.action_clients.java_format_system_model(" ", "ActionClients", "Client", self.name, package) - system_model_str+=self.params.java_format_system_model(" ", "Parameters", "Param", self.name, package) + system_model_str+=self.params.java_format_system_model(" ", "Parameters", "Parameter", self.name, package) system_model_str+="},\n" return system_model_str diff --git a/src/ros_graph_parser/snapshot.py b/src/ros_graph_parser/snapshot.py index a28686b..6c69fce 100644 --- a/src/ros_graph_parser/snapshot.py +++ b/src/ros_graph_parser/snapshot.py @@ -47,10 +47,6 @@ def create_ros_graph_snapshot(): for service_name, _ in services: services_dict[service_name] = rosservice.get_service_type(service_name) - param_list = master.getParamNames() - for param_name in param_list: - parameter_dict[param_name]=master.getParam(param_name) - print(rosparam.get_param(param_name)) # Get all nodes for s in state: @@ -81,6 +77,11 @@ def create_ros_graph_snapshot(): node.check_actions() nodes.append(node) + node_param = rg.Node("parameters_node") + for param_name in params: + node_param.params.add(rg.ParameterInterface(param_name,master.getParam(param_name),type(master.getParam(param_name)))) + nodes.append(node_param) + return nodes def dump_print(snapshot): From ee955c29ab992c8626961560673744914b278cf8 Mon Sep 17 00:00:00 2001 From: Harsh Deshpande Date: Thu, 20 Feb 2020 18:23:21 +0100 Subject: [PATCH 03/17] Added 2 services for ros and rossystem models & Code cleanup Signed-off-by: Harsh Deshpande --- CMakeLists.txt | 3 +- scripts/ros_node | 30 +++++------ src/ros_graph_parser/snapshot.py | 86 ++++++++++++++----------------- srv/{Scan.srv => GetROSModel.srv} | 4 +- 4 files changed, 57 insertions(+), 66 deletions(-) rename srv/{Scan.srv => GetROSModel.srv} (64%) diff --git a/CMakeLists.txt b/CMakeLists.txt index e2619ad..8e3bc09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,8 @@ catkin_python_setup() add_service_files( FILES - Scan.srv + GetROSModel.srv + GetROSSystemModel.srv ) generate_messages() diff --git a/scripts/ros_node b/scripts/ros_node index 893630e..aa73dbe 100755 --- a/scripts/ros_node +++ b/scripts/ros_node @@ -8,26 +8,24 @@ import rospy class RosGraphNode(): def __init__(self): - rospy.Service('scan_ros_graph', rg_srv.Scan, self.create_snapshot) - - def create_snapshot(self, request): - answer = rg_srv.ScanResponse() - answer.success, answer.message, answer.rosgraph = self._create_snapshot() + rospy.Service('get_ros_model', rg_srv.GetROSModel, + self.get_ros_model) + + rospy.Service('get_rossystem_model', rg_srv.GetROSSystemModel, + self.get_rossystem_model) + + def get_ros_model(self, request): + answer = rg_srv.GetROSModelResponse() + answer.success, answer.message, answer.model = rg.create_java_ros_model() return answer - def _create_snapshot(self): - try: - snapshot = rg.create_ros_graph_snapshot() - dump = dict() - dump['nodes']=dict() - for node in snapshot: - dump['nodes'][node.name] = node.dump_yaml() - return True, "Scanning Succeeded", str(dump) - except: - return False, "Scanning Failed", "" + def get_rossystem_model(self, request): + answer = rg_srv.GetROSSystemModelResponse() + answer.success, answer.message, answer.model = rg.create_java_system_model() + return answer if __name__ == "__main__": rospy.init_node("ros_graph_parser_node") RosGraphNode() - rospy.spin() \ No newline at end of file + rospy.spin() diff --git a/src/ros_graph_parser/snapshot.py b/src/ros_graph_parser/snapshot.py index 6c69fce..71f0c8c 100644 --- a/src/ros_graph_parser/snapshot.py +++ b/src/ros_graph_parser/snapshot.py @@ -11,7 +11,8 @@ BLACK_LIST_SERV = ["/set_logger_level", "/get_loggers"] BLACK_LIST_NODE = ["/rosout"] -ACTION_FILTER = ['cancel','goal','status', 'result', 'feedback'] +ACTION_FILTER = ['cancel', 'goal', 'status', 'result', 'feedback'] + def check_black_list(name, black_list): for bl_ in black_list: @@ -19,6 +20,7 @@ def check_black_list(name, black_list): return False return True + def create_ros_graph_snapshot(): master = rosgraph.Master('snapshot') node_names = list() @@ -35,18 +37,17 @@ def create_ros_graph_snapshot(): for param_name in master.getParamNames(): if param_name not in BLACK_LIST_PARAM and not(param_name.startswith('/roslaunch')): params.append(param_name) - state = master.getSystemState() #get the system state + state = master.getSystemState() # get the system state pubs, subs, services = state - #get all topics type + # get all topics type topic_list = master.getTopicTypes() for topic, topic_type in topic_list: topics_dict[topic] = topic_type - #get all service types + # get all service types for service_name, _ in services: - services_dict[service_name] = rosservice.get_service_type(service_name) - + services_dict[service_name] = rosservice.get_service_type(service_name) # Get all nodes for s in state: @@ -54,7 +55,7 @@ def create_ros_graph_snapshot(): for n in l: if n not in BLACK_LIST_NODE: node_names.append(n) - + node_names = list(set(node_names)) for n in node_names: node = rg.Node(n) @@ -83,43 +84,34 @@ def create_ros_graph_snapshot(): nodes.append(node_param) return nodes - -def dump_print(snapshot): - for node in snapshot: - node.dump_print() - -def dump_yaml(snapshot, file): - dump = dict() - dump['nodes']=dict() - for node in snapshot: - dump['nodes'][node.name] = node.dump_yaml() - with open(file, 'w') as outfile: - yaml.dump(dump, outfile, default_flow_style=False) - -def dump_java_ros_model(snapshot, ros_model_file, pkg_name): - ros_model_str = "PackageSet { package { \n" - ros_model_str+=" CatkinPackage "+pkg_name +" { " - ros_model_str+="artifact {\n" - for node in snapshot: - ros_model_str+=node.dump_java_ros_model() - ros_model_str = ros_model_str[:-2] - ros_model_str+="\n}}}}" - with open(ros_model_file, 'w') as outfile: - outfile.write(ros_model_str) - -def dump_java_system_model(snapshot, system_name, system_model_file, pkg_name): - system_model_str = "RosSystem { Name '%s'\n"%system_name - system_model_str+=" RosComponents ( \n" - for node in snapshot: - system_model_str+=node.dump_java_system_model(pkg_name) - system_model_str = system_model_str[:-2] - system_model_str+="\n)}" - with open(system_model_file, 'w') as outfile: - outfile.write(system_model_str) - -if __name__ == "__main__": - snapshot = create_ros_graph_snapshot() - dump_print(snapshot) - dump_yaml(snapshot, "ros_snapshot.yml") - dump_java_ros_model(snapshot, "dump.ros", "dummy_package") - dump_java_system_model(snapshot, "mysystem", "Mysystem.rossystem", "dummy_package") + + +def create_java_ros_model(pkg_name="dummy_pkg"): + try: + snapshot = create_ros_graph_snapshot() + ros_model_str = "PackageSet { package { \n" + ros_model_str += " CatkinPackage "+pkg_name + " { " + ros_model_str += "artifact {\n" + for node in snapshot: + ros_model_str += node.dump_java_ros_model() + ros_model_str = ros_model_str[:-2] + ros_model_str += "\n}}}}" + except: + return False, "Scanning Failed", "" + + return True, "Scanning succeeded", ros_model_str + + +def create_java_system_model(system_name="dummy_system", pkg_name="dummy_pkg"): + try: + snapshot = create_ros_graph_snapshot() + system_model_str = "RosSystem { Name '%s'\n" % system_name + system_model_str += " RosComponents ( \n" + for node in snapshot: + system_model_str += node.dump_java_system_model(pkg_name) + system_model_str = system_model_str[:-2] + system_model_str += "\n)}" + except: + return False, "Scanning Failed", "" + + return True, "Scanning succeeded", system_model_str diff --git a/srv/Scan.srv b/srv/GetROSModel.srv similarity index 64% rename from srv/Scan.srv rename to srv/GetROSModel.srv index 671734e..bf6c3fa 100644 --- a/srv/Scan.srv +++ b/srv/GetROSModel.srv @@ -1,5 +1,5 @@ - #Empty Trigger +#Empty Trigger --- bool success # indicate successful run of triggered service string message # informational, e.g. for error messages -string rosgraph # yaml format of rosgraph (pickled) \ No newline at end of file +string model # yaml format of rosgraph (pickled) \ No newline at end of file From f7cf8f000648df5b46ca2fe839d2eded79c37dde Mon Sep 17 00:00:00 2001 From: ipa-nhg Date: Tue, 25 Feb 2020 11:31:09 +0100 Subject: [PATCH 04/17] add except if can't connect to service --- src/ros_graph_parser/snapshot.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ros_graph_parser/snapshot.py b/src/ros_graph_parser/snapshot.py index 71f0c8c..9b84f9b 100644 --- a/src/ros_graph_parser/snapshot.py +++ b/src/ros_graph_parser/snapshot.py @@ -9,7 +9,7 @@ BLACK_LIST_PARAM = ['/rosdistro', '/rosversion', '/run_id'] BLACK_LIST_TOPIC = ["/tf", "/tf_static", "/rosout", "/clock"] BLACK_LIST_SERV = ["/set_logger_level", "/get_loggers"] -BLACK_LIST_NODE = ["/rosout"] +BLACK_LIST_NODE = ["/rosout","/head_cam"] ACTION_FILTER = ['cancel', 'goal', 'status', 'result', 'feedback'] @@ -47,7 +47,10 @@ def create_ros_graph_snapshot(): # get all service types for service_name, _ in services: - services_dict[service_name] = rosservice.get_service_type(service_name) + try: + services_dict[service_name] = rosservice.get_service_type(service_name) + except: + pass # Get all nodes for s in state: From b1440ada95a1be6cbd42f0e15a0a10d6f43f825d Mon Sep 17 00:00:00 2001 From: Harsh Deshpande Date: Fri, 20 Mar 2020 15:51:15 +0100 Subject: [PATCH 05/17] formatting --- src/ros_graph_parser/snapshot.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/ros_graph_parser/snapshot.py b/src/ros_graph_parser/snapshot.py index 9b84f9b..abb4e18 100644 --- a/src/ros_graph_parser/snapshot.py +++ b/src/ros_graph_parser/snapshot.py @@ -9,7 +9,8 @@ BLACK_LIST_PARAM = ['/rosdistro', '/rosversion', '/run_id'] BLACK_LIST_TOPIC = ["/tf", "/tf_static", "/rosout", "/clock"] BLACK_LIST_SERV = ["/set_logger_level", "/get_loggers"] -BLACK_LIST_NODE = ["/rosout","/head_cam"] +BLACK_LIST_NODE = ["/rosout", "/head_cam", + "/graph_monitor", "/ros_graph_parser_node"] ACTION_FILTER = ['cancel', 'goal', 'status', 'result', 'feedback'] @@ -48,7 +49,8 @@ def create_ros_graph_snapshot(): # get all service types for service_name, _ in services: try: - services_dict[service_name] = rosservice.get_service_type(service_name) + services_dict[service_name] = rosservice.get_service_type( + service_name) except: pass @@ -80,10 +82,11 @@ def create_ros_graph_snapshot(): node.check_actions() nodes.append(node) - + node_param = rg.Node("parameters_node") for param_name in params: - node_param.params.add(rg.ParameterInterface(param_name,master.getParam(param_name),type(master.getParam(param_name)))) + node_param.params.add(rg.ParameterInterface( + param_name, master.getParam(param_name), type(master.getParam(param_name)))) nodes.append(node_param) return nodes From 02ded694c2d9ecdf870843fd5be56bb1b73cfec1 Mon Sep 17 00:00:00 2001 From: ipa-nhg Date: Mon, 17 Feb 2020 17:13:47 +0100 Subject: [PATCH 06/17] add parameters support for the java snapshot model --- src/ros_graph_parser/core_class.py | 169 +++++++++++++++++++++++++++++ src/ros_graph_parser/snapshot.py | 8 ++ 2 files changed, 177 insertions(+) diff --git a/src/ros_graph_parser/core_class.py b/src/ros_graph_parser/core_class.py index bf227cc..3739f72 100644 --- a/src/ros_graph_parser/core_class.py +++ b/src/ros_graph_parser/core_class.py @@ -103,6 +103,170 @@ def iteritems(self): def iterkeys(self): return [x.resolved for x in self] +class ParameterInterface(object): + + def __init__(self, name, value, itype): + self.resolved = name + self.namespace = get_namespace(name) + self.minimal = name[len(self.namespace)-1:] + self.value = value + self.itype = self.get_type(value) + + + def __eq__(self, other): + if self.value == other.value and self.resolved == other.resolved: + return True + else: + return False + + def get_type(self,value): + itype=type(value) + itype = (str(itype)).replace("","") + if itype == 'float': + return 'Double' + elif itype == 'bool': + return 'Boolean' + elif itype == 'int': + return 'Integer' + elif itype == 'str': + return 'String' + elif itype == 'list' or itype=='dict': + if ":" in str(value): + return 'Struc' + else: + return 'List' + else: + return itype + + def set_value(self,value,indent): + str_param_value="" + if self.itype =="String": + str_param_value+="'"+self.value+"'" + elif self.itype == "Boolean": + str_param_value+=str(self.value).lower() + elif self.itype =="List": + str_param_value+=str(self.value).replace("[","{").replace("]","}") + elif self.itype == 'Struc': + str_param_value+=self.value_struc(self.value[0],indent+" ") + else: + str_param_value+=str(value) + return str_param_value + + def get_dict(self): + return {"Value":self.value, "Name": self.resolved, + "Namespace":self.namespace, "Minimal":self.minimal} + + def str_format(self, indent=""): + return ("%sType: %s\n%sName: %s\n%sNamespace: %s\n%sMinimal: %s\n")%( + indent, self.value, indent, self.resolved, indent, + self.namespace, indent, self.minimal) + + def java_format(self, indent="", value=""): + str_param = "%sParameter { name '%s' type %s "%( + indent,self.resolved, self.itype) + if self.itype == 'Struc': + str_param += self.types_struc(self.value[0],indent) + #str_param = str_param[:-2] + if self.itype == 'List': + str_param += self.form_list(self.value) + str_param+="}" + return str_param + + def types_struc(self,struc_dict,indent): + str_param="{\n" + indent_new=indent+" " + for struc_element in struc_dict: + sub_name = struc_element + sub_value = struc_dict[struc_element] + sub_type=self.get_type(sub_value) + str_param += "%s'%s' %s"%(indent_new, sub_name, sub_type) + if sub_type == 'List': + str_param += self.form_list(sub_value) + if isinstance(sub_value,dict): + str_param += self.types_struc(struc_dict[struc_element],indent_new) + str_param += ",\n" + str_param = str_param[:-2] + str_param+="}" + indent_new="" + return str_param + + def value_struc(self,struc_dict,indent): + str_param="{\n" + indent_new=indent+" " + for struc_element in struc_dict: + sub_name = struc_element + sub_value = struc_dict[struc_element] + sub_type=self.get_type(sub_value) + str_param += "%s{ '%s' { value "%(indent_new, sub_name) + if sub_type =="String": + sub_value="'"+sub_value+"'" + if sub_type == 'List': + sub_value=str(sub_value).replace("[","{").replace("]","}").replace("{{","{").replace("}}","}") + if sub_type == "Boolean": + sub_value=str(sub_value).lower() + if isinstance(sub_value,dict): + str_param += self.value_struc(struc_dict[struc_element],indent_new) + else: + str_param += "%s}}"%(sub_value) + str_param += ",\n" + str_param = str_param[:-2] + str_param+="}" + indent_new="" + return str_param + + def form_list(self,value_in): + str_param = "{" + for i in value_in: + str_param+=self.get_type(i) + if self.get_type(i)=="List": + str_param+=self.form_list(i) + str_param+="," + str_param = str_param[:-1] + str_param+="}" + return str_param + +class ParameterSet(set): + + def str_format(self, indent=""): + str_ = "" + for elem in self: + str_ += elem.str_format(indent) + "\n" + return str_ + + def java_format_ros_model(self, indent="", value="", name_block=""): + if len(self) == 0: + return "" + str_ = ("\n%s%s {\n")%(indent, name_block) + for elem in self: + str_+= elem.java_format(indent+" ", value) +",\n" + str_ = str_[:-2] + str_+="}" + return str_ + + def java_format_system_model(self, indent="", name_type="", name_type2="", node_name="", pkg_name="", name_type3=""): + if len(self) == 0: + return "" + if not name_type3: + name_type3 = name_type2 + str_ = ("%sRos%s {\n")%(indent, name_type) + for elem in self: + str_ += ("%s Ros%s '%s' {Ref%s '%s.%s.%s.%s' value %s},\n")%( + indent, name_type3, elem.resolved, name_type2, pkg_name, node_name, node_name, elem.resolved,elem.set_value(elem.value,indent)) + str_ = str_[:-2] + str_+="}\n" + return str_ + + def get_list(self): + return [x.get_dict() for x in self] + + def __str__(self): + self.str_format() + + def iteritems(self): + return [(x.resolved, x.itype) for x in self] + + def iterkeys(self): + return [x.resolved for x in self] class Node(object): def __init__(self, name=""): @@ -112,6 +276,7 @@ def __init__(self, name=""): self.publishers = InterfaceSet() self.subscribers = InterfaceSet() self.services = InterfaceSet() + self.params = ParameterSet() def get_namespace(self): return get_namespace(self.name) @@ -164,6 +329,7 @@ def dump_print(self): _str = _str +"\tServices:\n%s"%(self.services.str_format('\t\t')) _str = _str +"\tActionClients:\n%s"%(self.action_clients.str_format('\t\t')) _str = _str +"\tActionServers:\n%s"%(self.action_servers.str_format('\t\t')) + _str = _str +"\tParameters:\n%s"%(self.params.str_format('\t\t')) _str = _str + ("\n") print(_str) @@ -174,6 +340,7 @@ def dump_yaml(self): yaml_dict['Services'] = self.services.get_list() yaml_dict['ActionClients'] = self.action_clients.get_list() yaml_dict['ActionServers'] = self.action_servers.get_list() + yaml_dict['Parameters'] = self.params.get_list() return yaml_dict def dump_java_ros_model(self): @@ -184,6 +351,7 @@ def dump_java_ros_model(self): ros_model_str+=self.subscribers.java_format_ros_model(" ", "Subscriber", "message", "subscriber") ros_model_str+=self.action_servers.java_format_ros_model(" ", "ActionServer", "action","actionserver") ros_model_str+=self.action_clients.java_format_ros_model(" ", "ActionClient", "action","actionclient") + ros_model_str+=self.params.java_format_ros_model(" ", "Parameters", "parameter") ros_model_str+="}},\n" return ros_model_str @@ -194,6 +362,7 @@ def dump_java_system_model(self, package=""): system_model_str+=self.services.java_format_system_model(" ", "SrvServers", "Server", self.name, package, "ServiceServer") system_model_str+=self.action_servers.java_format_system_model(" ", "ActionServers", "Server", self.name, package) system_model_str+=self.action_clients.java_format_system_model(" ", "ActionClients", "Client", self.name, package) + system_model_str+=self.params.java_format_system_model(" ", "Parameters", "Parameter", self.name, package) system_model_str+="},\n" return system_model_str diff --git a/src/ros_graph_parser/snapshot.py b/src/ros_graph_parser/snapshot.py index 1b3b4b2..6c69fce 100644 --- a/src/ros_graph_parser/snapshot.py +++ b/src/ros_graph_parser/snapshot.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import rosgraph +import rosparam import rosservice import ros_graph_parser.core_class as rg import yaml @@ -25,6 +26,7 @@ def create_ros_graph_snapshot(): params = list() services_dict = dict() topics_dict = dict() + parameter_dict = dict() if not(master.is_online()): print("Error: ROSMaster not found") @@ -45,6 +47,7 @@ def create_ros_graph_snapshot(): for service_name, _ in services: services_dict[service_name] = rosservice.get_service_type(service_name) + # Get all nodes for s in state: for _, l in s: @@ -74,6 +77,11 @@ def create_ros_graph_snapshot(): node.check_actions() nodes.append(node) + node_param = rg.Node("parameters_node") + for param_name in params: + node_param.params.add(rg.ParameterInterface(param_name,master.getParam(param_name),type(master.getParam(param_name)))) + nodes.append(node_param) + return nodes def dump_print(snapshot): From cd6df7782d6aae69d0597cf1a5f9a88033e314e3 Mon Sep 17 00:00:00 2001 From: Harsh Deshpande Date: Thu, 20 Feb 2020 18:23:21 +0100 Subject: [PATCH 07/17] Added 2 services for ros and rossystem models & Code cleanup Signed-off-by: Harsh Deshpande --- CMakeLists.txt | 3 +- scripts/ros_node | 30 +++++----- src/ros_graph_parser/snapshot.py | 91 +++++++++++++++---------------- srv/{Scan.srv => GetROSModel.srv} | 4 +- srv/GetROSSystemModel.srv | 5 ++ 5 files changed, 66 insertions(+), 67 deletions(-) rename srv/{Scan.srv => GetROSModel.srv} (64%) create mode 100644 srv/GetROSSystemModel.srv diff --git a/CMakeLists.txt b/CMakeLists.txt index e2619ad..8e3bc09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,8 @@ catkin_python_setup() add_service_files( FILES - Scan.srv + GetROSModel.srv + GetROSSystemModel.srv ) generate_messages() diff --git a/scripts/ros_node b/scripts/ros_node index 893630e..aa73dbe 100755 --- a/scripts/ros_node +++ b/scripts/ros_node @@ -8,26 +8,24 @@ import rospy class RosGraphNode(): def __init__(self): - rospy.Service('scan_ros_graph', rg_srv.Scan, self.create_snapshot) - - def create_snapshot(self, request): - answer = rg_srv.ScanResponse() - answer.success, answer.message, answer.rosgraph = self._create_snapshot() + rospy.Service('get_ros_model', rg_srv.GetROSModel, + self.get_ros_model) + + rospy.Service('get_rossystem_model', rg_srv.GetROSSystemModel, + self.get_rossystem_model) + + def get_ros_model(self, request): + answer = rg_srv.GetROSModelResponse() + answer.success, answer.message, answer.model = rg.create_java_ros_model() return answer - def _create_snapshot(self): - try: - snapshot = rg.create_ros_graph_snapshot() - dump = dict() - dump['nodes']=dict() - for node in snapshot: - dump['nodes'][node.name] = node.dump_yaml() - return True, "Scanning Succeeded", str(dump) - except: - return False, "Scanning Failed", "" + def get_rossystem_model(self, request): + answer = rg_srv.GetROSSystemModelResponse() + answer.success, answer.message, answer.model = rg.create_java_system_model() + return answer if __name__ == "__main__": rospy.init_node("ros_graph_parser_node") RosGraphNode() - rospy.spin() \ No newline at end of file + rospy.spin() diff --git a/src/ros_graph_parser/snapshot.py b/src/ros_graph_parser/snapshot.py index 6c69fce..9b84f9b 100644 --- a/src/ros_graph_parser/snapshot.py +++ b/src/ros_graph_parser/snapshot.py @@ -9,9 +9,10 @@ BLACK_LIST_PARAM = ['/rosdistro', '/rosversion', '/run_id'] BLACK_LIST_TOPIC = ["/tf", "/tf_static", "/rosout", "/clock"] BLACK_LIST_SERV = ["/set_logger_level", "/get_loggers"] -BLACK_LIST_NODE = ["/rosout"] +BLACK_LIST_NODE = ["/rosout","/head_cam"] + +ACTION_FILTER = ['cancel', 'goal', 'status', 'result', 'feedback'] -ACTION_FILTER = ['cancel','goal','status', 'result', 'feedback'] def check_black_list(name, black_list): for bl_ in black_list: @@ -19,6 +20,7 @@ def check_black_list(name, black_list): return False return True + def create_ros_graph_snapshot(): master = rosgraph.Master('snapshot') node_names = list() @@ -35,18 +37,20 @@ def create_ros_graph_snapshot(): for param_name in master.getParamNames(): if param_name not in BLACK_LIST_PARAM and not(param_name.startswith('/roslaunch')): params.append(param_name) - state = master.getSystemState() #get the system state + state = master.getSystemState() # get the system state pubs, subs, services = state - #get all topics type + # get all topics type topic_list = master.getTopicTypes() for topic, topic_type in topic_list: topics_dict[topic] = topic_type - #get all service types + # get all service types for service_name, _ in services: - services_dict[service_name] = rosservice.get_service_type(service_name) - + try: + services_dict[service_name] = rosservice.get_service_type(service_name) + except: + pass # Get all nodes for s in state: @@ -54,7 +58,7 @@ def create_ros_graph_snapshot(): for n in l: if n not in BLACK_LIST_NODE: node_names.append(n) - + node_names = list(set(node_names)) for n in node_names: node = rg.Node(n) @@ -83,43 +87,34 @@ def create_ros_graph_snapshot(): nodes.append(node_param) return nodes - -def dump_print(snapshot): - for node in snapshot: - node.dump_print() - -def dump_yaml(snapshot, file): - dump = dict() - dump['nodes']=dict() - for node in snapshot: - dump['nodes'][node.name] = node.dump_yaml() - with open(file, 'w') as outfile: - yaml.dump(dump, outfile, default_flow_style=False) - -def dump_java_ros_model(snapshot, ros_model_file, pkg_name): - ros_model_str = "PackageSet { package { \n" - ros_model_str+=" CatkinPackage "+pkg_name +" { " - ros_model_str+="artifact {\n" - for node in snapshot: - ros_model_str+=node.dump_java_ros_model() - ros_model_str = ros_model_str[:-2] - ros_model_str+="\n}}}}" - with open(ros_model_file, 'w') as outfile: - outfile.write(ros_model_str) - -def dump_java_system_model(snapshot, system_name, system_model_file, pkg_name): - system_model_str = "RosSystem { Name '%s'\n"%system_name - system_model_str+=" RosComponents ( \n" - for node in snapshot: - system_model_str+=node.dump_java_system_model(pkg_name) - system_model_str = system_model_str[:-2] - system_model_str+="\n)}" - with open(system_model_file, 'w') as outfile: - outfile.write(system_model_str) - -if __name__ == "__main__": - snapshot = create_ros_graph_snapshot() - dump_print(snapshot) - dump_yaml(snapshot, "ros_snapshot.yml") - dump_java_ros_model(snapshot, "dump.ros", "dummy_package") - dump_java_system_model(snapshot, "mysystem", "Mysystem.rossystem", "dummy_package") + + +def create_java_ros_model(pkg_name="dummy_pkg"): + try: + snapshot = create_ros_graph_snapshot() + ros_model_str = "PackageSet { package { \n" + ros_model_str += " CatkinPackage "+pkg_name + " { " + ros_model_str += "artifact {\n" + for node in snapshot: + ros_model_str += node.dump_java_ros_model() + ros_model_str = ros_model_str[:-2] + ros_model_str += "\n}}}}" + except: + return False, "Scanning Failed", "" + + return True, "Scanning succeeded", ros_model_str + + +def create_java_system_model(system_name="dummy_system", pkg_name="dummy_pkg"): + try: + snapshot = create_ros_graph_snapshot() + system_model_str = "RosSystem { Name '%s'\n" % system_name + system_model_str += " RosComponents ( \n" + for node in snapshot: + system_model_str += node.dump_java_system_model(pkg_name) + system_model_str = system_model_str[:-2] + system_model_str += "\n)}" + except: + return False, "Scanning Failed", "" + + return True, "Scanning succeeded", system_model_str diff --git a/srv/Scan.srv b/srv/GetROSModel.srv similarity index 64% rename from srv/Scan.srv rename to srv/GetROSModel.srv index 671734e..bf6c3fa 100644 --- a/srv/Scan.srv +++ b/srv/GetROSModel.srv @@ -1,5 +1,5 @@ - #Empty Trigger +#Empty Trigger --- bool success # indicate successful run of triggered service string message # informational, e.g. for error messages -string rosgraph # yaml format of rosgraph (pickled) \ No newline at end of file +string model # yaml format of rosgraph (pickled) \ No newline at end of file diff --git a/srv/GetROSSystemModel.srv b/srv/GetROSSystemModel.srv new file mode 100644 index 0000000..bf6c3fa --- /dev/null +++ b/srv/GetROSSystemModel.srv @@ -0,0 +1,5 @@ +#Empty Trigger +--- +bool success # indicate successful run of triggered service +string message # informational, e.g. for error messages +string model # yaml format of rosgraph (pickled) \ No newline at end of file From 2b3458c4f5569441b86cbd8dbf65fa3fa8c9b145 Mon Sep 17 00:00:00 2001 From: ipa-nhg Date: Fri, 20 Mar 2020 14:58:01 +0100 Subject: [PATCH 08/17] readded deleted modules and avoid duplicated code --- src/ros_graph_parser/snapshot.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/ros_graph_parser/snapshot.py b/src/ros_graph_parser/snapshot.py index 9b84f9b..15c40ee 100644 --- a/src/ros_graph_parser/snapshot.py +++ b/src/ros_graph_parser/snapshot.py @@ -87,7 +87,28 @@ def create_ros_graph_snapshot(): nodes.append(node_param) return nodes - + +def dump_print(snapshot): + for node in snapshot: + node.dump_print() + +def dump_yaml(snapshot, file): + dump = dict() + dump['nodes']=dict() + for node in snapshot: + dump['nodes'][node.name] = node.dump_yaml() + with open(file, 'w') as outfile: + yaml.dump(dump, outfile, default_flow_style=False) + +def dump_java_ros_model(snapshot, ros_model_file, pkg_name): + sucess, text_msg, ros_model_str = create_java_ros_model(pkg_name) + with open(ros_model_file, 'w') as outfile: + outfile.write(ros_model_str) + +def dump_java_system_model(snapshot, system_name, system_model_file, pkg_name): + sucess, text_msg, system_model_str = create_java_system_model(system_name, pkg_name) + with open(system_model_file, 'w') as outfile: + outfile.write(system_model_str) def create_java_ros_model(pkg_name="dummy_pkg"): try: @@ -101,7 +122,6 @@ def create_java_ros_model(pkg_name="dummy_pkg"): ros_model_str += "\n}}}}" except: return False, "Scanning Failed", "" - return True, "Scanning succeeded", ros_model_str @@ -118,3 +138,10 @@ def create_java_system_model(system_name="dummy_system", pkg_name="dummy_pkg"): return False, "Scanning Failed", "" return True, "Scanning succeeded", system_model_str + +if __name__ == "__main__": + snapshot = create_ros_graph_snapshot() + dump_print(snapshot) + dump_yaml(snapshot, "ros_snapshot.yml") + dump_java_ros_model(snapshot, "dump.ros", "dummy_package") + dump_java_system_model(snapshot, "mysystem", "Mysystem.rossystem", "dummy_package") From 56d66b39af1cbe97b25e9f99aaf54b5e16d7ff04 Mon Sep 17 00:00:00 2001 From: ipa-nhg Date: Mon, 23 Mar 2020 16:54:04 +0100 Subject: [PATCH 09/17] [Fix]the Rossystem generated file missed 2 close braces ("}}") for structs of structs parameters --- src/ros_graph_parser/core_class.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ros_graph_parser/core_class.py b/src/ros_graph_parser/core_class.py index 3739f72..8800bae 100644 --- a/src/ros_graph_parser/core_class.py +++ b/src/ros_graph_parser/core_class.py @@ -147,7 +147,7 @@ def set_value(self,value,indent): elif self.itype =="List": str_param_value+=str(self.value).replace("[","{").replace("]","}") elif self.itype == 'Struc': - str_param_value+=self.value_struc(self.value[0],indent+" ") + str_param_value+=self.value_struc(self.value[0],indent+" ","") else: str_param_value+=str(value) return str_param_value @@ -190,7 +190,7 @@ def types_struc(self,struc_dict,indent): indent_new="" return str_param - def value_struc(self,struc_dict,indent): + def value_struc(self,struc_dict,indent,string_end): str_param="{\n" indent_new=indent+" " for struc_element in struc_dict: @@ -205,12 +205,12 @@ def value_struc(self,struc_dict,indent): if sub_type == "Boolean": sub_value=str(sub_value).lower() if isinstance(sub_value,dict): - str_param += self.value_struc(struc_dict[struc_element],indent_new) + str_param += self.value_struc(struc_dict[struc_element],indent_new,"}}") else: str_param += "%s}}"%(sub_value) str_param += ",\n" str_param = str_param[:-2] - str_param+="}" + str_param+="}%s"%(string_end) indent_new="" return str_param From aeec92a1fbaf322774978ea5154a087ac9248f43 Mon Sep 17 00:00:00 2001 From: ipa-nhg Date: Tue, 24 Mar 2020 08:14:15 +0100 Subject: [PATCH 10/17] [Fix] syntax ros actions definition for the java snapshot method --- src/ros_graph_parser/core_class.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ros_graph_parser/core_class.py b/src/ros_graph_parser/core_class.py index 8800bae..5f8c170 100644 --- a/src/ros_graph_parser/core_class.py +++ b/src/ros_graph_parser/core_class.py @@ -360,8 +360,8 @@ def dump_java_system_model(self, package=""): system_model_str+=self.publishers.java_format_system_model(" ", "Publishers", "Publisher", self.name, package) system_model_str+=self.subscribers.java_format_system_model(" ", "Subscribers", "Subscriber",self.name, package) system_model_str+=self.services.java_format_system_model(" ", "SrvServers", "Server", self.name, package, "ServiceServer") - system_model_str+=self.action_servers.java_format_system_model(" ", "ActionServers", "Server", self.name, package) - system_model_str+=self.action_clients.java_format_system_model(" ", "ActionClients", "Client", self.name, package) + system_model_str+=self.action_servers.java_format_system_model(" ", "ActionServers", "Server", self.name, package, "ActionServer") + system_model_str+=self.action_clients.java_format_system_model(" ", "ActionClients", "Client", self.name, package, "ActionClient") system_model_str+=self.params.java_format_system_model(" ", "Parameters", "Parameter", self.name, package) system_model_str+="},\n" return system_model_str From 2f724bdc328f2766728bd761c01ea8e705dc3502 Mon Sep 17 00:00:00 2001 From: Harsh Deshpande Date: Sat, 18 Apr 2020 22:06:15 +0200 Subject: [PATCH 11/17] Fix braces for nested params --- src/ros_graph_parser/core_class.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ros_graph_parser/core_class.py b/src/ros_graph_parser/core_class.py index 0f83332..3bc4520 100644 --- a/src/ros_graph_parser/core_class.py +++ b/src/ros_graph_parser/core_class.py @@ -116,6 +116,7 @@ def __init__(self, name, value, itype): self.minimal = name[len(self.namespace)-1:] self.value = value self.itype = self.get_type(value) + self.count = 0 def __eq__(self, other): if self.value == other.value and self.resolved == other.resolved: @@ -214,11 +215,15 @@ def value_struc(self, struc_dict, indent): if isinstance(sub_value, dict): str_param += self.value_struc( struc_dict[struc_element], indent_new) + self.count = self.count + 1 else: str_param += "%s}}" % (sub_value) str_param += ",\n" str_param = str_param[:-2] str_param += "}" + if self.count == 1: + str_param += "}}" + self.count = self.count - 1 indent_new = "" return str_param From 48b0618e75bbe2d0778296936e9a818f3aafd4be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nadia=20Hammoudeh=20Garc=C3=ADa?= Date: Mon, 20 Apr 2020 10:59:54 +0200 Subject: [PATCH 12/17] remove application(cob4-3) specific configuration --- src/ros_graph_parser/snapshot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ros_graph_parser/snapshot.py b/src/ros_graph_parser/snapshot.py index 5e90804..d6a594e 100644 --- a/src/ros_graph_parser/snapshot.py +++ b/src/ros_graph_parser/snapshot.py @@ -9,7 +9,7 @@ BLACK_LIST_PARAM = ['/rosdistro', '/rosversion', '/run_id'] BLACK_LIST_TOPIC = ["/tf", "/tf_static", "/rosout", "/clock"] BLACK_LIST_SERV = ["/set_logger_level", "/get_loggers"] -BLACK_LIST_NODE = ["/rosout", "/head_cam"] +BLACK_LIST_NODE = ["/rosout"] ACTION_FILTER = ['cancel', 'goal', 'status', 'result', 'feedback'] From 8a22a9f37597d3abfd4e3d7ecfbcba253a0cbd2f Mon Sep 17 00:00:00 2001 From: ipa-nhg Date: Tue, 1 Sep 2020 11:48:19 +0200 Subject: [PATCH 13/17] Adaptation for the cob4 experiments --- src/ros_graph_parser/core_class.py | 4 ++-- src/ros_graph_parser/snapshot.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ros_graph_parser/core_class.py b/src/ros_graph_parser/core_class.py index 3bc4520..e74646f 100644 --- a/src/ros_graph_parser/core_class.py +++ b/src/ros_graph_parser/core_class.py @@ -390,9 +390,9 @@ def dump_java_system_model(self, package=""): system_model_str += self.services.java_format_system_model( " ", "SrvServers", "Server", self.name, package, "ServiceServer") system_model_str += self.action_servers.java_format_system_model( - " ", "ActionServers", "Server", self.name, package) + " ", "ActionServers", "ActionServer", self.name, package) system_model_str += self.action_clients.java_format_system_model( - " ", "ActionClients", "Client", self.name, package) + " ", "ActionClients", "ActionClient", self.name, package) system_model_str += self.params.java_format_system_model( " ", "Parameters", "Parameter", self.name, package) system_model_str += "},\n" diff --git a/src/ros_graph_parser/snapshot.py b/src/ros_graph_parser/snapshot.py index d6a594e..5a9e9e0 100644 --- a/src/ros_graph_parser/snapshot.py +++ b/src/ros_graph_parser/snapshot.py @@ -6,7 +6,7 @@ import ros_graph_parser.core_class as rg import yaml -BLACK_LIST_PARAM = ['/rosdistro', '/rosversion', '/run_id'] +BLACK_LIST_PARAM = ['/rosdistro', '/rosversion', '/run_id','robot_description','/docker_control/stations','/docking_laser_filter/scan_filter_chain','/bms/diagnostics','/station_detector/stations','/scan_unifier_filter/scan_filter_chain'] BLACK_LIST_TOPIC = ["/tf", "/tf_static", "/rosout", "/clock"] BLACK_LIST_SERV = ["/set_logger_level", "/get_loggers"] BLACK_LIST_NODE = ["/rosout"] From 07badb967cb91e18c36db67d395c2749c18b2754 Mon Sep 17 00:00:00 2001 From: ipa-nhg Date: Fri, 15 Jan 2021 15:55:28 +0100 Subject: [PATCH 14/17] add service clients to the model parser --- CMakeLists.txt | 4 ---- src/ros_graph_parser/core_class.py | 13 ++++++++----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e3bc09..b080318 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,10 +19,6 @@ catkin_package( CATKIN_DEPENDS message_runtime ) -install(DIRECTORY launch - DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} -) - catkin_install_python(PROGRAMS scripts/ros_node scripts/print_snapshot scripts/java_snapshot diff --git a/src/ros_graph_parser/core_class.py b/src/ros_graph_parser/core_class.py index e74646f..cd12949 100644 --- a/src/ros_graph_parser/core_class.py +++ b/src/ros_graph_parser/core_class.py @@ -290,7 +290,8 @@ def __init__(self, name=""): self.action_servers = InterfaceSet() self.publishers = InterfaceSet() self.subscribers = InterfaceSet() - self.services = InterfaceSet() + self.service_clients = InterfaceSet() + self.service_servers = InterfaceSet() self.params = ParameterSet() def get_namespace(self): @@ -343,7 +344,7 @@ def dump_print(self): "\tPublishers:\n%s" % (self.publishers.str_format('\t\t')) _str = _str + \ "\tSubscribers:\n%s" % (self.subscribers.str_format('\t\t')) - _str = _str + "\tServices:\n%s" % (self.services.str_format('\t\t')) + _str = _str + "\tServices:\n%s" % (self.service_servers.str_format('\t\t')) _str = _str + \ "\tActionClients:\n%s" % (self.action_clients.str_format('\t\t')) _str = _str + \ @@ -356,7 +357,7 @@ def dump_yaml(self): yaml_dict = dict() yaml_dict['Publishers'] = self.publishers.get_list() yaml_dict['Subscribers'] = self.subscribers.get_list() - yaml_dict['Services'] = self.services.get_list() + yaml_dict['Services'] = self.service_servers.get_list() yaml_dict['ActionClients'] = self.action_clients.get_list() yaml_dict['ActionServers'] = self.action_servers.get_list() yaml_dict['Parameters'] = self.params.get_list() @@ -365,8 +366,10 @@ def dump_yaml(self): def dump_java_ros_model(self): ros_model_str = " Artifact "+self.name+" {\n" ros_model_str += " node Node { name " + self.name+"\n" - ros_model_str += self.services.java_format_ros_model( + ros_model_str += self.service_servers.java_format_ros_model( " ", "ServiceServer", "service", "serviceserver") + ros_model_str += self.service_clients.java_format_ros_model( + " ", "ServiceClients", "service", "serviceclient") ros_model_str += self.publishers.java_format_ros_model( " ", "Publisher", "message", "publisher") ros_model_str += self.subscribers.java_format_ros_model( @@ -387,7 +390,7 @@ def dump_java_system_model(self, package=""): " ", "Publishers", "Publisher", self.name, package) system_model_str += self.subscribers.java_format_system_model( " ", "Subscribers", "Subscriber", self.name, package) - system_model_str += self.services.java_format_system_model( + system_model_str += self.service_servers.java_format_system_model( " ", "SrvServers", "Server", self.name, package, "ServiceServer") system_model_str += self.action_servers.java_format_system_model( " ", "ActionServers", "ActionServer", self.name, package) From f811edcb4ebdbe8c70a008a9f64c6df927451a0c Mon Sep 17 00:00:00 2001 From: ipa-nhg Date: Fri, 15 Jan 2021 15:55:28 +0100 Subject: [PATCH 15/17] add service clients to the model parser --- CMakeLists.txt | 4 ---- src/ros_graph_parser/core_class.py | 13 ++++++++----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e3bc09..b080318 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,10 +19,6 @@ catkin_package( CATKIN_DEPENDS message_runtime ) -install(DIRECTORY launch - DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} -) - catkin_install_python(PROGRAMS scripts/ros_node scripts/print_snapshot scripts/java_snapshot diff --git a/src/ros_graph_parser/core_class.py b/src/ros_graph_parser/core_class.py index e74646f..cd12949 100644 --- a/src/ros_graph_parser/core_class.py +++ b/src/ros_graph_parser/core_class.py @@ -290,7 +290,8 @@ def __init__(self, name=""): self.action_servers = InterfaceSet() self.publishers = InterfaceSet() self.subscribers = InterfaceSet() - self.services = InterfaceSet() + self.service_clients = InterfaceSet() + self.service_servers = InterfaceSet() self.params = ParameterSet() def get_namespace(self): @@ -343,7 +344,7 @@ def dump_print(self): "\tPublishers:\n%s" % (self.publishers.str_format('\t\t')) _str = _str + \ "\tSubscribers:\n%s" % (self.subscribers.str_format('\t\t')) - _str = _str + "\tServices:\n%s" % (self.services.str_format('\t\t')) + _str = _str + "\tServices:\n%s" % (self.service_servers.str_format('\t\t')) _str = _str + \ "\tActionClients:\n%s" % (self.action_clients.str_format('\t\t')) _str = _str + \ @@ -356,7 +357,7 @@ def dump_yaml(self): yaml_dict = dict() yaml_dict['Publishers'] = self.publishers.get_list() yaml_dict['Subscribers'] = self.subscribers.get_list() - yaml_dict['Services'] = self.services.get_list() + yaml_dict['Services'] = self.service_servers.get_list() yaml_dict['ActionClients'] = self.action_clients.get_list() yaml_dict['ActionServers'] = self.action_servers.get_list() yaml_dict['Parameters'] = self.params.get_list() @@ -365,8 +366,10 @@ def dump_yaml(self): def dump_java_ros_model(self): ros_model_str = " Artifact "+self.name+" {\n" ros_model_str += " node Node { name " + self.name+"\n" - ros_model_str += self.services.java_format_ros_model( + ros_model_str += self.service_servers.java_format_ros_model( " ", "ServiceServer", "service", "serviceserver") + ros_model_str += self.service_clients.java_format_ros_model( + " ", "ServiceClients", "service", "serviceclient") ros_model_str += self.publishers.java_format_ros_model( " ", "Publisher", "message", "publisher") ros_model_str += self.subscribers.java_format_ros_model( @@ -387,7 +390,7 @@ def dump_java_system_model(self, package=""): " ", "Publishers", "Publisher", self.name, package) system_model_str += self.subscribers.java_format_system_model( " ", "Subscribers", "Subscriber", self.name, package) - system_model_str += self.services.java_format_system_model( + system_model_str += self.service_servers.java_format_system_model( " ", "SrvServers", "Server", self.name, package, "ServiceServer") system_model_str += self.action_servers.java_format_system_model( " ", "ActionServers", "ActionServer", self.name, package) From 1b0e487aa2057f1e8378a2c24793058edac9b035 Mon Sep 17 00:00:00 2001 From: ipa-nhg Date: Tue, 9 Mar 2021 10:04:52 +0100 Subject: [PATCH 16/17] update the python API to support the new models format see https://github.com/ipa320/ros-model/pull/125 --- src/ros_graph_parser/core_class.py | 50 +++++++++++++++--------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/ros_graph_parser/core_class.py b/src/ros_graph_parser/core_class.py index cd12949..f3e3bb3 100644 --- a/src/ros_graph_parser/core_class.py +++ b/src/ros_graph_parser/core_class.py @@ -137,7 +137,7 @@ def get_type(self, value): return 'String' elif itype == 'list' or itype == 'dict': if ":" in str(value): - return 'Struc' + return 'Struct' else: return 'List' else: @@ -152,8 +152,8 @@ def set_value(self, value, indent): elif self.itype == "List": str_param_value += str(self.value).replace( "[", "{").replace("]", "}") - elif self.itype == 'Struc': - str_param_value += self.value_struc(self.value[0], indent+" ") + elif self.itype == 'Struct': + str_param_value += self.value_struct(self.value[0], indent+" ") else: str_param_value += str(value) return str_param_value @@ -170,39 +170,39 @@ def str_format(self, indent=""): def java_format(self, indent="", value=""): str_param = "%sParameter { name '%s' type %s " % ( indent, self.resolved, self.itype) - if self.itype == 'Struc': - str_param += self.types_struc(self.value[0], indent) + if self.itype == 'Struct': + str_param += self.types_struct(self.value[0], indent) #str_param = str_param[:-2] if self.itype == 'List': str_param += self.form_list(self.value) str_param += "}" return str_param - def types_struc(self, struc_dict, indent): + def types_struct(self, struct_dict, indent): str_param = "{\n" indent_new = indent+" " - for struc_element in struc_dict: - sub_name = struc_element - sub_value = struc_dict[struc_element] + for struct_element in struct_dict: + sub_name = struct_element + sub_value = struct_dict[struct_element] sub_type = self.get_type(sub_value) str_param += "%s'%s' %s" % (indent_new, sub_name, sub_type) if sub_type == 'List': str_param += self.form_list(sub_value) if isinstance(sub_value, dict): - str_param += self.types_struc( - struc_dict[struc_element], indent_new) + str_param += self.types_struct( + struct_dict[struct_element], indent_new) str_param += ",\n" str_param = str_param[:-2] str_param += "}" indent_new = "" return str_param - def value_struc(self, struc_dict, indent): + def value_struct(self, struct_dict, indent): str_param = "{\n" indent_new = indent+" " - for struc_element in struc_dict: - sub_name = struc_element - sub_value = struc_dict[struc_element] + for struct_element in struct_dict: + sub_name = struct_element + sub_value = struct_dict[struct_element] sub_type = self.get_type(sub_value) str_param += "%s{ '%s' { value " % (indent_new, sub_name) if sub_type == "String": @@ -213,8 +213,8 @@ def value_struc(self, struc_dict, indent): if sub_type == "Boolean": sub_value = str(sub_value).lower() if isinstance(sub_value, dict): - str_param += self.value_struc( - struc_dict[struc_element], indent_new) + str_param += self.value_struct( + struct_dict[struct_element], indent_new) self.count = self.count + 1 else: str_param += "%s}}" % (sub_value) @@ -365,21 +365,21 @@ def dump_yaml(self): def dump_java_ros_model(self): ros_model_str = " Artifact "+self.name+" {\n" - ros_model_str += " node Node { name " + self.name+"\n" + ros_model_str += " Node { name " + self.name ros_model_str += self.service_servers.java_format_ros_model( - " ", "ServiceServer", "service", "serviceserver") + " ", "ServiceServer", "service", "ServiceServers") ros_model_str += self.service_clients.java_format_ros_model( - " ", "ServiceClients", "service", "serviceclient") + " ", "ServiceClients", "service", "ServiceClients") ros_model_str += self.publishers.java_format_ros_model( - " ", "Publisher", "message", "publisher") + " ", "Publisher", "message", "Publishers") ros_model_str += self.subscribers.java_format_ros_model( - " ", "Subscriber", "message", "subscriber") + " ", "Subscriber", "message", "Subscribers") ros_model_str += self.action_servers.java_format_ros_model( - " ", "ActionServer", "action", "actionserver") + " ", "ActionServer", "action", "ActionServers") ros_model_str += self.action_clients.java_format_ros_model( - " ", "ActionClient", "action", "actionclient") + " ", "ActionClient", "action", "ActionClients") ros_model_str += self.params.java_format_ros_model( - " ", "Parameters", "parameter") + " ", "Parameters", "Parameters") ros_model_str += "}},\n" return ros_model_str From 6e134741076d45faa6ed40d39d8a9d35c95bed42 Mon Sep 17 00:00:00 2001 From: Harsh Deshpande Date: Tue, 13 Apr 2021 10:17:37 +0200 Subject: [PATCH 17/17] moved java-related functions to ros_model_parser --- src/ros_graph_parser/core_class.py | 133 ----------------------------- 1 file changed, 133 deletions(-) diff --git a/src/ros_graph_parser/core_class.py b/src/ros_graph_parser/core_class.py index f3e3bb3..c500059 100644 --- a/src/ros_graph_parser/core_class.py +++ b/src/ros_graph_parser/core_class.py @@ -32,15 +32,6 @@ def get_dict(self): return {"Type": self.itype, "Name": self.resolved, "Namespace": self.namespace, "Minimal": self.minimal} - def str_format(self, indent=""): - return ("%sType: %s\n%sName: %s\n%sNamespace: %s\n%sMinimal: %s\n") % ( - indent, self.itype, indent, self.resolved, indent, - self.namespace, indent, self.minimal) - - def java_format(self, indent="", name_type="", interface_type=""): - return ("%s%s { name '%s' %s '%s'}") % ( - indent, name_type, self.resolved, interface_type, self.itype.replace("/", ".")) - class InterfaceSet(set): @@ -65,36 +56,6 @@ def get_with_type(self, itype): def remove_with_name(self, name): self.remove(self.get_with_name(name)) - def str_format(self, indent=""): - str_ = "" - for elem in self: - str_ += elem.str_format(indent) + "\n" - return str_ - - def java_format_ros_model(self, indent="", name_type="", interface_type="", name_block=""): - if len(self) == 0: - return "" - str_ = ("\n%s%s {\n") % (indent, name_block) - for elem in self: - str_ += elem.java_format(indent+" ", - name_type, interface_type) + ",\n" - str_ = str_[:-2] - str_ += "}" - return str_ - - def java_format_system_model(self, indent="", name_type="", name_type2="", node_name="", pkg_name="", name_type3=""): - if len(self) == 0: - return "" - if not name_type3: - name_type3 = name_type2 - str_ = ("%sRos%s {\n") % (indent, name_type) - for elem in self: - str_ += ("%s Ros%s '%s' {Ref%s '%s.%s.%s.%s'},\n") % ( - indent, name_type3, elem.resolved, name_type2, pkg_name, node_name, node_name, elem.resolved) - str_ = str_[:-2] - str_ += "}\n" - return str_ - def get_list(self): return [x.get_dict() for x in self] @@ -167,17 +128,6 @@ def str_format(self, indent=""): indent, self.value, indent, self.resolved, indent, self.namespace, indent, self.minimal) - def java_format(self, indent="", value=""): - str_param = "%sParameter { name '%s' type %s " % ( - indent, self.resolved, self.itype) - if self.itype == 'Struct': - str_param += self.types_struct(self.value[0], indent) - #str_param = str_param[:-2] - if self.itype == 'List': - str_param += self.form_list(self.value) - str_param += "}" - return str_param - def types_struct(self, struct_dict, indent): str_param = "{\n" indent_new = indent+" " @@ -241,35 +191,6 @@ def form_list(self, value_in): class ParameterSet(set): - def str_format(self, indent=""): - str_ = "" - for elem in self: - str_ += elem.str_format(indent) + "\n" - return str_ - - def java_format_ros_model(self, indent="", value="", name_block=""): - if len(self) == 0: - return "" - str_ = ("\n%s%s {\n") % (indent, name_block) - for elem in self: - str_ += elem.java_format(indent+" ", value) + ",\n" - str_ = str_[:-2] - str_ += "}" - return str_ - - def java_format_system_model(self, indent="", name_type="", name_type2="", node_name="", pkg_name="", name_type3=""): - if len(self) == 0: - return "" - if not name_type3: - name_type3 = name_type2 - str_ = ("%sRos%s {\n") % (indent, name_type) - for elem in self: - str_ += ("%s Ros%s '%s' {Ref%s '%s.%s.%s.%s' value %s},\n") % ( - indent, name_type3, elem.resolved, name_type2, pkg_name, node_name, node_name, elem.resolved, elem.set_value(elem.value, indent)) - str_ = str_[:-2] - str_ += "}\n" - return str_ - def get_list(self): return [x.get_dict() for x in self] @@ -337,22 +258,6 @@ def check_actions(self): self.action_servers.add(Interface(_action_name, _action_type)) self._clean_action_server() - def dump_print(self): - _str = "" - _str = "Node: \n\t%s" % (self.name) - _str = _str + \ - "\tPublishers:\n%s" % (self.publishers.str_format('\t\t')) - _str = _str + \ - "\tSubscribers:\n%s" % (self.subscribers.str_format('\t\t')) - _str = _str + "\tServices:\n%s" % (self.service_servers.str_format('\t\t')) - _str = _str + \ - "\tActionClients:\n%s" % (self.action_clients.str_format('\t\t')) - _str = _str + \ - "\tActionServers:\n%s" % (self.action_servers.str_format('\t\t')) - _str = _str + "\tParameters:\n%s" % (self.params.str_format('\t\t')) - _str = _str + ("\n") - print(_str) - def dump_yaml(self): yaml_dict = dict() yaml_dict['Publishers'] = self.publishers.get_list() @@ -362,41 +267,3 @@ def dump_yaml(self): yaml_dict['ActionServers'] = self.action_servers.get_list() yaml_dict['Parameters'] = self.params.get_list() return yaml_dict - - def dump_java_ros_model(self): - ros_model_str = " Artifact "+self.name+" {\n" - ros_model_str += " Node { name " + self.name - ros_model_str += self.service_servers.java_format_ros_model( - " ", "ServiceServer", "service", "ServiceServers") - ros_model_str += self.service_clients.java_format_ros_model( - " ", "ServiceClients", "service", "ServiceClients") - ros_model_str += self.publishers.java_format_ros_model( - " ", "Publisher", "message", "Publishers") - ros_model_str += self.subscribers.java_format_ros_model( - " ", "Subscriber", "message", "Subscribers") - ros_model_str += self.action_servers.java_format_ros_model( - " ", "ActionServer", "action", "ActionServers") - ros_model_str += self.action_clients.java_format_ros_model( - " ", "ActionClient", "action", "ActionClients") - ros_model_str += self.params.java_format_ros_model( - " ", "Parameters", "Parameters") - ros_model_str += "}},\n" - return ros_model_str - - def dump_java_system_model(self, package=""): - system_model_str = " ComponentInterface { name '" + \ - self.name+"'\n" - system_model_str += self.publishers.java_format_system_model( - " ", "Publishers", "Publisher", self.name, package) - system_model_str += self.subscribers.java_format_system_model( - " ", "Subscribers", "Subscriber", self.name, package) - system_model_str += self.service_servers.java_format_system_model( - " ", "SrvServers", "Server", self.name, package, "ServiceServer") - system_model_str += self.action_servers.java_format_system_model( - " ", "ActionServers", "ActionServer", self.name, package) - system_model_str += self.action_clients.java_format_system_model( - " ", "ActionClients", "ActionClient", self.name, package) - system_model_str += self.params.java_format_system_model( - " ", "Parameters", "Parameter", self.name, package) - system_model_str += "},\n" - return system_model_str