00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 from diarc.topology import *
00014
00015 class RosSystemGraph(Topology):
00016 def __init__(self):
00017 super(RosSystemGraph,self).__init__()
00018
00019 @property
00020 def nodes(self):
00021 return dict([(v.name,v) for v in self.vertices])
00022
00023 @property
00024 def topics(self):
00025 return dict(filter(lambda x: None not in x, [(topic.name,topic) for topic in self.edges]))
00026
00027 def nextFreeNodeIndex(self):
00028 """ returns the next available node index """
00029 return max(self.blocks.keys())+1 if len(self.blocks)>0 else 0
00030
00031 def nextFreeAltitudes(self):
00032 """ returns a 2-tuple of (posAltitude,negAltitude) of the avaliable altitudes """
00033 altitudes = [band.altitude for band in self.bands.values()] + [0]
00034 return (max(altitudes)+1,min(altitudes)-1)
00035
00036
00037
00038
00039 class Node(Vertex):
00040 def __init__(self,rsg,name=None):
00041 typecheck(rsg,RosSystemGraph,"rsg")
00042 super(Node,self).__init__(rsg)
00043
00044
00045 self.block.index = rsg.nextFreeNodeIndex()
00046
00047 self.name = name
00048 self.location = None
00049 self.pid = None
00050
00051 @property
00052 def publishers(self):
00053
00054
00055
00056
00057
00058 return self.sources
00059
00060 @property
00061 def subscribers(self):
00062 return self.sinks
00063
00064
00065 class Topic(Edge):
00066 def __init__(self,rsg,name=None,msgType=None):
00067 typecheck(rsg,RosSystemGraph,"rsg")
00068 super(Topic,self).__init__(rsg)
00069
00070
00071 self.posBand.altitude,self.negBand.altitude = rsg.nextFreeAltitudes()
00072 self.posBand.rank = self.posBand.altitude
00073 self.negBand.rank = self.posBand.altitude
00074
00075 self.name = name
00076 self.msgType = msgType
00077
00078 @property
00079 def publishers(self):
00080
00081 return self.sources
00082
00083 @property
00084 def subscribers(self):
00085
00086 return self.sinks
00087
00088
00089
00090
00091 class Publisher(Source):
00092 def __init__(self,rsg,node,topic):
00093 typecheck(rsg,RosSystemGraph,"rsg")
00094 typecheck(node,Node,"node")
00095 typecheck(topic,Topic,"topic")
00096 super(Publisher,self).__init__(rsg,node,topic)
00097
00098 self.snap.order = max(filter(lambda x: isinstance(x,int), [pub.snap.order for pub in node.publishers] + [-1]))+1
00099
00100 self.bandwidth = None
00101 self.msgType = None
00102
00103 @property
00104 def topic(self):
00105
00106 return self.edge
00107
00108 @property
00109 def node(self):
00110
00111 return self.vertex
00112
00113 class Subscriber(Sink):
00114 def __init__(self,rsg,node,topic):
00115 typecheck(rsg,RosSystemGraph,"rsg")
00116 typecheck(node,Node,"node")
00117 typecheck(topic,Topic,"topic")
00118 super(Subscriber,self).__init__(rsg,node,topic)
00119
00120
00121 self.snap.order = max(filter(lambda x: isinstance(x,int), [sub.snap.order for sub in node.subscribers] + [-1]))+1
00122
00123 self.bandwidth = None
00124 self.msgType = None
00125
00126 @property
00127 def topic(self):
00128
00129 return self.edge
00130
00131 @property
00132 def node(self):
00133
00134 return self.vertex
00135
00136