graph_builder.py
Go to the documentation of this file.
00001 # Functionalities to convert the map into Python objects.
00002 
00003 import rospy
00004 
00005 from lama_msgs.msg import LamaObject
00006 
00007 from lama_interfaces.map_agent import MapAgent
00008 from lama_interfaces.srv import ActOnMapRequest
00009 
00010 _map_agent = MapAgent()
00011 
00012 
00013 def get_vertex_from_graph(graph, id_):
00014     """Return the vertex which is a key of graph and has id_ as id
00015 
00016     Return a LamaObject
00017     """
00018     for lama_object in graph.iterkeys():
00019         if lama_object.id == id_:
00020             return lama_object
00021     return None
00022 
00023 
00024 def get_directed_graph():
00025     """Return the directed graph as a dict {vertex: [edge0, edge1, ...], ...}
00026 
00027     Return the directed graph as a dict {vertex: [edge0, edge1, ...], ...},
00028     where vertex is a LamaObject of type vertex and edge0 is a LamaObject
00029     of type edge and with edge0.references[0] = vertex.
00030     All vertices are listed as key. All edges are listed as values.
00031     """
00032     # Get the vertices (graph keys).
00033     graph = {}
00034     vertices = _map_agent.get_vertex_list()
00035     if vertices is None:
00036         return graph
00037     for vertex in vertices:
00038         # rospy deserializes arrays as tuples, convert to list.
00039         vertex.references = list(vertex.references)
00040         graph[vertex] = []
00041     # Get the edges (graph values).
00042     edges = _map_agent.get_edge_list()
00043     if edges is None:
00044         return graph
00045     for edge in edges:
00046         # rospy deserializes arrays as tuples, convert to list.
00047         edge.references = list(edge.references)
00048         first_vertex = get_vertex_from_graph(graph, edge.references[0])
00049         if first_vertex is None:
00050             rospy.logerr(('Vertex {} does not exist although ' +
00051                           'it is the first vertex of edge {}').format(
00052                               edge.references[0],
00053                               edge.id))
00054             continue
00055         graph[first_vertex].append(edge)
00056     return graph
00057 
00058 
00059 def get_directed_graph_index():
00060     """Return the directed graph as a dict {vertex_id: [v0_id, v1_id...], ...}
00061 
00062     Return the directed graph as a dict {vertex_id: [v0_id, v1_id, ...], ...},
00063     where vertex_id is the index of a LamaObject of type vertex and v0_id is
00064     the index of a LamaObject, meaning that their is an edge from vertex_id to
00065     v0_id.
00066     """
00067     map_graph = get_directed_graph()
00068     graph = {}
00069     for vertex, edges in map_graph.iteritems():
00070         graph[vertex.id] = [e.references[1] for e in edges]
00071     return graph
00072 
00073 
00074 def get_edges_with_vertices(v0, v1):
00075     """Return the list of edges from v0 to v1, as LamaObject
00076 
00077     Return the list of edges from v0 to v1, as LamaObject. Return None on
00078     service error.
00079     """
00080     edge = LamaObject()
00081     edge.type = edge.EDGE
00082     edge.references = [v0, v1]
00083     return _map_agent.get_edge_list(edge)
00084 
00085 
00086 def get_descriptors(object_id, interface, getter):
00087     """Retrieve the descriptors associated with LamaObject with id object_id
00088 
00089     Return a list of descriptors associated with getter.
00090 
00091     Parameters
00092     ----------
00093     - object_id: int, LamaObject's id
00094     - interface: str, interface name associated with getter
00095     - getter: ROS ServiceProxy for a ROS message type
00096     """
00097     map_action = ActOnMapRequest()
00098     map_action.action = map_action.GET_DESCRIPTOR_LINKS
00099     lama_object = LamaObject()
00100     lama_object.id = object_id
00101     map_action.object = lama_object
00102     map_action.interface_name = interface
00103     response = _map_agent.proxy(map_action)
00104     descriptors = []
00105     for descriptor_link in response.descriptor_links:
00106         getter_response = getter(descriptor_link.descriptor_id)
00107         descriptors.append(getter_response.descriptor)
00108     return descriptors


interfaces
Author(s): Gaël Ecorchard , Karel Košnar
autogenerated on Sat Jun 8 2019 19:03:14